The most common answer is:
“””
This program shows how indexing and slicing can be used with lists.
“””
Creating an empty list:
my_list = []
print my_list
Creating a list with things in it:
list_of_things = [“hi”, 3, 4.8]
thing_zero = list_of_things[0]
thing_one = list_of_things[1]
thing_two = list_of_things[2]
print thing_zero
print thing_one
print thing_two
print list_of_things
print len(list_of_things)
Unlike with a tuple, you can change a particular element in a list!
list_of_things[0] = 2
print list_of_things
Get everything starting at thing 0 and going up to BUT NOT INCLUDING thing 2
print list_of_things[0:2]
This gets things 1 and 2
print list_of_things[1:3]
This gets everything from thing 1
to the end.
print list_of_things[1:]
This gets everything from the beginning up to but not including
thing 2
print list_of_things[:2]
This gets the last thing.
print list_of_things[-1]
This gets the last two things.
print list_of_things[-2:]
This gets everything but the last thing.
print list_of_things[:-1]
You need to use parentheses with the print function calls to ensure it runs correctly in Python (especially Python 3). Here’s the corrected version of your code with proper formatting and standard ASCII quotation marks:
"""
This program shows how indexing and slicing can be used with lists.
"""
# Creating an empty list:
my_list = []
print(my_list)
# Creating a list with things in it:
list_of_things = ["hi", 3, 4.8]
thing_zero = list_of_things[0]
thing_one = list_of_things[1]
thing_two = list_of_things[2]
print(thing_zero)
print(thing_one)
print(thing_two)
print(list_of_things)
print(len(list_of_things))
# Unlike with a tuple, you can change a particular element in a list!
list_of_things[0] = 2
print(list_of_things)
# Get everything starting at thing 0 and going up to BUT NOT INCLUDING thing 2
print(list_of_things[0:2])
# This gets things 1 and 2
print(list_of_things[1:3])
# This gets everything from thing 1 to the end.
print(list_of_things[1:])
# This gets everything from the beginning up to but not including thing 2
print(list_of_things[:2])
# This gets the last thing.
print(list_of_things[-1])
# This gets the last two things.
print(list_of_things[-2:])
# This gets everything but the last thing.
print(list_of_things[:-1])
This script showcases how to:
- Print an empty list.
- Create a list with various types of elements (string, integer, float) and access individual elements by their index.
- Modify an element of the list (lists are mutable, in contrast to tuples).
- Use slicing to access subparts of the list, including using negative indices to start from the end of the list.