PYTHON PRACTICAL
FILE
Submitted By:
Name- Suraj Kumar Tiwari
Roll_no- GU-2019-3165
Class- B.TECH (ECE)
Semester- 5th
Submitted to – ER. Anchal Nayyar
1
Page
1.Write a program to print your name.
print("Suraj tiwari")
2.Write a program for addition ,subtraction, multiplication and division of two numbers.
a=10
b=5
add=a+b
sub=a-b
mul=a*b
div=a/b
print("Additon of a and b is:",add)
print("Subtraction of a and b is:",sub)
print("Multiplication of a and b is:",mul)
print("Division of a and b is:",div)
2
Page
3.Write a program to assign multiple values to multiple variables in one line.
X=20
Y=10
Z=30
print(X)
print(Y)
print(Z)
X,Y,Z="Suraj","Tiwari",21
print(X)
print(Y)
print(Z)
3
Page
4.Write a program to assign same value to multiple variables in one line.
x=y=z=33
print(x)
print(y)
print(z)
5.Write a program to create local and global variables.
global_var="SURAJ TIWARI"
def fun():
local_var='programming language'
print("python is",local_var)
fun()
print("Accessing global variable:",global_var)
6.Write a program to crate a global variable inside a function.
4
Page
x="Language"
def func():
global x
x="Programming Language"
print("Python is:",x)
print("Python is:",x)
func()
#after calling the func function the scope of x will change local to global
print("Python is:"+x)
7.Write a program to perform type conversion using int(),float() and
complex() methods.
x,y=10,20.55
z=30
print(x)
print(y)
print(z)
X=int(y)
Y=complex(z)
Z=float(x)
print("After conversion")
print(X)
print(Y)
print(Z)
5
Page
8.Write a program in reverse order with space between them.
fname=input("Enter your first name:")
lname=input("Enter your last name:")
full_name=lname+" "+fname
print("first name is:"+fname)
print("Last name is:"+lname)
print("full name in reverse order with space is:"+full_name)
9.Write a program to calculate simple interest.
P=300
R=2
T=5
SI=(P*R*T)//100
print("simple interset is:",SI,"%")
6
Page
10.Write a program to accept a string “institution” and display it in
uppercase. Also, calculate the length of string.
string="institution"
print(string.upper())
print("Lenght of a string('institution') is:",len(string))
11.Write a program to accept a string “introduction to python” and display
first alphabet of each word in uppercase.
7
string="introduction to python"
Page
print(string.title())
12.Write a program to accept a string “gLoBal VarIableS” and display the
string with changed case.
string="gLoBal VarIableS"
print("changed case of string is",string.swapcase())
8
Page
13.What will be the output of the following program?
Program:
x=[‘ RED’ ,3]
x.extend(‘ Green’ )
print(x)
14.Write a program to convert the string to list of characters.
Program:
x=”GNA UNIVERSITY”
print(list(x))
9
Page
15.Given a list :
y=[‘ abc’ ,’ xyz’ ,’ jkl’ ,’ ghi’ ,’ eer’ ,’ dfg’ ]
Write a program to display the output as:
[‘ abc’ ,’ jkl’ ,’ eer’ ]
Program:
y=[‘ abc’ ,’ xyz’ ,’ jkl’ ,’ ghi’ ,’ eer’ ,’ dfg’ ]
print(y[::2])
16.Write a program to add ‘ value’ to list:
mylist=[‘ element’ ,’ random’ ,’ section’ ]
and remove ‘ random’ from list.
Program:
mylist=[‘ element’ ,’ random’ ,’ section’ ]
mylist.append(‘ value’ )
mylist.remove(‘ random’ )
print(mylist)
10
Page
17.Write a program to display last second elememt from list using the concept
of negative indexing.
Program:
x=[‘ Apple’ ,’ DELL’ ,’ acer’ ,’ HP’ ]
print(x[-2])
18.Write a program to prompt a user to enter two numbers, a=20 and b=10.
Also, check if a is greater than b using ‘ ifstatement’ .
Program:
a=int(input(“enter the value of a”))
b=int(input(“enter the value of b”))
if(a>b):
print(a,”is greater than “,b)
19.Write a program to find greatest number using if-else statement.
Program:
x=int(input(“enter the value of x”))
11
y=int(input(“enter the value of y”))
if(x>y):
Page
print(x,”is greatest number”)
else:
print(y,”is greatest number”)
20.Write a program in python that consists of ‘ if-elif-else’ statement.
Program:
a=int(input(“enter the value of a”))
b=int(input(“enter the value of b”))
if(a>b):
print(a,”is greater than “,b)
elif(a==b):
print(a,”is equal to”,b)
else:
print(b,”is greater than”,a)
12
Page
21.Write a program to print 1 to 5 using ‘ for loop’ .
Program:
for i in range (1,6):
print(i)
22.Write a program to print 1 to 5 using ‘ while loop’ .
Program:
i =1
while (i<=5):
print(i)
i =i+1
23.Write a program to demonstrate the use of ‘ break statement’ .
Program:
For i in range(1,10):
13
If(i==3):
Page
print(i)
24. Write a program to demonstrate the use of break statement.
Program:
for i in range(1,6):
if(i==3):
break;
print(i)
25. write a program to demonstrate the use of continue statement.
Program:
for i in range(1,6):
if(i==3):
continue;
14
print(i)
Page
Output:
26. Write a program in python to import any module.
Program:
Def add(a,b):
Return a + b
Def subtract(a,b):
Return a – b
Def multiply(a,b):
Return a * b
Def divide(a,b):
Return a / b
Save the file as calculator.py then open compiler and type
Import calculator
Result1 = calculator.add(2,3)
Print(result1)
Result2 = calculator.subtract(6,3)
Print(result2)
Result3=calculator.multiply(10,3)
Print(result3)
Result4=calculator.divide(4,2)
15
Print(result4)
Page
Output:
27. Write a program to access the attributes of a class.
Program:
Class rectangle:
Length=10;
Breadth=5;
16
R1= rectangle ()
Page
Print(R1.length)
Print(R1.breadth)
Output:
28. Write a program to calculate area of rectangle. Pass the length and
breadth of the rectangle to the method named calc_rect_area()
Program:
Class rectangle:
Def calc_area_rect(self,length,breadth):
Print(‘length = ‘,length)
Print(‘breadth = ‘,breadth)
Return length*breadth
Ob1 = rectangle()
Print(‘Area of rectangle is ‘,obl.calc_area_react(5,4))
Output:
29. write a program to overload the + operator and perform addition of two
17
objects.
Page
Program:
Class OprOverloadingDemo:
Def __init__(self,X):
Self.X = X
Def ___add__(self,other):
Print(‘ the value of Ob1 = ‘, self.X)
Print(‘ the value of Ob2 = ‘,other.X)
Print(‘ the addition of two objects is:’ ,end= ‘’)
Return ((self.X+other.X))
Ob1 = OprOverloadingDemo(20)
Ob2 = OprOverloadingDemo(30)
Ob3 = Ob1+Ob2
Print(Ob3)
Output:
30.Write a program to demonstrate the concept of multilevel inheritance.
Program:
Class A:
Name = ‘ ’
Age=0
Class B(A):
Height= ‘ ’
18
Class C(B):
Page
Weight = ‘ ’
Def Read(self):
Print(‘Please enter the following values’)
Self.name=input (‘enter name:’)
Self.age=(int(input(‘Enter age:’)))
Self.height=(input(‘enter Height:’))
Self.weight=(int (input(‘Enter Weight:’))0
def Display(self):
print(‘entered values are as follows’)
print(‘name = ‘ ,self.name)
print(‘age = ‘ ,self.age)
print(‘height = ‘ ,self.height)
print(‘ weight = ‘ ,self.weight)
B1=C( )
B1.Read( )
B1.Display( )
Output:
31. Write a simple program on inheritance.
Program:
class A:
print(‘hello i am in base class’)
class B(A);
print(‘WOW!! Great ! I am Derived class’)
ob2 = A( )
19
Output:
Page
Page
20