Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Quizzma Latest Articles

8.2.3 A List Is Like A Mutable Tuple CodeHS Answers

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.

Was this helpful?




Quizzma Team

Quizzma Team

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.

Related Posts