Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Conditions & Statements

Conditional statements in Python are used to execute different blocks of code based on certain conditions. The key conditional statements are:

  • if
  • if-else
  • if-elif-else
  • Nested if

The if Statement

The if statement executes a block of code only if the condition is True.

Syntax

if condition:
    # Code to execute if condition is True

Example

age = 18

if age >= 18:
    print("You are eligible to vote.")  # Output: You are eligible to vote.

Key Points:

  • The condition inside if must be True for the block to execute.
  • Indentation is mandatory in Python (4 spaces or a tab).

The if-else Statement

If the condition is False, the else block is executed.

Syntax

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

Example

age = 16

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are NOT eligible to vote.")  # Output: You are NOT eligible to vote.

Key Points:

  • If the if condition is False, the code inside else runs.
  • else does not require a condition (it is the default case).

The if-elif-else Statement

The elif (short for else if) is used to check multiple conditions.

Syntax

if condition1:
    # Code if condition1 is True
elif condition2:
    # Code if condition2 is True
else:
    # Code if none of the above conditions are True

Example

marks = 75

if marks >= 90:
    print("Grade: A")
elif marks >= 80:
    print("Grade: B")
elif marks >= 70:
    print("Grade: C")  # Output: Grade: C
else:
    print("Grade: F")

Key Points:

  • The if condition is checked first.
  • If if is False, the program checks elif conditions one by one.
  • If none of the conditions are True, the else block executes.

Nested if Statements

You can place one if statement inside another.

Example

age = 20
has_id = True

if age >= 18:
    if has_id:
        print("You can enter the club.")  # Output: You can enter the club.
    else:
        print("You need an ID to enter.")
else:
    print("You are too young to enter.")

Key Points:

  • Inner if conditions are checked only if the outer if is True.
  • Useful when multiple conditions must be met.

The if Statement with Logical Operators

You can combine multiple conditions using logical operators:

  • and → Both conditions must be True.
  • or → At least one condition must be True.
  • not → Reverses the condition.

Example Using and

age = 20
has_license = True

if age >= 18 and has_license:
    print("You can drive.")  # Output: You can drive.
else:
    print("You cannot drive.")

Example Using or

has_passport = False
has_national_id = True

if has_passport or has_national_id:
    print("You can travel.")  # Output: You can travel.
else:
    print("You cannot travel.")

Example Using not

is_raining = False

if not is_raining:
    print("You can go outside.")  # Output: You can go outside.

Key Points:

  • and requires both conditions to be True.
  • or requires at least one condition to be True.
  • not reverses the condition (TrueFalse, FalseTrue).

Short-Hand (Ternary Operator)

Python allows short-hand one-line if-else statements

Syntax

variable = value_if_true if condition else value_if_false

Example

age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)  # Output: Adult

Best for simple conditions.

pass Statement in if Blocks

The pass statement is used when an if block is required but should do nothing.

Example

age = 18

if age >= 18:
    pass  # Placeholder, avoids syntax error
else:
    print("You are too young.")

Use pass as a placeholder when writing incomplete code.

Summary Table

StatementDescription
ifExecutes code if condition is True
if-elseRuns one block for True, another for False
if-elif-elseChecks multiple conditions
Nested ifif inside another if
Logical Operatorsand, or, not for multiple conditions
Ternary OperatorShort if-else in one line
pass StatementPlaceholder inside an if block

    Leave a Reply

    Your email address will not be published.

    Need Help?