Lecture 7

Today:

  • break and continue in while loops
  • [Lists, Lists, Lists]



Break and continue keywords

These are keywords:

  • break stops the loop
  • continue skips the rest of the current looping of the loop but continues to loop as usual afterwards


Let's print powers of two up to $2^{10}$.

In [1]:
x = 1
while x<1000:
    x *= 2
    print(x)
2
4
8
16
32
64
128
256
512
1024

Try to guess what will happen if we run this:

In [2]:
x = 1
while True:
    x *= 2
    if x > 100:
        break
    print(x)
2
4
8
16
32
64

When the break keyword was called, it stopped the loop without doing the print statement underneath.


Continue is similar, but it will skip the rest of only one looping. Afterwards, the loop will continue as before.

In [3]:
x = 1
while x<1000:
    x *= 2
    if x == 128 or x == 256:
        continue
    print(x)
2
4
8
16
32
64
512
1024

The above output should be missing 128 and 256. The print statement did not execute during those loopings of the loop because continue skipped over to the next loop.



Lists

So far, the types we have seen are: int, float, boolean, function (and builtin_function). Today, a brand new type in python: list.

Lists are really awesome. They represent sequences of objects. They allow us to store lots of things in memory.

Here is how to define them:

In [4]:
xs = [1,2,3,4,5]
In [5]:
print(xs)
[1, 2, 3, 4, 5]
In [6]:
type(xs)
Out[6]:
list


They can have all kinds of objects:

In [7]:
words = ["One", "for", "all"]
In [8]:
print(words)
['One', 'for', 'all']

Mixed too:

In [9]:
mylist = [1,2,"Hello", True]


If course you can access the elements. Here is the element at index 0, i.e. the 0th spot in the list.

In [10]:
mylist[0]
Out[10]:
1

The numbering always starts from zero.

In [11]:
print(mylist[0], mylist[1], mylist[2], mylist[3])
1 2 Hello True

This is what happens if you ask for more than they can give ya:

In [12]:
mylist[4]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-12-6b6cb915a21c> in <module>()
----> 1 mylist[4]

IndexError: list index out of range

The list had length 4 and by asking at index 4, we were asking for the 5th element.

Length of the list:

In [13]:
len(mylist)
Out[13]:
4

You can change the contents of a list after you have created it. (they are mutable, this is an important keyword)

In [14]:
mylist = [1,2,"Hello", True]
mylist[2] = "Goodbye"
print(mylist)
[1, 2, 'Goodbye', True]


Can we add an element? Yes: we use the append function. But the syntax is a little funny:

list_you_want_to_extend.append(the_new_element_you_are_adding)

In [15]:
mylist = [1,2,"Hello", True]
mylist.append("love")
print(mylist)
[1, 2, 'Hello', True, 'love']

The 5th element was "love". Just like in the movie.


Deleting elements in a list:

In [16]:
mylist = [14,13,12,11,10,12]
del mylist[2]
print(mylist)
[14, 13, 11, 10, 12]
In [17]:
mylist = [14,13,12,11,10,12]
mylist.remove(12)
print(mylist)
[14, 13, 11, 10, 12]


By the way, there is an empty list:

In [18]:
xs = []
print(xs)
[]
In [19]:
xs.append("element 0")
print(xs)
['element 0']


The range function

very useful

In [20]:
list(range(10))
Out[20]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In Python 2, range produces a list. But not so in Python 3. We have to call list(range(n)) to get an actual list. This is because range creates something which can produce a list but you have to ask it to. We'll come back to this.

In [21]:
# not a list
range(10)
Out[21]:
range(0, 10)


Other ways of using range.

In [22]:
list(range(10,15))
Out[22]:
[10, 11, 12, 13, 14]
In [23]:
list(range(10,22,3))
Out[23]:
[10, 13, 16, 19]


For loops

A very easy way to loop through a list.

In [24]:
for item in [1,10,100,1000,12344]:
    print(item)
1
10
100
1000
12344

Easier and cleaner than while loops.

In [25]:
total = 0
for i in range(100):
    total += i
print(total)
4950


Good exercises:

  • make a list of all the prime numbers up to 1000 (using append to add elements)
  • make a function that will return the maximum element of a list
  • make a function that will return the sum of the elements of a list
  • make a function that takes two lists, and checks to see if they have at least one common element