From 671d1722d46d385ff3f78c06e3d30a94a47deb2b Mon Sep 17 00:00:00 2001 From: vand <vand@dtu.dk> Date: Thu, 10 Jun 2021 19:33:44 +0000 Subject: [PATCH] Upload New File --- module3_wrapup.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 module3_wrapup.py diff --git a/module3_wrapup.py b/module3_wrapup.py new file mode 100644 index 0000000..69b5abe --- /dev/null +++ b/module3_wrapup.py @@ -0,0 +1,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 + + + + + + + + -- GitLab