Bca 2 Python Unit 3 Eng
Bca 2 Python Unit 3 Eng
Unit III
Iteration and Recursion: Conditional execution, Alternative execution, Nested conditionals, The
return statement, Recursion, Stack diagrams for recursive functions, Multiple assignment, The
while statement, Implementing 2-D matrices.
In order to write useful programs, we almost always need the ability to check conditions
and change the behavior of the program accordingly. Conditional statements give us this
ability. The simplest form is the if statement.
The boolean expression after if is called the condition. If it is true, then the indented statement
gets executed. If not, nothing happens.
WEBSOL BCA 2 PYTHON UNIT 3 WEBSOL
A second form of the if statement is alternative execution, in which there are two possibilities
and the condition determines which one gets executed. The syntax looks like this:
if x%2 == 0:
else:
If the remainder when x is divided by 2 is 0, then we know that x is even, and the program
displays a message to that effect. If the condition is false, the second set of statements is
executed. Since the condition must be true or false, exactly one of the alternatives will be
executed. The alternatives are called branches, because they are branches in the flow of
execution.
Nested conditionals
WEBSOL BCA 2 PYTHON UNIT 3 WEBSOL
The outer conditional contains two branches. The first branch contains a simple statement. The
second branch contains another if statement, which has two branches of its own. Those two
branches are both simple statements, although they could have been conditional statements as
well.
Although the indentation of the statements makes the structure apparent, nested
conditionals become difficult to read very quickly. In general, it is a good idea to avoid them
when you can.
There may be a situation when you want to check for another condition after a condition
resolves to true. In such a situation, you can use the nested if construct. Imagine a program that
reports whether a number is positive, negative, or zero. That program needs to select from 3
paths.
WEBSOL BCA 2 PYTHON UNIT 3 WEBSOL
Return statement
A return statement is used to end the execution of the function call and “returns” the result (value
of the expression following the return keyword) to the caller. The statements after the return
statements are not executed. If the return statement is without any expression, then the special
value None is returned.
# Python program to demonstrate return statement Output
def add(a, b): 5
# returning sum of a and b
return a + b
# calling function
res = add(2, 3)
print(res)
WEBSOL BCA 2 PYTHON UNIT 3 WEBSOL
Recursion in Python
The term Recursion can be defined as the process of defining something in terms of itself. In
simple words, it is a process in which a function calls itself directly or indirectly.
A complicated function can be split down into smaller sub-problems utilizing recursion.
Sequence creation is simpler through recursion than utilizing any nested iteration.
Recursive functions render the code look simple and effective.
A lot of memory and time is taken through recursive calls which makes it expensive for
use.
Recursive functions are challenging to debug.
The reasoning behind recursion can sometimes be tough to think through.
1. def facto(n):
2. if n == 1:
3. return n
4. else:
5. return n*facto(n-1)
6. # take input from the user
7. num = int(input("Enter a number: "))
8. # check is the number is negative
9. print(facto(num))
WEBSOL BCA 2 PYTHON UNIT 3 WEBSOL
stack diagram to represent the state of a program during a function call. The same kind of
diagram can make it easier to interpret a recursive function.
Remember that every time a function gets called it creates a new instance that contains the
function’s local variables and parameters. For a recursive function, there might be more than
one frame on the stack at the same time.
The basic assignment statement does more than assign the result of a single expression to a
single variable. The assignment satement also copes nicely with assigning multiple variables at
one time. The left and right side must have the same number of elements. For example, the
following script has several examples of multiple assignment.
Python allows you to assign a single value to several variables simultaneously. For example −
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to the
same memory location.
print(x) print(x)
print(y) print(y)
print(z) print(z)
In Python, While Loops is used to execute a block of statements repeatedly until a given
condition is satisfied. And when the condition becomes false, the line immediately after the
loop in the program is executed. While loop falls under the category of indefinite iteration.
Indefinite iteration means that the number of times the loop is executed isn’t specified
explicitly in advance.
WEBSOL BCA 2 PYTHON UNIT 3 WEBSOL
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
2- D Array :
Two dimensional array is an array within an array. It is an array of arrays. In this type of array
the position of an data element is referred by two indices instead of one. So it represents a table
with rows an dcolumns of data. In the below example of a two dimensional array, observer that
each array element itself is also an array.A 2D array is an array of arrays that can be represented
in matrix form like rows and columns. In this array, the position of data elements is defined with
two indices instead of a single index.