Skip to content

Lists


Related course:

Lists

In Python you define a list using brackets.  An empty list would simply be list = [].  A list can have any number of elements. 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])

You should see

Exercises

Try the exercises below

1. Print the last item, "Denver", of 
   list = [ "New York", "Los Angles", "Boston", "Denver" ]

2. Try to get this output: 
    city : New York
    city : Los Angles
    city : Boston
    city : Denver

After completing these continue with the next exercise.

Answers

Exercise 7.1: Optional parameters
Dictionary

Be First to Comment

    Leave a Reply