Tutorial 4
Tutorial 4
234128 – Introduction to
Computing with Python
if x > 0: if x > 0:
print(”Positive”) print (”Positive” )
• Such pair of checks complete one another: If one passes, the other
one surely fails and vise versa.
• These sorts of conditions (if x do this, otherwise do that…) are
common.
if expression:
statement
statement
else:
statement
statement
• if the expression is True, only initial statements will be evaluated.
• if the expression is False, only secondary statements will be evaluated.
print(y)
• Even better:
Using Python standard library max function:
if a < -10:
… if a < -10:
else: …
if a == 10: elif a == 10:
else:
…
<=> …
elif a > 10:
…
if a > 10:
… else:
else: print(‘???’)
print(‘???’) Cleaner and more readable code
• Section I:
- If all forms are filled, print “Thank you!”
- If at least one form is missing, print “Please do not leave empty fields”
• Section II:
- If one form is filled, print “Thank you!”
- If all forms are missing, print “Please do not leave empty fields”
Ord:
character to
number
We don’t need to
remember this!
Location of letter
in the ABC = Unicode value
of letter - Unicode value
of ‘A’
ord(‘A’) = 65
• For a single character (char) to chr(65) = ‘A’
be UPPERCASE, it must fulfil:
• Or: ord(‘Z’) = 90
chr(90) = ‘Z’
65 <= ord(char) <= 90