Python Lab
Python Lab
a=int(input("enter a values:"))
b=a
print("the value of b :",b)
Output :
enter a values:420
the value of b:420
M.Sc.IVSemester Page1
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output :
Enter the string: Computer Science
NAME= Computer Science
M.Sc.IVSemester Page2
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
def factorial(x):
if x==1:
return x
else:
return x*factorial(x-1)
n=int(input("Enter a number:"))
if n<0:
print("Negative number does not exit")
elseif n==0:
print("Factorial of zero is:1")
else:
print("The factorial of" ,n," is", factorial(n))
Output:
Enteranumber:5
Thefactorialof5is 120
M.Sc.IVSemester Page3
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
n=int(input("Enter a number:"))
sum=0
n1=n while(n!=0):
sum=sum+(n%10)
n=n//10
print("The sum of",n1,"number is:",sum)
Output:
Enter a number:780
The sum of 780 number is:15
M.Sc.IVSemester Page4
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output :
Enter value of A:15
Enter value of B:20
Enter value of C:10
The quadratic equation is 15x^2+20x+10=0
M.Sc.IVSemester Page5
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output :
Enter the weight in Kilogram :15
After converting 15KG to pounds is:33.075lb
M.Sc.IVSemester Page6
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output:
Enter number of elements:5
Enter the element:90
Enter the element:70
Enter the element:50
Enter the element:30
Enter the element:10
The largest number is 90
M.Sc.IVSemester Page7
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
def fib(n):
a=0
b=1
if n==1:
print(a)
else:
print(a)
print(b)
for i in range(2,n):
c=a+b
a=b
b=c
print(c)
ele=int(input("Enterthenumber:"))
fib(ele)
Output:
Enterthenumber:5 0
1
1
2
3
M.Sc.IVSemester Page8
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
count=0
n=2
print("The first 10 even numbers are:")
while count<10:
print(n)
n=n+2
count=count+1
Output:
Thefirst10evennumbersare: 2
4
6
8
10
12
14
16
18
20
M.Sc.IVSemester Page9
PROBLEMSOLVINGWITHPYTHONLAB
10. Python program to program to demonstrate While loop with else.
count=0
n=2
print("Thefirst10evennumbersare:")
while count<10:
print(n)
n=n+2
count=count+1
else:
print("End of while loop")
Output:
Thefirst10evennumbersare: 2
4
6
8
10
12
14
16
18
20
End of while loop
M.Sc.IVSemester Page10
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
11. Python program to print the prime numbers for a user provided
range.
m=int(input("Enter starting prime number:"))
n=int(input("Enter end prime number:"))
print("Prime number between",m,"to",n,"are:")
for i in range(m,n+1):
if i>1:
for j in range(2,i):
if(i%j)==0:
break
else:
print(i)
Output:
Enter starting prime number:10
Enter end prime number:25
Prime number between10to25are:
11
13
17
19
23
M.Sc.IVSemester Page11
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output :
Enter a character:D
The ASCII value of D is:68
M.Sc.IVSemester Page12
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
13. Python program to find the sum of several natural numbers using
recursion.
def sum_numbers(n):
result=0
if n==1:
result=1
else:
result=n+sum_numbers(n-1)
return result
n=int(input("Enter a number:"))
sum=sum_numbers(n)
print(f"Sum of first",n,"numbers:",sum)
Output:
Enter a number:10
Sum of first 10 numbers: 55
M.Sc.IVSemester Page13
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
14. Python string program for the following functions.
a. Lower()
str="COMPUTERSCIENCE"
print(str.lower())
Output:
computerscience
b. Upper()
str="computerscience"
print(str.uper())
Output:
COMPUTERSCIENCE
c. isdigit()
str="143"
print(str.isdigit())
str="Computer"
print(str.isdigit())
Output:
True
False
d. isspace()
str=""
print(str.isspace())
str="computer"
print(str.isspace())
Output:
True
False
M.Sc.IVSemester Page14
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
e. Find()
f. Replace()
str="Good Morning"
new_str=str.replace("Good","Great")
print(new_str)
Output:
Great Morning
g. Count()
str="Problem solving with Python program"
char_count=str.count("a")
print(char_count)
Output:
1
h. Len()
str="Computer science"
length=len(str)
print("The length of given string:",length)
Output:
The length of given string: 16
M.Sc.IVSemester Page15
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output:
(1,3,5, 7,9)
(2,4,6, 8,10)
M.Sc.IVSemester Page16
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
16. Python program to demonstrate the function that prints a tuple whose
values are the cube of a number between 1and15.
tuple=(1,2)
result=[(num,num**3)for num in tuple]
print(result)
Output:
[(1,1),(2,8)]
M.Sc.IVSemester Page17
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
1. copy()
dict={"a":4,"b":5,"c":6}
dict_c=dict.copy()
dict_c["b"]=2
print(dict)
print(dict_c)
Output:
{'a':4,'b':5,'c':6}
{'a':4,'b':2,'c':6}
2. items()
dict={"a":4,"b":5,"c":6}
for key,value in dict.items():
print(key,"-",value)
Output:
a-4
b -5
c -6
3. update()
dict={"a":4,"b":5,"c":6}
dict_up={"a":8,"m":2,"v":7}
dict.update(dict_up)
print(dict)
Output:
{'a':8,'b':5,'c':6,'m':2,'v':7}
M.Sc.IVSemester Page18
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
4. keys()
dict={"a":10,"b":20,"c":30}
for key in dict.keys():
print(key)
Output:
a
b
c
5. any()
dict={"a":10,"b":20,"c":30}
print(any(dict))
Output:
True
M.Sc.IVSemester Page19
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python
Lab
18. Python program to Write a custom exception that could be raised when the
text entered by a user consists of less than 6 characters.
Class CustomException(Exception):
pass
try:
a=input("Enter a string:")
if len(a) < 6:
raise CustomException
else:
print("String have more than 6 characters")
except CustomException:
print("String have less than 6 characters")
Output :
1. Enter a string:Computer
String have more than 6 characters
2. Enter a string:CS
String have less than 6 characters
M.Sc.IVSemester Page20
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
19.Python program to write a function to print the hash of any given file in
python. (Hint: Use SHA-1 algorithm).
import hashlib
def hash_file(filename):
h = hashlib.sha1()
with open(filename,'rb') as file:
chunk = 0
while chunk!=b'':
chunk=file.read(1024)
h.update(chunk)
return h.hexdigest()
message=hash_file("python18.py")
print(message)
Output :
0ed5842eab466d0e9174d2116dd30d5dadf42fac
M.Sc.IVSemester Page21
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
20. Python program to write a program to write data in a file for both write
and append modes.
Output:
M.Sc.IVSemester Page22
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output:
x= 10
y=20
M.Sc.IVSemester Page23
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
22. Python program that defines a class with two methods: inputStr() that
will get the string using console input and printStr() that will print the
string in uppercase. Also, test the class methods with a function.
Class ab():
def getString(self):
self.s=input('Enter the string:')
def printString(self):
print(self.s.upper())
c = ab()
c.getString()
c.printString()
Output:
M.Sc.IVSemester Page24
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output:
Enter number of rows:6
Full Pyramid Pattern of Stars(*):
*
**
***
****
*****
******
M.Sc.IVSemester Page25
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
24. Python program to find the sum of the following series:1+2+3
+…+n
def sum_of_series(n):
return n*(n+1)//2
n=int(input("Enter the positive integer(n):"))
if n<0:
print("Please enter a positive integer:")
else:
result=sum_of_series(n)
print(f"The sum of the series1+2+........+{n}is:",result)
Output :
Enter the positive integer(n):10
The sum of the series1+2+........+10is: 55
M.Sc.IVSemester Page26
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Import turtle
t=turtle.Turtle()
r = 50
t.circle(r)
Output :
M.Sc.IVSemester Page27
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
26. Python program to draw a shape and fill it with color.
Import turtle
t=turtle.Turtle()
r=int(input("Enter the radius of the circle:"))
col=input("Enter the color name or hex value of color(#RRGGBB):")
t.fillcolor(col)
t.begin_fill()
t.circle(r)
t.end_fill()
Output :
M.Sc.IVSemester Page28
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
27. Python program to display date, time by using built in python modules.
Import time
now = time.ctime()
print("Current time:",now)
Output :
Current time:Wed Nov1002:01:062010
M.Sc.IVSemester Page29
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
28. Python program to display date, month, and year by using built in python
modules.
Output :
Current date:2010-11-10
Current Day:10
Current Month:11
Current Year:2010
M.Sc.IVSemester Page30
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
Output :
0b1010
10 is True
False
True
14
M.Sc.IVSemester Page31
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
30. Python program to remove punctuations from a string.
punctuations='''!()-[]{};:'"\,<>./?@#$%^&*_+=~'''
my_str = input("Enter a string: ")
no_punct=""
for char in my_str:
if char not in punctuations:
no_punct=no_punct+char
print(no_punct)
Output :
Enterastring:CO!@#$MP%^&UT*()-=ER_+}{
COMPUTER
M.Sc.IVSemester Page32
Government Degree Collage & P.G Center,Sindhanur Problem Solving with Python Lab
#Createalist
list = []
#Append elements to the list
list.append(3)
list.append(2)
list.append(1)
#Add the elements of tuple to the list.
list.extend((4, 5, 6))
#Sort the list in ascending order
list.sort()
# Print the list
print("List=",list)
Output :
List=[1,2,3,4,5,6]
M.Sc.IVSemester Page33