Statements in Python

In Python Programming language there are several Statements and each statement has a different role. Python Programming Language is not complete without statements.

Types of Python Statements

The type of Statements which we use in the Python Programming language are listed below:-

  1. Return Statement
  2. Break Statement
  3. Continue Statement
  4. If Statement
  5. If-else Statement
  6. Elif Statement

Let's discuss and study all these statements one by one.

Return Statement

A Return Statement in Python is used to end the execution of the function call and "returns" the result. The statements after the return statement are not executed. 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.

Let's understand the use of a Return Statement with the help of an example.

Use of Return Statement

In the above code, you can see that func1 gives the value of the return statement in the form of output.

Break Statement

The break statement terminates the current loop and resumes execution at the next statement. The break statement is used to bring the control out of the loop when some external condition is triggered. The Break statement can be used in both whiles and for loops.

It is time to understand the use of the Break statement with the help of an example:-

Use of Break Statement

In the above code, you can see that our code stopped working after the implementation of Break Statement.

Continue Statement

In Python, The Continue Statement skips the code that comes after it and the control is passed back to the start for the next iteration. Basically, in simpler words, it returns the control to the beginning of a loop. Just like Break Statement, continue Statement can be used in both for loop and while loop.

Here is the best Example for Continue Statement in Python:-

Use of Continue Statement

In the above code, you can see that 7 is missing from the output as we had used the continue statement and this results in skipping of 7 and printing all the next elements.

If Statement

If statements are control flow statements which helps us to run a particular code only when a certain condition is satisfied. This is the most simple decision-making statement. If you want to print a specific message if the condition which is given is true then in that case we use the If statement in python. If there are multiple conditions in a program then the best suggestion is to use nested If statements.

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

Use of If Statement

From the above code, you can clearly see that when our statement is true it gave us the output and this is the best example for If Statement in python.

If-Else Statement

The if-else statement is used 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

Use of If-Else Statement

From the above code, you can clearly see that one statement is correct and the other one is an incorrect statement and the function of the If-Else statement is to print the statement which is required to the user either it is true or false.

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 execute the respective block of statements. The most important fact about the Elif statement in If-Elif-Else 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 an example

Use of If-Elif-Else Statement

From the 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. So due to which we get the correct statement as our desired output.

Thanks for Reading. Hope you liked it. Many more to come up soon. Make sure to subscribe to our Newsletter.

NOTE:- The awesome artwork used in this article was created by Anton Fritsler (kit8)


Get the latest episodes directly in your inbox