Python Control Statements


If in Python

 

The order in which a program executes is known as program's control flow. In Python programming language, the control flow python is regulated by conditional python statements, If statement python, loops and functions calling. In this we will learn about all the Python statements which are used in Python Programming language.

 

Types of Python Statements

There are a number of Python Statements:-

  1. Return Statement

  2. Break Statement

  3. Continue Statement

  4. Pass Statement

  5. If Statement

  6. If-Else Statement

  7. Elif Statement

So it is the time to study all the above mentioned Python Statements briefly with the help of a general example

 

Return Statement

The main function of Return Statement in Python is to end the execution of the function and "returns" the result. The statements which come in use after the return statement are not executed. Return Statement in Python is used when you want to execute the program. If the return statement is without any expression (means it does not have anything except return), then a special value i.e. NONE is returned.

Learning the use of Return Statement with the help of an example

 

Python return

 

Execute python Example

As you can see from the code explained above func1 gives the value of return statement and provides us the output

 

Break Statement

Break statements in python are used to terminate the current loop and resumes execution at the next statement. The main function of Break statement in python is used to bring the control out of the loop when under any external condition is triggered to the function or loop. One can use Break Statement in for loop and while loop.

It is the time to understand the use of Break Statement with the help of an example

 

Break in Python

 

From above code you can see that our code stopped working after the implementation of Break Statement when the value of the “val” function reaches “a”  it executes the break statement.

 

Continue Statement

In Python, we use Continue Statement to skip the code which comes after the continue keyword and there is a control in the statement which is passed back to the start for the next iteration. The major functionality of Continue Statement is to return the control at the beginning of the loop. Continue Statement can also be used in both for loop and while loop like Break Statement.

Let's understand the best example which will explain the use of Continue Statement

 

python continue

 

As you can see from the above code that number 7 is missing in the output as we had used continue statement at number 7. So this number has skipped from the for loop which results in printing all the numbers which are used in the loop.

 

Pass Statement

The Pass Statement is used as a placeholder for future in a python code. In simple words we use a pass statement when we want to run a given set of instructions empty, means without any arguments. Statements which are empty or which do not have any statement are not allowed in loops, function definitions, class definitions, or in if statements. For a bigger set of codes, Pass statement is the best statement which is used in a python program.

Let's understand the use of pass statement with the help of an example

 

Python Pass

 

 

From the above code it is clearly visible that when the pass statement is executed we get no value in the output.

 

 

If Statement

If statements is a type of control flow statements which is used to run a particular code or particular set of statements only when a certain condition is satisfied. This is the most simple decision making statement. For printing a specific message or a specific statement, then this point needs to be ensured that if the condition which is given is true then in that case we use If statement. If there are multiple conditions in a program then the best suggestion is to use nested If statements.

Let's understand more about If statement with the help of an example

 

If in python

 

 

As i had explained about If Statement that when any statement is true it will give us the output and this is the best example for If Statement in python.

 

 

If-Else Statement

The main purpose of if-else statement is to execute both the true part and the false part of a given condition in a python program. If the condition is true then the if block code is executed and if the given condition is false then the else block code will be executed.

Given below is the example of If-Else Statement

 

Python find greater Number

 

From the above code you can clearly see that one statement is correct and the other one is incorrect statement and the function of If-Else statement is to print the statement which are needed or which are true when the condition is satisfied.

 

Elif Statement

The Elif Statement is generally used as If-Elif-Else Statement. Elif stands for Else-If. The If-Elif-Else statement will contain multiple conditional expressions but only one boolean expression evaluates to true and executes the respective block of statements. The most important fact about the If-Elif-Else statement is that you can only use the If and Else statement only once whereas you can use Elif Statement multiple times in your python program.

So let's understand this If-Elif-Else Statement with a example

 

Python find greater number using if statement

 

From above code it is clearly visible there should be 3 possibilities in this case and only one possibility will be true and the other two will be false at a single time. Thus, this is the reason why we are getting the desired output.

More Tutorials