COMP
COMP
PYTHON PROGRAMS
LWrite aDrogram to check whether a
number is divisible by 2 or 3 using nested if
num=float(input('Enter a number))
if num%2-0:
if num%3--0:
print ("Divisible by 3 and 2")
else
print ("divisible by 2 not divisible by 3")
else:
if num%3=-0:
print ("divisible by 3 not divisible by 2")
else
print ("not Divisible by 2 not divisible by 3")
cra+b
print("Sum".c)
elif
choice2:
asint(input{(Enter first number')
print("Subtraction=",)
elif choicen3:
Cea'b
print("Multiplication", c)
elif choicen4:
asintinput(Enter first number')
brintlinput('Enter second number)
cra/b
print("Division",)
else
print("Wrong choice")
<br>
PS C: .
\Users\ Pc\Desktop> & "C:/Program Files/Python 312/python exe" c:/Users/Pc/Desktop/p2
1. Sum of two numbers
2. Subtaction of two numbers
3. Multiplication of two numbers
4. Division of two numbers
Enter your choice4
Enter first number48
Enter second number3
Division 16.0
PS C:\Users\Pc\Desktop>
if num%2=-0:
sum=sum+numn
print("Sunm of even values=";sum)
4.
Write a
Program to find factorialof a
number
n=intinput("Enter a number-")
while i<en:
f-i
iitl
print("Eactorial of"n,""0
S C:\
Users|Pc\ Desktop) &
"C:/Progran Files/Pythonš12/python.exe" c:/Users/Pc/Desktop/p3.py
Enter a nunbere5
Factorial of 5 = 128
PS C:\Users\Pc\Desktop>
|
<br>
5. Write a
Program to print table of any number.
while ice10:
printin,"*"4,"n*)
9 X2 18
9X3= 27
9 X4 = 36
45
9X5-
9x6- 54
9 x 7= 63
9X 8 = 72
9 X9- 81
9X 9e
18
PS C:\Users\Pc\Desktop>
<br>
6.
Write a Progranm to use default arguments in a function.
retun
printinfo("'aman" 45)
printinfo("Parth")
Age 45
Name: Parth
Age 35
PS C:\Users\Pc\Desktop>
<br>
7.
Write a Program to check whether a value exists in dictionarv.
aDict=('Bhavna':1,"Richard":2,"Firoza"3,"Arshnoor"4)
val=int{input('Enter value)
fag-0
for k in aDict:
if val=na Dict(k|:
f flague0:
print('value not found")
Enter value5
value not found
PS C:\Users\Pc|Desktop>
<br>
8. Write a
Programto find largest among twonumbers using a ISer defned function,
def largest):
a=intinput"Enter first number="})
b=int(input("Enter second number=")
if a>b
print ("Largest value=%d%a)
else
return
largest!)
9. Write a Program to find sum of two numbers using a user definedfunction with parameters.
sum(4,5)
sumni,n2)
PS C:\Users\Pe\Desktop> &
"C:/Program Files/Python312/python.exe c:/Users/Pc/Desktop/p8.py
Su 9
retum
= (10,20o,30)
listi
PS C: \Users\Pc\Desktop> &
"C:/Program Files/Python312/python. exe" c:/Users/Pc/Desktop/p9.py
outside function before calling function [18, 20, 30]
inside the function before change [10, 20, 30)
inside the function after change [100, 20, 30]
outside function after calling function [10, 28, 3e]
PS C:\Users\Pe\Desktop>
<br>
IL. Write a function that receives two numbers and generates a randomn number from that range. Using
unction the main should be ahle to nrint three numbers randamly
import random
return random.randint(xv)
12. Write a
function that receivestwo string arguments and checks whether they are same-length strings. (retuns
True in this case otherwise False).
def check(sls2):
flen[s1) == len(s2):
return True
else:
return False
$1 = input(" Enter First String: ")
13. Write a pvthon programtotake input for 3 numbers, check and printthe largest number?
m=a
else
iffb>c:
m=b
else:
print("Max no =",m)
f(op=="":
Cza+b
print("Sum="c)
alifop-=*"):
cab
print("Product ="c)
eliffop=-"k
ifa>b):
cra-b
else:
cab-a
print("Difference ="c)
eliffop""):
c=a/b
printt"'Division =".c)
else
print('inyalid operator")
Enter 2nd no 42
Enter the operator (t,) -
=
Difference 28
PS C:\Users\Pc\Desktop> & "C:/Program Files/Python312/python.axe" c:/Users/Pc/Desktop/pi4. py
Enter 1st no 45
Enter znd no 76
Enter the operator (*,,"/) s
Invalid operator
PS C:Users\Pe\Desktop>
<br>
def pernum(num):
divsum 0
for in rangel1,num):
i
if numi =-0:
divsum +
X=int(input('Enter a number"))
pernumx)