Now that we understand how we can use objects to store and model our data, we only need to be able to control the flow of our program in order to have a program that can, in principle, do anything!
Specifically we need to be able to:
Once we have these, we can write computer programs to process information in arbitrary ways: we are Turing Complete!
Conditionality is achieved through Python’s if
statement:
x = -3
if x < 0:
print(x, " is negative")
print("This is controlled")
print("Always run this")
-3 is negative
This is controlled
Always run this
The controlled statements are indented. Once we remove the indent, the statements will once again happen regardless.
Python expects an indented statement after an if statement. If the controlled statement is missing or not indented, an error will be generated.
Python’s if statement has optional elif (else-if) and else clauses:
x = -3
if x < 0:
print("x is negative")
else:
print("x is positive")
x is negative
x = 5
if x < 0:
print("x is negative")
elif x == 0:
print("x is zero")
else:
print("x is positive")
x is positive
choice = 'high'
if choice == 'high':
print(1)
elif choice == 'medium':
print(2)
else:
print(3)
1
True
and False
are used to represent boolean (true or false) values.
1 > 2
False
Comparison on strings is alphabetical.
"UCL" > "KCL"
True
But case sensitive:
"UCL" > "kcl"
False
There’s no automatic conversion of the string True to true:
True == "True"
False
Be careful not to compare values of different types.
In python two there were subtle implied order comparisons between types, but it was bad style to rely on these. In python three, you cannot compare these.
'1' < 2
—————————————————————————
TypeError Traceback (most recent call last)
<ipython-input-8-b67fbc3e6cdc> in <module>()
—-> 1 ‘1’ < 2
TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’
Any statement that evaluates to True
or False
can be used to control an if
Statement.
Various other things automatically count as true or false, which can make life easier when coding:
mytext = "Hello"
if mytext:
print("Mytext is not empty")
mytext2 = ""
if mytext2:
print("Mytext2 is not empty")
Mytext is not empty
We can use logical not and logical and to combine true and false:
x = 3.2
if not (x>0 and type(x) == int):
print(x,"is not a positive integer")
3.2 is not a positive integer
not
also understands magic conversion from false-like things to True or False.
Examples strings:
not not "Who's there!" # Thanks to Mysterious Student
True
bool("")
False
bool("James")
True
Examples list:
bool([])
False
bool(['a'])
True
Examples dictionaries:
bool({})
False
bool({'name': 'James'})
True
Examples integers:
bool(0)
False
bool(1)
True
not 2==3
True
But subtly, although these quantities evaluate True or False in an if statement, they’re not themselves actually True or False under ==:
[] == False
False
bool([]) == bool(False)
True
In Python, indentation is semantically significant.
You can choose how much indentation to use, so long as you are consistent, but four spaces is conventional. Please do not use tabs.
In the notebook, and most good editors, when you press <tab>
, you get four spaces.
No indentation when it is expected, results in an error:
x = 2
if x>0:
print(x)
File “
print(x)
^
IndentationError: expected an indented block
But
x = 2
if x>0:
print(x)
2
A statement expecting identation must have some indented code.
This can be annoying when commenting things out. (With #
)
if x>0:
# print x
print("Hello")
File “<ipython-input-24-ffabc0ff1732>”, line 3
print(“Hello”)
^
IndentationError: expected an indented block
So the pass
statement is used to do nothing.
if x>0:
pass
print("Hello")
Hello