Skip to main content

1. Conditional Statements (if, elif, else)

Used to execute different code based on conditions.

Syntax


if

Runs only if the condition is True.
Output

if...else

Output

if...elif...else

Checks multiple conditions.
Output

Nested if

Output

Short Hand if


Short Hand if…else (Ternary Operator)


Logical Operators

and

Both conditions must be True.

or

At least one condition must be True.

not

Reverses the result.

Comparison Operators


Membership Operators


Identity Operators

Output

Quick Revision

Conditional Statements

  • if → Check one condition.
  • elif → Check another condition.
  • else → Runs if all conditions are False.
  • Nested ifif inside another if.
  • Ternary operator → Short if-else.