# Python: Booleans and Conditionals
([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/HJ9zbYZnL) ... [next](https://hackmd.io/@alexhkurz/SJ1DcL43L))
So far we explored Python-as-a-calculator. Nevertheless, we have already seen some indication that Python is more powerful than a calculator. For example, we have seen that we can extend Python by importing libraries to make it more powerful. This is a hallmark of programming languages: They can always be extended in such a way that anything computable at all is indeed computable (given enough time and resources). This is not true for a non-programmable calculator.
We have also seen that Python has types, something we are not familiar with from calculators. We will see that there are many types, even user definable ones. In this session, we will look at truthvalues, also known as booleans. [^types]
>>> 1<2
True
We see that `1<2` evaluates to `True`.
**Activity:** Can you discover the meaning of the operations `<`, `<=`, `>=`, `==`, `!=` by asking questions to the Python REPL.
`True` and `False` are so-called truth-values. What is the type of a truth-value?
>>> type(True)
<class 'bool'>
The truth-values `True` and `False` are also called Booleans in honor of [George Boole](https://en.wikipedia.org/wiki/George_Boole) and we will see in a moment why.
First let us emphasise that to understand a data type means to understand the operations it supports. So far we have learnt that
- `True` and `False` are of type `bool`.
- If `x` and `y` are of type `int` then `x>y`, etc is of type `bool`.
But there is much more.
**Activity:** Using Python's `!=` and `and` as in `True and True` to complete the following two tables (`!=` means "not equals" or "exclusive or")
`!=` | `False` | `True` | _______________ | `and` | `False` | `True` |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
|`False`| | |_______________ |`False`| | | |
|`True`| | |_______________ |`True`| | | |
Compare these two tables to the ones in the session on the [field of two numbers](https://hackmd.io/@alexhkurz/HyzjN3qsI). Also make tables for other operations on booleans such as `or` and `not` and `==`.
**Remark:** Tables that show how to compute with truth-values are known as **truth tables**.
**Activity:** Compare the truth tables of `!=` and `and` with the tables of addition and multiplication for $\{even,odd\}$. What do you find?
**Remark:** Also compare your answer above with the tables of $+$ and $\cdot$ of the [field of two elements](https://hackmd.io/@alexhkurz/HyzjN3qsI).
## Conditionals
Once we have Booleans we can use them to determine the next action depending on testing other data. For example, run the following with different values for `n`. That is, copy-paste the code below, after replacing `n` by some value, in the Python REPL: [^mod]
if n % 2 == 0:
print(n,'is even')
elif n % 2 == 1:
print(n,'is odd')
else:
print(n, 'is not an integer')
[^types]: If you are asking now what the definition of a type is, congratulations. Great question. But for now all you will get is a list of examples. So far we have seen integers, floats, decimals and fractions.
## References
From the Python Language Reference:
- [Boolean Values](https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values)
- [Value comparisons](https://docs.python.org/3/reference/expressions.html#value-comparisons)
- [Boolean operations](https://docs.python.org/3/reference/expressions.html#index-81)
- [The `if` statement](https://docs.python.org/3/reference/compound_stmts.html#the-if-statement)
[^mod]: Recall from a previous session or the [Python Language Reference](https://docs.python.org/3/reference/expressions.html?highlight=arithmetic#binary-arithmetic-operations):
The % (modulo) operator yields the remainder from the division
of the first argument by the second.