• Srujan kachhwaha

Python Control Flow, Know The Ways You Can Manage Execution Flow.

To master any programming language, we should have a clear understanding of how every statement is executed. In programming, designing a proper flow of implemented logic is an art.


Every coder is an author, And like any other good author, A programmer's code should be smooth, easy to understand, and easy to go. In this post, I will explain what are different control flow ways by which you can author your code like a bestseller!



There are three ways by which you can manage the flow of your program in python.

  • Conditional Statements

  • Loops

  • control statements keywords (continue, break, pass)

Conditional statements


Conditional statements in python are the statements that got executed when the given condition satisfies.

Python supports these conditional statements:

  • if

  • if-else

  • if-elif-else

Look at the below example to understand:


>>>brands = ['Nike','Puma','Adiddas','Rebook']
>>> if 'Nike' in brands:
...    print('We have Nike in our store!')
... print('This Statement is out of if Block.')
We have Nike in our store!
This statement is out of if block

To understand the above example, you need to understand the concept of blocks in python. The blocks in python help to differentiate between normal control flow and conditional control flow.




if condition == True:
    stmt-1
    stmt-2
    stmt-3
print(normal flow-1)
print(normal flow-2)

stmt-1, stmt-2, stmt-3 are within if block. You observe the indentation, indentations are used to define a block in python.


Every statement within the block must follow the same indentation. In the above example, I gave an indentation of one tab space.


Let's come back to our example,

>>>brands = ['Nike','Puma','Adiddas','Rebook']
>>> if 'Nike' in brands:
...        print('We have Nike in our store!')
... print('This Statement is out of if Block.')
We have Nike in our store!
This statement is out of if block

Now, here you must have understood that the if block statements execute only if the 'Nike' in brands returns True.


Example - 2

>>>brands = ['Nike','Puma','Adiddas','Rebook']
>>> if 'Jordan' in brands:
...       print('We have Nike in our store!')
... print('This Statement is out of if Block.')
This statement is out of if block


if-else block in Python


The if-else statement in python is like saying do this if possible, otherwise do this.

Let's look at the example


brands = ['nike','puma','Yezzey']

if 'Adidas' in brands:
    print("We have Adidas In Our Store")
else:
    print("We Dont Have Nike In Our Store")

The output of the above code section will be

"We Dont Have Nike In Our Store

Loops In Python


Loops help us when we need to repeat the execution of a statement. Let's say your strict teacher gives you a punishment to write "I Will Do My Homework" 50 times, Then looping statement help you to write that in one line and let computers do the 50 times repetition for you.



There are various ways in which loops can be implemented in python. We will discuss them in the next post. See you people in the next reading!

24 views0 comments