Newer
Older
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
## module 3, example - not needing selection statement
x = 5
# I want to make sure that x is not smaller than 0
# One way of achieving this:
if x<0:
x = 0
# A better way of achieving the same:
x = max(x,0)
#%% module 3, example - short-circuit operators
name = 'Vedrana'
# I want to check if the name starts with 'Ved'. But what if name has less than
# 3 letters? I need to make sure that name[:3] is not evaluated if name is too
# short. I add the first condition and the second condition will not be
# evaluated if the first is false.
if len(name)>2 and name[:3]=='Ved':
print('Cool')
#%% module 3, example -- an extra condition
x = 4
# I want to check whethr x is bigger than 5
# A bad way of achieving this:
if x>5:
print('Bigger than 5')
elif x<=5:
print('Smaller or equal 5')
# A better way of achieving this:
if x>5:
print('Bigger than 5')
else:
print('Smaller or equal 5')
#%% module 3, example -- nothing to fall to
# Make sure that return value is assigned in the function call
def give_verdict(x):
x = ''
if x>0:
verdict = 'Bigger than 0'
elif x<0:
verdict = 'Smaller than 0'
return verdict
print(give_verdict(0))
#%% module 3, example -- using boolean flags
is_rgb = False
if is_rgb: # dont write "if is_rgb==True"
print('special treatment for color images')
else:
print('no special treatment')
#%% module 3, example -- debugging, line by line
x = 41.7
if x > 137/4:
x = 2*x + 154/18
elif x< 117/4:
x = x + 154/18