0% found this document useful (0 votes)
12 views27 pages

Python Lab Programs

The document contains a series of Python programs that demonstrate various programming concepts, including printing messages, mathematical operations, conditional statements, and data structures like lists, tuples, and dictionaries. Each program is accompanied by example inputs and outputs to illustrate its functionality. Topics covered include basic arithmetic, string manipulation, file handling, and more advanced concepts like recursion and matrix operations.

Uploaded by

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

Python Lab Programs

The document contains a series of Python programs that demonstrate various programming concepts, including printing messages, mathematical operations, conditional statements, and data structures like lists, tuples, and dictionaries. Each program is accompanied by example inputs and outputs to illustrate its functionality. Topics covered include basic arithmetic, string manipulation, file handling, and more advanced concepts like recursion and matrix operations.

Uploaded by

ritikaravo95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] Program to print Hello World!

print('Hello,World!')

Output
Hello,World!
2. .Python Program to Add 2 Numbers.

num1=input('Enter a 1st number:')

num2=input('Enter a 2nd number:')

Sum=float(num1)+float(num2)

print('The sum of {0} and {1} is {2}'.format(num1,num2,Sum))

output:

Enter a 1st number:101

Enter a 2nd number:102

The sum of 101 and 102 is 203.0


3. .Python Program to find the Square Root

num=float(input('Enter a number:'))

num_sqrt=num**0.5

print('The Square root of {0} is {1}'.format (num,num_sqrt))

output:

Enter a number:5

The Square root of 5.0 is 2.23606797749979


4. .Python Program to Calculate the Area of a Triangle.

a=float(input('Enter first side:'))

b=float(input('Enter second side:'))

c=float(input('Enter third side:'))

s=(a+b+c)/2

area=(s*(s-a)*(s-b)*(s-c))**0.5

print('The area of the triangle is {0}'.format(area))

output:

Enter first side:5

Enter second side:6

Enter third side:7

The area of the triangle is 14.696938456699069


5. .Python Program to Solve Quadratic Equation.

import cmath

a=float(input('Enter the value of a:'))

b=float(input('Enter the value of b:'))

c=float(input('Enter the value of c:'))

d=(b**2)-(4*a*c)

sol1=(-b - [Link](d))/(2*a)

sol2=(-b + [Link](d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

output:

Enter the value of a:1

Enter the value of b:5

Enter the value of c:6

The solution are (-3+0j) and (-2+0j)


6. .Python Program to Swap Two Variables.

x=input('Enter value of x:')

y=input('Enter value of y:')

temp=x

x=y

y=temp

print('The value of x after swapping:{}'.format(x))

print('The value of y after swapping:{}'.format(y))

output:

Enter value of x:500

Enter value of y:666

The value of x after swapping:666

The value of y after swapping:500


7. .Python Program to Generate a Random Number.

import random

import datetime

[Link]([Link]())

print([Link]())

print([Link]())

print([Link](10,100))

output:

0.09822590575612977

0.5129968351039604

77
8. .Python Program to Convert Kilometers to Miles.

kilometers=float(input("Enter value of kilometers:"))

conv_fac=0.62137

miles=kilometers*conv_fac

print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))

output:

Enter value of kilometers:3.5

3.50 kilometers is equal to 2.17 miles


9. .Python Program to Convert Celsius To Fahrenheit.

celsius = float(input("Enter the celsius"))

fahrenheit = (celsius * 1.8)+32

print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit'%(celsius,fahrenheit))

output:

Enter the celsius37.2

37.2 degree Celsius is equal to 99.0 degree Fahrenheit


10. .Python Program to Check if a Number is Positive Negative or 0.

num=float(input("Enter a number"))

if num>=0:

if num==0:

print("Zero")

else:

print("This is positive number")

else:

print("This is negative number")

output:

Enter a number: 5

This is positive number

Enter a number: 0

Zero

Enter a number: -3

This is negative number


PART- B

11. .Python Program to Check if a Number is Odd or Even.

num=int(input("Enter a number:"))

if(num%2)==0:

print("{0} is Even". format(num))

else:

print("{0} is Odd".format(num))

output:

Enter a number: 2

2 is Even

Enter a number: -5

-5 is Odd
12. .Python Program to Check Leap Year.

year=int(input("Enter a year:"))

if(year%4)==0:

if(year%100)==0:

if(year%400)==0:

print("{0} is a leap year". format(year))

else:

print("{0} is not a leap year". format(year))

else:

print("{0} is a leap year". format(year))

else:

print("{0} is not a leap year". format(year))

output:

Enter a year: 2000

2000 is a leap year

Enter a year: 2001

2001 is not a leap year


13. .Python Program to Find the Largest Among Three Numbers.

num1=float(input("Enter first number:"))

num2=float(input("Enter second number:"))

num3=float(input("Enter third number:"))

if(num1>=num2)and(num1>=num3):

largest=num1

elif(num2>=num1)and(num2>=num3):

largest=num2

else:

largest=num3

print("The largest number is",largest)

output:

Enter first number:11

Enter Second number:22

Enter third number:33

The largest number is 33.0


14. .Python Program to Demonstrate String functions.

S1='NitiAayog'

S2='And Quite Flows The Don'

S3='1234567890'

S4='Make $1000 a day'

print('S1=',S1)

print('S2=',S2)

print('S3=',S3)

print('S4=',S4)

print('check isalpha on S1,S2')

print([Link]())

print([Link]())

print('check isdigit on S3,S4')

print([Link]())

print([Link]())

print('check isalnum on S1,S2,S3,S4')

print([Link]())

print([Link]())

print([Link]())

print([Link]())

print('check islower on S1,S2')

print([Link]())

print([Link]())
print('check isupper on S2,S2')

print([Link]())

print([Link]())

print('check startswith and endswith on S2')

print([Link]('and'))

print([Link]('and'))

output:

S1= NitiAayog

S2= And Quite Flows The Don

S3= 1234567890

S4= Make $1000 a day

check isalpha on S1,S2

True

False

check isdigit on S3,S4

True

False

check isalnum on S1,S2,S3,S4

True

False

True

False
check islower on S1,S2

False

False

check isupper on S2,S2

False

False

check startswith and endswith on S2

False

False
15. .Python Program to perform List Operation.

names=['anil','amol','aditya','avi','alka']

print(names)

[Link](2, 'anuj')

print(names)

[Link]('zulu')

print(names)

[Link]('avi')

print(names)

i=[Link]('anil')

names[i]='anilkumar'

print(names)

[Link]()

print(names)

[Link]()

print(names)
Output:

['anil', 'amol', 'aditya', 'avi', 'alka']

['anil', 'amol', 'anuj', 'aditya', 'avi', 'alka']

['anil', 'amol', 'anuj', 'aditya', 'avi', 'alka', 'zulu']

['anil', 'amol', 'anuj', 'aditya', 'alka', 'zulu']

['anilkumar', 'amol', 'anuj', 'aditya', 'alka', 'zulu']

['aditya', 'alka', 'amol', 'anilkumar', 'anuj', 'zulu']

['zulu', 'anuj', 'anilkumar', 'amol', 'alka', 'aditya']


16. .Python Program to Tuple Operation.

my_tuple=()

print(my_tuple)

my_tuple=(1,2,3)

print(my_tuple)

my_tuple=(1,"Hello",3.4)

print(my_tuple)

my_tuple=("mouse",[8,4,6],(1,2,3))

print(my_tuple)

ouput:

()

(1, 2, 3)

(1, 'Hello', 3.4)

('mouse', [8, 4, 6], (1, 2, 3))


17. .Python Program to Find the Sum of Natural Numbers.

num=16

if num<0:

print("Enter a positive number")

else:

sum=0

while(num>0):

sum += num

num-=1

print("The sum is",sum)

ouput:

The sum is 136


18. .Python Program to Demonstrate Dictionary operations.

my_dict={'name':'Jack','age':26}

my_dict['age']=27

print(my_dict)

my_dict['address']='Downtown'

print(my_dict)

output:

{'age': 27, 'name': 'Jack'}

{'age': 27, 'name': 'Jack', 'address': 'Downtown'}


19. .Python Program to Convert Decimal to Binary.

dec=10

print("The decimal value of",dec,"is:")

print(bin(dec),"in binary.")

output:

The decimal value of 10 is:

0b1010 in binary.
20. .Python Program to Find ASCII Value of Character.

c='p'

print("The ASCII value of '''+c+'’' is",ord(c))

output:

The ASCII value of '''+c+''' is 112


21. .Python Program to Find Factorial of Number Using Recursion.

def recur_factorial(n):

if n==1:

return n

else:

return n*recur_factorial(n-1)

num=float(input("Enter the number"))

if num<0:

print("Sorry,factorial does not exist for negative numbers")

elif num==0:

print("The facroeial of 0 is 1")

else:

print("The factorial of",num,"is",recur_factorial(num))

output:

Enter the number: 5

The factorial of 5.0 is 120.0


22. .Python Program to Add Two Matrices.

X=[[12,7,3],

[4,5,6],

[7,8,9]]

Y=[[5,8,1],

[6,7,3],

[4,5,9]]

result=[[0,0,0],

[0,0,0],

[0,0,0]]

for i in range(len(X)):

for j in range(len(X[0])):

result[i][j]=X[i][j]+Y[i][j]

for r in result:

print(result)

output:

[[17, 15, 4],

[10, 12, 9],

[11, 13, 18]]


23. Python Program to Illustrate Different Set Operations.

a={10,20,30,40,50,60,70}

b={33,44,51,10,20,50,30,33}

print(a|b)

print(a&b)

print(a-b)

print(b-a)

print(a^b)

print(b^a)

print(a>=b)

print(a<=b)

output:

{33, 70, 40, 10, 44, 50, 51, 20, 60, 30}

{10, 20, 50, 30}

{40, 60, 70}

{33, 51, 44}

{33, 51, 70, 40, 60, 44}

{33, 51, 70, 40, 44, 60}

False

False
24. .Python Program to Demonstrate File Handling.

msg1=’Pay taxes with a smile…\n’

msg2=’I tried , but they wanted money!\n’

msg3=’Don\’t feel bad…\n’

f=open(‘messages’,’w’)

[Link](msg1)

[Link](msg2)

[Link](msg3)

[Link](msg4)

[Link]()

f=open(‘messages’,’r’)

data=[Link]()

print(data)

[Link]()

output:

Pay taxes with a smile…

I tried, but they wanted money!

Don’t feel bad…

It is alright to have no talent!

You might also like