Reading exercices in Python

  1. What does the following code do?
if 2 + 2 == 5:
    print("Sorry?")
else:
    print("Bonjour!")

. . .

Answer: It prints ‘Bonjour!’

. . .


  1. What does the following program do?
state = 1
for i in range(100):
        if state == 1: 
            print('A')
        if state == 2:
            print('B')
        if state == 3:
            print('C')
        state = state + 1
        if state == 4:
            state = 1

. . .

Answer: It prints a sequence of alternating ‘A’, ‘B’ and ‘C’


. . .

  1. What does the following program do (‘%’ is the “modulo” operator that returns the remainder of the integer division)
n = 100
while n > 0:
    if (n % 7) == 0:
        print(n)
    n = n - 1

. . .

Answer: It prints the numbers between 1 and 100 that are divisible by 7

A more efficient version would be:

    [x for x in range(101) if x%7 == 0]

  1. What does the function ssn do in the following code?
def ss(l):
    s = 0
    for x in l:
        s = s + x * x
    return s
    
def ssn(n):
    return ss(range(n+1))

. . .

Answer: It returns the sum of squares of the first ‘n’ integers


  1. What does the following function do?
def un(l):
    u = []
    for x in l:
        if not(x in u):
            u.append(x)
    return u

. . .

Answer: It returns the set of elements in the list l