Docsity

Python List Operations, Assignments of Computer Science

Toggle sidebar
bg1
Lab Assignment (LIST)
Realize the following with related to list in Python
1. Creation of list, user input list, use of len() and eval()
2. Indexing of list for both positive and negative.
# Creation of list, user input list, use of len() and eval()
list1 = []; list2 = list(); #Creation of empty list
print("enter elements enter # when done")
entry=input("element no 1 = ")
list1.append(entry)
i=2
while(entry!='#'):
entry = input(f"element no {i} = ")
i+=1
list1.insert(i-1,entry)
if(entry=="#"):
list1.pop()
print(f" you have entered {i-2} elements")
print(list1)
print(len(list1))
print(list1[0])
print(list1[-1])
3. Creation of multi-dimensional list.
#multi-dimensional list
list3 = []
# n=int(input("no of elements:-"))
n=5;r=5;c=6;list3=[];k=1
for i in range(r):
i=[]
for j in range(1,c+1):
i.append(k)
k+=1
list3.append(i)
for i in range(len(list3)):
Date:20/06/22
pf3

Partial preview of the text

Download Python List Operations and more Assignments Computer Science in PDF only on Docsity!

Lab Assignment (LIST)

Realize the following with related to list in Python

1. Creation of list, user input list, use of len() and eval()

2. Indexing of list for both positive and negative.

Creation of list, user input list, use of len() and eval()

list1 = []; list2 = list(); #Creation of empty list print("enter elements enter # when done") entry=input("element no 1 = ") list1.append(entry) i= 2 while(entry!='#'): entry = input(f"element no {i} = ") i+= 1 list1.insert(i- 1 ,entry) if(entry=="#"): list1.pop() print(f" you have entered {i- 2 } elements") print(list1) print(len(list1)) print(list1[ 0 ]) print(list1[- 1 ])

3. Creation of multi-dimensional list.

#multi-dimensional list list3 = []

n=int(input("no of elements:-"))

n= 5 ;r= 5 ;c= 6 ;list3=[];k= 1 for i in range(r): i=[] for j in range( 1 ,c+ 1 ): i.append(k) k+= 1 list3.append(i) for i in range(len(list3)):

for j in list3[i]: print(j,end=' ') print()

4. Different operators with list with small programming.

l1 = [ 1 , 2 , 3 ,"alok"] l2 = [ 21 , 32 , 43 ,"alka"] #arithmetic print(l1+l2) print(l2* 3 ) #relational print(l1>l2);print(l1=l2);print( l1<=l2) #membership operator print( 2 in l1) print( 23 not in l1) #identity operator print(l2[ 0 ] is 21 ) #slicing print(l2[::- 1 ])

5. List functions

  • max()
  • min()
  • sum()
  • sorted()
  • shuffle()
  • any()
  • all() l2 = [ 21 , 32 , 43 , 33 , 333 , 0 ] print(max(l2)) print(min(l2)) print(sum(l2)) l2.sort();print(l2) print(any(l2)) print(all(l2)) import random random.shuffle(l2)