Python Control Statements 1
Python Control Statements 1
Python Control Statements 1
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:
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 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))
1. Loan Amount: Input the desired loan amount that you wish to
avail.
2. Loan Tenure (In Years): Input the desired loan term for which you
wish to avail the loan.
3. Interest Rate (% P.A.): Input interest rate.
4. EMI=[ [P*R*(1+R)N] / [(1+R)N-1] ]
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]
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
for x in range(4):
print(x, end=” “)
else:
print("\nFinally finished!")
output: 0 1 2 3
Finally finished!
Nested Loops
A nested loop is a loop inside another loop.
Thanks