Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Practical: -01

1.Print the version of python.


Code:-
import sys
print(sys.version)
output:-

Practical 2
1. Write a Python program to display your name using Interactive mode.

2. Write a Python program to display “MSBTE” using script mode.


Practical No. 3
1. Write a program to convert U.S dollars to Indian rupees.
Code:-
print("Conversion of Us dollar to Indian Rupees note:1$=70 INR")
usDollar=int(input("Enter Dollar Amount= "))
inr=usDollar*71.27
print("INR=",inr)
output:-

2. Write a program to convert bits to Megabytes, Gigabytes and Terabytes.


Code:-
print("Conversion of bites to MB,GB,TB")
bits=int(input("Enter number of bits"))
byte=bits/8
kb=byte/1024
mb=kb/1024
gb=mb/1024
tb=gb/1024
print("Megabites=",mb)
print("Gigabites=",gb)
print("Terabites=",tb)
3. Write a program to find Square root of number.
Code:-
import math
print("Finding Square root of a number")
no=int(input("Enter number"))
squareRoot=math.sqrt(no)
print("Square root=",squareRoot)
Output :-

4. Write a program to find area of rectangle


print("Calculating Area of rectangle")
length=int(input("Enter Length="))
breadth=int(input("Enter breadth="))
print("Area=",(length*breadth))
Output: -
5. Write a program to calculate area and perimeter of the square.
Code:-
import math
print("Calculating Area and perimeter of Square")
s=int(input("Enter side="))
print("Area=",s*s)
print("Perimeter=",(4*s))
Output: -

6. Write a program to calculate surface volume and area of cylinder.


print("Calculating area and volume of cylinder")
pi=3.14
r=int(input("Enter Radias="))
h=int(input("Enter height="))
print("Volume=",pi*(r*r))
print("Area=",(2*pi*r*h+2*pi*(r*r)))
7. Write a program to swap the value of two variables.
print("Swapping 2 Numbers")
no1=int(input("Enter 1st no="))
no2=int(input("Enter 2nd no="))
temp=no1
no1=no2
no2=temp
print("1st number:=",no1)
print("2nd number:=",no2)
Output:-
Practical 4
1.Write a program to check whether a number is even or odd.
print("Checking Whether the number is even or odd ")
no=int(input("Enter Number:"))
if no%2==0:
print("Number is even")
else:
print("Number is odd")

2. Write a program to find out absolute value of an input number.


print("Finding the absolute value of the number")
no=int(input("Enter Number:"))
print("Absolute value of ",no,"=",abs(no))
OUTPUT: -
3. Write a program to check largest number among the three numbers.
print("Finding largest number among three numbers")
n1=int(input("Enter 1st no:"))
n2=int(input("Enter 2nd no:"))
n3=int(input("Enter 3rd no:"))
if n1>n2 and n1>n3:
print("This no is the largest",n1)
if n2>n1 and n2>n3:
print("This no is the largest",n2)
if n3>n1 and n3>n2:
print("This no is the largest",n3)

output: -

4. Write a program to check if the input year is leap year or not.


print("Checking whether the number is a leap year or not")
year=int(input("Enter year:"))
if year%4==0:
if year%400==0:
if year%100==0:
print("leap year")
else:
print("not leap year")
else:
print("leap year")
else:
print("Not leap year")

5. Write a program to check if a number is positive, negative or zero.


print("Check if the number is positive, negative, or zero")
n=int(input("Enter no"))
if n==0:
print("Number is zero")
if n<0:
print("Number is negative")
if n>0:
print("Number is positive")
6. Write a program that takes the marks of 5 subjects and display the
grade.
print("Displaying the grades")
print("Enter morks of the following subjects")
math=float(input("Math:"))
c=float(input("c:"))
java=float(input("java:"))
python=float(input("python:"))
cpp=float(input("C++:"))
avg=(math+c+java+python+cpp)/5
print("Average=",avg)
if avg<=100 and avg>=75:
print("Grade=Distinction Pass")
elif avg<75 and avg>=60:
print("Grade=First class Pass")
elif avg<60 and avg>=40:
print("Grade=Pass")
elif avg<40 and avg>0:
print("Sorry you are fail Try again")
else:
print("Invalid marks")
Practical 5
1. What would be the output from the following python code
x=10
while x>5:
print x
x-=1

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

Q. 2 Write a program to print all even numbers between 1 to 100 using


while loop.
num=1
print("Even Numbers from 1-100")
while num<=100:
if num%2==0:
print(num)
num=num+1

Q.6 Write a program to Reverse a given number.


num=int(input("Enter a number to reverse"))
rev=0
print("Original Number ",num)
while num!=0:
digit=int(num%10)
num=int(num/10)
rev=int(rev*10+digit)
print("Reversed Number ",rev)
Q.3 Write a program to find the sum of first 10 natural numbers using for
loop.
num=1
sum=0
print("Sum of numbers from 1-10")
for i in range(1,11):
sum+=i
print("sum= ",sum)

Q.4 Write a program to print Fibonacci series.


max=int(input("Enter Max value"))
a=0
b=1
next_term=a+b
if max>1:
print(a)
print(b)
print("Fibonacci series from 0-",max)
while next_term<=max:
print(next_term)
a=b
b=next_term
next_term=a+b
else:
print("Enter value more than 1")

Q.5 Write a program to calculate factorial of a number.


num=int(input("Enter Number to find factorial"))
fact=1
print("Factorial of ",num)
while num>=1:
fact=fact*num
num=num-1
print(fact)
Q.7 Write a program takes in number and finds the sum of digits in
number.
num=int(input("Enter a number to find sum"))
sum1=0
print("Original Number ",num)
while num!=0:
digit=int(num%10)
num=int(num/10)
sum1=int(sum1+digit)
print("sum= ",sum1)

Q.8 Write a program takes in number and checks whether it is a


palindrome or not.
num=int(input("Enter a number to check whether the number is palindrome or
not "))
rev=0
num1=num
print("Original Number ",num)
while num!=0:
digit=int(num%10)
num=int(num/10)
rev=int(rev*10+digit)
print("Reversed Number",rev)
if num1==rev:
print("Palindrome Number")
else:
print("Not Palindrome Number")

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)

2.Write a python program to multiplies all the items in a list.


list=[1,2,3,4,5,6,7,8,9,10]
print("list=",list)
mul=1
for i in list:
mul=mul*i
print("Multiplication of all the digits in list=",mul)
3.Write a python program to get the largest number from a list.
list=[3,5,8]
print("list=",list)
print("Largest no is",max(list))

4. Write a python program to get the smallest number from a list.


list=[5,7,8,9]
print("list=",list)
print("The Smallest number from list",min(list))

5.Write a python program to reverse a list.


list=[1,2,3,4,5,6,7]
print("list=",list)
list.reverse()
print("Reverse of list=",list)
output:-

6.Write a python program to find common items from two lists.


list1=[10,20,30,40,50]
list2=[30,40,50,60,70]
print("list1=",list1)
print("list2=",list2)
print("common items from list1 and list2=",set(list1)&set(list2))

7.Write a python program to select the even items of a list.


list=[1,2,3,4,5,6,7,8,9,10]
print("List=",list)
print("Even numbers from list=")
for i in list:
if i%2==0:
print(i)
Practical 7
1.Create a tuple and find the minimum and maximum value from it.
t=(1,2,3,4,5,6,7,8,9)
print("Tuple=",t)
print("Minimum value from Tuple=",min(t))
print("Maximum Value from tuple=",max(t))

2.Write a python program to find the repeated items of a tuple.


t=(1,1,2,3,4,5,5,5,6,7)
print("tuple=",t)
print("printing number of 1's in tuple")
count=t.count(1)
print(count)

3.Print the number in words for example:1234=>One Two Three Four


t2=("one","two","three","four","five","six","seven","eight","nine","ten")
print("Printing Number in words")
i=int(input("Enter number between 1 to 10:-"))
if i==1:
print(t2[0])
elif i==2:
print(t2[1])
elif i==3:
print(t2[2])
elif i==4:
print(t2[3])
elif i==5:
print(t2[4])
elif i==6:
print(t2[5])
elif i==7:
print(t2[6])
elif i==8:
print(t2[7])
elif i==9:
print(t2[8])
elif i==10:
print(t2[9])
else:
print("wrong number chooise");
Practical 8
1.Write a python program to create a set ,add members in a set and remove
one item from set.
a={1,2,3,4,5}
print("set=",a)
a.add(6)
print("Adding 6 in set a\n",a)
a.remove(1)#a.discard(3)
print("Removing 1 in set a\n",a)

2.Write a python program to perform the following operations on set: the


intersection of sets, the union of sets, set difference, symmetric difference,
clear a set.
s1={1,2,3,4,5,6,7}
s2={5,6,7,8,9,10}
print("s1=",s1)
print("s2=",s2)
print("intersections of set",s1&s2)
print("Union of set",s1|s2)
print("Difference of seta s1=",s1-s2)
print("Symmetric difference =",s1^s2)
s1.clear()
print("S1 is cleared=",s1)
3.Write a python program to find maximum and minimum value in a set.
s1={1,2,3,4,5}
print("set=",s1)
print("Maximum value=",max(s1))
print("Minimum value=",min(s1))

4.Write a python program to find length of a set.


set={0,1,2,3,4,5}
print("set=",set)
print("length=",len(set))
Practical 9
1.Write a python script to sort(ascending and descending ) a dictionary by
value.
d={1:'one',2:'two',5:'five',3:'three'}
print(sorted(d))

2.Write a python script to concatenate following dictionaries to create a


new one.
dic5={"Sample Dictionary="}
dic1={1:10,2:20}
dic2={3:30,4:40}
dic3={5:50,6:60}
dic4={}
print("dic1=",dic1,"\ndic2=",dic2,"\ndic3=",dic3,)
for d in (dic1, dic2, dic3):
dic4.update(d)
'''dic4.update(dic1)
dic4.update(dic2)
dic4.update(dic3)'''
print("Sample Dictionary=",dic4)
3.Write a python program to combine two dictionary adding values for
common keys.
d1={'a':'100','b':'200','c':'300'}
d2={'a':'300','b':'200','d':'400'}
d3={}
print("d1=",d1,"\nd2=",d2)
d3.update(d1)
d3.update(d2)
print("Combining d1 and d2 into d3=",d3)

4.Write a python program to print all unique values in a dictionary.


L = [{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"},
{"VII":"S005"}, {"V":"S009"},{"VIII":"S007"}]
print("Original List: ",L)
u_value = set( val for dic in L for val in dic.values())
print("Unique Values: ",u_value)
output: -

5.Write a python program to find the biggest 3 values in a dictionary.


from heapq import nlargest
dict={1:234,2:212,3:255,4:243,5:241,6:251}
print("Original Dictionary=",dict)
three_largest = nlargest(3, dict, key=dict.get)
print("Index of elements with the highest index",three_largest)

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:

2. Write a Python program to generate a random float where the value is


between 5 and 50 using Python math module.
Code:
import random.
print("printing random float number between 5 and
50=",random.uniform(5,50))
Output:

Practical No:11

11.1. Write a Python function that takes a number as a parameter and


check the number is prime or not.
Code:
def prime(n):
if n>1:
for i in range(2,n):
break
if(n%i)==0:
print(n,"number is not prime")
else:
print(n,"number is prime")

a=int(input("Enter number:-"))
prime(a)
Output:

11.2. Write a Python function to calculate the factorial of a number (a non-


negative integer). The function accepts the number as an argument.
Code:
def fact(num):
fact=1
if(num<0):
print("Factorial does not exist for negative number")
elif(num==0):
print("Factorial of 0 is 1")
else:
for i in range(1,num+1):
fact=fact*i
print("factorial of number:",fact)
n=int(input("Enter a number="))
fact(n)
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.2:-Write a Python program that will calculate area and circumference


of circle using inbuilt Math Module.
Code
import math
a=float(input("Enter radius of circle:"))
area=math.pi*a*a
sur=2*math.pi*a
print("Area of circle is:",area)
print("Circumference of circle is:",sur)
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:

13.2. Write a Python program to concatenate two strings.


str1="Hello"
str2="Good Morning"
str3=str1+ str2
print(str3)
Output:
13.3. Write a NumPy program to generate six random integers between 10
and 30.
import numpy as np
x=np.random.randint(low=10,high=30,size=6)
print(x)
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:

14.3. Write a Python program to create a class 'Degree' having a method


'getDegree' that prints "I got a degree". It has two subclasses namely
'Undergraduate' and 'Postgraduate' each having a method with the same
name that prints "I am an Undergraduate" and "I am a Postgraduate"
respectively. Call the method by creating an object of each of the three
classes.
Code:
class Degree:
def getDegree(self):
print("I got a degree")
class Undergraduate(Degree):
def getDegree(self):
print("I am an Undergraduate")
class Postgraduate(Degree):
def getDegree(self):
print("I am postgraduate")
d=Degree()
u=Undergraduate()
p=Postgraduate()
d.getDegree()
u.getDegree()
p.getDegree()
Output:
Practical No:15

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.1 .Write a Python program to Check for ZeroDivisionError Exception.


Code:
x=int (input("Enter First value"))
y=int(input("Enter Second value"))
try:
result=x/y
except ZeroDivisionError:
print("Division by Zero")
else:
print("Result is",result)
finally:
print("Execute finally Block")
Output:

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:

You might also like