Question | Click to View Answer |
Explain the following code and what it prints to the console. numbers = [10, 20, 30]
print(numbers)
|
The |
What does the following code print to the console? letters = ["a", "y", "w"]
print(letters[1])
|
The List indexing begins at zero, so |
What does the following code print to the console? odd_numbers = [1, 7, 13, 19]
print(odd_numbers[0:3])
|
This example uses the slicing operator to take the first three elements from a list of odd numbers.
|
What does the following code print to the console? fruits = ["apple", "pear", "cherry"]
print(fruits[-1])
|
Negative indexing is used to fetch items from a list, starting at the end of the list. "cherry" is in position -1, "pear" is in position -2, and apple is in position -3. In this example, we can fetch "cherry" with either |
What does the following code print to the console? countries = []
countries.append("brazil")
print(countries)
|
The countries variable is assigned to an empty list and then the "brazil" string is added to the list. Python lists are mutable, so elements can be added or deleted from lists. |
What does the following code print to the console? some_names = ["li", "fei"]
more_names = ["mike", "phil"]
print(some_names + more_names)
|
The In previous lessons, we've seen how the |
What does the following code print to the console? brands = ["colgate", "dial"]
del brands[0]
print(brands)
|
The |
What does the following code print to the console? stuff = ["aa", "bb", "cc", "dd"]
print(stuff[:2])
|
We could have used |
What does the following code print to the console? stuff = ["aa", "bb", "cc", "dd"]
print(stuff[2:])
|
We need to omit the second operand to the slice operator to get items all the way to the end of the list. |
What does the following code print to the console? languages = ["spanish"]
languages.extend(["chinese", "polish"])
print(languages)
|
The languages = ["spanish"]
languages.append("chinese")
languages.append("polish")
print(languages)
|