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

COMP

Uploaded by

aawasthimail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

COMP

Uploaded by

aawasthimail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

<br>

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

PS C:\Users\Pc\Desktop> & "c:/Program Files/Pythaon312/python. exe c:/Users/Pc/Desktop/p..Py


Enter a number 36
e
clasDriDesktn) & Me:/Proaram Files/Python312/python.exe c:/Users/Pc/Desktop/pl·PY
Enter a number48
Divisible by 3 and 2
PS C:\Users\Pe\Desktop> & "C:/Program Files/Python 312/python.exe c:/Users/Pc/Desktop/pl.py

divisible by 3 not divisible by 2


PS C:\Users\Pc\Desktop>
<br>

2.Write a program to find sum, subtraction, multiplication and division of values.

print("1. Sum of two numbers")

print("2. Subtaction of two numbers")

print("3. Multiplication of two numbers")

print("4. Division of two numbers")


choicesint(input('Enter your chaice')
if choice1:
asint(input(Enter first number)
bsint(input(Enter second number')

cra+b
print("Sum".c)
elif
choice2:
asint(input{(Enter first number')

bint(input('Enter second number)


cua-b

print("Subtraction=",)
elif choicen3:

arint(input(Enter first number)


buint(input('Enter second number)

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>

3. Write a Program to find sum of even numbers from 1


to7
sum-=0
for num in range(8):

if num%2=-0:
sum=sum+numn
print("Sunm of even values=";sum)

PS C: \Users\Pc\ Desktop> & "C:/Progran Files/Python312/python.exe" c:/Users/Pc/Desktop/ p3.py


Sum of even values= 12
PS C:\Users \Pc\ Desktop>
<br>

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.

n=int(input("Enter a number whose table you want="))

while ice10:

printin,"*"4,"n*)

PS C:\Users\Pc\Desktop> & "Ci/Program Files/Python 312/python.exe" ct/Users/Pc/Desktop/p5. py


Enter a number whose table you want9
9X1 =
9

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.

def printinfo( name, age = 35 ): #default argument


print ("Name:", name)
print ("Age", age)

retun

printinfo("'aman" 45)

printinfo("Parth")

PS C:\Users\Pc\ Desktop> & "C:/Program Files/Python312/python.exe" c:/Users/Pc/Desktop/p16.py


Name: aman

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|:

print("value found at key"*)


flag=1

f flague0:
print('value not found")

PS C: \Users\Pc\Desktop> & "C:/Program Files/Python312/python.exe" c:/Users/Pc/Desktop/p6. py


Enter value4
value found at key Arshnoor
PS C:\Users\Pc\Desktop> & "C:/Progran Files/Python312/python. exe" c:/Users/Pc/Desktop/p6.py

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

print ("Largest value%d%b)

return
largest!)

PS C: \Users \Pc \Desktop> & c:/Program Files/Pythons12/python.exe" c:/Users/Pc/Desktop/p7.py

Enter first nunber=98


Enter second nunber=78
Largest value=98
PS C: UsersiPc\Desktop>
<br>

9. Write a Program to find sum of two numbers using a user definedfunction with parameters.

def suma,b): ta andb are formal parameters


c-a+b
print"'sum=",c)

sum(4,5)

nin2-evaltinput('Enter two values')

sumni,n2)

PS C:\Users\Pe\Desktop> &
"C:/Program Files/Python312/python.exe c:/Users/Pc/Desktop/p8.py
Su 9

Enter two values4,5


Sut 9
PS C:\Users\Pc\Desktap> &
"C:/Program Files/Python312/python .exe c:/Users/Pe/Desktop/p8.py
Su 9

Enter two values 6,7


sut 13
PS C:\Users\Pc\Desktop> &
"C:/Program Files /Python312/python .exe c:/Users/Pc/Desktop/p8.py
Sune 9

Enter two values 6,5


sut= 11
PS C:\Users\ Pc\Desktop> |
<br>

10. Write a Progrm to pass a


list as function argument and modify it.

def changeme( mylist

print ("inside the function before change", mylist}


mylist(O]=1000

print ("inside the function after change", mylist)

retum

= (10,20o,30)
listi

print ("outside function before caling function", ist1)


changeme ist1)
print ("outside function after calling function", list1)

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

def random numx,y:

return random.randint(xv)

ni=intinput("Enter the first number:")


n2-int(input("Enter the second number:"))
print("Random No.1:",fandom_ numini,n2))
print("Random No.2:",andom_num(n1,n2))

print("Random No.3:",random_ numinl,n2))

PS C:\Users\Pe\Desktop> & "C:/Program Files/Pythan312/python.exe" c:/Users/Pe/Desktop/pl1.py


Enter the first number:7
Enter the second nunber:8
Random No. 1: 7
Randon No,2: 8
Randon No.3: 7
PS C:\Users\Pc\Desktop> & "C:/Program Files/Pythan312/python.exe" c:/Users/Pc/Desktop/pl1.py
Enter the first number: 78
Enter the second nunber:98
Rarndom No.1: 93
Random No.2: 88
Randon No.3: 93
PS C:\Users\Pc\Desktop>
<br>

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

$2= ingut{"Enter Second String:")

print("The Stringis same length?"check(s1,s2))

PS C: \Users\Pc\Desktop> & "C:/Program Files/Python 312/python.exe" c:/Users/Pc/Desktop/p12.py


Enter First String: Digvijay
Enter Second String: Manas
The String is same length? False
PS C:\Users\Pc\Desktop> & "C:/Program Files/Python 312/python .exe" c:/Users/Pc/Desktop/p12.py

Enter First String: Digi


Enter Second String: Manu
The String is same length? True
PS C:\Users\Pc\Desktop>
<br>

13. Write a pvthon programtotake input for 3 numbers, check and printthe largest number?

a=int[input"Enter 1st no "))


b=int(input(Enter 2nd no ")

cint(input("Enter Jrd no ")


f(a>b and a>c):

m=a

else

iffb>c:
m=b

else:

print("Max no =",m)

PS C:\Users\ Pe\Desktop> & "C:/Program Files/Python312/python.axe" c:/Users/ Pc/Desktop/p13.py


Enter 1st no 76
Enter 2nd no 92
Enter 3rd no 12
Max no = 92
py
PS C:\Users\ Pc\Desktop> & "C:/Program Files/Python312/python.exe" c:/Users/Pe/Desktop/p13.
Enter 1st no 12
Enter Znd no 21
Enter 3rd no 13
Max no = 21
PS C:\Users \Pc\Desktop>|
<br>

14. pvthon programtotake input for 2 numbers and an operator(+.-,)


Write a Based on the
pperator caleulate and print the result?

a=int[lnput('Enter 1st no ")


beint(inputEnter 2nd no ")
op=input"Enter the operator (+*h)

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

PS C:\Users\Pe\Desktop» & "C:/Progran Files/Pythan312/python.exe e:/Users/Pc/Desktop/p14.py


Enter 1st no 14

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>

15. Write a program to find weather an inputted number is perfect ot not

def pernum(num):
divsum 0
for in rangel1,num):
i

if numi =-0:
divsum +

if divsum == numprint("Number ls perfect")


else:print(" Number is not perfect")

X=int(input('Enter a number"))
pernumx)

PS C:\Users\Pc\Desktop> & "C:/Progran Files/Python312/python.exe c:/Users/Pc/Desktop/p15.py


Enter a number 14
Number is not perfect
PS C:\Users\Pc\Desktop> & "C:/Program Files/Python312/python.exe" c:/Users/Pc/Desktop/p15.py
Enter a number 496
Number is perfect
PS C:\Users\Pe\Desktop>I

You might also like