How toTech

How to apply Python if statements correctly?

Apply Python if statements: It is very certain that our lives drive us through by the decisions we make. This is precisely similar to the programming languages too. We execute statements in a programming language basing on different conditions. These conditional statements are the ones which can change the direction of our entire program. These statements pave the way for executing the next block of statements. In this article we will get know about how to apply these conditional statements (if statements) correctly in Python.

Python supports various conditional statements. These statements are so flexible and easy to use. All of this is supported through the Python libraries. Python has been very compatible for the daily programmers. This is because of the short and direct coding statements supported by the Python libraries. Python has been so extensively used that it is a basic language to work on for the beginners. All of the sensible work on Python now makes it difficult for the programmers to delve into other languages.

Python if Statements

As mentioned above, Python supports various conditional statements. These statements decide the way for the entire program to go through. The conditional statements that Python language supports are,
a) if
b) if else
c) Nested if
d) if-elif ladder

We will go through each one of the above statements in detail below.

if Statement

As the name suggests, the Python executes certain set of statements only ‘if’ the set of statements are true. This is the simplest set of statements to be executed in Python. It is the best decision making statement because it just gives an out and out result based on only one clause i.e, true/false.

The syntax of the if statement is as follows,

if condition: 
# Statement to execute if
# condition is true

The block under an if statement is identified as follows,

if condition: 
statement 1
statement 2

# Here if the condition is true, if block
# will consider only statement 1 to be inside
# its block

The flow chart for the if statement is depicted below.

If Flowchart

Here is a basic example on how the if statement can be executed in Python.

#python program to illustrate If statement

i = 8
if (i > 15):
print ("10 is less than 15")
print ("I am Not in if")

The output of the above set of statements is as follows,

Output:
I am Not in if

The statement below the if statement is not executed because the condition in the if statement is false. There by the output comes out as above.
You can also use the looping statements in Python. To know how to use and learn more about looping statements in Python click on the link below.

How to use loops in Python

if else Statement

The if statement only executes a set of statements if the given conditions are true. Now, if we want to pave a different path for the false statements we can use the else statement with the if statement. You can get into more detail in regard to the if else statement.

The syntax of the if else statement is as follows,

if (condition):   
# Executes this block if
# condition is true

else:
# Executes this block if
# condition is false

The flow chart for the if else statement is depicted below,

if else Flowchart

Here is a basic example on how the if else statement can be executed in Python,

#python program to illustrate If else statement
#!/usr/bin/python


i = 20;
if (i < 15):
print ("i is smaller than 15")
print ("i'm in if Block")
else:
print ("i is greater than 15")
print ("i'm in else Block")
print ("i'm not in if and not in else Block")

The output of the above set of statements is as follows,

Output:
i is greater than 15
i’m in else Block
i’m not in if and not in else Block

The block of code following the else statement is executed as the condition present in the if statement is false after call the statement which is not in block.
If you have any doubts in your mind in regard to how to print in Python you can visit the link illustrated below.

How to print in Python with examples

Nested if Statement

A nested if is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.

The syntax of the Nested if statement is as below,

if (condition1):    
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here

The flow chart of the Nested if statements is as follows,

Nested If flowchart

Here is an example on how you can deal with the nested if in Python,

#python program to illustrate nested If statement
#!/usr/bin/python


i = 10
if (i == 10):
# First if statement
if (i < 15):
print ("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print ("i is smaller than 12 too")
else:
print ("i is greater than 15")

The output of the above code is as follows,

Output:
i is smaller than 15
i is smaller than 12 too

if-elif else ladder

Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

The syntax of the if-elif else ladder is as follows, 

if (condition):     
statement
elif (condition):
statement
.
.
else:
statement

The flowchart for the if-elif else ladder is depicted below,

if-elif else ladder flowchart

Here is an example to illustrate the if-elif else ladder statements.

#Python program to illustrate if-elif-else ladder
#!/usr/bin/python


i = 20
if (i == 10):
print ("i is 10")
elif (i == 15):
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")

The output of the above set of statements is as below,

Output:
i is 20

FAQ’s

Here are a set of few FAQ’s which can clear up your mind in regard to the if statements in Python. Scroll down below to know more

How do you exit an if statement in Python?

In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You’ll put the break statement within the block of code under your loop statement, usually after a conditional if statement. In this small program, the variable number is initialized at 0.

Does an if statement need an else in Python?

If you require code to run only when the statement returns true (and do nothing else if false) then an else statement is not mandatory. Yes, It is possible. You can’t write else without an if condition.

How do you break out of an if statement in Python?

Break Statement. In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You’ll put the break statement within the block of code under your loop statement, usually after a conditional if statement.

To conclude: Hence through this article, we have learnt about how we can apply Python if statements correctly. Comment your views below in the comment section and do share the content. For some more of interesting content in regard to latest tech articles and news, visit Morphigo.com and delve into some amazing stuff. Cheers!

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close