Python: Introduction to Lists

Question Click to View Answer

Explain the following code and what it prints to the console.

numbers = [10, 20, 30]
print(numbers)

[10, 20, 30] is printed to the console.

The numbers variable is assigned to a list that has three elements. The list is then printed to the console.

What does the following code print to the console?

letters = ["a", "y", "w"]
print(letters[1])

"y" is printed to the console.

The letters variable is assigned to a list of strings. String indexing is used to return the item in position 1 of the list (letters[1]).

List indexing begins at zero, so "a" is in position 0, "y" is in position 1, and "w" is in position 2.

What does the following code print to the console?

odd_numbers = [1, 7, 13, 19]
print(odd_numbers[0:3])

[1, 7, 13] is printed to the console.

This example uses the slicing operator to take the first three elements from a list of odd numbers.

odd_numbers[0:3] takes all items starting at position 0 and ending at position 3.

What does the following code print to the console?

fruits = ["apple", "pear", "cherry"]
print(fruits[-1])

"cherry" is printed to the console.

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 fruits[2] or fruits[-1].

What does the following code print to the console?

countries = []
countries.append("brazil")
print(countries)

['brazil'] is printed to the console.

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)

['li', 'fei', 'mike', 'phil'] is printed.

The + operator combines two arrays into a single array. The + operator performs array concatenation when both of the operands are arrays (["li", "fei"] and ["mike", "phil"] are the operands).

In previous lessons, we've seen how the + operator performs addition when the operands are numbers (e.g. 3 + 4) and performs string concatenation when the operands are strings (e.g. "big" + "law").

What does the following code print to the console?

brands = ["colgate", "dial"]
del brands[0]
print(brands)

['dial'] is printed.

The del keyword can be used to delete items from a list. brands[0] deletes the first element in the list.

What does the following code print to the console?

stuff = ["aa", "bb", "cc", "dd"]
print(stuff[:2])

['aa', 'bb'] is printed.

We could have used stuff[0:2] to get the same result. When the first operand to the slice operator is omitted, it is assumed to be 0.

What does the following code print to the console?

stuff = ["aa", "bb", "cc", "dd"]
print(stuff[2:])

['cc', 'dd'] is printed.

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)

['spanish', 'chinese', 'polish'] is printed.

The extend method is useful for adding multiple items to a list. We could have used two append statements, but this syntax is more verbose.

languages = ["spanish"]
languages.append("chinese")
languages.append("polish")
print(languages)