Python : Lab Assignment No.2 (List & Tuple)

Python : Lab Assignment No.2 (List & Tuple)

Ques 1) Write a program to access the elements of the list.

nums = [5, 15, 35, 8, 18]
for i in nums:
    print(i, end=" ")

Ques 2) Write a program to perform the several operations on the list.(append ,insert, pop, pop(n), remove, extend, count, sort, clear)

a = [1, 2]
print(a)
a.append(10)
print(a)
a.insert(1,20)
print(a)
a.pop()
print(a)
a.pop(1)
print(a)
a.reverse(2)
print(a)
b = [6, 4, 3]
a.extend(b)
print(a)
print("Count of 1 is",a.count(1))
a = a.sort()
print(a)
a.clear()
print(a)

Ques 3) Write a program to access the elements of the tuple using index and without index.

my_tuple = ("Python")

for i in range(0, len(my_tuple)):
    print(my_tuple[i], end=" ")

for i in my_tuple:
    print(i,end=" ")

Ques 4) Write a program to perform the several operations on the tuple.

a = (1, 2, 3, 4)
print(a)
a = a + (7,)
print(a)
print("Slicing a[2:4] is", a[2:4])
b = ('a', 'b')
c = (a, b)
print("Tuple c is", c)

Ques 5) Write a program to access the elements of the nested list.

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for l in a:
    for i in l:
        print(i, end=" ")
    print()

Ques 6) Write a program to access the elements of the nested tuple.

a = ((3, 4, 5), (4, 5, 7), (1, 4))
for t in a:
    for i in t:
        print(i, end=" ")
    print()

Reactions

Post a Comment

0 Comments