Xtralien Scientific Python: Conditionals


Conditions are how decisions are made. In Python we can use conditions to change what our program does. In programming this is usually represented by a boolean, i.e. a True or a False value.


Booleans are a type of value that is either truthy or falsey. This goes along with the two states of truth that are used in the real world.

In Python these are represented with the True and False values. Most non-boolean types can also be seen as directly boolean.

For lists, tuples and strings they are seen to be falsey when their length is \(0\) and truthy otherwise.

For numbers, \(0\) is considered falsey and any other number is truthy.

Boolean operators

To coerce a boolean value from non-boolean values, such as strings or numbers you normally perform a test that you want to check for. This may be testing wether a number is in a certain range.

x = 5
y = 1 < x < 10

In the example above y now contains the Boolean result of the test (True).

The Boolean operators that can be used are as follows.

Operator Name Example
< Less than a < 5
<= Less than or equal to a <= 5
>= Greater than or equal to a >= 1
> Greater than a > 1
== Equal to a == 1
!= Not Equal to a != 2
!/not Not (Inverse of Boolean representation) !a

In the case of the ! operator, this get the Boolean value of the variable and then return the opposie of that.

It is also possible to combine the reults of comparisons into a single result using and and or.

Left Operator Right Result
True and True True
True and False False
False and True False
False and False False
True or True True
True or False True
False or True True
False or False False


The simplest conditional in Python is the if structure. At it’s simplest this will create a boolean from the expression given and check if the value is either True or False.

The value of this expression is evaluated and used to decide wether the code inside the if statement is run.

value = True

if value:
    # Code to run if value is truthy
    print("Value evaluated to True")

The above example will print Value evaluated to True to the console because value is currently set to True. If value was set to a falsey value then none of the above code would be run.


Else is an extension to if, and can be used as a default. This is analagous to if 1 do 2, otherwise do 3. By using this structure you can provide defaults, or some alternative code to run that should only run if the condition (value) is falsey.

value = False

if value:
    # Never called because of False
    print("Value is truthy")
    # Called because value is False
    print("Value is falsey")

The example above shows that when the value provided to the if statement is falsey then the else will run.


Python provides a third keyword that is a mixture of both if and else. You use the elif keyword when you want to check for another condition. You might do this to check if an element is in a certain range.

An example of this would be checking if any roots exist in a quadratic equation.

The part of the equation we need to check this is as follows.

n = b2 - 4ac
a = 2
b = 4
c = 1

n = (b ** 2) - (4 * a * c)

if n > 0:
    print("There are 2 real roots")
elif n == 0:
    print("There is a single real root")
    print("There are no real roots")

The example above shows a simple program that can determine the number of real roots in a quadtratic. The elif keyword is used to check another condition. This will only be tested if the first case evaluates as falsey. If this case also evaluates as falsey then the final case (the else will run). This is similar to other programming languages, however in Python there is no concept of a switch/case so using if, elif and else are the only methods of making decisions.

Assigning values using or

Sometimes you will want to set a vaariable to a certain value on a condition. You can either do this by using a standard if/elif/else like below.

if some_value:
    value = 1
    value = 2

However this is quite a large structure. Luckily, in Python it is possible to compress this to a single line like below.

value = 1 if some_value else 2

This means that you can do short optional assignments without having to write more code than is needed. Additionally, it is arguably clearer to read the second version than the first.

Taking this further if you wanted to assign a value contingent on it being truthy, then you could write the following.

value = some_value if some_value else 1

The above example would check that some_value is truthy and if so then value would be assigned the value that is currently held in some_value. If not then the value 1 would be stored in value.

Due to the common appearance of this pattern there is a shorter way to do this in Python.

value = some_value or 1

The example above is essentially the same as the previous example, checking some_value to ensure it is truthy and then setting the value as appropriate. The additional benefit is that this can be easily extended to provide cascading checks.

value = some_value or some_other_value or 1

The above example will check each value to ensure it is truthy and move onto the next value until a truthy value is found, or it reaches the end of the assignment. Then the appropriate value is set.