Table of Contents [show]
In this blog, we will
discuss the Python If-Else statement.
Like other popular
programming languages, Python has some control flow commands. Control flow is
directed to the hierarchy in which the program should be executed. In general,
the control flow of a program runs from top to bottom.
However, control flow
instructions violate the general top-down order of execution by involving
decisions, looping, and more. This allows the program to first run a specific
block of code based on the conditions used.
Conditional Python statements
Decision-making in
programming, very similar to real life, is very important because it helps us
decide what the program should do next. Decision-making helps in determining
the flow of program execution. Decision-making is implemented using if-else in
Python. Conditional reasoning in Python is mainly based on the "if
else" structure.
Starting with the if
statement, it is the most basic statement for making decisions. It simply
decides whether a particular block of code will be executed or not based on the
condition provided in the if statement. If the condition established in the if
statement is true, the code block is executed, and if it is false, the code
block is not executed.
Our Learners Also Read: Top 25 Python Interview Questions and Answers in 2022
Syntax of the if statement in Python:
```if test
expression:
statement(s)```
The Python program
first considers the test expression. It is a condition in an if statement in
Python. If the condition is satisfied or if the condition is true, only the
statements in the body of the if statement is executed.
Note
The body of an if statement in Python begins after an indentation, unlike other
languages that use parentheses to write the body of if statements.
Let's look at an
example implementation of the if statement.
```a = 5
if (a <10):
print (5 is less
than 10)
print (Statement
after if statement)
Output:
5 is less than 10
Statement after if
statement```
If else in Python
Suppose a statement
in Python tells a program what to do if a condition is true. If the condition
is false, the program resumes to execute the statements that follow the if. If
we want the program to run some statement if the condition is true and some
other state only if the condition is false, then we use if else in Python.
If-else Statement
The if-else
statement executes both the true and false elements of a provided condition. If
the situation is true, the if block code is run, and if the situation is false,
the else block code is run.
Syntax:
```if(condition):
#Runs this block if
the condition is true
else:
#Runs this block if
the condition is false```
You must note here
that Python utilizes indentation in both blocks to define the scope of the
code. Other programming languages often use braces for this purpose.
First, the test
expression is checked. If true, the statements listed in the body of the if
block is executed. Next, the if block is displayed below the commands. If the
test expression estimates to false, the statements in the else body are
completed, and the statements below the if-else are performed.
The following is an
example that better demonstrates how if-else works:
```i = 20;
if (i < 15):
print (i is smaller
than 15)
else:
print (i is greater
than 15)
print (statement
after if statement)
Output:
i is greater than 15
statement after if
statement```
Nested IF Statement
If a statement is
present in another if statement, it is called a nested IF statement. This
situation occurs when you need to filter variables numerous times.
Syntax:
```if (condition1):
#Runs if condition1 is true
if (condition2):
#Runs if condition2 is true
#Condition2 terminates here
#Condition1
terminates here```
In nested IF
statements, you must always look after the indentation to define the scope of
each statement. You can have multiple levels of nesting as needed, but by using
this the program may be less optimized and more challenging to read and
comprehend. Therefore, you must always try to underrate the use of nested IF
statements.
Let's look at the
following if example in Python:
```a = 20
if (a == 20):
# First, if statement
if (a < 25):
print (a is smaller
than 25)
else:
print (a is greater
than 25)
else:
print (a is not
equal to 20)
Output:
a is smaller than
25```
Here let's discuss
the above nested if example in Python. Since a is 20, the program enters the
first if statement. It inspects the nested if statements, executes them as
true, and prints to the screen that a is less than 25.
Now the nested
statements are not executed, and the program jumps to the statement after the
end of the if block. This is how a nested if condition works in Python.
If-Elif-Else Statement
Checks the
condition of an if statement. If false, the elif statement is evaluated. If the
elif condition is false, the else statement is evaluated.
Syntax:
```if (condition):
statement
elif (condition):
statement
.
.
else:
Statement```
For better
understanding, let's look at the following example of if elif else statement.
```a = 50
if (a == 20):
print (variable a is
20)
elif (a == 30):
print (variable a is
30)
elif (a == 40):
print (variable a is
40)
else:
print (variable a is
greater than 40)
Output:
variable a is greater
than 40```
The program checks
the first if statement in the above example. Because it shifts out to be false,
the block of the if statement is not executed, and the program proceeds to the
following elif statement. It also turns out to be false, and the body of the
elif block is again skipped, and the program proceeds to the following elif
statement. The exact is happening here also. Since all the situations were
false, the program eventually reaches the last statement and runs the else
body. So the output as "the value of variable a is greater than 40".
Lambda if else in Python
Whenever you use an
if statement in a lambda function, a value is returned established on the
conditional reasoning contained in the if-else statement.
```x = lambda n: n**2
if n%2 == 0 else n**3
print(x(4))
print(x(3))
The output will be
16
27```
Conclusion
In this blog, we have
seen if,if-else, if-elif-else, nested IF statements, and practiced some
valuable examples.