Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

8.2.3 A List Is Like A Mutable Tuple CodeHS Answers, Study Guides, Projects, Research of Marketing Business-to-business (B2B)

8.2.3 A List Is Like A Mutable Tuple CodeHS Answers

Typology: Study Guides, Projects, Research

2024/2025

Available from 05/25/2025

CrackSmart
CrackSmart 🇺🇸

109 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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:]
pf3

Partial preview of the text

Download 8.2.3 A List Is Like A Mutable Tuple CodeHS Answers and more Study Guides, Projects, Research Marketing Business-to-business (B2B) in PDF only on Docsity!

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:])