0% found this document useful (0 votes)
18 views3 pages

PWP PR 6

The document contains a series of Python code snippets demonstrating various list operations, including accessing elements, summing and multiplying values, finding the largest and smallest numbers, reversing a list, deleting even elements, and removing common elements between two lists. It also showcases methods of the list class such as append, extend, count, insert, remove, and pop. Each code snippet is accompanied by its output, illustrating the results of the operations performed.

Uploaded by

arqamqazi549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

PWP PR 6

The document contains a series of Python code snippets demonstrating various list operations, including accessing elements, summing and multiplying values, finding the largest and smallest numbers, reversing a list, deleting even elements, and removing common elements between two lists. It also showcases methods of the list class such as append, extend, count, insert, remove, and pop. Each code snippet is accompanied by its output, illustrating the results of the operations performed.

Uploaded by

arqamqazi549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No.

6
#Name: Qazi Arqam Arif
#Enrollment: 2205690362
#Batch: 2

Code:
#Access List Element
L = [['COMPUTER', 'AIML'], ['Mechanical', 'MECHATRONICS'], 'CIVIL', 'ROBOTICS']
print("Length: ",len(L))

print(L[1])
print(L[1:3])
print(L[-2])
print(L[-3:3])
print(L[1:-4])
print(L[-3:-4])
print(L[:])
print(L[1][1])
Output:
Length: 4
['Mechanical', 'MECHATRONICS']
[['Mechanical', 'MECHATRONICS'], 'CIVIL']
CIVIL
[['Mechanical', 'MECHATRONICS'], 'CIVIL']
[]
[]
[['COMPUTER', 'AIML'], ['Mechanical', 'MECHATRONICS'], 'CIVIL', 'ROBOTICS']
MECHATRONICS

Code:
# Sum of element
L = [11, 2, 4, 20]
print("Sum of all elements: ", sum(L))
Output:
Sum of all elements: 37

Code:
# Multipilication of all elements
L = [11, 2, 4, 20]
m=1
for i in L:
m *= i
print("Multipilication of all elements: ", m)
Output:
Multipilication of all elements: 1760

Code:
# Largest and smallest number
L = [11, 2, 4, 20]
print("Largest no: ", max(L))
print("Smallest no: ", min(L))

Output:
Largest no: 20
Smallest no: 2

Code:
# Reverse
L = [11, 2, 4, 20]
L.reverse()
print(L)
Output:
[20, 4, 2, 11]

Code:
# Delete all even elements from a given list
L = [1, 3, 2, 4, 5, 6]
k=0
for i in range(len(L)):
if L[k] % 2 == 0:
del(L[k])
continue
k += 1
print(L)
Output:
[1, 3, 5]

Code:
# Delete common elements
L1 = [10, 20, 30]
L2 = [20, 10, 50, 50]

for i in L1:
if i in L2:
print(i)
Output:
10
20

Code:
# Methods of list class
L = [10, 20, 30]

#append(): add single element at last position


L.append('OOP')
print(L)
#extend(): add multiple elements at last position
L.extend("PWP")
print(L)
L.extend((1, 2))
print(L)

#count(): will return count of specific element in a list


L1 = [1, 2, 3, 1, 2, 3, 4, 1]
print(L1.count(1))

#insert(): add single element at specific position


L1.insert(3, 'SERVER')
print(L1)

#remove(): delete single element from list by using element value


L2 = ['Mange','Grapes','potato','Watermelon']
L2.remove('potato')
print(L2)

# pop(): delete single element from list by using element position


print("Deleted element: ", L2.pop(1))
print(L2)

e1 = L2.pop() # last element will be deleted


print(e1)
print(L2)
Output:
[10, 20, 30, 'OOP']
[10, 20, 30, 'OOP', 'P', 'W', 'P']
[10, 20, 30, 'OOP', 'P', 'W', 'P', 1, 2]
3
[1, 2, 3, 'SERVER', 1, 2, 3, 4, 1]
['Mange', 'Grapes', 'Watermelon']
Deleted element: Grapes
['Mange', 'Watermelon']
Watermelon
['Mange']

You might also like