Decision Loop PDF
Decision Loop PDF
firstif.py
v1=input("Enter the value of v1: ")
if(int(v1)==10):
print("the value of v1 is: ",v1)
print("in first if")
v2=input("Enter the value of v2: ")
if(int(v2)==100):
print("the value of v2 is: ",v2)
print("in second if")
OUTPUT:
>>>
Enter the value of v1: 10
the value of v1 is: 10
in first if
Enter the value of v2: 100
the value of v2 is: 100
in second if
##1. IF ELSE
exnested.py
a=input("Enter the value for a: ")
b=input("Enter the value for b: ")
c=input("Enter the value for c: ")
if(a>b):
if(a>c):
print("a is larger than b and c")
else:
print("c is larger than a and b")
else:
if(b>c):
print("b is larger than a and c")
else:
print("c is larger a and b")
OUTPUT:
Enter the value for a: 3
Enter the value for b: 4
Enter the value for c: 5
c is larger a and b
>>>
Enter the value for a: 0
Enter the value for b: 6
Enter the value for c: 2
b is larger than a and c
##2. IF ELSE
nestedifex.py
age = int(input(" Please Enter Your Age Here: "))
if age < 18:
print(" You are Minor ")
print(" You are not Eligible to Work ")
else:
if age >= 18 and age <= 60:
print(" You are Eligible to Work ")
print(" Please fill in your details and apply")
else:
print(" You are too old to work as per the Government rules")
print(" Please Collect your pension!")
OUTPUT:
>>>
Please Enter Your Age Here: 13
You are Minor
You are not Eligible to Work
>>>
Please Enter Your Age Here: 20
You are Eligible to Work
Please fill in your details and apply
>>>
Please Enter Your Age Here: 65
You are too old to work as per the Government rules
Please Collect your pension!
##IF..ELIF..ELSE
fourif.py
a=input("Enter value for a: ")
if(int(a)<20):
print("a is less than 20")
if(int(a)==15):
print("a is 15")
elif(int(a)==10):
print("a is 10")
elif(int(a)==5):
print("a is 5")
elif(int(a)<5):
print("a is less than 5")
else:
print("a is greater than 15 and less than 20")
else:
print("a is greater than 20")
OUTPUT:>>> Enter value for a: 15
a is less than 20
a is 15
>>>
Enter value for a: 10
a is less than 20
a is 10>>>
Enter value for a: 5
a is less than 20
a is 5
>>>
Enter value for a: 22
a is greater than 20
## 1.FOR
factorial.py
a=[1,2,3,4,5]
#n=int(input("Enter the value of n"))
for i in range(5):
#a.append(input("Enter the %d item:"%i))
#for i in range(n):
f=1
for fact in range(1,a[i]):
f*=fact
fact+=1
print("factorial is %d is %d:"%(i,f))
OUTPUT:>>> factorial is 0 is 1
factorial is 1 is 1
factorial is 2 is 2
factorial is 3 is 6
factorial is 4 is 24
OR
OUTPUT
##2.FOR
firstfor.py
for letter in 'PYTHON':
print("Current letter: ",letter)
fruits=['banana','apple','grapes','mango','melon']
for fruit in fruits:
print("current fruit: ",fruit)
OUTPUT:
>>>
Current letter: P
Current letter: Y
Current letter: T
Current letter: H
Current letter: O
Current letter: N
current fruit: banana
current fruit: apple
current fruit: grapes
current fruit: mango
current fruit: melon
##FOR ELSE
## WHILE ELSE
secondwhile.py
reversedNumber = 0
n=input("Enter an integer: ")
while(int(n)>0):
remainder =int(n)%10
reversedNumber = reversedNumber*10 + remainder
n = int(n)/10;
print("Reversed Number = ", reversedNumber)
else:
print("reverse of a number is not possible")
OUTPUT:
nestediffun.py
# menu() function prints the main menu, and prompts for a choice
def menu():
#print what options you have
print "Welcome to calculator in Python"
print "your options are:"
print " "
print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
print "5) Quit calculator"
print " "
return input ("Choose your option: ")
OUTPUT:
Welcome to calculator in Python
your options are:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator