Python language basics 10: conditional statements

Introduction

In the previous post we looked at relational operators in Python. We saw how to compare two integers using operators like ‘==’ or ‘<='. In this post we will look at a very important building block of any popular – and probably any not-so-popular – programming language: the if-statement.

The word ‘if’ is also an important element of the English language – and its translated version of any other spoken language. You can create one or more condition for your actions like in the following examples:

If the weather is nice then we will play football else we will go to the movie.
If everything goes well then I will save the world tonight else the world will crumble.

It works exactly the same way in programming. The segment between "if" and "else" is a boolean statement: “the weather is nice”, that is either true or false. In everyday life the result can be indecisive, like “it is cloudy but it is not raining”, but in programming the boolean statement will have a definite result. The weather is either good or bad depending on how you’ve defined “good” and “bad” in your program logic.

The if statement in Python

Not surprisingly the keyword ‘if’ is reserved for conditional statements. It’s important to recall the role of whitespace in Python as the indented rows below the if statement will be executed if the boolean condition is true. Check the syntax, the if statement must be closed with a colon ‘:’ :

val = 5
if val > 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')

All three statements will be printed on the standard output window.

Change the boolean condition…

if val < 4:

…and not surprisingly the if-block will be ignored.

OK, what about the ‘else’ section of our example sentences? Easy:

val = 5
if val < 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')
else:
    print('The value is smaller than 4')
    print('Bye')

Guess which block is executed? The else block of course with two printouts. The ‘else’ statement is a catch-all statement which is executed if the boolean expression is false. However, we can refine our conditions further. Consider the following sentence:

If the sun is shining then we’ll play football, else if the rain is falling then we’ll stay inside, else if it starts snowing then we’ll start a snowball fight, else we’ll go shopping.

Note that only one of these statements will be carried out depending on the weather conditions. In reality it’s conceivable that the sun is shining and it’s raining at the same time so that the action we carry out might be inconclusive. That’s not that case in programming. The first expression that results in “true” wins regardless of any other outcomes. If all statements give false then the else statement wins. The “else if” bit is expressed by the “elif” keyword in Python:

val = 1
if val < 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')
elif val < 3:
    print('Carrying our the first elif block')
elif val < 2:
    print('Executing the second elif block')
else:
    print('The value is smaller than 4')
    print('Bye')

Can you guess which block is executed? The first one as the first boolean condition results in true. The other branches are ignored even if they are also true.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: