Informatics Practices
What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del myList[:5]
print(myList)
Python List Manipulation
1 Like
Answer
[6, 7, 8, 9, 10]
Working
In the given code, myList
is initialized as [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. The del myList[:5]
statement deletes all elements in the list from the beginning up to, but not including, index 5. This removes the first five elements (1, 2, 3, 4, 5) from myList
. The subsequent print(myList)
statement then outputs the modified list, which is [6, 7, 8, 9, 10].
Answered By
1 Like
Related Questions
What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for i in range(0, len(myList)): if i % 2 == 0: print (myList[i])
What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del myList[3:] print(myList)
What will be the output of the following code segment?
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del myList[::2] print(myList)
Write the output of the following Python program code:
Str2 = list("Cam@123*") for i in range(len(Str2)-1): if i==5: Str2[i] = Str2[i]*2 elif (Str2 [i].isupper()): Str2 [i] = Str2 [i]*2 elif (Str2 [i].isdigit()): Str2 [i] = 'D' print(Str2)