16.3. Working with Lists

In earlier chapters we worked with lists of numbers as shown below.

But a list in Python can also hold different types of things in the same list, like numbers and strings. You start and end a list with square brackets ([]) and separate elements with commas as shown in line 1 below. The function len will return the number of items in the list. You can do “arithmetic” with lists using * and +, just like you can with strings. Multiplication repeats items in the list. We can add two lists together, even if one of the lists only has a single item in it.

Activity: CodeLens 16.3.2 (Simple_Lists)

Note

Remember that a list starts with a [ and ends with a ]. Items in the list are separated by commas.

Run the code below to see what type of error you get if you forget the ending ] as shown below on line 1. Also see what happens if you don’t separate the list items with commas as shown on line 2. Fix the code to run and print.

Note

Discuss topics in this section with classmates.

Show Comments
You have attempted of activities on this page