if 2 + 2 == 5:
print("Sorry?")
else:
print("Bonjour!")
. . .
Answer: It prints ‘Bonjour!’
. . .
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’
. . .
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]
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
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