0% found this document useful (0 votes)
27 views

Revision 2 - Python Class 9

Uploaded by

coffee90yum
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Revision 2 - Python Class 9

Uploaded by

coffee90yum
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Revision 2

Artificial Intelligence (Subject Code: 417)


Python Programs – Class IX

Relational Operators

>, <, >=, <=, ==, !=)

# Write a program to enter day number and print the corresponding


weekday

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")

# Write a program to enter weekday and print the corresponding day


number
day=input("Enter no")
if(day=="Monday"):
print("1")
elif(day=="Tuesday"):
print("2")
elif(day=="Wednesday"):
print("3")
elif(day=="Thursday"):
print("4")
elif(day=="Friday"):
print("5")
elif(day=="Saturday"):
print("6")
elif(day=="Sunday"):
print("7")
else:
print("Not a weekday")
# Write a program to check greater of two given numbers

no1=int(input("Enter First no"))


no2=int(input("Enter Second no"))

if(no1>no2):
print("No1 is greater")
else:
print("No2 is greater")

# Write a program to check whether the number is positive or negative

no=int(input("Enter First no"))


if(no>0):
print("No is positive")
elif(no<0):
print("No is negative")
elif(no==0):
print("No is zero")

# Write a program to check whether the person is allowed to vote or not

age=int(input("Enter Age"))
if(age>=18):
print("You are allowed to vote")
else:
print("You are not allowed to vote")

# use of not equal to


A = 1
B = 2
C = 2
print(A!=B)
print(B!=C)

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.

n1=int(input("Enter First No"))


n2=int(input("Enter Second Number"))
op=input("Enter Operator +, - ,*, / only")
if(op=="+"):
res=n1+n2
print("Result ",res)
elif(op=="-"):
res=n1-n2
print("Result ",res)
elif(op=="*"):
res=n1*n2
print("Result ",res)
elif(op=="/"):
res=n1/n2
print("Result ",res)
else:
print("Enter +,-,*,/ only")

Nested if: When if, is used inside another if then it is said to be


nested if

Single line comment is created with # symbol

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

amount=int(input("Enter billing amount"))


gender=input("Enter Gender")
if(amount>10000):
if(gender=="F"):
amount=amount-amount*0.15
print("amount after 15% discount”, amount)
elif(gender=="M"):
amount=amount-amount*0.10
print("amount after 10% discount”, amount)
else:
print("Mention M of F only")
else:
print("No discount as the amount is not greater than 10000 ")

'''Write a Menu for the following choices


1 for addition
2 for subtraction'''

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

amount=int(input("Enter billing amount"))


gender=input("Enter Gender")
if(amount>10000):
if(gender=="F"):
amount=amount-amount*0.15
print("amount after 15% discount",amount)
elif(gender=="M"):
amount=amount-amount*0.10
print("amount after 10% discount",amount)
else:
print("Mention M of F only")
else:
print("No discount as the amount is not greater than 10000 ")

# write a program to input a number and check whether it is multiple of


5 or not. This check is valid only for positive numbers

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

Augmented Assignment Operator


(+=, -=, *=, /=, //=, %=, **=)

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)

Output will show remainder: 0

a=2
a**=4
print(a)

Output will show power: 16

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:

 Using sequence: a sequence of values is used on which the loop


iterates.

Syntax of for loop


for <counter variable> in <sequence>:
Statements

for i in [1,2,3,4,5]:
print(i)

output
1
2
3
4

for i in ["hello", "how are you", "friends"]:


print(i)

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

 Where for is a keyword


 Counter variable is any identifier that keeps a count of the
loop
 Sequence is any value like integer, string, list
 Statements are always indented can be single or a block

 Using range function

The range ( ) function is an inbuilt function that is used to


generate a set of values between the specified range.

Syntax of for loop

for <var> in range (<start>, <end+1>, <step>):


statements

 for, in, range are keywords


 Start, end and step are parameters of range( ) function and
will always be integers
 Start is starting value of loop and end is ending value + 1 of
loop
 Step is the number of steps taken

# write a program to print the series from 1 to 10 using for loop

for i in range(1,11,1):
print(i)

 If two parameters are used, then the step value becomes 1 by


default

# write a program to print the series from 1 to 10 using for loop

for i in range(1,11):
print(i)

 If start< end, then the step value should be positive integer

# write a program to print the table of 2 using for loop

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)

# write a program to print the odd numbers in 1 to 10 using for loop

for i in range(1,11,2):
print(i)

# write a program to print the table of 10 using for loop

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

# write a program to print the series from 0 to 9 using for loop

for i in range(10):
print(i)

 If start> end, then the step value should be negative integer

for i in range(5,3):
print(i)

output
No output

# write a program to print the series from 10 to 1 using for loop

for i in range(10,0,-1):
print(i)

 If start>=end and step value is not specified, then it assumes as


positive which is an invalid condition to run a loop so the loop
will not execute at all

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

 This loop is also called an entry-controlled loop as it checks for


the condition in the beginning only. If the condition is true, then
the body of the loop will be executed. If the condition is false,
then it will not be allowed to enter within the loop and it stops.

Syntax of the while loop is:

while <condition>
Statements
Increase/ Decrease

 Where, while is a keyword.


 condition is a criterion to repeat the instructions. The
instructions repeat till the condition is True. As soon as the
condition is False, the control exits from the loop.
 Statements are always indented can be single or a block and are
repeated till the condition is True.
 It is used to repeat a set of instructions as long as the condition
is true. It means when the number of iterations are not
fixed/indefinite before we start with the execution of a loop. It
is therefore known as indefinite loop.

Example

# write a program to print the series from 1 to 10 using while loop

i=1
while(i<=10):
print(i)
i=i+1

# write a program to print the series from 10 to 1 using while loop

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

print("sum of first five natural numbers",sum)

You might also like