Python Programs
Session: 2022-23
Prepared by: Nischaya
XI A
1. Write a program to print Welcome message
on the screen.
Program Output
print(“Welcome !!”) Welcome !!
2. Write a program in python to add two
numbers taken as input from the user.
Program Output
print("Program to add two no. ") Program to add two no.
n1 = int(input("Enter 1st no.:")) Enter 1st no.:45
n2 = int(input("Enter 2nd no.:")) Enter 2nd no.:34
nsum=n1+n2 Sum: 79
print("Sum:",nsum)
3. Write a program in python to find out whether a
year entered through keyboard is leap year or not.
Program Output
year = int(input("Enter year:")) Enter year:2024
if (year%4==0): It's leap year!!
print("It's leap year!!")
else:
print("Not a leap year!!")
4. Write a program in python to calculate simple interest where
Principal, Rate and time(in years) taken as input from the user.
Program Output
p=int(input("Enter principle Enter principle amount:5000
amount:")) Enter rate of interest:200
r=int(input("Enter rate of Enter time(in years):2
interest:"))
The simple interest is: Rs.
t=int(input("Enter time(in
20000.0
years):"))
si = (p*r*t)/100
print("The simple interest is:
Rs.",si)
5. Write a program in python to check greatest
among three integers
Program Output
n1 = int(input("Enter 1st no.:")) Enter 1st no.:1
n2 = int(input("Enter 2nd no.:")) Enter 2nd no.:2
n3 = int(input("Enter 3rd no.:")) Enter 3rd no.:3
if (n1 >=n2) and (n1 >=n3): Largest no.: 3
print("Largest no.:",n1)
elif (n2 >= n1) and (n2 >= n3):
print("Largest no.:",n2)
else:
print("Largest no.:",n3)
6. Write a program in python to calculate Total, Percentage, Average marks
scored by a student in 5 subject and also print grade scored by them based
on the criteria provided: (a)Percentage >=90, Grade A ||(b)Percentage >=75
and Percentage<90, Grade B ||(c) Percentage >=60 and Percentage <75,
Grade C ||(d) Grade D for others.
Program
s1 = int(input("Marks in Sceince:")) Output
s2 = int(input("Marks in Mathematics:")) Marks in Sceince:90
s3 = int(input("Marks in Social Sceince:"))
Marks in Mathematics:90
s4 = int(input("Marks in Hindi:"))
s5 = int(input("Marks in English:")) Marks in Social Sceince:90
percen =((s1+s2+s3+s4+s5)/500)*100 Marks in Hindi:90
print("Percentage:", percen)
if (percen >=90):
Marks in English:90
print("Grade A!!") Percentage: 90.0
elif (percen>=75) and (percen<90):
Grade A!!
print("Grade B")
elif (percen>=60) and (percen<75):
print("Grade C")
else:
print("Grade D")
7. Write a program in python to calculate Mathematical
table of a number taken as input from the user
Program Output
no = int(input("Enter no.:")) Enter no.:99
The Multiplication Table of: 99
print("The Multiplication Table 99 x 1 = 99
of:", no)
99 x 2 = 198
for num in range(1,11): 99 x 3 = 297
print(no,"x",num,"=",no*num) 99 x 4 = 396
99 x 5 = 495
99 x 6 = 594
99 x 7 = 693
99 x 8 = 792
99 x 9 = 891
99 x 10 = 990
8.Write a program in python to calculate
factorial of a number
Program Output
init=1 Enter no.:5
n = int(input("Enter no.:")) The Factorial of 5 is 120
for i in range(1, n+1):
init = init*i
print("The Factorial
of",n,"is",init)
9. Write a program in python to display Fibonacci series
up to n terms where n is taken as input from the user[0
1 1 2 3 5 8…………….]
Program Output
term1=0 Enter no.:5
term2=1 01123
last = 0
n = int(input("Enter no.:"))
while (last<n):
print(term1, end=' ')
nth = term1 + term2
term1 = term2
term2 = nth
last = last + 1
10. Write a program in python to find reverse of
a number taken as input from user
Program Output
num = int(input("Enter no.:")) Enter no.:12345
rev = 0 Reverse of given no.: 54321
while (num>0):
r = num%10
rev = (rev*10) + r
num = num//10
print("Reverse of given no.:",rev)
11.Write a program in python to check whether a
number is palindrome or not
Program Output
n=int(input("Enter no.:")) Enter no.:12
temp=n The no. is not Palindrome!
rev=0
while (n>0):
dig = n%10
rev = rev*10+dig
n=n//10
if (temp==rev):
print("The no. is Palindrome!")
else:
print("The no. is not Palindrome!")
12. Write a program in python to check whether
a number is Armstrong or not
Program Output
n = int(input("Enter no.:")) Enter no.:123
ssum = 0 123 is not Armstrong no.
temp = ssum
while temp > 0 :
digit = temp % 10
ssum += digit**3
temp //= 10
if n==ssum:
print(n,"is an Armstrong no.")
else:
print(n,"is not Armstrong no.")
13. Write a menu driven program in python to
have an option to find Area of circle , area of
square and option to exit from the program
print("<Automated Area Calculator>")
print('''Available operations:''')
print(''' 1.Area of Circle''')
print(''' 2.Area of Square''')
print(''' 3.Exit''')
choice = int(input("Enter your choice:"))
if choice==1:
print("<Area of Circle>")
rad = int(input("Enter radius:"))
arc = (3.14*(rad**2))
print("Area:", ar)
elif choice==2:
print("<Area of Square>")
side = int(input("Enter length of side:"))
ars = side**2
print("Area:", ars)
elif choice==3:
exit()
else:
Output
<Automated Area Calculator>
Available operations:
1.Area of Circle
2.Area of Square
3.Exit
Enter your choice:2
<Area of Square>
Enter length of side:16
Area: 256
14. Write a program in python to display factors
of a number entered by the user .
Program Output
x = int(input("Enter no. for Enter no. for factors:15
factors:")) 1 3 5 15
for i in range(1, x+1):
if (x%i==0):
print(i, end=' ')
15. Write a program in python to read a string and check whether it is
palindrome or not
Program Output
x = input("Enter string:") Enter string:pyp
w="" Palindrome !
for i in x:
w=i+w
if (x==w):
print("Palindrome !")
else:
print("Not a Palindrome !")
16. Write a python program to sum all the
items in a list
Program Output
total = 0 Sum of all elements in given list:
146
listl = [15, 19, 13, 99]
for element in range(0,
len(listl)):
total = total + listl[element]
print("Sum of all elements in
given list:", total)
17. Write a python program to count total
number of words in a string
Program Output
listl = input("Enter something:") Enter something:i am inevitable
wlist = listl.split() No. of words: 3
words=len(wlist)
print("No. of words:", words)
18. Write a program in python search the
position of a number in a given list
Program Output
list1=['java','ruby','html','stackov 1
erflow']
print(list1.index('ruby'))
19. Write a program in python to accept the string count number
of digits , alphabets and other character.
Program Output
digits = list('1234567890') Enter something:hello123
alphabets = Total letters found: 5
list('abcdefghijklmnopqrstuvwxyz')
string=list(input("Enter something:"))
Total digits found: 3
total_digits=0
total_alphabets=0
for s in string:
if s in digits:
total_digits += 1
else:
total_alphabets += 1
print("Total letters
found:",total_alphabets)
print("Total digits found:",total_digits)
20.Write a program in python to accept the string and
count the number of vowels in a string
Program Output
string=list(input("Enter Enter something:something
something:")) Number of vowels are: 3
vowel = 0
for i in string:
if(i=='a' or i=='e' or i=='i' or
i=='o' or i=='u' or i=='A' or i=='E'
or i=='I' or i=='O' or i=='U'):
vowel=vowel + 1
print("Number of vowels are:",
vowel)
print(“Thank You”)