Unit-2 Python Control Flow-Functions
Unit-2 Python Control Flow-Functions
A program’s control flow is the order in which the program’s code executes.
The control flow of a Python program is regulated by conditional statements, loops, and
function calls.
Python has three types of control structures:
• Sequential - default mode
• Selection - used for decisions and branching
• Repetition - used for looping, i.e., repeating a piece of code multiple times.
Sequential:
Sequential statements are a set of statements whose execution process happens in a
sequence. The problem with sequential statements is that if the logic has broken in any one
of the lines, then the complete source code execution will break.
## This is a Sequential statement
a = 20 b = 10 c = a - b
print("Subtraction is : ", c)
Output:
Subtraction is : 10
Example-1
n = 10
Unit-2 Python Control Flow IV Semester BCA
if n % 2 == 0:
print(n," is an even number")
Output:
10 is an even number
if-else
if-else: The if-else statement evaluates the condition and will execute the body of if if the
test condition is True, but if the condition is False, then the body of else is executed.
Syntax:
if (condition):
# executes this block if
# Condition is true else:
# executes this block if
# Condition is false
Example-1
n=5
if n % 2 == 0:
print(n," is even") else:
print(n," is odd") Output:
5 is odd
Unit-2 Python Control Flow IV Semester BCA
elif
elif: The if-elif-else statement is used to conditionally execute a statement or a block of
statements.
Syntax:
if (condition):
statement elif
(condition):
statement
.
. else:
statement
Example-1
x = 15
y = 12 if x == y: print("Both
are Equal") elif x > y:
print("x is greater than y")
else:
print("x is smaller than y")
Output:
x is greater than y
Unit-2 Python Control Flow IV Semester BCA
nested if:
Nested if statements are an if statement inside another if statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
Example-1
a = 5 b = 10 c = 15
if a > b: if a > c:
print("a value is big")
else:
print("c value is big") elif
b > c:
print("b value is big") else:
print("c is big") Output:
c is big
Repetition
A repetition statement is used to repeat a group(block) of programming instructions.
Unit-2 Python Control Flow IV Semester BCA
for loop:
A for loop is used to iterate over a sequence that is either a list, tuple, dictionary, or a set.
We can execute a set of statements once for each item in a list, tuple, or dictionary.
Syntax:
for iterator_var in sequence:
statements(s)
Example-1
# Python program to illustrate
# Iterating over range 0 to n-1
n=4
for i in range(0, n):
print(i)
Output:
0
1
2
3
Unit-2 Python Control Flow IV Semester BCA
While loop:
In Python, while loops are used to execute a block of statements repeatedly until a given
condition is satisfied. Then, the expression is checked again and, if it is still true, the body is
executed again. This continues until the expression becomes false.
Syntax:
while expression:
statement(s)
Example-1
i = 1 while
i < 6:
print(i)
i += 1
Output:
automatic objects that were created in that scope are destroyed. Python supports the
following control statements.
• Break statement
• Continue statement
• Pass statement
Break statement
The break statement is used to terminate the loop or statement in which it is present. After
that, the control will pass to the statements that are present after the break statement, if
available. If the break statement is present in the nested loop, then it terminates only those
loops which contain break statement.
Syntax:
break
Example
#Exit the loop when i is 3:
i = 1 while
i < 6:
print(i) if
i == 3:
break
i += 1
Output:
Unit-2 Python Control Flow IV Semester BCA
Continue statement
Continue is also a loop control statement just like the break statement. continue statement is
opposite to that of break statement, instead of terminating the loop, it forces to execute the
next iteration of the loop.
Syntax:
Continue
Example
#Continue to the next iteration if i is 3:
i = 0 while
i < 6:
i += 1
if i == 3:
continue
print(i)
Output:
Unit-2 Python Control Flow IV Semester BCA
Pass statement
As the name suggests pass statement simply does nothing. The pass statement in Python is
used when a statement is required syntactically but you do not want any command or code
to execute. It is like null operation, as nothing will happen is it is executed. Pass statement
can also be used for writing empty loops. Pass is also used for empty control statement,
function and classes.
Syntax: pass
Example:
a = 33
b =200
if b > a:
pass
# having an empty if statement like this, would raise an error without the pass statement
Output:
Parameter Values
Parameter Description
Example-1
#Create a sequence of numbers from 0 to 5, and print each item in the sequence:
x = range(6) for
n in x: print(n)
Output:
Syntax:
Unit-2 Python Control Flow IV Semester BCA
exit( )
Example:
Output:
Unit-2 Python Functions IV Semester BCA
Python Functions
A function is a block of statements that return the specific task. (Function is a block of
code that performs a specific task) The idea is to put some commonly or repeatedly done
tasks together and make a function so that instead of writing the same code again and again
for different inputs, we can do the function calls to reuse code contained in it over and over
again.
Types of function
There are two types of function in Python programming:
Standard library functions - These are built-in functions in Python that are available to
use.
User-defined functions - We can create our own functions based on our requirements.
1
Unit-2 Python Functions IV Semester BCA
3
Unit-2 Python Functions IV Semester BCA
Output:
Sum: 9
4
Unit-2 Python Functions IV Semester BCA
print('Square:',square)
Output:
Square: 9
In the above example, we have created a function named find_square(). The function accepts
a number and returns the square of the number.
Default Arguments
A default argument is a parameter that assumes a default value if a value is not provided in
the function call for that argument. In Python, we can provide default values to function
arguments. We use the = operator to provide default values.
The following example illustrates Default arguments. def
add_numbers( a = 7, b = 8):
sum = a + b
print('Sum:', sum)
# function call with two arguments
add_numbers(2, 3)
# function call with one argument
add_numbers(a = 2)
# function call with no arguments
add_numbers( ) Output:
6
Unit-2 Python Functions IV Semester BCA
Keyword Arguments
In keyword arguments, arguments are assigned based on the name of arguments. The
idea is to allow the caller to specify the argument name with values so that the caller
does not need to remember the order of parameters. For example,
# Python program to demonstrate Keyword Arguments def
student(firstname, lastname):
print(firstname, lastname) # Keyword
arguments student(firstname='PES',
lastname='IAMS')
student(lastname='IAMS', firstname='PES')
Output:
7
Unit-2 Python Functions IV Semester BCA
Python Recursion
Recursion is the process of defining something in terms of itself.(Function call by itself)
In Python, we know that a function can call other functions. It is even possible for the
function to call itself. These types of construct are termed as recursive functions.
The following image shows the working of a recursive function called recurse.
8
Unit-2 Python Functions IV Semester BCA
Let's look at an image that shows a step-by-step process of what is going on:
advantages of Recursion