Quick Reach
Decision-making in Python
Where there are more than one options and you have to choose one, the decision has to be taken based on certain criteria. In programming languages, one of the ways to choose an option or making a decision is by using the if and if…else statement.
This chapter will explain how to use the Python if and if else statements with examples.
Types of Python if statements
Python provides following types of if statements with details of each.
The If statement
The if Python statement is used when you have just one condition to check. If the condition evaluates as true it will execute the given statements.
Syntax of If statement
if condition:
statement(s) to be executed
Where the condition contains a logical expression. If the condition is true then statements inside the if block will be executed. If it turns to be false then the parser will go to next statements outside of the if block.
A few things to be noted in Python if statement:
- The Boolean expression is used to evaluate conditions.
- Unlike many other languages, curly braces are not used for multiple statements in the If block. Instead, Python uses indentation. The default indentation is four spaces, however, this can be changed.
Example of using If statement in Python
In the following example if a variable evaluates as true it will execute two statements. See the example by clicking the link below:
1
2
3
4
5
6
7
|
num1 = 1
if num1:
print (“This is IF test example”)
print (num1)
|
The Output
This is IF test example
1
Example with one true and other false in IF statement
The following example will execute two If statements. Only the statement inside the if block, which is true will be executed. As such, in our example the first If statement is true while other is false, only the code in the first If statement will execute.
After the second if block, another statement is placed (just for illustration purpose). By the indentation you can see, it is outside of the if block, that will execute whether the condition is true or false.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
num1 = 1
if num1:
print (“This evaluates to be true”)
print (num1)
num2 = 0
if num2:
print (“This is false”)
print (num2)
|
The output will be
This evaluates to be true
1
0
Python If else statement
The if else statement is used to execute the statement(s) when a condition in the if block is false. In the above example, we just used the if statement. So if a condition is false, no statements within the if block will be executed.
The code inside the else part of the if statement will be executed if the given condition is false. The example below will give you the even better idea.
Syntax of if else in Python
Following is the syntax of Python if else statement:
if condition:
statement(s) to be executed
else:
statement(s) to be executed
Python else if example
The following example shows how to use the Python else if statement. The else block will execute the given statements as the condition is false.
1
2
3
4
5
6
7
8
9
10
11
|
num1 = 0
if num1:
print (“This evaluates to be true”)
print (num1)
else:
print (“The if condition gets false”)
|
The output will be
The if condition gets false
elif statement in Python
The elif statement of Python is explained in its own chapter. Click the link below to learn about it.
Leave A Comment?