Python

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

PAVENDAR BHARATHIDASAN COLLEGE OF ARTS AND SCIENCE

THANJAI NATARAJAN NAGAR,

MATHUR, PUDUKOTTAI ROAD, TIRUCHIRAPPALLI, PINCODE-622515.

DEPARTMENT OF _____________________________

___________ DEGREE EXAMINATION

NAME : ________________________________

REG.NO : ________________________________

SUBJECT : ________________________________

SUBJECT CODE: ________________________________

SEMESTER : ________________________________
PAVENDAR BHARATHIDASAN COLLEGE OF ARTS AND SCIENCE

THANJAI NATARAJAN NAGAR,

MATHUR, PUDUKOTTAI ROAD, TIRUCHIRAPPALLI, PINCODE-622515.

DEPARTMENT OF _________________________

Certified that this bonafide recorded for the _______________ practical work
done by ____________________ for ___________ semester in the subject
_____________________________________ During the academic year ______________

Register No:___________________________

STAFF INCHARGE HEAD OF THE DEPARTMENT

Submitted for the practical examination held on ___________________

INTERNAL EXAMINER EXTERNAL EXAMINER

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

print("the number of vowels in" ,s ,"is", count)

OUTPUT
enter a string:python
the number of vowels in python is 1

#program to in the sum of all iteams in a list


list=input("enter a list:")
list1=map(int,list.split())
sum=0
for i in list1:
sum+=i
print("the sum of all iteam in list", list,"is",sum)
OUTPUT
enter a list:1 2 3 4
the sum of all iteam in list 1 2 3 4 is 10
FUNCTION

#function to check whether a number is even or odd


def oddeven(x):
if x%2==0:print("the number", x, "is even")
else: print("the number", x, "is odd")
num=int(input("enter the number:"))
oddeven(num)

OUTPUT
enter the number:4
the number 4 is even

#to calculate the sum of three given number,


#if the values are equal then return thrice of their sum

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']

2. OPERATION ON LIST AND TUBLE


#operation on list
#len()
list1=['abcd',147,2.43,'tom']
print(len(list1))

#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))

#max() , min(), sum(), sorted()


set1={1200,147,2.43,1.12}
set2={213,100,289}
print("maximum value in:",set1,"is",max(set1))
print("minimum value in:",set2,"is",min(set2))

print("sum of element in",set1,"is",sum(set1))


print("sum of element in",set2,"is",sum(set2))

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))

#add(), remove(), discard(),pop()


set1={1,2,3,4,6,7,5}
set1.add(9)
print("set add the item to set1:",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

#built-in dictionary function


dict1={'name':'tom','age':20,'height':160}
print(dict1)
print('lenght of dictionary=',len(dict1))
print('representation of dictionary=',str(dict1))
print('type(variable)=',type(dict1))
#built-in dictionary methods
dict1.clear()
print(dict1)

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

{'name': 'tom', 'age': 20, 'height': 160}


lenght of dictionary= 3
representation of dictionary= {'name': 'tom', 'age': 20, 'height': 160}
type(variable)= <class 'dict'>
{}
{'name': 'tom', 'age': 20, 'height': 160}
keys in dictionary: dict_keys(['name', 'age', 'height'])
values in dictionary: dict_values(['tom', 20, 160])
items in dictionary: dict_items([('name', 'tom'), ('age', 20), ('height', 160)])
{'weight': 60}
dict1 updated dict2: {'name': 'tom', 'age': 20, 'height': 160, 'weight': 60}
dict1.get(age): 20
dict1.setdefault(age): tom
dict1.setdefault(phone): None

5. SIMPLE OOP- CONSTRUCTOR - CREATE A CLASS FOR REPRESENTING A


CAR
#oop in python
class Car:
def __init__(self,model,year,color,price):
self.model=model
self.year=year
self.color=color
self.price=price
def Audicar(self):
print("Model:",self.model)
print("Year:",self.year)
print("Color:",self.color)
print("Price:",self.price)
def Hyundaicar(self):
print("Model:",self.model)
print("Year:",self.year)
print("Color:",self.color)
print("Price:",self.price)
car1=Car("Audi",2023,"black",1200000)
car1.Audicar()

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

def accelerate(self, speed):


print(f"The {self.name} is accelerating at {speed} km/h.")

def accelerate(self, speed, time):


distance = speed * time
print(f"The {self.name} covers a distance of {distance} km in {time} hours.")

class Bus(Vehicle):
def __init__(self, name, capacity):
super().__init__(name)
self.capacity = capacity

def accelerate(self, speed):


print(f"The {self.name} bus is accelerating at {speed} km/h.")

def accelerate(self, speed, time):


distance = speed * time
print(f"The {self.name} bus covers a distance of {distance} km in {time} hours.")
print(f"It has a seating capacity of {self.capacity} passengers.")

# Creating instances of the Vehicle and Bus classes


car = Vehicle("Car")
bus = Bus("Bus", 50)
# Calling the accelerate method of the Vehicle class with one argument
#car.accelerate(100)

# 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

The Car covers a distance of 200 km in 2 hours.


The Bus bus covers a distance of 240 km in 3 hours.
It has a seating capacity of 50 passengers.
7. Files – Reading And Writing - Perform The Basic Operation Of Reading And
Writing With Student File

import os

f=open("student.txt","r+")
f.write("i am python program")

r=f.readline()
print(r)

print("name of the file:",f.name)


print("closed or not",f.closed)
print("mode of file",f.mode)

f.close()
print("file",f.name,"closed")
print("closed or not",f.closed)

os.rename("student.txt","stud.txt")
print("file renamed")

OUT PUT

name of the file: student.txt


closed or not False
mode of file w+
file student.txt closed
closed or not True
file renamed
8. EXCEPTION HANDLING
try:
a=int(input("first number"))
b=int(input("second number"))
result=a/b
print("result=",result)
except:
print("error occured")
else:
print("successful division")
finally:
print("completed exception handling program")
Out put

first number2
second number5
result= 0.4
successful division
completed exception handling program

You might also like