If statements in Python


Keywords: if statement, conditional statement, else statement, elif statement

if else statement Python

The if statements in Python is a condition that must be fulfilled in order for a desired command to be executed. All programming is based on conditions. It is the if-statements that control the dependent conditions, which gives the foundation for all logic in programming

What is an if statement in Python?

The if statement in Python tests if the logical expression (if-condition in the image) is fulfilled. The logical expression is a condition that is either true or false. For example, if you press a lamp button, the lamp should either be turned on or off. Not both at the same time. With an if statement, you can decide whether to turn the power on or off (depending on whether the lamp is off or on).

In other words, we use a logical expression to decide if our programs should execute a certain code or not. Meaning, by using the if statement, you can specify what should happen if a condition is met or not. This creates conditional programming, which is the basis of all logic in a program.

If statements Python programming

What the figure above illustrates is that: 

  • First, the program starts and arrives at the if condition
  • When the if condition is true, that is, the condition is met, then the program proceeds and performs one or more operations that we have specified
  • But if the condition is not met, in other words false, then the program skips the operations and moves on

Remember: It is possible to use one or more conditions that indicate whether the program should perform the operation or not.

Create if statement in Python

  • The If statements in Python is easily created by using the reserved word If 
  • Followed by the logical expression in parentheses (), then ended by :
  • The program runs if the condition is true (fulfilled condition)

Syntax: If statement in Python

if (logical expression):

    Operation / Operations …
  • The if statement is easily declared by the reserved word if
  • All operations to be performed when the if statement is true are written with indentation
  • The if statement ends when you stop indenting the code.
    Operation / Operations ..
  • Is the operations that we want to perform when the logical condition is met.
  • Note, the code on line three is indented and therefore part of the if statement, if we would stop indenting the code, those parts of our code would not be included in the if statement

Example 1: How to use if statement in Python

Below is an example of how an if statement works.

  • All code that has an indentation after the if statement will be executed.
  • When the if statement is over, the code continues without indentation
  • Since we set our variable run_operation to true, our if statement will execute the code within
run_operation = True

if(run_operation):
    print("If statement has started")
    print("All operations that are indented will be performed")

print("This command is not included in the if statement because the code is not indented")

The result in this case will be:

If statement has started
All operations that are indented will be performed
This command is not included in the if statement because the code is not indented

However, if we instead set the variable run_operation to False, then the code inside the if statement will not be performed

run_operation = False

if(run_operation):
    print("If statement has started")
    print("All operations that are indented will be performed")

print("This command is not included in the if statement because the code is not indented")

The result  will be:

This command is not included in the if statement because the code is not indented

Example 2: If statement in Python with multiple expressions

If we want to check if an integer variable is within an interval, for example, between 0 and 200, we easily set up the if statement with the logical expression.

  • If the variable num is greater than 0 and less than 200, we get a printout, Number: num
num = 50

if(num > 0 and num < 200):
    print("Number:", num)

And in this case we get the result

Number: 50

Summary: If statement in Python

The if statement in Python is a conditional statement that determines whether the program should perform an operation, or several operations, depending on whether one or more specified conditions are met or not. The condition is a logical expression that is either true or false.

  • The condition statements, and more specifically the if statement, are used as a decision point to check a number of different conditions.
  • The if statement enables us to control the logic of our programs.
  • It is possible to use one or more conditions that indicate whether the program should perform the operation.
  • For more technical and specific information about the If statements in Python we recommend the Python docs website

Syntax: If statement in Python

if (logical expression):

    Operation / Operations …