List can be seen as a collection: they can hold many variables. An empty list would simply be
In Python you define a list using brackets.

list = []
A list can have any number of elements. They are similar to arrays in other programming languages.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

Example

Lists can hold both numbers and text. Regardless of contents, they are accessed in the same fashion. To access a list add the id between the brackets, such as list[0], list[1] and so on. Type the code below and run it:
#!/usr/bin/python

list = [ "New York", "Los Angles", "Boston", "Denver" ]

print(list) # prints all elements
print(list[0]) # print first element

list2 = [1,3,4,6,4,7,8,2,3]

print(sum(list2))
print(min(list2))
print(max(list2))
print(list2[0])
print(list2[-1])

This should output:

list

Exercise

  1. Make a program that displays the states in the U.S.

    states = [ 'Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming' ] 

  2. Display all states starting with the letter M

Download answers