Suraj Tiwari Python Programming
Suraj Tiwari Python Programming
FILE
Submitted By:
Name- Suraj Kumar Tiwari
Roll_no- GU-2019-3165
Class- B.TECH (ECE)
Semester- 5th
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
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.
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))
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.extend(‘ Green’ )
print(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])
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)
12
Page
21.Write a program to print 1 to 5 using ‘ for loop’ .
Program:
for i in range (1,6):
print(i)
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)
print(i)
Page
Output:
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:
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