Summary: If statements in Python


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

Python programming

Summary of the chapter “If statements in Python”. In this chapter we have looked at the basics of conditional statements that allow you to decide what will happen in the program depending on the state of a Boolean expression (True or False). We do this with the if, elif and else statements.

If-, else- and elif Statements

A condition statement in Python always starts with an if condition. If the if condition is true, then all the operation belonging to the if statement will be performed. However, the if condition is false, the program skips all operations in the if statement and proceeds to the else if statements accordingly. If none of the if or else if statements are fulfilled, then the operations in the else statement will be performed.

The condition is either true or false

The conditional statements are evaluated by conditions. The terms can have two states, true, or false. If the condition is true, the condition statement will be executed, or if the condition statement is false, then the condition statement will not be executed.

The conditional statements control our program

With the help of if-statements we can:

  • Control all programming with logic.
  • Control terms with Boolean expressions (true or false)
  • Decide the operations to perform or not

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 …

Summary: Else statement in Python

The else statement in Python is used to specify a block of code to be executed if the condition in the if statement is false. The keywords used for the if statement and else statement describes their relationship quite well. “If this condition is true, perform the associated code block, else, execute this code block”.

  • The else statement must be used in conjunction with the if statement.
  • The else statement is declared using the reserved word else followed by the code block belonging to the else statement in indent
  • Else statement has no condition that it evaluates and is executed based on the result of the if statements condition
  • For more technical and specific information about the Else statements in Python we recommend the Python docs website

Syntax: Else statement in Python

if logical expression
   statement doing

else 
   what this statement should perform

Summary: Elif statement in Python

In summary

  • The elif statement is used in conjunction with the if statement and the else statement
  • An elif statement is used to test additional conditions, and if true, execute a specified code block
  • If the if statements condition is true, the else if statement conditions will not be evaluated, as it has a lower priority
  • The elif statement is declared with the reserved words else if followed by a logical expression in parentheses ( ). The logical expression evaluates a condition that is either true or false
  • For more technical and specific information about the Elif statements in Python we recommend the Python docs website

Syntax: Declare elif statement in Python

if (logical expression):
   statement executing

elif (logical expression):
   what elif statement should perform

else (logical expression):
   what else statement should do