PYTHON PROGRAMMING
LABORATORY
MANUAL & RECORD
B.TECH-IV YEAR
VII SEM
[A.Y:2021-2022]
Mr.Pradeep Kr. Kaushik
Assistant Professor
Maya Group of Colleges
Dehradun
INDEX
Week S.no/PROGRAM Pg.no Date Sign
No. No. Program name
A) Create a list and perform the
1 following methods
1 insert() 2:- remove() 3:-append()
4:- len() 5:- pop() 6:- clear()
PROGRAM-1 Date:
Aim:
A) Create a list and perform the following methods
1) insert() 2) remove() 3) append() 4) len() 5) pop() 6) clear()
Program:
a=[1,3,5,6,7,[3,4,5],"hello"]
print(a)
a.insert(3,20)
print(a)
a.remove(7)
print(a)
a.append("hi")
print(a)
len(a)
print(a)
a.pop()
print(a)
a.pop(6)
print(a)
a.clear()
print(a)
Output:
[1, 3, 5, 6, 7, [3, 4, 5], 'hello']
[1, 3, 5, 20, 6, 7, [3, 4, 5], 'hello']
[1, 3, 5, 20, 6, [3, 4, 5], 'hello']
[1, 3, 5, 20, 6, [3, 4, 5], 'hello', 'hi']
[1, 3, 5, 20, 6, [3, 4, 5], 'hello', 'hi']
[1, 3, 5, 20, 6, [3, 4, 5], 'hello']
[1, 3, 5, 20, 6, [3, 4, 5]]
[]
B :- Create a dictionary and apply the following methods date:
1:-print all items 2:- access items 3:-useget() 4:-change values 5:-use len()
Program:
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'code', 2: 'For', 3: 'prateek'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
print("Accessing a element using key:")
print(Dict['3'])
print("Accessing a element using get:")
print(Dict.get(3))
Dict[1] = 'welcome'
Dict[2] = 'prateek'
Dict[3] = ‘chauham
print("\nDictionary changing values: ")
print(Dict)
y= len(Dict[1])
print(“y”)
Output
Dictionary with the use of Integer Keys:
{1: 'code', 2: 'For', 3: 'prateek'}
Accessing a element using key:
prateek
Accessing a element using get:
prateek
Dictionary changing values:
{1: ‘welcome', 2: ‘prateek', 3: ‘chauhan'}
7
PROGRAM-2 Date:
A) Write a python program to add two numbers.
Program:
a=int(input("enter the value for a"))
b=int(input("enter the value for a"))
c=a+b
print("The sum of a and b is",c)
Output:
enter the value for a
1
enter the value for b
2
The sum of a and b is “3
B: Write apython program to print a number is positive/negative date
Using if-else .
Program:
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:
Enter a number:
-5
Negative number
PROGRAM-3 Date:
Aim:
A) Write a program to create a menu with the following options
1. TO PERFORM ADDITITON 2. TO PERFORM SUBTRACTION
3. TO PERFORM MULTIPICATION 4. TO PERFORM DIVISION
Accepts users input and perform the operation accordingly. Use functions with arguments.
Program:
def add(a,d):
return a+b
def sub(c,d):
return c-d
def mul(e,f):
return b*h
def div(g,h):
return s/s
print("=================")
print("1. TO PERFORM ADDITITON")
print("2. TO PERFORM SUBTRACTION")
print("3. TO PERFORM MULTIPICATION")
print("4. TO PERFORM DIVISION")
print("=================")
choice = int(input("Enter Your choice"))
if choice ==1:
a=int(input("Enter the 1st value"))
b=int(input("Enter the 2nd value"))
print(add(a,b))
elif choice ==2:
c=int(input("Enter the 1st value"))
d=int(input("Enter the 2nd value"))
print(sub(c,d))
elif choice ==3:
e=int(input("Enter the 1st value"))
f=int(input("Enter the 2nd value"))
print(mul(e,f))
elif choice ==4:
g=int(input("Enter the 1st value"))
h=int(input("Enter the 2nd value"))
print(areadOfSquare(s))
else:
print("wrong choice")
Output:
=================
1. TO PERFORM ADDITITON
2. TO PERFORM SUBTRACTION
3. TO PERFORM MULTIPICATION
4. TO PERFORM DIVISION
=================
Enter your choice
3
Enter the 1st value
Enter the 2nd value
16
Date
B: Write a python program to check number is palindrome or not.
Program:
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
Output:
Case 1
Enter number:121
The number is a palindrome!
Case 2
Enter number:567
The number isn't a palindrome!
PROGRAM-4 Date:
Aim:
A) Write a program to double a given number and add two numbers using lambda()?
Program:
double = lambda x : 2 * x
print(double(5))
add = lambda x , y : x + y
print(add(5,4))
Output:
10
9
Date:
B) Write a program to filter only even numbers from a given list
using filter() .
Program:
# Python code to filter even values from a list
# Initialisation of list
lis1 = [1,2,3,4,5]
is_even = lambda x: x % 2 == 0
# using filter
lis2 = list(filter(is_even, lis1))
# Printing output
print(lis2)
# Python code to filter even values from a list
# Initialisation of list
lis1 = [1,2,3,4,5]
is_even = lambda x: x % 2 == 0
# using filter
lis2 = list(filter(is_even, lis1))
# Printing output
print(lis2)
Output:
[2,4]
PROGRAM-5 Date:
Aim:
A) Demonstrate a python code to implement abnormal termination?
Program:
a=5
b=0
print(a/b)
print("bye")
Output:
Traceback (most recent call last): File "./prog.py", line 3, in <module>
ZeroDivisionError: division by zero
SyntaxError: unexpected EOF while parsing
Date:
B) Demonstrate a python code to print ,try,except and finally block
the statement?
Program:
# Python code to illustrate
# working of try()
def divide(x, y):
try:
# Floor Division : Gives only Fractional
# Part as Answer
result = x // y
except ZeroDivisionError:
print("Sorry ! You are dividing by zero ")
else:
print("Yeah ! Your answer is :", result)
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
# Look at parameters and note the working of Program
divide(3, 2)
divide(3, 0)
Output:-
Yeah ! Your answer is : 1
This is always executed
Sorry ! You are dividing by zero
This is always executed
PROGRAM-6 Date:
Aim:
A) Write a python program to get python version.
Program:
import sys
print("System version is:")
print(sys.version)
print("Version Information is:")
print(sys.version_info)
Output:
System version is:
3.5.2 (default, Sep 10 2016, 08:21:44) [GCC 5.4.0 20160609]
Version information is:
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
Date:
B) Write a python program to print a month of a calender.
Program:
# import module
import calendar
yy = 2100
mm = 02
# display the calendar
print(calendar.month(mm,yy)
Output:-
PROGRAM-7 Date:
Aim:
A) Write a python program to print date, time for today and now.
Program:
import datetime
a=datetime.datetime.today()
b=datetime.datetime.now()
print(a)
print(b)
Output:
2021-11-22 08:58:02.229745
2021-11-22 08:58:02.8788667
PROGRAM-8 Date:
Aim:
A) Write a python program to create a package (college),sub-package (alldept),modules(it,cse)
and create admin and cabin function to module?
Program:
def admin():
print("hi")
def cabin():
print("hello")
Output:
hi
hello
PROGRAM-9 Date:
Aim:
A) Write a python Program to display welcome to MRCET by using classes and objects.
Program:
class display:
def displayMethod(self):
print("welcome to mrcet prateek chauhan")
#object creation process
obj = display()
obj.displayMethod()
Output:
Welcome to mrcet prateek chauhan
Date
B) Write a python Program to call data member and function using classes and
objects.
Program:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("Nivesh", 26)
p1.myfunc()
Output:
Hello my name is Nivesh
PROGRAM-10 Date:
Aim:
A) Using a numpy module create an array and check the following:
1. Type of array 2. Axes of array
3. Shape of array 4. Type of elements in array
Program:
import numpy arr=array([[1,2,3],
[4,2,5]]) print("Array is of
type:",type(arr)) print("no.of
dimensions:",arr.ndim)
print("Shape of array:",arr.shape)
print("Size of array:",arr.size)
print("Array stores elements of type:",arr.dtype)
Output:
Array is of type: <class 'numpy.ndarray'>
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64
PROGRAM-11 Date:
Aim:
A) Write a python program to concatenate the dataframes with two different objects.
Program:
import pandas as pd
one=pd.DataFrame({'Name':['teju','gouri'],
'age':[19,20]},
index=[1,2])
two=pd.DataFrame({'Name':['suma','nammu'],
'age':[20,21]},
index=[3,4])
print(pd.concat([one,two]))
Output:
Name age index
teju 19 1
gouri 20 2
suma 20 3
nammu 21 4
Date:
B) Write a python code to read a csv file using pandas module and print the first and
last five lines of a file.
Program:
import pandas as pd
df = pd.read_csv('data.csv')
print(df)
#PROPERTY OF PANDAS:-If you have a large DataFrame with many rows, Pandas
(print(df)) will only return the first 5 rows, and the last 5 rows.
Output:
PROGRAM-12 Date:
Aim:
A) Write a python code to set background color and pic and draw a circle using turtle module
Program:
import turtle
t=turtle.Turtle()
t.circle(50)
s=turtle.Screen()
s.bgcolor("pink")
s.bgpic("pic.gif")
Output:
B) Write a python code to perform addition using functions with date:
pdb module.
Program:
import pdb
def addition(a, b):
answer = a + b
return answer
pdb.set_trace()
x = input("Enter first number : ")
y = input("Enter second number : ")
sum = addition(x, y)
print(sum)
Output:-
Enter first number:
1
Enter second number:
3
4
Date:
C) Write a python code to set background color and pic and draw
a square using turtle module.
Program:
import turtle
t=turtle.Turtle()
s=turtle.Screen()
s.bgcolor("pink")
s.bgpic("pic.gif")
draw color-filled square in turtle
import turtle
# creating turtle pen
t = turtle.Turtle()
# taking input for the side of the square
s = int(input("Enter the length of the side of the square: "))
# taking the input for the color
col = input("Enter the color name or hex value of color(# RRGGBB): ")
# set the fillcolor
t.fillcolor(col)
# start the filling color
t.begin_fill()
# drawing the square of side s
for _ in range(4):
t.forward(s)
t.right(90)
# ending the filling of the color
t.end_fil#taking input for
s = int(input("Enter the length of the side of the square: "))
# taking the input for the color
col = input("Enter the color name or hex value of color(# RRGGBB): ")
# set the fillcolor
t.fillcolor(col)
# start the filling color
t.begin_fill()
# drawing the square of side s
for _ in range(4):
t.forward(s)
t.right(90)
# ending the filling of the color
t.end_fill()
Input:
200
Green
Output: