Darpan Python
Darpan Python
(AUTONOMOUS)
PRACTICAL NO :- 01
Code : -
Output:-
Code : -
Output : -
Page | 9
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
Code : -
Output :-
Code :-
a=10
Page | 10
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
b=20
c=30
d=40
e=50
Output : -
CODE :-
Page | 11
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
(PQ). Write a python code to create different variables and display its
values.
CODE:-
name="par
th"
>>>age=18
>>>height=5.9
>>>print("variable values are",name,age,height)
OUTPUT : -
****************************
PRACTICALNUMBER : - 02
Page | 12
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
CODE : -
# addition print('Sum:
', a + b)
# subtraction print('Subtraction:
', a - b)
# multiplication print('Multiplication:
', a * b)
# division print('Division:
', a / b)
# modulo print('Modulo:
', a % b)
# power print('Power:
', a ** b)
OUTPUT : -
Page | 13
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
CODE : -
OUTPUT : -
Page | 14
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
CODE : -
#
a
ss
ig
n
1
0
to
a
a
=
1
0
#
a
ss
ig
n
5 to b b = 5
# assign the sum
of a and b to a a
+= b # a = a + b
print("a+=b
Page | 15
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
is",a)
# assign the subtraction of a and b to a
a -= b # a = a - b print("a-=b is",a)
OUTPUT : -
CODE : -
a=5b=6
print((a >
2) and (b
>= 6))
print((a >
2) or (b >=
6))
print(not(a > 2))
OUTPUT : -
CODE : -
x1 = 5 y1 = 5 x2 =
'Hello' y2 = 'Hello' x3
= [1,2,3] y3 = [1,2,3]
print(x1 is not y1) #
prints False print(x2
is y2) # prints True
print(x3 is y3) #
prints False
OUTPUT : -
CODE : -
message =
'Hello world'
dict1 = {1:'a',
2:'b'}
# check if 'H' is present in
message string print('H' in
message) # prints True #
check if 'hello' is present in
message string print('hello'
not in message) # prints
True # check if '1' key is
present in dict1 print(1 in
dict1) # prints True # check if
Page | 17
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
'a' key is present in dict1
print('a' in dict1) # prints
False
OUTPUT :-
*************************
PRACTICAL NUMBER : - 03
CODE : -
OUTPUT : -
CODE : -
Page | 19
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
str="LEO,CRIS,NEYMAR,SUAREZ,INEASTA"
print("Third friend is",str[10:15])
print("This all are my friend",str)
print("The first letter of 4th friend name
is",str[16:17]) print("The capital A
occurs",str.count("A"))
OUTPUT :-
CODE :-
OUTPUT : -
Page | 20
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
CODE : -
begins_with_hello = my_string.startswith("hello")
print(f"String begins with 'hello': (begins_with_hello)")
ends_with_sad = my_string.endswith("sad")
print(f"String ends with 'sad' : (ends_with_sad)")
half_index = string_length
// 2 first_half =
my_string[:half_index]
second_half =
my_string[half_index:]
Page | 21
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
Page | 22
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
Practical 4
Q1. Create a list with the name List I which contains 4 integer values and list
2 which contains 4 characters values and perform the following operation.
a. Display all the list item from the list 1
b. Display all the list items from the list 2
c. Display third element of the list 1
d. Display last element of the list 2
e. Display sequence of list Items from the list 1 from 1 to 3
CODE :
List1 = [1,2,3,4]
List2 = ["A","B","C","D"]
print(List1)
print(List2)
print("3rd elt :",List1[2])
print("3rd elt :",List2[3])
print("Printing 1 to 3 from List 1",List1[0:3])
OUTPUT :
Q2. Write a python code to create a list which contains the book details. (
Book name, author, price, publication, edition, etc) and
perform the following operation •
a. Display all the book details.
b. Display price of the book
c. Update the price of the book by ₹ 200.
d. Update the edition of the book to the latest edition.
CODE :
Book = ["Python","Leo messi",1500,"Leo messi productions",2022]
print(Book)
print(Book[2])
Book[2] = Book[2] + 200
Page | 23
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
print(Book)
Book[4] = 2024
OUTPUT :
Q3. Create a list with five elements and remove element at the index I and
return its value.
CODE :
A = [10,20,30,40,50]
print("Original List",A)
val = A.pop(1)
print("Now my List is ",A)
print("Remove list item ",val)
OUTPUT :
Q4. Create a list with five elements and remove the first occurrence of value
20 .
CODE :
A = [10,20,30,40,50]
print("Original List",A)
val = A.remove(20)
print("Now my List is ",A)
OUTPUT :
Q5. Create a Python program which reads 2 values From the user and display
their multiplication.
CODE :
Page | 24
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
a = int(input("Enter val of a "))
b = int(input("Enter val of b "))
print(" a = ", a ," b = ", b )
print(" a*b = ", a*b )
OUTPUT :
Q6. Write a program to read student data such as Student Name, Student
Age, marks for 3 subjects and to display Average marks.
CODE :
StdName = input("Enter name : ")
StdAge = input("Enter age : ")
S1 = float(input("Enter marks for sub1 : "))
S2 = float(input("Enter marks for sub2 : "))
S3 = float(input("Enter marks for sub3 : "))
avg = (S1+S2+S3)/3
print(StdName)
print(StdAge)
print(avg)
OUTPUT :
Q7. Write a program to read 3 values from the user for creating list and
display their values and their addition.
CODE :
a=[0,0,0]
a[0] = int (input ("Enter value for 1st element of list"))
a[1]= int (input ("Enter value for 2nd element of list"))
a[2] = int (input ("Enter value for 3rd element of list"))
Page | 25
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
print ( a[0], a[1], a[2])
add=( a[0] + a[1] + a[2])
print (add)
OUTPUT :
Q8. Write a program to create a list and add n number of item to it.
CODE :
a = []
print("List:", a)
OUTPUT :
Q9. List.append(obj)
Appends object to list
CODE :
Page | 26
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
OUTPUT :
Q10. list.count(obj)
Returns count of how many times obj occurs in list
CODE :
Q11. List.extend(seq)
Appends the contents of seq to list
CODE :
Q12. List.index(obj)
Returns the lowest index in list that obj appears
CODE :
Page | 27
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
Q13. List.insert(index,obj)
Insert object obj into list at offset index.
CODE :
Q14. List.pop(obj=list[-1])
Removes and returns last object or obj from list
CODE :
Q15. List.remove(obj)
Removes object obj from list
CODE :
Q16. List.revese()
CODE :
str = "malayalam"
listl = list(str)
list2 = listl.copy()
res = listl.reverse()
if listl == list2:
print ("The string is a palindrome")
else:
print ("The string is not a palindrome")
OUTPUT :
Q17 list.sort([func])
Sorts objects of list, use compare func if given
CODE :
Page | 29
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
PRACTICAL 5
Q1: Write a program to create a tuple with the name T1 with five
values and tuple T2 with 5 values and perform following operations.
CODE:
T1 = (13, 2, 4, 5) T2 = (6, 8, 9, 10)
print("The values of T1 and T2 is:",
T1 + T2) print("The 4th element
is:", T1[3]) print("The 2nd element
is:", T2[1])
print("The addition of 4th and 2nd elements are.",
T1[3] + T2[1]) NewTup = T1 + T2 print(NewTup)
OUTPUT:
OUTPUT:
Page | 30
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
Avg = (students[1] + students[2] + students[3] +
students[4]) / 4 percent = (Avg / 100) * 100) print("The
average percentage of Darpan is:", percent)
OUTPUT:
print("Tuple:", my_tuple)
OUTPUT:
OUTPUT:
Page | 31
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
1. index():-
CODE:
Thistu(1,3,5,7,8,9,6,7,0,8)
x = thistu.index(8)
print(x)
OUTPUT:
2. count():-
CODE:
thistu = (1, 3, 5, 7, 8, 9, 6,
7, 0, 8) x =
thistuple.count(8) print(x)
OUTPUT:
3. all():-
Code:
print(all([True, True, False]))
OUTPUT:
Page | 32
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
4. any():- Code:
L = [False, False, True, False, False]
print(any(L))
OUTPUT:
5. clear():-
Code:
Car = {
"brand": "ford",
"Model": "mustang",
"Year": 1964
}
Car.clear()
print(Car)
OUTPUT:
6. copy():- Code:
Car = {
"brand": "ford",
"Model": "mustang",
"Year": 1964
}
x=
Car.copy()
print(x)
OUTPUT:
Page | 33
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
7.
fromkeys():-
Code:
x = ('key1', 'key2',
'key3')
y=0
thisadict =
dict.fromkeys(x, y)
print(thisadict)
OUTPUT:
8.get(
):-
Code
:
Car = {
"brand": "ford",
"Model":"mustang",
"Year": 1964
}
x=
Car.get("Model")
print(x)
OUTPUT:
Page | 34
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
PRACTICAL 6
CODE:
Number = {"Val1":19, "Val2":23, 'Val3':50, "Val4":4,
"Val5":91} print("All the values of dictionary:", Number)
print("The value of last element is:", Number['Val5'])
Number['Val3'] = Number['Val3'] + 100 print("Is the second
element greater than 50?", Number['Val2'] > 50) del
Number["Val4"] print("The updated 3rd element is:",
Number['Val3']) print("The dictionary after deleting the
fourth element:", Number) modulo = Number['Val5'] % 10
print("The modulo of the last element with 10 is:", modulo)
OUTPUT
Page | 35
PYTHON PROGRAMMING-I
GURUNANAK KHALSA COLLEGE Rollno -265 Rizwan Pathan
(AUTONOMOUS)
CourseDetails = {
'CName': "Python",
'StudReg': 230,
'CDays': ['Fr', 'Sat', 'Sun'],
'CFees': 2500} print("The Course Details are:", CourseDetails) print('The
details of course days are:', CourseDetails['CDays']) print('The details of
Course fees are:', CourseDetails['CFees']) CourseDetails['CFees'] =
CourseDetails['CFees'] + 1000 print('The updated course fees are:',
CourseDetails['CFees']) print('The number of students registered for the
course are:', CourseDetails['StudReg']) del CourseDetails['CDays'][0]
print('The updated course days are:', CourseDetails['CDays'])
OUTPUT:
Q3:Write a Python program to create a dictionary with the six subject marks
for the stude Calculate the percentage of students and display details with
percentage.
CODE:
Sub = {'S1': 30, 'S2': 40, 'S3': 50, 'S4': 60, 'S5': 70, 'S6': 80}
OUTPUT:
Page | 36
PYTHON PROGRAMMING-I