##IF
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
Factorial of a single number
n = eval(input("Please enter a whole number: "))
fact = 1
for factor in range(n,1,-1):
fact = fact * factor
print("The factorial of", n, "is", fact)
OUTPUT
Please enter a whole number: 5
The factorial of 5 is 120
##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
Write example using for else
##1.WHILE
firstwhile.py
count=0
while(count<5):
print(count,"is less than 5")
count+=1
else:
print(count,"is not less than 5")
OUTPUT:
>>>
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
## 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:
Enter an integer: 134
Reversed Number = 4
Reversed Number = 43
Reversed Number = 431
reverse of a number is not possible
>>>
Enter an integer: -2
reverse of a number is not possible
##FUNCTIONS WITH NESTED IF
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: ")
# this adds two numbers given
def add(a,b):
print a, "+", b, "=", a + b
# this subtracts two numbers given
def sub(a,b):
print b, "-", a, "=", b - a
# this multiplies two numbers given
def mul(a,b):
print a, "*", b, "=", a * b
# this divides two numbers given
def div(a,b):
print a, "/", b, "=", a / b
# NOW THE PROGRAM STARTS, AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
add(input("Add this: "),input("to this: "))
elif choice == 2:
sub(input("Subtract this: "),input("from this: "))
elif choice == 3:
mul(input("Multiply this: "),input("by this: "))
elif choice == 4:
div(input("Divide this: "),input("by this: "))
elif choice == 5:
loop = 0
print "Thankyou for using calculator!"
OUTPUT:
Welcome to calculator in Python
your options are:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
Choose your option: 1
Add this: 2
to this: 3
2+3=5
Welcome to calculator in Python
your options are:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
Choose your option: 2
Subtract this: 3
from this: 2
2 - 3 = -1
Welcome to calculator in Python
your options are:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
Choose your option: 3
Multiply this: 3
by this: 4
3 * 4 = 12
Welcome to calculator in Python
your options are:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
Choose your option: 4
Divide this: 10
by this: 2
10 / 2 = 5
Welcome to calculator in Python
your options are:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Quit calculator
Choose your option: 5
Thankyou for using calculator!