So far we've seen python do some things which we could never have done by hand- constructing large vectors, doing 100s of calculations simultaneously, etc. This already makes python a useful tool. However, there's another fundamental aspect of computer programming that we haven't even addressed yet, and it's what makes computer programs useful in most real world operations.
When we looked at vectors, we saw how python can treat a list of numbers all in one go, instead of doing them one at a time. Occasionally however, we come across problems where we need to go step by step. Take the simple example of determining if a number is even. Given a particular number, we determine if it is exactly divisible by 2, or not.
These are simply operations we perform in our minds all the time: basically, making either/or decisions. Suppose we have a function
$$f(x)= \begin{cases} \frac{x^2-x}{x},& \text{if } x\geq 1\\ 0, & \text{otherwise} \end{cases} $$
clearly we have to make a decision about which form of the function we use based on the value of x. I.E "if x is 1 or bigger do this, otherwise do that".
When we want to make decisions like this in python, we use a special construct called an if statement. By default, python will execute all of the commands you enter in order (from top to bottom). However, if we want to performs something only under certain conditions we use the if statement. You might think of it something like this:
The specific example of the function above could be represented similarly as:
The if-statement can be though of as branching the code. The basic syntax is
if <statement_is_true>:
<command 1>
else:
<command 2>
<ordinary command>
where the bits in < > would need to be filled in for specific cases. Notice here again, like with functions, the indented code following the colon (:) is considered to be part of the if statement, and unindented code marks the end. So <ordinary command> gets executed either way. Simple example
x=1
if (x==1):
print('x is 1')
print('that was an if statement')
when python sees an if statement, it looks at the statement in brackets to see whether it is true or false. In our case we are asking if x is equal to 1- that is what the double equals sign (==) means- compare these two numbers and see if they are equal. If the statement is true (in our case it will be), then python executes all of the following commands- in our case it will print out 'x is 1'. If, on the other hand it is not true it skips the commands and moves on. So if we execute this code, it should print
x is 1
that was an if loop
In this case we did not include an else statement. But we could just as well have added something like this
x=2
if (x==1):
print('x is 1')
else:
print('x is not 1')
print('that was an if loop')
In this case (note we changed the value of x), the statement is no longer true, so python executes the code following the else statement: we should get
x is not 1
that was an if loop
Note that because print('that was an if loop') is not indented, this is considered to be an ordinary piece of code, and it executes whether the statement was true or not.
Let's look at the flow chart for this example:
whereby you can clearly see the 'branching' character of the logical flow. You should also note that both branches return to the same 'trunk' eventually'.
There are many different comparisons which can be made, all of which either give a value of True or False.
if (x == y): # equal
if (x > y): # greater than
if (x < y): # less than
if (x >= y): # greater than or equal
if (x <= y): # less than or equal
if (x != y): # not equal
Importantly, you can also string together these statements to make more involved logical tests. We can do this with the keywords 'and' and 'or' like this:
if ( x == y or x == 0 ):
if ( x < y and x != 0 ):
This might get a little confusing, so to make things clearer you might like to put each statement in brackets:
if ( (x == y) or (x == 0) ):
if ( (x < y) and (x != 0) ):
Remember that for logical expressions like this there are simple tables to remember what is true and false
AND: both expressions need to be true
| INPUTS | True | False |
|---|---|---|
| True | TRUE | FALSE |
| False | FALSE | FALSE |
OR: only one expression needs to be true
| INPUTS | True | False |
|---|---|---|
| True | TRUE | TRUE |
| False | TRUE | FALSE |
This leads to the following outcomes (for instance)
if ( ( 1 == 1 ) and ( 2 == 1 ) ) : # will be false as 2 does not equal 1
if ( ( 1 == 1 ) or ( 2 == 1 ) ) : # will be true as 1 does equal 1
Once you have read this information then proceed to try the exercises "4.0 Conditions", "4.1 Debug conditions" and if you're feeling brave "4.2 Conditions (Harder)"