Python 1 To 16 Practical
Python 1 To 16 Practical
Python 1 To 16 Practical
Practical 2
1. Write a Python program to display your name using Interactive mode.
output: -
2.Change the following python code from using a while loop to for loop.
Print the pattern using loop
for i in range(0,4):
print("\r")
for j in range(0,i+1):
print("*",end="")
n=0
rows=3
for i in range(1, rows + 1):
for j in range (1, (rows - i) + 1):
print(end = " ")
while n != (2 * i - 1):
print("*", end = "")
n=n+1
n=0
print()
k=1
n=1
for i in range(1, rows):
for j in range (1, k + 1):
print(end = " ")
k=k+1
while n <= (2 * (rows - i) - 1):
print("*", end = "")
n=n+1
n=1
print()
output:-
Code:-
rows=5
k=1
n=1
num=0
for i in range(1, rows):
if(rows%2==0):
num=0
else:
num=1
for j in range (1, k + 1):
print(end = " ")
k=k+1
while n <= (2 * (rows - i) - 1):
if rows%2==0:
if num==1:
print("1", end = "")
n=n+1
num=0
else:
print("0", end = "")
n=n+1
num=1
else:
if num==1:
print("1", end = "")
n=n+1
num=0
else:
print("0", end = "")
n=n+1
num=1
n=1
print()
output:-
Practical 6
1.Write a python program to sum all the items in the list .
list=[1,2,3,4,5,6,7,8,9,10]
print("list=",list)
sum=0
for i in list:
sum=sum+i
print("Sum of all the digits in list=",sum)
Practical No.10
1. Write a Python function that accepts a string and calculate the number
of upper case letters and lower case letters.
Code:
def check(str):
upper=0
lower=0
for i in str:
if i.isupper():
upper+=1
elif i.islower():
lower+=1
else:
pass
print("Upper case=",upper)
print("Lower case=",lower)
str=input("Enter string to calculate upper and lower cases in string\n")
check(str)
Output:
Practical No:11
a=int(input("Enter number:-"))
prime(a)
Output:
11.3. Write a Python function that accepts a string and calculate the
number of upper case letters and lower case letters.
def fun(str):
lower=0
upper=0
for i in range(len(str)):
if(str[i]>='A' and str[i]<='Z'):
upper+=1
elif(str[i]>='a' and str[i]<='z'):
lower+=1
else:
print(" ")
return(upper,lower)
str="NMPI computer dept"
print("College name=",str)
u,l=fun(str)
print("Uppercase character=",u)
print("Lowercase character=",l)
Output:
Practical No:12
12.1. Write a Python program to create a user defined module that will ask
your college name and will display the name of the college.
Code:
import A
print("your college name is",A.display());
Output:
12.3. Write a Python program that will display Calendar of given month
using Calendar Module.
Code:
import calendar
y=2021
m=6
print(calendar.month(y,m))
Output:
Practical No:13
13.1. Write a Python program to create two matrices and perform addition,
subtraction, and multiplication and division operation on matrix.
Code:
import numpy
x=numpy.array([[1,2],[4,5]])
y=numpy.array([[7,8],[9,10]])
print("Addition of 2 matrix",numpy.add(x,y))
print("Substration of 2 matrix",numpy.subtract(x,y)) print("multiply of 2
matrix",numpy.multiply(x,y)) print("Divictiom of 2 matrix",numpy.divide(x,y))
Output:
Practical No:14
14.1. Write a Python program to create a class to print an integer and a
character with two methods having the same name but different sequence
of the integer and the character parameters. For example, if the
parameters of the first method are of the form (int n, char c), then that of
the second method will be of the form (char c, int n)
class display:
Code:
def printmsg(self,x,y):
if type(x)==int:
print("integer message=",x)
else:
print("Character message=",x)
disp=display()
disp.printmsg(20,"abc")
disp.printmsg("abc",20)
Output: -
14.2. Write a Python program to create a class to print the area of a square
and a rectangle. The class has two methods with the same name but
different number of parameters. The method for printing area of rectangle
has two parameters which are length and breadth respectively while the
other method for printing area of square has one parameter which is side
of square.
Code:
class Area:
def print_area(self,l,b=None):
if b is not None:
print("Area of rectangle=",l*b)
else:
print("Area of square=",l*l)
area=Area()
area.print_area(11,11)
area.print_area(13)
Output:
15.1. Create a class Employee with data members: name, department and
salary. Create suitable methods for reading and printing employee
information.
Code:
class Employee:
def get_emp(self,name,department,salary):
self.name=name
self.department=department
self.salary=salary
def put_emp(self):
print("Name=",self.name)
print("Department=",self.department)
print("Salary=",self.salary)
employee=Employee()
nm=input("Enter name:-")
dept=input("Enter department:-")
sal=input("Enter salary:-")
employee.get_emp(nm,dept,sal)
employee.put_emp()
Output:
15.2. Python program to read and print students information using two
classes using simple inheritance.
Code:
class student:
def getdata(self,r,n):
self.rollno=r
self.name=n
def putdata(self):
print("Rollno=",self.rollno)
print("Name=",self.name)
class result(student):
def getm(self,p):
self.per=p
def putm(self):
print("Percentage=",self.per)
s=result()
s.getdata(10,"Swransh")
s.putdata()
s.getm(96)
s.putm()
Output:
15.3.Write a Python program to implement multiple inheritance.
Code:
class Father:
def Business(self):
print("Father enjoys business work")
class Mother:
def cooking(self):
print("Mother enjoys cooking")
class Child(Father,Mother):
def playing(self):
print("Child loves playing")
c=Child()
c.Business()
c.cooking()
c.playing()
Output:
Practical No.16
16.2 Write a Python program to create user defined exception that will
check whether the password is correct or not?
Code:
class error(Exception )
pass
def verify_password(pswd):
if(str(pswd)!="ABC"):
raise error
else:
print("valide password="+str(pswd))
verify_password("ABC")
verify_password("xyz")
Output: