Conditional Statements in Python

In Python, you can create a conditional structure using the if statement.

The Syntax of the if Statement

if (expression):
indentationblock of code

If the condition is true, the if statement executes the block of code.

If the condition is false, the program skips to the first statement after the conditional structure.

Note: In Python, the block of code is not enclosed in braces or other keywords. Instead, Python uses indentation, moving the start of the lines to the right to indicate that they are part of the conditional structure.

An Example

In the following code, I ask the user to enter a number.

The entered number is then assigned to the variable n.

n = int(input('Enter a number: '))
if (n == 5):
indentationprint('You entered 5')
print('You did not enter 5')

In the second line, the if statement checks if the variable n is equal to 5.

  • If the condition (n == 5) is true, the Python interpreter executes the block of code inside the if statement. In this case, the block consists only of the first print statement. The result will be:

    You entered 5

  • If the condition (n == 5) is false, it means the user entered a different number. The interpreter skips to the first statement after the if block, which is the second print statement. The result will be:

    You did not enter 5

The else Clause

The if statement can also include an alternative block of code to execute when the condition is not met.

This second block is placed after the else clause:

if (condition):
indentationblock of code 1
else:
indentationblock of code 2

If the condition is true, the if statement executes block 1.

If the condition is false, the if statement executes block 2.

An Example

In the following code, I ask the user to enter an integer and assign it to the variable n.

n = int(input('Enter a number: '))
if (n > 0):
indentationprint('The number is positive')
else:
indentationprint('The number is not positive')

Here, I check if the number is greater than 0.

  • If the condition is true, the if statement executes the first block and prints the message "The number is positive".

    The number is positive

  • If the condition is false, the if statement executes the second block and prints the message "The number is not positive".

    The number is not positive

Chained Conditions (ELIF)

In an if statement, you can also include multiple blocks for chained conditions.

In this case, you use the elif clause:

if (condition):
indentationblock of code 1
elif (condition2):
indentationblock of code 2
elif (condition3):
indentationblock of code 3

Elif stands for "else if".

There is no limit to the number of elif clauses you can use.

An Example

In the following code, I ask the user to enter a number and assign it to the variable n.

n = int(input('Enter a number: '))
if (n > 0):
indentationprint('The number is positive')
elif (n < 0):
indentationprint('The number is negative')
else:
indentationprint('The number is zero')

The program executes as follows:

  • If the first condition is true (n > 0), the program prints "The number is positive" and skips to the end of the conditional structure.

    The number is positive

  • If the first condition is false, the elif statement checks if n is less than zero (n < 0). If true, the program prints "The number is negative" and skips to the end of the conditional structure.

    The number is negative

  • If the elif is also false, the program executes the block after the else clause, printing "The number is zero".

    The number is zero

Nested Conditions

In Python, you can also nest multiple if statements.

if (condition):
indentationif (condition2):
indentationindentationif (condition3):

There is no limit to the number of nested if statements.

An Example

In the following code, I ask the user to enter a number.

Then I check if the number is positive, negative, or zero.

n = int(input('Enter a number: '))
if (n > 0):
indentationprint('The number is positive')
else:
indentationif (n == 0):
indentationindentationprint('The number is zero')
indentationelse:
indentationindentationprint('The number is negative')

Inside the else clause, there is a nested if statement.

  • If the condition (n > 0) is true, the program displays the message "The number is positive".
  • If the condition (n > 0) is false, the program executes the else block, where there is a nested if statement that checks if the number is zero (n == 0). If it is not zero, then the number must be negative.
 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

FacebookTwitterLinkedinLinkedin
knowledge base

Python

  1. The Python Language
  2. How to Install Python on Your PC
  3. How to Write a Program in Python
  4. How to Use Python in Interactive Mode
  5. Variables
  6. Numbers
  7. Logical Operators
  8. Iterative Structures (or Loops)
  9. Conditional Structures
  10. Exceptions
  11. Files in Python
  12. Classes
  13. Modules

Miscellaneous

Source