×

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look into different control statement structures like Sequential, Repetition, and Selection with examples of each structure written in Python programming language along with self-explanatory comments in the code that we will be writing.

Control Flow Statements

Programming Languages Control flow is the order in which the Program or the code we have written executes in a particular order. The Control in Programming languages differs in many ways; for instance, here in our Python Programming Language, the Control Flow is regulated by Function calls that the code does, loops like for loop, while loop, do-while loop, nested loops and also by Conditional statements.

Importance of Control Statements

The Control Flow Statements will let you give the power to have control over the flow of execution of the code that we write. In Python Programming Language or any programming language in general, the flow of an implementation of the code can be controlled by placing the looping, branching, decision making and adding some additional blocks to the code we write. It is essential to have the control flow statements because the code we write in a particular file, the flow of execution generally flows top to bottom. Still, with the help of control flow statements, we can now control certain decisions, perform tasks that require frequent repetition or jump from a specific part of a different section if needed.

The three most important types of Control Structures that we have in Python Programming Language are

  1. Sequential – it is the default mode.
  2. Repetition is generally used for looping, which means repeating a particular condition multiple times by the same piece of code.
  3. Selection – Its primary purpose is branching out code and making decisions.

Sequential Control Flow Statements

The Sequential Control Flow Statements are the by-default flow of execution statements. The flow of execution generally happens from top to bottom, and the problem with them is if the logic that we have written in the code breaks, then the complete source code file execution might break, which may cause a throwing error.

Code

## This is a Sequential statement example
 
a=20233
b=1033
c=a-b
print("Subtraction is of a-b is : ",c)

Output

Subtraction is of a-b is:  19200

Selection/Decision Control Flow Statements

In Python Programming Language, the Selection statements are also known as the branching or decision-making statements allowing us to make the code that we write go through the pass a variety of checks in between and will only execute if the condition is true

Selection Control Statements are:

If-else

Code

variety of checks in between Code
nn = 500
if nn % 2 == 0:
   print("nn is even")
else:
   print("nn is odd")

Output

nn is even

Simple if

Code

nn = 100
if nn % 2 == 0:
   print("this is an even number")

Output

this is an even number

If-elif-else

Code

x = 1500
y = 122212
if x == y:
   print("Both are Equal")
elif x > y:
    print("x is greater than y")
else:
    print("x is smaller than y")

Output

x is smaller than y

Nested if

Code

x = 1500
y = 122212
if x == y:
   print("Both are Equal")
elif x > y:
    print("x is greater than y")
else:
    print("x is smaller than y")

Output

c is bigger here

Repetition Control Flow Statements

As we have already discussed in the introduction, the Repetition Control Flow Statements are used to repeat a specific code multiple times or stop writing a particular code more times; we prefer the repetition statement where the loop passes through the statement and does the work for us.

Types of repetitive statements

For loop

Code

list_example = [1, 2, 3, 4, 5]
for i in range(len(list_example)):
     print(list_example[i], end = " ")
 
for j in range(0,10):
    print(j, end = " ")

Output

1 2 3 4 5 0 1 2 3 4 5 6 7 8 9

While loop

Code

m = 105
i = 100
while i < m:
     print(i, end = " ")
     i = i + 1
print("Ending now")

Output

100 101 102 103 104 Ending now

Related Topics

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Python Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

Map Syntax in Python

Introduction: In Python, a function called map acts as an iterator, returning a result after each item in an iterable has been subjected to a function (tuple, lists, etc.). When you...

6 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 minutes read.

Python Set discard() method

Python Set discard() method The set.discard() method in Python removes a specified element from the set (if present). Syntax set.discard(value) Parameter value- This parameter represents the element to be removed from the set. Return None Example 1 # Python program explaining # the...

1 minute read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

How to Update Python?

How to Update Python In this article, we will discuss how we can update Python in our system. For a better understanding, this article will cover all the steps right from installation...

4 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

Python sum() function

Python sum() function The sum() function in python sums start and the items of an iterable from left to right and returns the total.  Syntax sum(iterable[, start]) Parameter iterable: This parameter represents the sequence to sum. start: It is optional...

1 minute read.