if, elif, and else statements in Python - Taleem Dunya

Lecture 08

if, elif, and else statements in Python

Certainly! The if, elif, and else statements in Python are used for conditional execution. They allow you to check multiple conditions and execute different blocks of code based on the condition that evaluates to True. Here's an example that demonstrates the usage of if, elif, and else: Certainly! The if, elif, and else statements in Python are used for conditional execution. They allow you to check multiple conditions and execute different blocks of code based on the condition that evaluates to True. Here's an example that demonstrates the usage of if, elif, and else:
In this program, the user is prompted to enter a number. The program then evaluates the number and checks the conditions using if, elif, and else.

  • If the number is greater than 0, it prints "The number is positive."
  • If the number is less than 0, it prints "The number is negative."
  • If the number is equal to 0, it prints "The number is zero."
  • The elif statement allows you to check additional conditions if the preceding if condition(s) evaluate to False.
  • The else statement is optional and executes a block of code when none of the preceding conditions are met.