Conditional Statment in Python
In this article, we will explore the various types of conditional statements.
Conditional Statement in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false, and also it is a decision-making statement in programming languages that decide the direction of the flow of program execution.
In Python decision-making statements are :
- if statement.
- if-else statement.
- Elif statement.
Let’s discuss these in detail,
01. if statement :
If statements are control flow statements that help us to run a particular code only when a certain condition is satisfied. For example, if you want to print a message on the screen only when a condition is true then you can use an if statement to accomplish this in programming.
We can understand by Flowchart:
Let’s see a simple example of an if statement:
02. if-else :
Simply, The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed, and if the condition is false, the else block code is executed.
Syntax of if-else :
Let’s understand by Flow chart:
Examples of if-else :
03. Elif :
The Elif condition is used to include multiple conditional expressions after the if condition or between the if and else conditions. The Elif block is executed if the specified condition evaluates to True. In the above example, the Elif conditions are applied after the if condition.
Syntax of Elif’s statement :
let’s understand by Flow chart:
Example of Elif statement :
Another example, tell me even or odd game :
Another example, guessing the grade for your marks:
For the above code, you can give different marks in the input section and you will get different grades.
The above2 codes are examples of Elif’s statements.
You can use as many Elifs as you can, there is no limit.
So that’s it for this blog. Next, we will discuss more about Python in another blog.
If I made any spelling mistakes somewhere sorry, please ignore and try to search yourself by google it.
Thank you.
#vevcodelab