×

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

Python Examples

In this tutorial, we will see some examples related to python. This will include some basic example questions and their code in Python. Write a program to print “Hello Python”. print(‘Hello Python’) Output Hello...

5 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.

Important Difference between Python 2.x and Python 3.x with Example

The comparison between Python 2 and Python 3 is given in the article that follows. Python is a computer language that can perform more tasks than other languages and is...

5 minutes read.

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm...

3 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Python String index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute 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.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Python Variables

Variables are one of the most important terms we should be familiar with if we want to be good programmers. In simpler words, we use variables to store a value....

4 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.