5 Python Control and Conditional Statements 1 2 Lyst1729232058766
5 Python Control and Conditional Statements 1 2 Lyst1729232058766
Flowchart
Example:
Age=int(input(“Enter Age: “))
If ( age>=18):
Print(“You are eligible for vote”)
If(age<0):
Print(“You entered Negative Number”)
Syntax:
if ( condition):
…………………..
else:
…………………..
Flowchart
Example-1:
Age=int(input(“Enter Age: “))
if ( age>=18):
print(“You are eligible for vote”)
else:
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 evaluates
when first condition became true.
So, it is also multi-decision making construct.
Syntax: FlowChart
if ( condition-1):
if (condition-2):
……………
……………
else:
……………
……………
else:
…………………..
…………………..
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 numbers
x=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))
Where all 3 parameters are of integer type Start and Step Parameters are
Start value is Lower Limit optional default value will be as
Stop value is Upper Limit Start=0 and Step=1
Step value is Increment / Decrement
Note: The Lower Limit is included but Upper Limit is not included in
result. Example
L=list(range(1,20,2)
Print(L) Output: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Output: 1 2 3 4 5 6 7 8 9 10 Output: 10 9 8 7 6 5 4 3 2 1
Nested Loops
A nested loop is a loop inside another loop.
Jumping Statements
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 to
that loop in which it lies. The execution continues from the statement
which find out of loop terminated by break.
n=1 n+=1
while(n<=5): print()
print("n=",n)
k=1 Exit the loop when x is "banana":
while(k<=5): Output: n= 1
if(k==3): k= 1 k= 2 n= 2
break k= 1 k= 2 n= 3
print("k=",k, end=" ") k+=1 k= 1 k= 2 n= 4
k= 1 k= 2 n= 5
k= 1 k= 2
i=0
while i <=10:
i+=1
if (i%2==1):
continue
print(I, end=” “)
output: 2 4 6 8 10
Thanks