Wednesday Lecture

Practice Exercises

Try to solve the following:

  1. Print the word hello 5 times. Use a for loop.

  2. Make a list containing the word hello 5 times. Use list comprehension.

  3. Make a list containing the even numbers from 0 to 20 (inclusive). Save it with the name even_list.

  4. Make a list containing the squares of the numbers in even list.

  5. (This is probably too hard. Try to find the answer by Google searching.) Write code that turns an integer like 8245313 into a list of integers, like [8,2,4,5,3,1,3].

Zoom recording from lecture on 1/5/2022

Answer 1

for i in range(5):
    print('hello')
hello
hello
hello
hello
hello

Since the variable name i is never used in the above code, some people would replace it with an underscore _. I don’t necessarily recommend doing this, but it is good to recognize it if you see it in somebody else’s code.

for _ in range(5):
    print('hello')
hello
hello
hello
hello
hello

Answer 2

I don’t have a precise definition for what makes the following list comprehension, but the most important aspects are:

  • Square brackets (that makes it a list)

  • The thing you want to go into the list (in this case, the string "hello")

  • How many repetitions/iterations there should be.

["hello" for x in range(5)]
['hello', 'hello', 'hello', 'hello', 'hello']

Some other approaches which are not list comprehension.

You can combine two lists using +:

[1,3,5]+["hello" for i in range(2)]
[1, 3, 5, 'hello', 'hello']

So it makes sense that if you use *, that combines multiple copies of the same list:

["hello"]*5
['hello', 'hello', 'hello', 'hello', 'hello']

Answer 3

Here is one approach, using list comprehension:

even_list = [x for x in range(0,22,2)]
even_list
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Here is another approach, which converts the range object to a list object directly:

even_list = list(range(0,22,2))
even_list
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Timing in Deepnote notebooks

The two approaches seem comparable in terms of speed.

%%timeit
[x for x in range(0,2000000,2)]
39.9 ms ± 228 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%%timeit
list(range(0,2000000,2))
22.7 ms ± 1.16 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Answer 4

Again using list comprehension:

[i**2 for i in even_list]
[0, 4, 16, 36, 64, 100, 144, 196, 256, 324, 400]

Answer 5

This question was difficult, but I hope each step in the answer makes sense.

n = 8245313
n_str = str(n)
n_list = list(n_str)
n_final = [int(x) for x in n_list]
n_final
[8, 2, 4, 5, 3, 1, 3]

If you want, you can do the whole thing at once:

n = 8245313
n_final = [int(x) for x in list(str(n))]
n_final
[8, 2, 4, 5, 3, 1, 3]

Defining a function in Python

Here is the basic syntax.

def f(x):
    return x**2
f(10)
100

It works equally well with multiple variables and multiple lines of code (just make sure they’re all indented).

def g(x,y):
    z = x+y
    return ["hello" for i in range(z)]
g(2,1)
['hello', 'hello', 'hello']
def h(n):
    i = n+2
    return i**2
h(10)
144
h(2)+2
18

It is okay to have the function not return anything, but then there is no output to use.

def j(n):
    for i in range(n):
        print("hi")

Maybe it looks like there is an output:

z = j(5)
hi
hi
hi
hi
hi

But we can see that the “output” is a NoneType:

type(z)
NoneType

This can be a common source of mistakes. For example, maybe the author of the next code wants v to be equal to [3,3,2,5], but it isn’t.

w = [3,3,2]
v = w.append(5)
v
type(v)
NoneType

The expression w.append(5) did not generate an output; instead, it changed the value of w.

w
[3, 3, 2, 5]