Conditionals

You have already encountered conditional (or comparison) operators in this course before (not least during our session on while loops), performed simple arithmetic comparisons and probably came across them in other programming languages as well – so what comes next should be 'old hat'.

Briefly, conditional statements in the form of simple if statements tell Python to perform a certain action depending on whether the stated condition is True or False (in this context, please note that these two so-called Boolean values are always written without quotes!). The syntax of a simple if statement is

if condition :
   indentedStatement

which translates into "If the stated condition evaluates to True, execute the indented statement." Or, to express this in a more visual form:


Source: https://www.tutorialspoint.com

Just like loop constructs in Python, indentation is mandatory for any kind of conditional statement. Accordingly, in its most basic form, a simple if statement would look as follows:


In [1]:
m = 5

if m > 0:
    print("Larger than zero.")


Larger than zero.

In this example, it is True that our variable m is larger than zero, and therefore, the print call ('if code' in the above figure) is executed. Now, what if the condition were not True? Well...


In [2]:
n = -5

if n > 0:
    print("Larger than zero.")

In such a case, the condition evaluates to False and the print call included in the indented statement is simply skipped. However, showing (or rather creating) no output at all is not always desirable, and in the majority of use cases, there will definitely be a pool of two or more possibilities that need to be taken into consideration.

In order to overcome such limitations of simple if statements, we could insert an else clause followed by a code body that should be executed once the initial condition evaluates to False ('else code' in the above figure) in order for our code to behave more informative.


In [3]:
if n > 0:
    print("Larger than zero.")
else:
    print("Smaller than or equal to zero.")


Smaller than or equal to zero.

At this point, be aware that the lines including if and else are not indented, whereas the related code bodies are. Due to the black-and-white nature of such an if-else statement, exactly one out of two possible blocks is executed. Starting from the top,

  • the if statement is evaluated and returns False (because n is not larger than zero),
  • hence the indented 'if code' underneath is skipped.
  • Our code now jumps directly to the next statement that is not indented
  • and evaluates the else statement included therein.
  • Since else means: "Perform the following operation if all previous conditional statements (plural!) failed", which evaluates to True in our case,
  • the subsequent print operation is executed.

Now, what if we wanted to know if a value is larger than, smaller than, or equal to zero, i.e. add another layer of information to our initial condition "Is a value larger than zero or not?". In order to solve this, elif (short for 'else if' in other languages) is the right way to go as it lets you insert an arbitrary number of additional conditions between if and else that go beyond the rather basic capabilities of else.


In [4]:
if n > 0:
    print("Larger than zero.")
elif n < 0:
    print("Smaller than zero.")
else:
    print("Exactly zero.")


Smaller than zero.

And similarly,


In [5]:
p = 0

if p > 0:
    print("Larger than zero.")
elif p < 0:
    print("Smaller than zero.")
else:
    print("Exactly zero.")


Exactly zero.

Of course, indented blocks can have more than one statement, i.e. consist of multiple indented lines of code. In addition, they can embrace, or be embraced by, for or while loops. For example, if we wanted to count all the non-negative entries in a list, the following code snippet would be a proper solution that relies on both of the aforementioned features.


In [1]:
x = [0, 3, -6, -2, 7, 1, -4]

## set a counter
n = 0

for i in range(len(x)):
    # if a non-negative integer is found, increment the counter by 1
    if x[i] >= 0:
        print("The value at position", i, "is larger than or equal to zero.")
        n += 1
    # else do not increment the counter    
    else:
        print("The value at position", i, "is smaller than zero.")
        
    if i == (len(x)-1):
        print("\n")
        
print(n, "out of", len(x), "elements are larger than or equal to zero.")


The value at position 0 is larger than or equal to zero.
The value at position 1 is larger than or equal to zero.
The value at position 2 is smaller than zero.
The value at position 3 is smaller than zero.
The value at position 4 is larger than or equal to zero.
The value at position 5 is larger than or equal to zero.
The value at position 6 is smaller than zero.


4 out of 7 elements are larger than or equal to zero.

Brief digression: continue and break

There are (at least) two key words that allow for an even finer control of what happens inside a for loop, viz.

  • continue and
  • break.

As the name implies, continue moves directly on to the next iteration of a loop without executing the remaining code body.


In [6]:
for i in range(5):
    if i in [1, 3]:
        continue
    print(i)


0
2
4

break, on the other hand, breaks out of the innermost loop. Here, (i) the remaining code body following the break statement in the current iteration, but also (ii) any outstanding iterations are not executed anymore.


In [7]:
for i in range(5):
    if i == 2:
        break
    print(i)


0
1

Bear in mind that break jumps out of the innermost loop only. This still means that other for (or while) loops embracing the one broken out from will continue running until the last item has been processed (or the condition is no longer True). See also the official Python Docs for a full list of control flow tools.


Alright, now that you know your way around with conditional if-elif-else constructs, its time to move on to some more sophisticated use-case scenarios. Therefore, head over to W02-2: Conditionals and tackle the tasks waiting for you there.