Munch Lab

Exercise: The ground work

The following exercise is supposed to train your understanding of Python expressions and how they are evaluated by Python. Remember:

  1. Python evaluates one line (one statement) at a time all the way through the file.
  2. Expressions can be reduced to a single Python value e.g. a number or a string.

There is a reason why there are lots of questions in this exercise but no answers. You are supposed to find them yourself – also if it takes you a while. That is the way you build understanding. Some of the questions may seem trivial but do them anyway. If you only understand these concepts superficially it will come back and bite you in the ass when things get more complicated.

Read each exercise and think hard about the questions before you code anything. Then try out the code. It is crucial that you type it in – as boring as it may be. Train your accuracy and attention to detail. Do not copy and paste. Play around with each bit of code. Make small changes and see how it behaves.

The key topics to understand here are: statements, expressions, substitution, reduction.

In the first exercises you are asked to explicitly do the substitution and reduction steps on paper, but you should keep doing it for the remaining exercises until it becomes second nature.

Exercise 1

Compare this:

print "Hello"

to this:

print
"Hello"

What is the difference? How many statements in each? What is printed in each case and why?

Exercise 2

1.2 * (3 + 4) / 5.2

What does the last expression evaluate to? Try to explicitly make all the substitutions and reductions on a piece of paper like we did at the lecture.

Exercise 3

10 % 3 - 2

What does the last expression evaluate to? Try to explicitly make all the substitutions and reductions on a piece of paper like we did at the lecture.

Exercise 4

11 % (7 - 5)**2

What does the last expression evaluate to? Try to explicitly make all the substitutions and reductions on a piece of paper like we did at the lecture.

Exercise 5

a = 5
x = 9
banana = 7
x + 4 * a > banana

What does the last expression evaluate to? Try to explicitly make all the substitutions and reductions on a piece of paper like we did at the lecture. What happens if you write and run the code? Why?

Exercise 6

dance = 'can'
dance = dance + dance
print dance

What does the last expression evaluate to? Try to explicitly make all the substitutions and reductions on a piece of paper like we did at the lecture. What happens if you write and run the code? Why?

Exercise 7

foo = 30
bar = 50
baz = bar + foo
print baz
bar = 10
print baz

What is printed the first and second time? The first print statement prints 80. But what about the second print statement? Does that print 80 or 40? Find out and make sure you understand why it prints what it prints.

Exercise 8

1 == "1"
1 == 1.0

What does this reduce to? Try and print it and see once you think you know. If you were wrong, make sure you understand why.

Exercise 9

a = "1"
b = "2"
c  = a + b
print a, b, c
print a + b == 3

What is printed here? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 10

a = 1
b = 2
c  = a + b
print a, b, c
print a + b == 3

What is printed here? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 11

print "apples" == "pears"

What is printed here? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 12

print "bananas" > "one banana"

What is printed here? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 13

print "banana" < "Banana"

What is printed here? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 14

print 3 / 4
print 3.0 / 4
print 3 / 4.0

What is printed here? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 15

x = 4
doris = 7
print y + doris / x

What will happen if you run this code? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 16

print '%d is larger than %d' % (4, 3')

What will happen if you run this code? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 17

print '%s is the best thing since %s' % ('Python', 'sliced bread')

What will happen if you run this code? Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

Exercise 18

my_template = '%s is the best thing since %s'
language = 'Python'
print my_template % (language, 'sliced bread')
print my_template % (language, 1900 + 89)

What will happen if you run this code? Make sure you do all the substitutions and reductions in your head. Write the code and see for yourself once you think you know. If you were wrong, make sure you understand why.

2 thoughts on “Exercise: The ground work”

  1. Hej Kasper.
    Der er muligvis en fejl i opgave 7, hvor du har skrevet at baz = bar + baz, men du mener baz = bar + foo, ikke? for ellers passer det ikke med, hvad du skriver.
    Derud over er vi i tvivl om, hvor du gerne vil have at vi skriver kommandoerne, for nogle gange skriver du print, som om vi skal skrive i sublime, andre gange ikke, som om vi skal skrive direkte i terminalen i python. Det er lidt forvirrende at du varierer mellem at skrive print nogle steder, og andre steder ikke.
    Mvh. Anne, Mira og Lena

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: