Pythons=12s=append56prints2
There are two errors in the code:
- The
append()method should be called on the lists, not on its own. - After appending
[5,6]tos, the new list will have three elements, so attempting to accesss[2]will result in anIndexError.
Here is the corrected code:
s = [1, 2]
s.append([5, 6])
print(s[2]) # prints [5, 6]
Note that s[2] returns the entire list [5, 6], not just the element 5. If you want to access the element 5, you can do so by indexing into the inner list: s[2][0] would return 5
原文地址: https://www.cveoy.top/t/topic/hdIS 著作权归作者所有。请勿转载和采集!