1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Example: if-else
x = 3
if x > 0:
# if block
print("x is positive")
sign = 1
elif x == 0:
# elif (else-if) block
print("x equals zero")
sign = 0
else:
# else block
print("x is negative")
sign = -1
print(sign) # new block
# Example: for-loops
a = [1, 2, 3, -4]
for x in a:
# start of the for block
print(x)
print(x) # new block, but x is still in scope!
# Example: the enumerate function
a = [1, 2, 3, -4]
for i, x in enumerate(a):
print('a at', i, '=', x)
# Example: a loop combined with a conditional
a = [1, 2, 3, -4]
abs_sum = 0
for x in a:
if x > 0:
abs_sum += x
else:
abs_sum += -x
print(abs_sum)
# Example: alternatively, the Pythonic way to replace the above
print(sum(abs(x) for x in a))
# Example: else as a completion clause
s = 'Jane is 42 years old'
for c in s:
if c.isdigit():
print('Found:', c)
break
else:
print('No digit was found!')
Enter to Rename, Shift+Enter to Preview