Python Conditions and if elif and else Statements

About Python if Conditional Statement

In Python, the if conditional statement is used for conditional execution. It evaluates a condition and executes a block of code if the condition is true.

Syntax of if condition
  • if condition: The 'if' keyword is followed by a condition with colon (:).
  • if_statemnet: Block of code to be executed if the 'if' condition is true.
Examples of Python if Conditional Statement
Output of the above example
20 is greater than 15
                                

In the above example:

  • a = 20 and b = 15 are two variables.
  • if a > b: is the 'if' condition. The condition is true as a is greater than b.
  • print("20 is greater than 15.") is the code block under if statement.

In the above exaple, as the 'if' condition is true, the code block under 'if' statement is executed.

About Python if ... else Conditional Statements

In Python, the if conditional statement is used for conditional execution. It evaluates a condition and executes a block of code if the condition is true.

Whenever, the if condition is false, the block of code under 'else' statement will be executed.

Comparison operators ('<', '>', '==', '!=', '<=', '>='), logical operators ('and', 'or', 'not'), and membership tests ('in', 'not in') are commonly used for comparison in the if condition.

The block of code after the 'if' and 'else' conditions must be indented consistently.

Syntax of if...else condition
  • if condition: The 'if' keyword is followed by a condition with colon (:).
  • if_statemnet: Block of code to be executed if the 'if' condition is true.
  • else: The 'else' keyword is followed by a colon (:).
  • else_statemnet: Block of code to be executed if the 'if' condition is false.

Code blocks after 'if', and 'else' statements have consistent indentation.

Example of if...else condition
Output of the above example
y is greater than x.
                                

In the above example:

  • x = 10 and y = 20 are two variables.
  • if x > y: is the 'if' condition. The condition is not true as x is not greater than y.
  • print("x is greater than y.") is the code block under if statement.
  • else: defines 'else' statement.
  • print("y is greater than x.") is the code block under else statement.

In the above example, the code block under 'else' statement is executed as the 'if' condition is false.

About Python if...elif...else Conditional Statements

In Python, the if conditional statement is used for conditional execution. It evaluates a condition and executes a block of code if the condition is true.

Whenever, the if condition is false, the block of code under 'else' statement will be executed.

Comparison operators ('<', '>', '==', '!=', '<=', '>='), logical operators ('and', 'or', 'not'), and membership tests ('in', 'not in') are commonly used for comparison in the if condition.

The block of code after the 'if' and 'else' conditions must be indented consistently.

Syntax of if...elif...else condition
  • if condition: The 'if' keyword is followed by a condition with colon (:).
  • if_statemnet: Block of code to be executed if the 'if' condition is true.
  • elif condition: The 'elif' keyword is followed by a condition with colon (:).
  • elif_statemnet: Block of code to be executed if the 'elif' condition is true.
  • else: The 'else' keyword is followed by a colon (:).
  • else_statemnet: Block of code to be executed if the 'if' and 'elif' conditions are false.
Code blocks after 'if', 'elif', and 'else' statements have consistent indentation.
Example of if...elif...else condition
Output of the above example
q is greater than p.
                                

In the above example:

  • p = 10 and q = 20 are two variables.
  • if p > q: is the 'if' condition. The condition is not true as p is not greater than q.
  • print("p is greater than q.") is the code block under if statement.
  • elif q > p: is the 'elif' condition. The condition is true as q is greater than p.
  • print("q is greater than p.") is the code block under if statement.
  • else: defines 'else' statement.
  • print("p is equal to q.") is the code block under else statement.

In the above exaple, the code block under 'elif' statement is executed as the 'if' condition is false and 'elif' condition is true.

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python if...elif...else conditional statements in the following Python code editor and click Execute button to check the output. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output