|
A list is a collection which is ordered and changeable. In Python lists are written with square brackets.
|
RYB_color = ["Red","Yellow","Blue"] |
RYB_color = ["Red","Yellow","Blue"] |
|
print(RYB_color[1]) |
|
print(RYB_color[-2]) |
RYB_Secondary = ["Red","Yellow","Blue","Orange","Green","Purple"] |
|
|
Note: Index 5 is not included.
|
|
Example 2 |
|
|
Note: Index -2 is not included.
|
|
Example 3 |
|
|
Note: By leaving out the start value, the range will start at the first item.
|
|
Example 4 |
|
|
Note: By leaving out the end value, the range will go on to the end of the list.
|
RYB_color = ["Red","Yellow","Blue"] |
RYB_color = ["Red","Yellow","Blue"] |
RYB_color = ["Red","Yellow","Blue"] |
|
|
Using the
append() method to append an item in the and of the list. |
|
RYB_color.append("White") |
|
|
Use the
insert() method to add an item at the specified index. |
|
RYB_color.insert(2,"White") |
RYB_color = ["Red","Yellow","Blue"] |
|
RYB_color.remove("Yellow") |
#remove the item "Yellow" |
RYB_color.pop() |
#remove the indicated |
del RYB_color[1] |
#remove the item "Yellow" |
del RYB_color |
#delete the whole list |
RYB_color.clear() |
#return an empty list |
RYB_color = ["Red","Yellow",Blue"] |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.