Python Experiment No: 4 Study and Implementation of Control Structure and Function.
Study and Implementation of Control Structure and Function.
Aim:-
Theory:-
As with most imperative languages, there are three main categories of program flow control
1) loops
2) branches
3) function calls
Loops
In Python, there are two kinds of loops, 'for' loops and 'while' loops.
For loops
A for loop iterates over elements of a sequence (tuple or list). A variable is created to represent
the object in the sequence. For example,
l = [100,200,300,400]
for i in l:
print i
O/P
100
200
300
400
for i in range(10, 0, -1):
print i
O/P
10
9
-----
1
While loops
A while loop repeats a sequence of statements until some condition becomes false. For example:
x = 5
while x > 0:
print x
x = x – 1O/P
5
4
3
2
1
Branches
There is basically only one kind of branch in Python, the 'if' statement. The simplest form of the
if statement simple executes a block of code only if a given predicate is true, and skips over it if
the predicate is false
>>> x = -6
>>> if x > 0:
... print "Positive"
... elif x == 0:
... print "Zero"
... else:
... print "Negative"
...
'Negative'
Function:-
A function is the simplest callable object in Python, but there are others, such as classes1 or
certain class instances.
Defining functions
def functionname(arg1, arg2, ...):
statement1
statement2
...
Q1) Explain branching statement in python.
Q2) Explain all loop statement in python.
Q3) Explain working of continue and break statement.3
Q4) Write a syntax to create user defined function in python.
Aim:-
Theory:-
As with most imperative languages, there are three main categories of program flow control
1) loops
2) branches
3) function calls
Loops
In Python, there are two kinds of loops, 'for' loops and 'while' loops.
For loops
A for loop iterates over elements of a sequence (tuple or list). A variable is created to represent
the object in the sequence. For example,
l = [100,200,300,400]
for i in l:
print i
O/P
100
200
300
400
for i in range(10, 0, -1):
print i
O/P
10
9
-----
1
While loops
A while loop repeats a sequence of statements until some condition becomes false. For example:
x = 5
while x > 0:
print x
x = x – 1O/P
5
4
3
2
1
Branches
There is basically only one kind of branch in Python, the 'if' statement. The simplest form of the
if statement simple executes a block of code only if a given predicate is true, and skips over it if
the predicate is false
>>> x = -6
>>> if x > 0:
... print "Positive"
... elif x == 0:
... print "Zero"
... else:
... print "Negative"
...
'Negative'
Function:-
A function is the simplest callable object in Python, but there are others, such as classes1 or
certain class instances.
Defining functions
def functionname(arg1, arg2, ...):
statement1
statement2
...
Q1) Explain branching statement in python.
Q2) Explain all loop statement in python.
Q3) Explain working of continue and break statement.3
Q4) Write a syntax to create user defined function in python.
Comments
Post a Comment