Python Programming Laboratory Manual & Record: Assistant Professor Maya Group of Colleges Dehradun
Python Programming Laboratory Manual & Record: Assistant Professor Maya Group of Colleges Dehradun
Python Programming Laboratory Manual & Record: Assistant Professor Maya Group of Colleges Dehradun
LABORATORY
MANUAL & RECORD
B.TECH-IV YEAR
VII SEM
[A.Y:2021-2022]
Aim:
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
Program:
Output:
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:
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("=================")
if choice ==1:
a=int(input("Enter the 1st value"))
print(add(a,b))
print(sub(c,d))
print(mul(e,f))
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
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
# Printing output
print(lis2)
lis1 = [1,2,3,4,5]
is_even = lambda x: x % 2 == 0
# using filter
lis2 = list(filter(is_even, lis1))
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:
Program:
# Python code to illustrate
# working of try()
else:
print("Yeah ! Your answer is :", result)
finally:
# this block is always executed
# regardless of exception generation.
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:
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:
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:
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:
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)
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
# 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
Input:
200
Green
Output: