Introduction to if-else condition in Python: Definition, Basic Usage, and Example Program

Definition of if-else Condition: 

In Python, the if-else statement allows you to run different blocks of code based on whether a condition is true or false:

  • if: Runs code if the condition is true.
  • else: Runs different code if the condition is false.

Simple example of the if-else condition syntax:


if condition: # Code to execute if condition is True else: # Code to execute if condition is False

Example:

number = 10 if number > 5: print("Number is greater than 5") else: print("Number is 5 or less")

Output:

Number is greater than 5

Post a Comment

0 Comments