Unit 2
Unit 2
Unit: 2 Selection/Conditional Branching statements: if, if-else, nested if and if-elif-else statements.
Iterative Statements: while loop, for loop, else suite in loop and nested loops. Jump Statements:
continue and pass statements.
1. if statement
2. if else statement
3. Ladder if else statement (if-elif-else)
4. Nested if statement
Python If statements
This construct of python program consist of one if condition with one block of statements.
When conditionbecomes true then executes the block given below it.
Syntax:
if ( condition):
…………………..
…………………..
Example:
Age=int(input(“Enter Age: “))If (
age>=18):
Print(“You are eligible for vote”)
If(age<0):
Print(“You entered Negative Number”)
Flowchart
Example-1:
Age=int(input(“Ent
er Age: “))
if ( age>=18):
print(“You are eligible for vote”)
else:
print(“You are not eligible for vote”)
Example-2:
N=int(input(“Enter
Number: “))
if(n%2==0):
print(N,“ is Even Number”)
Else:
print(N,“ is Odd Number”)
Python Ladder if else statements (if-elif-else)
This construct of python program consist of more than one if condition.When first condition
evaluates result as true then executes the block given below it. If condition evaluates result
as false, it transfer the control at else part to test another condition. So, it is multi-decision
making construct.
Syntax:
if ( condition-1):
…………………..
………………….
.elif (condition-2):
…………………..
………………….
.elif (condition-3):
…………………..
…………………..
else:
…………………..
…………………..
Example:
num=int(input(“Enter Number: “))If (
num>=0):
Print(“You entered positive number”)elif ( num<0):
Print(“You entered Negative number”)
else:
Print(“You entered Zero ”)
Python Nested if statements
It is the construct where one if condition take part inside of other if condition. This construct
consist of more than one if condition. Block executes when condition becomes false and next
condition evaluateswhen first condition became true.
So, it is also multi-decision making construct.
Example:
num=int(input(“Enter Number: “))If (
num<=0):
if ( num<0):
Print(“You entered Negative number”)
else:
Print(“You entered Zero ”)
else:
Print(“You entered Positive number”)
Program: find largest number out of given three numbersx=int(input("Enter First Number:
"))
y=int(input("Enter Second Number: "))
z=int(input("Enter Third Number: ")) if(x>y and
x>z):
largest=x elif(y>x
and y>z):
largest=y elif(z>x
and z>y):
largest=z
print("Larest Value in %d, %d and %d is: %d"%(x,y,z,largest))
1. Loan Amount: Input the desired loan amount that you wish toavail.
2. Loan Tenure (In Years): Input the desired loan term for which youwish to avail the loan.
3. Interest Rate (% P.A.): Input interest rate.4. EMI=[
[P*R*(1+R)N] / [(1+R)N-1] ]
The iteration (Looping) constructs mean to execute the block of statements again and again
depending upon the result of condition. This repetition of statements continues till condition
meets True result.As soon as condition meets false result, the iteration stops.
Python supports following types of iteration statements
1. while
2. for
Four Essential parts of Looping:
(condition):
…………………..
Updation in control variable
..…………………
Flowchart
# initialization
while(num<=10): # condition testing
print(num, end=” “)
Body of loop
num + = 1 # Increment
The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number. The common format of
range() is as given below:
Note: The Lower Limit is included but Upper Limit is not included in result.
Example
range(5) => sequence of 0,1,2,3,4
range(2,5) => sequence of 2,3,4 range(1,10,2)
=> sequence of 1,3,5,7,9range(5,0,-
1) => sequence of 5,4,3,2,1
range(0,-5) => sequence of [ ] blank list (default Step is +1)
range(0,-5,-1) => sequence of 0, -1, -2, -3, -4
range(-5,0,1) => sequence of -5, -4, -3, -2, -1
range(-5,1,1) => sequence of -5, -4, -3, -2, -1, 0
L=list(range(1,20,2)
Print(L) Output: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Python for loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a string etc.)
With for loop we can execute a set of statements, and for loop can also execute once for
each element in a list, tuple, set etc.
Output: 1 2 3 4 5 6 7 8 9 10 Output: 10 9 8 7 6 5 4 3 2 1
The else keyword in for loop specifies a block of code to be executed when theloop is
finished:
for x in range(4):
print(x, end=” “)
else:
print("\nFinally finished!")
output: 0 1 2 3
Finally finished!
Nested Loops
output: Jaipur
: apple
Jaipur : mango
Jaipur : cherry
Delhi : apple Delhi
: mango Delhi :
cherry Mumbai :
apple Mumbai :
mangoMumbai :
cherry
Un- Conditional Control Construct
(pass, break, continue, exit(), quit())
The pass statement do nothing, but it used to complete the syntax of programming concept.
Pass is useful in the situation where user does not requires any action but syntax requires a
statement. The Python compiler encounters pass statement then it do nothing but transfer
the control in flow ofexecution.
break Statement
The jump- break statement enables to skip over a part of code that used in loop even if the
loop condition remains true. It terminates tothat loop in which it lies. The execution
continues from the statement which find out of loop terminated by break.
n=1
while(n<=5):
print("n=",n) Output:
k=1 n= 1
while(k<=5):
if(k==3): k= 1 k= 2
break n= 2
print("k=",k, end=" ")
k+=1
k= 1 k= 2
n+=1 n= 3
print() k= 1 k= 2
Exit the loop when xis "banana": n= 4
k=x 1in k=
fruits = ["apple", "banana", "cherry"] for 2
fruits:
if x == "banana":break n= 5
print(x) k= 1 k= 2
output: apple
Continue Statement
Continue statement is also a jump statement. With the help ofcontinue statement, some of
statements in loop, skipped overand starts the next iteration. It forcefully stop the current
iteration and transfer the flow of control at the loop controlling condition.
i=0
while i <=10:i+=1
if (i%2==1):
continue print(I,
end=” “)
output: 2 4 6 8 10