Understanding Control Flows in Python

DS - VRP
4 min readJul 6, 2021

Decision Making is one of the most important skills in any industry or field. In Python, we can make decisions with help of the if statements. if statement works on different conditions.

List of comparison operators in python.

The following are the examples :

1st example of if statement.

In the first example, we can see that there are two variables a & b. Here we are comparing the variables with the greater than assignment. In the control flows it is necessary to use indentation. Indentation is the space before the next line starts. In the above example space before the print statement is the indentation, which usually follows the semicolon(:) in the previous line.

Elif keyword in python shows that “If the previous condition is not true then try this condition”

Example with the elif condition.

Else Keyword will execute if previous conditions are not true.

An example with Else keyword.

Shorthand if-else statement can be used if there only one statement to execute.

Example for the Shorthand if else.

Loops are useful when we have to repeat the execution until a specific condition met. it allows us to repeat the process over and over again. There are mainly two types of loops. They are while loop and For loop.

  1. While loop is a set of statements as long as the condition is true. It is an indefinite loop.
An example of a while loop.

If we won’t increment the ‘i’ then the loop will continue forever.

Break Statement in the loop helps us to exist the loop at a specific iteration.

Continue Statement in the loop, it stops at the current iteration and continues with the next iteration

Pass Statement just acts as a placeholder in the iteration

Nested While Loop

Example of Nested While Loop

In the Nested while loop, the first loop will print numbers from 1 to 10. The second, while loop will print another set of numbers which get printed with each first loop number and print(i*j), will print the multiplication of both the set of numbers, i+=1 will help us to increase the count up to 10 for the first loop. print(“/n”) will create a new line.

2. For Loop

For Loop is a definite loop.

samples of for loop.

Tables using For loop

we are first initializing n and creating a table by using the For loop and print function. We can create a table of any number by changing n.

Break in for loop

Continue in the For loop

Else in the For loop

Nested For loop

The first loop print the columns and the second loop will print the rows, print(x,y) function will print the adjective and the side of the fruit by side.

This is my understanding of the Control flows.

Thank you for reading

--

--

DS - VRP

A budding data scientist who is learning and evolving everyday