0% found this document useful (0 votes)
158 views4 pages

List Problems

The document demonstrates various operations and methods that can be performed on lists in Python. It defines several lists with different data types and values. It then prints the lists, performs indexing and slicing on the lists to access elements, concatenates lists, copies a list, appends and pops elements from lists, and performs other common list methods like reversing, finding minimum/maximum values, and updating element values. One list operation generates an error.

Uploaded by

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

List Problems

The document demonstrates various operations and methods that can be performed on lists in Python. It defines several lists with different data types and values. It then prints the lists, performs indexing and slicing on the lists to access elements, concatenates lists, copies a list, appends and pops elements from lists, and performs other common list methods like reversing, finding minimum/maximum values, and updating element values. One list operation generates an error.

Uploaded by

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

LIST OPERATIONS & METHODS

List1=[10,20,"ABC",[100,200,300]]
List2=["Apple","Orange","Grape"]
List3=[(1,2),[3,4],"56",78,9.0]
List4=[10,20,30,40,50,60,70,80,90]
print("List1",List1)
print("List2",List2)
print("List3",List3)
print("List4",List4)
print("List1[2]",List1[2])
print("List1[3]",List1[3])
print("List1[3][2]",List1[3][2])
print("List3[0]",List3[0])
print("List3[0][0]",List3[0][0])
print("List4[:]",List4[:])
print("List4[1:]",List4[1:])
print("List4[1:-1]",List4[1:-1])
print("List4[1:5]",List4[1:5])
print("List4[1:5:-1]",List4[1:5:-1])
print("List4[5:1:-1]",List4[5:1:-1])
print("List4[2:-5]",List4[2:-5])
print("List4[6:2]",List4[6:2])
print("List4[6:2:-1]",List4[6:2:-1])
print("List4[-8:-2]",List4[-8:-2])
print("List4[-2:-8]",List4[-2:-8])
print("List4[-2:-8:-2]",List4[-2:-8:-2])
print("List4[::2]",List4[::2])
print("List4[::-1]",List4[::-1])
print("List4[2::-2]",List4[2::-2])
print("List4[::]",List4[::])
print("List4[2:3]",List4[2:3])
print("List4[2:2]",List4[2:2])
print("Length of List3",len(List3))
print("Append Python to List 3")
List3.append("Python")
print("List3",List3)
print("Reversing List3")
List3.reverse()
print("List3",List3)
print("Pop an element from List3, List3.pop()",List3.pop())
print("Pop 2nd Element from List3, List3.pop(2)",List3.pop(2))
print("Maximum in List4",max(List4))
print("Minimum in List4",min(List4))
print("Copy List4 to List5, List5=list4.copy() - CLONING")
List5=List4.copy()
print("List4",List4)
print("List5",List5)
print("Update value of list4[3]=100")
List4[3]=100
print("List4",List4)
print("List5",List5)
print("Aliasing List6=List1")
List6=List1
print("Update value of list1[1]=Programming")
List1[1]="Programming"
print("List1",List1)
print("List6",List6)
print("Concatenation List 1+List2\n",List1+List2)

OUTPUT
List1 [10, 20, 'ABC', [100, 200, 300]]
List2 ['Apple', 'Orange', 'Grape']
List3 [(1, 2), [3, 4], '56', 78, 9.0]
List4 [10, 20, 30, 40, 50, 60, 70, 80, 90]
List1[2] ABC
List1[3] [100, 200, 300]
List1[3][2] 300
List3[0] (1, 2)
List3[0][0] 1
List4[:] [10, 20, 30, 40, 50, 60, 70, 80, 90]
List4[1:] [20, 30, 40, 50, 60, 70, 80, 90]
List4[1:-1] [20, 30, 40, 50, 60, 70, 80]
List4[1:5] [20, 30, 40, 50]
List4[1:5:-1] []
List4[5:1:-1] [60, 50, 40, 30]
List4[2:-5] [30, 40]
List4[6:2] []
List4[6:2:-1] [70, 60, 50, 40]
List4[-8:-2] [20, 30, 40, 50, 60, 70]
List4[-2:-8] []
List4[-2:-8:-2] [80, 60, 40]
List4[::2] [10, 30, 50, 70, 90]
List4[::-1] [90, 80, 70, 60, 50, 40, 30, 20, 10]
List4[2::-2] [30, 10]
List4[::] [10, 20, 30, 40, 50, 60, 70, 80, 90]
List4[2:3] [30]
List4[2:2] []
Length of List3 5
Append Python to List 3
List3 [(1, 2), [3, 4], '56', 78, 9.0, 'Python']
Reversing List3
List3 ['Python', 9.0, 78, '56', [3, 4], (1, 2)]
Pop an element from List3, List3.pop() (1, 2)
Pop 2nd Element from List3, List3.pop(2) 78
Maximum in List4 90
Minimum in List4 10
Copy List4 to List5, List5=list4.copy() - CLONING
List4 [10, 20, 30, 40, 50, 60, 70, 80, 90]
List5 [10, 20, 30, 40, 50, 60, 70, 80, 90]
Update value of list4[3]=100
List4 [10, 20, 30, 100, 50, 60, 70, 80, 90]
List5 [10, 20, 30, 40, 50, 60, 70, 80, 90]
Aliasing List6=List1
Update value of list1[1]=Programming
List1 [10, 'Programming', 'ABC', [100, 200, 300]]
List6 [10, 'Programming', 'ABC', [100, 200, 300]]
Concatenation List 1+List2
[10, 'Programming', 'ABC', [100, 200, 300], 'Apple', 'Orange', 'Grape']
2. One of the following 10 statements generates an error. Which one? (Your answer
should be a number between 1 and 10.)

x = ["sun",[17],2,"king",[3,4]] # Statement 1
y = x[0:8] # Statement 2
z=x # Statement 3
w=y # Statement 4
z[0] = 0 # Statement 5
y[0] = y[0][0:3] + 'k' # Statement 6
y[1][1:3] = [5,8] # Statement 7
x[0] = x[0][1:3] # Statement 8
w[4][0] = 1000 # Statement 9
a = (x[4][1] == 4) # Statement 10
Answer: Statement 8
3. Consider the following lines of Python code.
x = [589,'big',397,'bash']
y = x[2:]
u=x
w=y
w = w[0:]
w[0] = 357
x[2:3] = [487]

Answer: x[2] == 487, y[0] == 397, u[2] == 487, w[0] == 357


3. What is the value of second after executing the following lines?
first = "kaleidoscope"
second = ""
for i in range(len(first)-1,-1,-2):
second = first[i]+second
Answer: aedsoe
4. What is the value of list1 after the following lines are executed?

def mystery(l):
l[0:2] = l[3:5]
return()
list1 = [34,17,12,88,53,97,62]
mystery(list1)
Answer: [88, 53, 12, 88, 53, 97, 62]

You might also like