Revision 2 - Python Class 9
Revision 2 - Python Class 9
Relational Operators
no=int(input("Enter no"))
if(no==1):
print("Monday")
elif(no==2):
print("Tuesday")
elif(no==3):
print("Wednesday")
elif(no==4):
print("Thursday")
elif(no==5):
print("Friday")
elif(no==6):
print("Saturday")
elif(no==7):
print("Sunday")
else:
print("Not a weekday")
if(no1>no2):
print("No1 is greater")
else:
print("No2 is greater")
age=int(input("Enter Age"))
if(age>=18):
print("You are allowed to vote")
else:
print("You are not allowed to vote")
Output
True
False
# Write a program to input two numbers and an operator from the user.
Display the result calculated based on the operator entered.
Multiline comment is created with single quote ''' or double quote """
followed by the text in multiple lines
# write a program to input the billing amount and gender. If the billing
amount is more than 10000 and gender is female, then discount is 15%
otherwise discount is 10% calculate the billing amount accordingly
print("Menu")
print("1 Addition")
print("2 Subtraction")
ch=int(input("Enter your choice 1 or 2"))
if(ch==1):
n1=int(input("Enter first no"))
n2=int(input("Enter second no"))
res=n1+n2
print("Result ",res)
elif(ch==2):
n1=int(input("Enter first no"))
n2=int(input("Enter second no"))
res=n1-n2
print("Result ",res)
else:
print("Enter 1 or 2 only")
# write a program to input the billing amount and gender. If the billing
amount is more than 10000 and gender is female, then discount is 15%
otherwise discount is 10% calculate the billing amount accordingly
no=int(input("Enter a number"))
if(no>0):
if(no%5==0):
print(no," is multiple of 5")
else:
print(no," is not multiple of 5")
else:
print("Invalid no")
Assignment Operator (=)
a=10
a=10
a=a+10
print(a)
or
a=10
a+=10
print(a)
Output: 20
a=20
a-=10
print(a)
Output: 10
a=2
a*=3
print(a)
Output: 6
a=10
a/=5
print(a)
Output: 2.0
a=10
a//=5
print(a)
Output: 2.0
a=10
a%=5
print(a)
a=2
a**=4
print(a)
For Loop
It is used to repeat a set of instructions for a fixed number of
times. It means when the number of iterations are known/definite
before we start with the execution of a loop. It is therefore also
known as definite loop.
Indentation of statements is must to specify the block of
statements to be repeated using for loop.
There are two ways using for loop:
for i in [1,2,3,4,5]:
print(i)
output
1
2
3
4
output
hello
how are you
friends
for i in ["hello"]:
print(i)
output
hello
for i in "hello":
print(i)
output
h
e
l
l
o
for i in range(1,11,1):
print(i)
for i in range(1,11):
print(i)
for i in range(2,21,2):
print(i)
# write a program to print the even numbers in 1 to 10 using for loop
for i in range(2,11,2):
print(i)
for i in range(1,11,2):
print(i)
for i in range(10,101,10):
print(i)
If one parameter is used, then the start becomes zero and step
value becomes 1 by default
for i in range(10):
print(i)
for i in range(5,3):
print(i)
output
No output
for i in range(10,0,-1):
print(i)
for i in range(5,5):
print(i)
output
No output
# write a program to print “hello” 5 times using for loop
for i in range(1,6,1):
print(“hello”)
# write a program to print first five natural numbers using for loop
for i in range(1,6,1):
print(i)
While Loop
while <condition>
Statements
Increase/ Decrease
Example
i=1
while(i<=10):
print(i)
i=i+1
i=10
while(i>=0):
print(i)
i=i-1
# write a program to print the sum of first 5 natural numbers using
while loop
i=1
sum=0
while(i<=5):
sum=sum+i
i=i+1