Python Lab Manual
Python Lab Manual
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(a)
#insert()
a.insert(3,20)
print(a)
#remove()
a.remove(7)
print(a)
#append()
a.append("hi")
print(a)
c=len(a)
print(c)
#pop()
a.pop()
print(a)
a.pop(6)
print(a)
# clear()
a.clear()
print(a)
output:
[1, 3, 5, 6, 7, 4, 'hello']
[1, 3, 5, 20, 6, 7, 4, 'hello']
[1, 3, 5, 20, 6, 4, 'hello']
[1, 3, 5, 20, 6, 4, 'hello', 'hi']
8
[1, 3, 5, 20, 6, 4, 'hello']
[1, 3, 5, 20, 6, 4]
[]
3. Create a tuple and perform the following methods.
#creating a tuple
rainbow=("v","i","b","g","y","o","r")
print(rainbow)
colour=("violet","blue","green","yellow","orange","red")
print(colour)
# Add items in tuples
rainbow_colour=rainbow+colour
print(rainbow_colour)
#length of the tuple
c=len(rainbow_colour)
print(c)
#Access items in tuple
print("rainbow[2]:",rainbow[2])
"""rainbow[1:3] means all the items in rainbow tuple
starting from an index value of 1 up to an index value of 4"""
print("rainbow[1:3]",rainbow[1:3])
print("rainbow[0:4]",rainbow[0:4])
output:
#Source code:
# creating a dictionary
college={'name': "QIS", 'code': "INDIA",'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
output:
output:
a=datetime.datetime.today()
b=datetime.datetime.now()
print(a)
print(b)
Output:
2022-11-30 17:18:52.879383
2022-11-30 17:18:52.879382
12. Using a numpy module create an array and check the following:
1. Type of array 2. Axes of 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)
print("Array stores elements of type:",arr.dtype)
Output:
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
15. WAP which accepts the radius of a circle from user and compute the area(
Use math module)
import math as M
radius = float(input("Enter the radius of the circle"))
area_of_circle = M.pi*radius*radius
circumference_of_circle = 2*M.pi*radius
print("the area of circle is", area_of_circle)
print("the circumference of circle is", circumference_of_circle)
Output:
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
sorted_list_keys=sorted(character_count_dict.keys())
for each_key in sorted_list_keys:
print(each_key,character_count_dict.get(each_key))
def main():
word=input("enter a string")
construct_character_dict(word)
if __name__=="__main__":
main()
Output:
1
A 1
B 1
C 2
E 1
K 1
L 1
N 1
S 1
7. Write a program for filter() to filter only even numbers from a given list.
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:
Output:
Beginning date
2022-12-05
Ending date
2022-12-15