Python
Python
Python
DEPARTMENT OF _____________________________
NAME : ________________________________
REG.NO : ________________________________
SUBJECT : ________________________________
SEMESTER : ________________________________
PAVENDAR BHARATHIDASAN COLLEGE OF ARTS AND SCIENCE
DEPARTMENT OF _________________________
Certified that this bonafide recorded for the _______________ practical work
done by ____________________ for ___________ semester in the subject
_____________________________________ During the academic year ______________
Register No:___________________________
INDEX
S.N DATE TITLE PAGE NO SIGNATURE
O
1. FLOW CONTROLS ,FUNCTION AND STRING MANIPULATION
FLOW CONTROLS
#program to count the number of vowels
s=input('enter a string:')
count=0
for i in s:
if i in 'aeiouAEIOU':
count+=1
OUTPUT
enter a string:python
the number of vowels in python is 1
OUTPUT
enter the number:4
the number 4 is even
def sum_thrice(x,y,z):
sum=x+y+z
if x==y==z:
sum=sum*3
return sum
a=int(input("enter first number:"))
b=int(input("enter second number:"))
c=int(input("enter third number:"))
print("sum_thrice:",sum_thrice(a,b,c))
OUTPUT
enter first number:2
enter second number:2
enter third number:2
sum_thrice: 18
STRING MANIPULATION
#len()
s="String formatting function"
print("lenght of",s,"is",len(s))
OUTPUT:
lenght of String formatting function is 26
#lower()
s="String formatting function"
print(s.lower())
OUTPUT
string formatting function
#upper()
s="String formatting function"
print(s.upper())
OUTPUT:
STRING FORMATTING FUNCTION
#swapcase()
s="String formatting function"
print(s.swapcase())
OUTPUT:
sTRING FORMATTING FUNCTION
#capitalize()
s="String formatting function"
print(s.capitalize())
OUTPUT:
String formatting function
#title()
s="String formatting function"
print(s.title())
OUTPUT:
String Formatting Function
#lstrip()
s=" String formatting function"
print(s.lstrip())
OUTPUT:
String formatting function
#rstrip()
s="String formatting function "
print(s.rstrip())
OUTPUT:
String formatting function
#strip()
s=" String formatting function "
print(s.strip())
OUTPUT:
String formatting function
#max()
s="String formatting function"
print('maximum charater is',max(s))
OUTPUT:
maximum charater is u
#replace()
s="String formatting function"
print(s.replace('String','python'))
OUTPUT:
python formatting function
#zfill()
s="String formatting function"
print(s.zfill(30))
OUTPUT:
0000String formatting function
#isdecimal()
s=u"String formatting function"
print(s.isdecimal())
OUTPUT:
False
#isalpha()
s="String formatting function"
print(s.isalpha())
OUTPUT:
True
#split()
s="String formatting function"
print(s.split())
OUTPUT:
['String', 'formatting', 'function']
#max()
list1=[1200,147,2.43,1.12]
list2=[213,100,289]
print('maximum value in:',list1,"is",max(list1))
print('maximum value in:',list2,"is",max(list2))
#min()
list1=[1200,147,2.43,1.12]
list2=[213,100,289]
print('minimum value in:',list1,"is",min(list1))
print('miniimum value in:',list2,"is",min(list2))
#list(seq)
tuple=('abcd',147,2.43,'tom')
print('list:',list(tuple))
#append()
list=['abcd',147,2.43,'tom']
list.append(100)
print(list)
#count()
print(list.count(147))
#remove(obj)
list.remove('tom')
print(list)
#index()
print(list.index(2.43))
#extend()
list1=[1200,147,2.43,1.12]
list2=[213,100,289]
list1.extend(list2)
print(list1)
#reverse()
list.reverse()
print(list)
#insert(index,obj)
list.insert(2,222)
print(list)
OUTPUT
4
maximum value in: [1200, 147, 2.43, 1.12] is 1200
maximum value in: [213, 100, 289] is 289
minimum value in: [1200, 147, 2.43, 1.12] is 1.12
miniimum value in: [213, 100, 289] is 100
list: ['abcd', 147, 2.43, 'tom']
['abcd', 147, 2.43, 'tom', 100]
1
['abcd', 147, 2.43, 100]
2
[1200, 147, 2.43, 1.12, 213, 100, 289]
[100, 2.43, 147, 'abcd']
[100, 2.43, 222, 147, 'abcd']
#TUPLE
first_tuple=('abcd',147,2.43,'tom',74.9)
small_tuple=(111,'tom')
print(first_tuple)
print(first_tuple[0])
print(first_tuple[1:3])
print(first_tuple[2:])
print(first_tuple*3)
print(first_tuple+small_tuple)
#len()
tuple1=('abcd',147,2.43,'tom',74.9)
print(len(tuple1))
#max()
tuple1=(1200,147,2.43,1.12)
tuple2=(213,100,289)
print('maximum value in:',tuple1,"is",max(tuple1))
print('maximum value in:',tuple2,"is",max(tuple2))
#min()
tuple1=(1200,147,2.43,1.12)
tuple2=(213,100,289)
print('minimum value in:',tuple1,"is",min(tuple1))
print('miniimum value in:',tuple2,"is",min(tuple2))
#tuple(seq)
list=['abcd',147,2.43,'tom']
print('tuple:',tuple(list))
OUTPUT
('abcd', 147, 2.43, 'tom', 74.9)
abcd
(147, 2.43)
(2.43, 'tom', 74.9)
('abcd', 147, 2.43, 'tom', 74.9, 'abcd', 147, 2.43, 'tom', 74.9, 'abcd', 147, 2.43, 'tom', 74.9)
('abcd', 147, 2.43, 'tom', 74.9, 111, 'tom')
5
maximum value in: (1200, 147, 2.43, 1.12) is 1200
maximum value in: (213, 100, 289) is 289
minimum value in: (1200, 147, 2.43, 1.12) is 1.12
miniimum value in: (213, 100, 289) is 100
tuple: ('abcd', 147, 2.43, 'tom')
3. OPERATION ON SETS
#operation on set
s1={1,2,3}
print(s1)
s2={1,2,3,2,1,2}
print(s2)
s3={1,2.4,'apple','tom'}
print(s3)
s4=set([1,2,3,4])
print(s4)
#len()
set1={'abcd',147,2.43,'tom'}
print(len(set1))
set2=sorted(set1)
print("sum of elements",set1,"sorting",set2)
#any(), all()
set1=set()
set2={1,2,3,4}
print("any(set):",any(set1))
print("any(set):",any(set2))
print("all(set):",all(set1))
print("all(set):",all(set1))
set1.remove(3)
print("delete the item in set1",set1)
set1.discard(9)
print("discard the item in set1",set1)
set1.pop()
print("pop the set1 item",set1)
#union(),intersection(),difference()
set1={3,8,2,6}
set2={4,2,1,9}
print(set1)
print(set2)
set3=set1.union(set2)
print("union",set3)
set3=set1.intersection(set2)
print("intersection",set3)
set3=set1.difference(set2)
print("difference",set3)
OUTPUT
{1, 2, 3}
{1, 2, 3}
{'apple', 1, 'tom', 2.4}
{1, 2, 3, 4}
4
maximum value in: {1200, 2.43, 147, 1.12} is 1200
minimum value in: {289, 100, 213} is 100
sum of element in {1200, 2.43, 147, 1.12} is 1350.55
sum of element in {289, 100, 213} is 602
sum of elements {1200, 2.43, 147, 1.12} sorting [1.12, 2.43, 147, 1200]
any(set): False
any(set): True
all(set): True
all(set): True
set add the item to set1: {1, 2, 3, 4, 5, 6, 7, 9}
delete the item in set1 {1, 2, 4, 5, 6, 7, 9}
discard the item in set1 {1, 2, 4, 5, 6, 7}
pop the set1 item {2, 4, 5, 6, 7}
{8, 2, 3, 6}
{1, 2, 4, 9}
union {1, 2, 3, 4, 6, 8, 9}
intersection {2}
difference {8, 3, 6}
4. OPERATIONS ON DICTIONARY
#operations on dictionary
dict1={'name':'tom','age':20,'height':160}
dict2=dict1.copy()
print(dict2)
print('keys in dictionary:',dict1.keys())
print('values in dictionary:',dict1.values())
print('items in dictionary:',dict1.items())
dict1={'name':'tom','age':20,'height':160}
dict2={'weight':60}
print(dict2)
dict1.update(dict2)
print('dict1 updated dict2:',dict1)
dict1={'name':'tom','age':20,'height':160}
print('dict1.get(age):',dict1.get('age'))
print('dict1.setdefault(age):',dict1.setdefault('name'))
print('dict1.setdefault(phone):',dict1.setdefault('phone'))
#print('dict1.has_key(key):',dict1.has_key('phone'))
OUTPUT
car2=Car("Hyundai",2022,"gray",100000)
car2.Hyundaicar()
OUTPUT
Model: Audi
Year: 2023
Color: black
Price: 1200000
Model: Hyundai
Year: 2022
Color: gray
Price: 100000
6. Method overloading – create classes for vehicle and bus
class Vehicle:
def __init__(self, name):
self.name = name
class Bus(Vehicle):
def __init__(self, name, capacity):
super().__init__(name)
self.capacity = capacity
# Calling the accelerate method of the Vehicle class with two arguments
car.accelerate(100, 2)
# Calling the accelerate method of the Bus class with one argument
#bus.accelerate(80)
# Calling the accelerate method of the Bus class with two arguments
bus.accelerate(80, 3)
OUTPUT
import os
f=open("student.txt","r+")
f.write("i am python program")
r=f.readline()
print(r)
f.close()
print("file",f.name,"closed")
print("closed or not",f.closed)
os.rename("student.txt","stud.txt")
print("file renamed")
OUT PUT
first number2
second number5
result= 0.4
successful division
completed exception handling program