Skip to main content

Command Palette

Search for a command to run...

Control flow in python

Published
4 min readView as Markdown

Control Flow

In the programs we have seen that there is always a series of statements executed by Python in top-down order. What if you wanted to change the flow of how it works? For example, we want the program to take some decisions and do different things depending on different situations, such as printing ‘Good Morning’ or ‘Good Evening’ depending on the time of the day?

This can be achieved using control flow statements. There are three control flow statements in Python -

  1. if
  2. for
  3. while

The if statement

The if statement is used to check a condition: if the condition is true, we run a block of statements (called the if-block), else we process another block of statements (called the else-block). The else clause is optional.

number = 23
guess = int(input('Enter an integer : '))

if guess == number:
    # New block starts here
    print('Congratulations, you guessed it.')
    # New block ends here
elif guess < number:
    # Another block
    print('No, it is a little higher than that')
    # You can do whatever you want in a block ...
else:
    print('No, it is a little lower than that')
    # you must have guessed > number to reach here

print('Done')
# This last statement is always executed,
# after the if statement is executed.

Output

Enter an integer : 50
No, it is a little lower than that
Done

Enter an integer : 22
No, it is a little higher than that
Done

Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done

How It Works

In this program, we take guesses from the user and check if it is the number that we have. We set the variable number to any integer we want, say 23. Then, we take the input from the user using the input() function. Functions are just reusable pieces of codes. We’ll read more about them in the next lesson.

We supply a string to the built-in input function which prints it to the screen and waits for input from the user. Once we enter something and press [enter] key, the input() function returns what the user entered, as a string. We then convert this string to an integer using int and then store it in the variable guess.You can use int to convert a string to an integer (assuming the string contains a valid integer in the text).

Next, we compare the guess of the user with the number we have chosen. If they are equal, we print a success message. Notice that we use indentation levels to tell Python which statements belong to which block. This is why indentation is so important in Python. I hope you are sticking to the “consistent indentation” rule.

Notice how the if statement contains a colon at the end - we are indicating to Python that a block of statements follows.

Then, we check if the guess is less than the number,we inform the user that they must guess a little higher . Here we used a elif statement which is used when we have multiple conditional block.

The elif and else statements must also have a colon at the end of the logical line followed by their corresponding block of statements (with proper indentation)

You can have another if statement inside the if-block of an if statement and so on - this is called a nested if statement.

Remember that the elif and else parts are optional. A minimal valid if statement is:

if True:
    print('Yes, it is true')

After Python has finished executing the complete if statement along with the associated elif and else clauses, it moves on to the next statement in the block . In this case, it is the main block of the program, and the next statement is the print(‘Done’) statement. After this, Python sees the ends of the program and simply finishes up.

In the next article we talk about the while and for control statement

20 views

More from this blog

Untitled Publication

17 posts