Python Lab Manual Final
Python Lab Manual Final
a=10
b="Python"
c = 10.5
d=2.14j
e=True
print("Data type of Variable a :",type(a))
print("Data type of Variable b :",type(b))
print("Data type of Variable c :",type(c))
print("Data type of Variable d :",type(d))
print("Data type of Variable e :",type(e))
Output:
a=[1,3,5,6,7,4,"hello"]
print("The list created is", a)
#insert()
a.insert(3,20)
print("Insert the element 20 at index position 3. The modified list is\n", a)
#remove()
a.remove(7)
print("Remove the element 7 from the list. The modified list is \n", a)
#append()
a.append("hi")
print("Appended the list with a string\n", a)
c=len(a)
print("The length of the list is \n", c)
#pop()
a.pop()
print("After popping the element from the list \n", a)
a.pop(6)
print("After popping the element at index position 6 from the list, modified list is
\n", a)
# clear()
a.clear()
print("The cleared list is \n", a)
Output:
rainbow=("v","i","b","g","y","o","r")
print("The tuple of alphabets creating a rainbow is \n", rainbow)
colour=("violet","Indigo","blue","green","yellow","orange","red")
print("The tuple of colours in a rainbow is \n", colour)
('v', 'i', 'b', 'g', 'y', 'o', 'r', 'violet', 'Indigo', 'blue', 'green', 'yellow', 'orange', 'red')
True
5. Use len( )
#Source code:
# creating a dictionary
college={'name': "QIS", 'code': "QISIT",'pincode': 560050 }
print(college)
#adding items to dictionary
college["location"] = "IBP"
print(college)
#changing values of a key
college["location"] = "vijayawada"
print(college)
#know the length using len()
print("length of college is:",len(college))
#Acess items
print("college['name']:",college['name'])
# use get ()
x=college.get('pincode')
print(x)
#to copy the same dictionary use copy()
mycollege= college.copy()
print(mycollege)
Output:
def add(n1,n2):
return n1+n2
def sub(n1,n2):
return n1-n2
def mul(n1,n2):
return n1*n2
def div(n1,n2):
return n1/n2
print("Welcome to the Arithmetic Program")
print("1. TO PERFORM ADDITION")
print("2. TO PERFORM SUBTRACTION")
print("3. TO PERFORM MULTIPLICATION")
print("4. TO PERFORM DIVISION")
print("0. To Exit")
x = int(input(" Enter the first number\n"))
y = int(input(" Enter the second number\n"))
choice =1
while(choice!=0):
choice = int(input("Enter your choice"))
if choice == 1:
print(x, "+" ,y ,"=" ,add(x,y))
elif choice == 2:
print(x, "-" ,y ,"=" ,sub(x,y))
elif choice == 3:
print(x, "*" ,y ,"=" ,mul(x,y))
elif choice == 4:
print(x, "%" ,y ,"=" ,div(x,y))
elif choice ==0:
print("Exit")
else:
print("Invalid Choice");
Output:
Output:
def find_even_numbers(list_items):
print(" The EVEN numbers in the list are: ")
for item in list_items:
if item%2==0:
print(item)
def main():
list1=[2,3,6,8,48,97,56]
find_even_numbers(list1)
if __name__=="__main__":
main()
Output:
The EVEN numbers in the list are:
2
6
8
48
56
7. Write a program for filter() to filter only even numbers from a given list.
def even(x):
return x % 2 == 0
a=[2,5,7,16,8,9,14,78]
result=filter(even,a)
print(" original List is :",a)
print(" Filtered list is : ",list(result))
Output:
import datetime
a=datetime.datetime.today()
b=datetime.datetime.now()
print("now date and time is:",b)
print("Today date is :",a)
print("current year :",a.year)
print("current month :",a.month)
print("current day :",a.day)
print(a.strftime("%a"))
print(a.strftime("%b"))
print(a.strftime("%c"))
Output:
now date and time is: 2023-02-28 19:39:01.936566
Today date is : 2023-02-28 19:39:01.936567
current year : 2023
current month : 2
current day : 28
Tue
Feb
Tue Feb 28 19:39:01 2023
9. Write a python program to add some days to your present date and print
the date added.
from datetime import datetime
from datetime import timedelta
from datetime import date
print("Beginning date")
print(Begindatestring)
Output:
Beginning date
2023-02-28
Ending date
2023-03-10
10. Write a program to count the number of characters ina string and store
them in a dictionary data structure.
def construct_character_dict(word):
character_count_dict=dict()
for each_character in word:
character_count_dict[each_character]=character_count_dict.get(each_character,0)+1
print(character_count_dict)
def main():
word=input("enter a string :")
construct_character_dict(word)
if __name__=="__main__":
main()
Output:
enter a string :klebca
{'k': 1, 'l': 1, 'e': 1, 'b': 1, 'c': 1, 'a': 1}
Source Code:
import collections
import pprint
file_input = input('File Name: ')
with open(file_input, 'r') as info:
count = collections.Counter(info.read().upper())
value = pprint.pformat(count)
print(value)
Output:
12. Using a numpy module create an array and check the following:
1. Type of array 2. Axes of array3. Shape of array 4. Type of elements in
array
import numpy as np
arr=np.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)
no.of dimensions: 2
Size of array: 6
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
1 teju 19
2 gouri 20
3 suma 20
4 nammu 21
14. Write a python code to read a csv file using pandas module and print frist
five and last five lines of a file.
Source Code:
import pandas as pd
pd.set_option('display.max_rows', 50)
pd.set_option('display.max_columns', 50)
diamonds = pd.read_csv('List.csv')
print("First 5 rows:")
print(diamonds.head())
print("Last 5 rows:")
print(diamonds.tail())
Output:
First 5 rows:
REG NO NAME COURSE FEES LANGUAGE
0 101 jagan BCA 100000 KAN
1 102 jithin Bcom 100000 HIN
2 103 preethi BCA 100000 KAN
3 104 manoj BBA 30000 HIN
4 105 nikitha MBA 200000 TEL
Last 5 rows:
REG NO NAME COURSE FEES LANGUAGE
6 107 pavan ts Mtech 600000 TAMIL
7 108 ramu BSC 45000 KAN
8 109 radha BCA 100000 KAN
9 110 sita BCA 100000 KAN
10 111 raj BCom 100000 TEL
15. WAP which accepts the radius of a circle from user and compute the
area(Use math module)
import math as M
area_of_circle = M.pi*radius*radius
circumference_of_circle = 2*M.pi*radius
Output: