Python Programs
Python Programs
2. Basic #taking two numbers from user The first no. =30
arthematic a=int(input("The first no. =")) The second no. =5
operation b=int(input("The second no. =")) The sum of two numbers = 35
#adding two numbers The difference of two numbers
res=a+b = 25
print("The sum of two numbers =", res) The product of two numbers =
#subtratcting two numbers 150
res=a-b The quotient of two numbers =
print("The difference of two numbers =", res) 6.0
#multiplying two numbers The quotient of floor division of
res=a*b two numbers = 6
print("The product of two numbers =", res) The remainder of two numbers
#dividing two numbers =0
res=a/b 30 raised to the power 5 =
print("The quotient of two numbers =", res) 24300000
#floor division of two numbers
res=a//b
print("The quotient of floor division of two
numbers =", res)
#finding remainder of two numbers
res=a%b
print("The remainder of two numbers =", res)
#raising a number
res=a**b
print(a, "raised to the power", b, "=", res)
OR
#Or
Ch-9
No Satement Program Ouptput
1. F-7 of book #defining Enter speed =220
def speed(a): Demerit points = 30.0
if 70>=a: License suspended
print("OK")
else:
d=(a-70)/5
print("Demerit points =",d)
if d>12:
print("License suspended")
#taking value from user
a=int(input("Enter speed ="))
speed(a)
2. Write a #defining function Enter no. = 6
function that def speed(a): 14
returns the s=0
sum of for i in range(a + 1):
multiples of if i % 3 == 0 or i % 5 == 0:
3 and 5 s += i
between 0 return s
and the limit
For ex, if the #taking value from user
limit is 10, a = int(input("Enter no. = "))
then it print(speed(a))
returns the
sum of 3, 5,
6, 9, 10, that
is 33.
3. F-6 a=int(input("Enter a no. =")) Enter a no. =15
if a%3==0 and a%5==0: IceWater
print("IceWater")
else:
if a%3==0:
print("Ice")
elif a%5==0:
print("Water")
else:
print(a)
4. Prime or a = int(input("Enter a number: ")) Enter a number: 8
composite status=False Composite number
no. if a==0 or a==1:
print("Not prime nor composite")
else:
for i in range(2, a):
if a%i==0:
status=True
break
if status==True:
print("Composite no.")
else:
print("Prime no.")
5. Print the a=int(input("Enter 1st student's age=")) Enter 1st student's age=38
youngest b=int(input("Enter 2nd student's age=")) Enter 2nd student's age=43
and oldest c=int(input("Enter 3rd student's age=")) Enter 3rd student's age=5
age of if a>b and a>c: 2nd student is eldest
student from print("1st student is eldest") 3rd student is youngest
3 students. elif b>a and b>c:
print("2nd student is eldest")
else:
print("3rd student is eldest")
11. Print a rows = int(input("Enter the number of rows: ")) Enter the number of rows: 5
pyramid *
for i in range(rows): ***
print(" " * (rows - i - 1), end="") *****
print("*" * (2 * i + 1)) *******
*********
12. Leap year or year = int(input("Enter a year: ")) Enter a year: 2023
not 2023 is not a leap year.
if (year % 4 == 0 and year % 100 != 0) :
print(year, "is a leap year.")
elif(year % 400 == 0):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
13. Swapping no. a=int(input("Enter 1st no.")) Enter 1st no.6
Using 3 b=int(input("Enter 2nd no.")) Enter 2nd no.4
variables c=a 1st no. is 4
a=b 2nd no. is 6
b=c
print("1st no. is", a)
print("2nd no. is", b)
14. Swapping no. a=int(input("Enter 1st no.")) Enter 1st no.5
Using 2 b=int(input("Enter 2nd no.")) Enter 2nd no.98
variables a=a+b 1st no. is 98
b=a-b 2nd no. is 5
a=a-b
print("1st no. is", a)
print("2nd no. is", b)
15. Print- r=int(input("Enter the no of rows:")) Enter the no of rows:4
1 for i in range(r): 1
12 for j in range(i+1): 12
123 print(j+1, end='') 123
1234 print() 1234
16. Print- r=int(input("Enter the no of rows:")) Enter the no of rows:4
1 for i in range(r): 1
22 for j in range(i+1): 22
333 print(i+1, end='') 333
4444 print() 4444
17. Print- r=int(input("Enter the no of rows:")) Enter the no of rows:4
1234 for i in range(r): 1234
123 for j in range(r-i): 123
12 print(j+1, end='') 12
1 print() 1
18. Print- r=int(input("Enter the no of rows:")) Enter the no of rows:4
4444 for i in range(r, 0, -1): 4444
333 for j in range(i): 333
22 print(i, end='') 22
1 print() 1
19. Print no. a=int(input("Enter a no:")) Enter a no:7
Names form if a==1: Seven
1 to 9 using print("One")
if-else elif a==2:
print("Two")
elif a==3:
print("Three")
elif a==4:
print("Four")
elif a==5:
print("Five")
elif a==6:
print("Six")
elif a==7:
print("Seven")
elif a==8:
print("Eight")
elif a==9:
print("Nine")
else:
print("Choose between 0 and 9")
20. Print the a=int(input("Enter a no:")) Enter a no:5
decade if a==1: Fifty
name using if print("Ten")
else elif a==2:
print("Twenty")
elif a==3:
print("Thirty")
elif a==4:
print("Fourty")
elif a==5:
print("Fifty")
elif a==6:
print("Sixty")
elif a==7:
print("Seventy")
elif a==8:
print("Eighty")
elif a==9:
print("Ninety")
else:
print("Choose a decade no.")
21. Print no. a=int(input("Enter a no:")) Enter a no:17
Names from if a==11: Seventeen
11 to 19 print("Eleven")
elif a==12:
print("Twelve")
elif a==13:
print("Thirteen")
elif a==14:
print("Fourteen")
elif a==15:
print("Fifteen")
elif a==16:
print("Sixteen")
elif a==17:
print("Seventeen")
elif a==18:
print("Eighteen")
elif a==19:
print("Nineteen")
else:
print("Choose a no. between 11 and 19")
22. No. Names ones=["Zero", "One", "Two", "Three", "Four", Enter a no.96
from 0-99 "Five", "Six", "Seven", "Eight", "Nine"] Ninety Six
teen=["Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen"]
tens=["", “”, "Twenty", "Thirty", "Forty", "Fifty",
"Sixty", "Seventy", "Eighty", "Ninety"]
a=int(input("Enter a no."))
r=a%10
q=a//10
if a < 10:
print(ones[r])
elif a < 20:
print(teen[r])
else:
if r==0:
print(tens[q])
else:
print(tens[q], ones[r])
23. No. Names ones=["Zero", "One", "Two", "Three", "Four", Enter a no.357
from 0-999 "Five", "Six", "Seven", "Eight", "Nine"] Three Hundred Sixty Seven
teen=["Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen"]
tens=["", "Twenty", "Thirty", "Forty", "Fifty",
"Sixty", "Seventy", "Eighty", "Ninety"]
a=int(input("Enter a no."))
h=a//100 # hundreds place
t=(a%100)//10 # tens place
r=a%10 # ones place
Extra
No Satement Program Ouptput
1. Check a=int(input("Enter the no. of elements in a list:")) Enter the no. of elements in a
whether a list_1=[] list:3
list is for i in range(a): Enter element:12
palindrome. b=input("Enter element:") Enter element:33
list_1.append(b) Enter element:12
print(list_1) ['12', '33', '12']
Palindrome
list_2=list_1[::-1]
if list_1==list_2:
print("Palindrome")
else:
print("Not a palindrome")
2. Check how a=int(input("Enter the no. of studnets:")) Enter the no. of studnets:4
many list_1=[] Enter the grade:A
students got for i in range(a): Enter the grade:B
A grade. b=input("Enter the grade:") Enter the grade:A
list_1.append(b) Enter the grade:C
2
print(list_1.count("A"))
3. Calculate def square(): Enter a no :2
square using a=int(input("Enter a no :")) 4
def s=a*a
return s
print(square())
4. Calculate CI def interest(): Enter the principal:1000
and SI using p=int(input("Enter the principal:")) Enter the rate:10
def r=int(input("Enter the rate:")) Enter the time period:2
t=int(input("Enter the time period:")) SI = 200.0
si=(p*t*r)/100 CI = 210.00000000000023
ci=(p*((1+r/100)**t))-p
print("SI =", si)
print("CI =", ci)
interest()
5. Follow these # Step 1: Input elements in a list Enter the first element
steps in lst=[] (integer): 32
coments first=int(input("Enter the first element: ")) The final tuple is: (32, 64)
[Link](first)