Python: If-Statement

In real life, there are times when we must make choices and decide what to do next based on those decisions. Similar scenarios occur in programming where we must make decisions and then execute the next block of code based on those decisions.

If Statement in Python

1)If statement

The if statement is the simplest decision-making statement. It is used to determine whether or not a specific statement or block of statements will be executed, i.e. if a certain condition is valid, then a block of statements is executed, otherwise not.

Syntax:

if condition:
    statement 1
    statement 2
    statement 3

We don’t have the definition of brackets in Python to indicate the beginning and end of a block. Instead, it represents a block of indentation. After the “if statement,” for example, there are two other sentences with one degree of indent. It denotes the “if” statement’s block field. In Python, this set of statements in a block is known as a suite.

The keyword “if” is often followed by a conditional expression that should evaluate to a bool value, i.e., True or False. If the condition is True, the interpreter executes the statements in the “if” suite, which are the code statements in the if-block.

Python, as we know, uses indentation to define a block. As a result, the block inside an if statement will be found, as seen in the example below.

if condition:
   statement1
statement2

#If the condition is valid, the if block will only consider
# statement1 to be within its block.

2)Examples of if statement

Example -1:

IF statement is true

Below is the implementation:

height = 70
# checking if the height is greater than 100 or not
if (height < 100):
    print("height is less than 100")
else:
    print("height is greater than 100")

Output:

height is less than 100

Example -2:

IF statement is false

Below is the implementation:

height = 120
# checking if the height is greater than 100 or not
if (height < 100):
    print("height is less than 100")
else:
    print("height is greater than 100")

Output:

height is greater than 100

3)If statement with multiple conditions

In all of the preceding instances, we provide a single condition with the if-statement, but we can also provide multiple conditions.

Example -1:

If statement is true

Below is the implementation:

height = 50
# checking if the height is between 10 to 100
if (height > 10 and height < 100):
    print("height is between 10 to 100")
else:
    print("height is greater than 100 or less than 10")

Output:

height is between 10 to 100

Explanation:

The if-statement in this case checks several conditions, and if all of them satisfy and evaluate to True, the interpreter executes the code in the if-block.

Example -2:

IF statement is false

Below is the implementation:

height = 120
# checking if the height is between 10 to 100
if (height > 10 and height < 100):
    print("height is between 10 to 100")
else:
    print("height is greater than 100 or less than 10")

Output:

height is greater than 100 or less than 10

Related Programs:

Leave a Comment