Variables and Operators#

Python’s approach to variables and operators is simple and beautiful, although beginners need some time to see both simplicity and beauty. In each exercise below keep track of what’s happening in detail behind the scenes. That is, track the creation of objects and which name is tied to which object. Before solving the exercises read the chapter on Variables and Operators (sections Operators as Member Functions and Efficiency are not required here).

Global vs. Local Variables 1#

The following code contains an error. Find it (by running the code) and correct it.

def do_something():
    n = n + 1
    print('something')
    
n = 0    # counter for function calls

for i in range(0, 100):
    if i % 11 == 0:
        do_something()

print('function called', n, 'times')

Solution:

# your modifications

def do_something():
    n = n + 1
    print('something')
    
n = 0    # counter for function calls

for i in range(0, 100):
    if i % 11 == 0:
        do_something()

print('function called', n, 'times')

Global vs. Local Variables 2#

The following code shall print 10 lines each containing 5 numbers. Make it work correctly.

def print_n_times_5_numbers(m, n, o, p, q):
    for k in range(0, n):
        print(m, n, o, p, q)

n = 10    # number of rows to print

print('printing', n, 'times 5 numbers:')
print_n_times_5_numbers(42, 23, 32, 24, 111)

Solution:

# your modifications

def print_n_times_5_numbers(m, n, o, p, q):
    for k in range(0, n):
        print(m, n, o, p, q)

n = 10    # number of rows to print

print('printing', n, 'times 5 numbers:')
print_n_times_5_numbers(42, 23, 32, 24, 111)
printing 10 times 5 numbers:
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111
42 23 32 24 111

List of Squares#

Make the following code work correctly.

a = [1, 2, 3, 4, 5]

squares = a
for i in range(0, len(squares)):
    squares[i] **= 2

print('squares of', a, 'are:')
print(squares)

Solution:

# your modifications

a = [1, 2, 3, 4, 5]

squares = a
for i in range(0, len(squares)):
    squares[i] **= 2

print('squares of', a, 'are:')
print(squares)
squares of [1, 4, 9, 16, 25] are:
[1, 4, 9, 16, 25]

Similar Code, Different Results#

Why do the following two code cells yield different results? Explain in detail what’s happening!

a = 2
b = a
a = 5
print(b)
2
a = [2]
b = a
a[0] = 5
print(b[0])
5

Solution:

# your answer

2 > 3?#

Guess why the condition 2 <= 3 == True evaluates to False. How to repair?

if 2 <= 3 == True:
    print('2 <= 3')
else:
    print('2 > 3, really?')

Solution:

# your modifications

if 2 <= 3 == True:
    print('2 <= 3')
else:
    print('2 > 3, really?')
2 > 3, really?

Minus Minus#

Why do the following two code cells yield different results?

a = 5
b = 2
c = 3
print(a - b - c)
0
a = 5
b = 2
c = 3
a -= b - c
print(a)
6

Solution:

# your answer