Experiment No: 4 (A) : Chandigarh University Python Programming ITP-268
Experiment No: 4 (A) : Chandigarh University Python Programming ITP-268
ITP-268
Experiment no : 4(a)
AIM: write a program to demonstrate the use of string operations and inbuilt strings functions.
1. Write a program to retrive the strings in reverse as well as in normal form
Program:
name = 'welcome to strigs'
length=len(name)
i=0
for n in range(-1,(-length-1),-1):
print(name[i],"\t",name[n])
output:
Program:
str1='hello'
str2='shivam'
str=str1+str2
print(str)
CHANDIGARH UNIVERSITY Python programming
ITP-268
output:
Program:
a="shivam"
print(5*a)
output:
Program:
str1="javascript"
str2="assit"
str3="seomount"
str4="java"
str5="it"
str6="seo"
print(str4 in str1)
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
output:
str1="shivam"
str2="gautam"
print(str1==str2)
print(str1<=str2)
print(str1>=str2)
CHANDIGARH UNIVERSITY Python programming
ITP-268
Experiment no : 4(b)
Aim: program to demonstrate various strings functions
1. Capitalize()
Program:
str1= "abc"
print("abc".capitalize())
output:
2. Count(string)
Program:
msg="hello python"
substring="l"
print(substring.count("l"))
substring2="n"
print(substring2.count("n"))
CHANDIGARH UNIVERSITY Python programming
ITP-268
3. Endswith(string)
Program:
str1="welcome to punjab"
substring1="punjab"
substring2="to"
substring3="welcome"
print(str1.endswith(substring1))
print(str1.endswith(substring2))
print(str1.endswith(substring3))
output:
4. Find(string)
Program:
str1="welcoem to ssit"
substring1="come"
substring2="to"
print(str1.find(substring1))
print(str1.find(substring2))
output:
5. Index(string)
Program:
str="welcome to the world of ssit"
substr1="come"
substr2="of"
print(str.index(substr1))
print(str.index(substr2))
print(str.index(substr1,3,10))
print(str.index(substr2,19))
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
6. Isalnum()
Program:
str="welcome to the world of ssit"
print(str1.isalnum())
str1="python46"
print(str1.isalnum())
output:
7. isspace()
str1="";
print(str1.isspace())
str2="welcome to ssscii"
print(st2.isspace())
output:
False
False
>>>
8. len(string)
program:
str1=""
CHANDIGARH UNIVERSITY Python programming
ITP-268
print(len(string))
str2="welcome to ssii"
print(len(string))
output:
9. lower()
program:
string1="hello pythom"
print(string1.lower())
string2="welcome to sscii"
print(string2.lower())
output:
hello pythom
welcome to sscii
>>>
10. upper()
program:
string1="hello pythom"
print(string1.upper())
string2="welcome to sscii"
print(string2.upper())
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
11. startwith(string)
program:
string1="hello pythom"
print(string1.startwith("hello"))
string2="welcome to sscii"
print(string2.startwith('come',3,7))
12. swapcase()
program:
string1=”hello python”
print(string1.swapcase())
string2=”welcome to sscii”
print(string2.swapcase())
output:
13. lstrip()
program:
string1="hello python"
print(string1.lstrip())
string2="welcome to python"
print(string2.lstrip())
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
14. rstrip()
program:
string1="hello python"
print(string1.rstrip())
string2="welcome to python"
print(string2.rstrip())
output:
15. isalpha()
program:
string1="hello python"
print(string1.isalpha())
string2="welcome to python"
print(string2.isalpha())
output:
16. isdigit
program:
string1=”hello python”
print(string1.isdigit())
string2=”12345678”
print(string2.isdigit())
CHANDIGARH UNIVERSITY Python programming
ITP-268
17. isupper
program:
string1="hello python"
print(string1.isupper())
string2="welcome to python"
print(string2.isupper())
output:
18. islower
program:
string1="hello python"
print(string1.islower())
string2="welcome to python"
print(string2.islower())
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
Program:
l1=list[10,20,30,40,50]
print(l1)
output:
2. List operations
a. Adding lists
The + operator is used to concatenation operator is used to join two lists.
Program:
list1=[10,20,30,40,50]
list2=[50,70,80,90]
list3=list1+list2
print(list3)
output:
b. Replacing lists
The * operator is used to replicate the elements of lists.
Program:
list1=[10,20,30]
list2=[20,30,40]
list3= 2*list1
print(list3)
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
Program:
list1=[1,2,3,5,7,9]
print(list([0:6])
print(list([1:6])
print(list([2:6])
print(list([3:6])
print(list([4:6])
print(list([5:6])
print(list([6:6])
output:
c. Updating
Program:
list1=[2,4,6,10,12]
print(list1)
list1[2]=78
print(list1)
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
output:
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
Experiment no :5(b)
Aim: implementing of list inbuilt function.
1. max()
it is used to find the greatest elements in a list.
List1=[10,20,30,40,50]
list2=[20,30,60]
print(max(list1))
print(max(list2))
output:
2. len()
it is used to find what is length of list and how many elements contain in the list.
program:
list1=[1,2,3,4,5,6,7]
list2=[3,6,9,12,15]
print(len(list1))
print(lem(list2))
output:
3. relational operations
program:
list1=[1,2,3,4,5]
list2=[2,4,6,8,9,]
print(list1==list2)
print(list1>=list2)
print(list1<=list2)
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
4. index(obj)
program:
list1=[1,3,5,7,9,11]
list2=[2,4,6,8,10]
print(list1.index(1))
print(list2.index(4))
output:
5. count(obj)
program:
list=['a','b','c','d','e','a','e']
print(list)
list.count('a')
print(list)
output:
6. pop()
removes the element from the given postion also it returns the removed elements.
program:
list=[22,33,44,55,66,77]
print(list)
list.pop(1)
print(list)
CHANDIGARH UNIVERSITY Python programming
ITP-268
output:
7. insert()
it is used to insert the elements in the list.
program:
list1=[2,4,6,8,10]
print(list1)
list1.insert(4,12)
print(list1)
output:
8. extend(sequence)
appends all the elements of list2 to the list1
program:
list1=[1,2,3,4,5]
list2=[6,7,8,9,10]
print(list1)
print(list2)
list1.extend(list2)
print(list1)
output:
9. remove(object)
it is used to remove the fist occurrence of element x from the list.
CHANDIGARH UNIVERSITY Python programming
ITP-268
program:
list1=[4,8,16,20,24,28]
print(list1)
list1.remove(8)
print(list1)
output:
10. reverse()
it is used to reverse the elements of list.
program:
list=[5,10,15,20,25]
print(list)
list.reverse()
print(list)
output:
11. sort()
sort the elements of list.
program:
list1=['g','f','a','c','e']
print(list1)
list1.sort()
print(list1)
output:
program:
list1=[10,20,30,40,50]
print(list1)
list2=[20,30,50]
print(list2)
print(20 in list2)
pprint(50 in list1)
output:
13. min()
it returns the element with the lowest value.
program:
list1=[10,20,30,40,50]
list2=[20,30,60]
print(min(list1))
print(min(list2))
output:
CHANDIGARH UNIVERSITY Python programming
ITP-268
Experiment no : 6
Aim: . Program to demonstrate creation and accessing of dictionary and apply different kinds
of operations on them.
program:
data={100:'Ravi',101:'Vijay',102:'Rahul'}
print(data)
OUTPUT:
array
program:
d={}
d[1]='Ravi'
d[2]='Manoj'
d['name']='Hari'
d[4]='Om'
print d[2]
print(d['name'])
print(d[1])
print(d)
CHANDIGARH UNIVERSITY Python programming
ITP-268
OUTPUT:
keys
print(dc[key])
program:
dict1=({"name":"shivam","rollno":41})
dict2=({"name":"rahul","rollno":3})
print(dict1["name"])
print(dict2["rollno"])
OUTPUT:
dc[key]='new value'
program:
dict1=({"name":"shivam","phone no":8045463413})
dict2=({"name":"rahul","phone no":8194843532})
dict1["name"]="gautam"
dict2["phone no"]=7696234751
print(dict1)
print(dict2)
OUTPUT:
CHANDIGARH UNIVERSITY Python programming
ITP-268
del data[key]
program:
dict1=({"name":"shivam","phone no":8045463413})
del dict1["name"]
print(dict1)
OUTPUT:
a) len(dc)
PROGRAM:
d1=dict({"name":"shivam","rollno":41})
print(len(d1))
OUTPUT:
b) keys()
PROGRAM:
d1=({"name":"shivam","age":"21","salary":"10000"})
print(d1.keys())
d2={}
print(d2.keys())
OUTPUT:
CHANDIGARH UNIVERSITY Python programming
ITP-268
C) values()
print(dc.values())
PROGRAM:
d1=({"name":"shivam","age":"21","salary":"10000"})
print(d1.values())
d2=({"age":"21","rollno":"41"})
print(d2.values())
OUTPUT:
D) items()
print(dc.items())
PROGRAM:
d1=({"name":"shivam","age":"21","salary":"10000"})
print(d1.items())
d2=({"age":"21","rollno":"41"})
print(d2.items())
OUTPUT:
E) update()
dc1.update(dc2)
CHANDIGARH UNIVERSITY Python programming
ITP-268
PROGRAM:
d1=({1:"one",2:"two"})
d2=({3:"three"})
(d1.update(d2))
print(d1)
OUTPUT:
F) clear()
dc1.clear()
PROGRAM:
d1=({1:"one",2:"two"})
print(d1.clear())
OUTPUT:
G) copy()
dc1=dc2.copy()
PROGRAM:
dict1={}
dict2=({"age": "21","rollno":"41"})
dict1=dict2.copy()
print(dict1)
OUTPUT:
CHANDIGARH UNIVERSITY Python programming
ITP-268
CHANDIGARH UNIVERSITY Python programming
ITP-268
Experiment no:7
Aim: Program to demonstrate creation and accessing of tuples and apply different kinds of
operations on them.
PROGRAM:
data= (10,20,'KAMAL',34.5)
print(data)
data2 ="a",10,20.9
print(data2)
data3=()#empty tuple
print(data3)
print(data4)
print(data5)
print(data7)
print(data8)
OUTPUT:
CHANDIGARH UNIVERSITY Python programming
ITP-268
data1=(1,2,3,4,5,6,7,8)
print(data1[0])
print(data1[0:2])
print(data1[-3:-1])
print(data1[0:])
print(data1[:2])
OUTPUT:
3) tuple operations:
a) Adding tuples
PROGRAM:
T1=tuple(1,23)
T2=tuple(2,3)
T3=t1+t2
Print(t3)
CHANDIGARH UNIVERSITY Python programming
ITP-268
b) Replicating tuples
tup1=(1,2,3)
tup2=tup1*3
print(tup2)
OUTPUT:
c) Tuple slicing
t1=tuple("tennis")
print(t1)
print(t1[0])
print(t1[1:6])
print(t1[2:6])
print(t1[: : -1])
print(t1[-1 :])
CHANDIGARH UNIVERSITY Python programming
ITP-268
f) min(tuple)
PROGRAM:
t1=("chandigarh")
print(min(t1))
OUTPUT:
max(tuple)
PROGRAM:
print(max(t1))
OUTPUT:
CHANDIGARH UNIVERSITY Python programming
ITP-268
len(tuple)
PROGRAM:
print(len(t1))
OUTPUT: