Question-125: When you run a below code snippet, then what would be printed on console?
listA= [1,2,3,4,5]
listB=listA[:]
listA[0]=999
print(listB[0])
- [1,2,3,4,5]
- Get all Questions and Answer from here
- You need to have paid subscription to access all questions
- Thanks for considering Python Certification Material
Answer: D
Exp:
List slicing
- If you want to create a new list from existing list then you can use slice.
- You can have exactly same copy of the list, which is duplicated in memory.
- If you want some portion of the list and create a new list then also you can use slice.
- In below program both listA and listB are different even in memory.
Creating a new list using existing list, with slice (“:”)
listA= [1,2,3,4,5] #Creating a new list using existing list listB=listA[:] You can access to full explanation to question and answer from this page. |