top of page

Control Flow Execution In Python

Writer's picture: Srujan kachhwahaSrujan kachhwaha

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 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 In Python


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

Python supports these conditional statements:

  • if

  • if-else

  • if-elif-else

Look at the below code snippet:

>>>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 and indentations 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)

statement-1, statement-2, statement-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.


>>>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 statement 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 control flow is used when we want a certain code block to repeat its execution until a specified condition is True.


Types of Loop Statement In Python:

  • for loop

  • while loop


for loop in python


>>> for i in ["Nike","Addidas","Puma","Rebook"]:
...    print(i)

Observe the above code, the for loop will iterate to all the values in the list, assign each value to the variable i, and then print the statement within the block will print those.

output:

Nike
Addidas
Puma
Rebook

While loop in python


>>> l = ["Nike", "Addidas", "Puma", "Rebook"]
>>> i = 0
>>> while i < len(l):
...    print(l[i])
...    i+=1

The above while loop will check the value of i every time, it will execute the statements within its block until the value of i is less than the length of list l.


22 views0 comments

Recent Posts

See All

Nodemon in Node.js

Nodemon is a very powerful and useful tool. Every time we make changes in our node.js program, we need to save it and then run it again...

Comentarios


Graphic Cubes

Subscribe To Get Latest Updates

Subscribe to our newsletter • Don’t miss out!

Thanks for subscribing!

Blogger
bottom of page