In Python, a list is created by enclosing a comma-separated sequence of values in square brackets []
. For example, the following code creates a list of integers:
main.py26 chars2 lines
You can also create a list of strings:
main.py48 chars2 lines
To access an element in a list, you can use square brackets []
with the index of the element you want to access. Note that the index starts at 0 in Python. For example, to access the second element in the my_list
, you can use:
main.py28 chars2 lines
You can also modify the elements of a list by assigning new values to specific indexes. For example, to change the third element in fruits
to "pear"
, you can use:
main.py19 chars2 lines
Finally, you can add new elements to a list using the append()
method:
main.py18 chars2 lines
This will add 6
to the end of my_list
.
gistlibby LogSnag