Python Conditional Statements

Python Conditional Statements

Python conditional statements are used to perform different actions based on whether a certain condition is true or false. These statements are a key part of decision-making in programming. Python provides key conditional statements like `if`, `elif`, and `else` for decision-making.:


1. if Statement

The if statement executes a block of code only when a specified condition is metTrue, the block of code inside the if statement is executed.

Syntax:

  if condition:
  # code to execute if the condition is true

Example:

  x = 10
  if x > 5:
  print("x is greater than 5")
 

Output:

x is greater than 5


2. if-else Statement

The if-else statement adds an alternative block of code to execute when the condition is False.

Syntax:

  if condition:
    # code to execute if the condition is true
  else:
    # code to execute if the condition is false

Example:

  x = 3
  if x > 5:
      print("x is greater than 5")
  else:
      print("x is not greater than 5")

Output:

x is not greater than 5


3. if-elif-else Statement

The if-elif-else structure enables evaluating multiple conditions sequentially. The elif (short for "else if") keyword is used to check additional conditions after the initial if.

Syntax:

  if condition1:
      # code to execute if condition1 is true
  elif condition2:
      # code to execute if condition2 is true
  else:
      # code to execute if neither condition1 nor condition2 is true

Example:

  x = 7
  if x > 10:
      print("x is greater than 10")
  elif x == 7:
      print("x is equal to 7")
  else:
      print("x is less than 10 and not equal to 7")

Output:

x is equal to 7


4. Nested if Statements

An if statement inside anotherif statement is called a nested condition.

Syntax:

  if condition1:
    if condition2:
        # code to execute if both condition1 and condition2 are true

Example:

  x = 15
  if x > 10:
      if x % 2 == 0:
          print("x is greater than 10 and even")
      else:
          print("x is greater than 10 and odd")

Output:

x is greater than 10 and odd


5. Short-Hand if Statement

A single-line statement simplifies conditional execution.

Syntax:

  if condition: statement

Example:

  x = 10
  if x > 5: print("x is greater than 5")

Output:

x is greater than 5


6. Short-Hand if-else Statement (Ternary Operator)

You can write the if-else statement in a single line using a shorthand.

Syntax:

statement_if_true if condition else statement_if_false

Example:

  x = 5
  print("x is even") if x % 2 == 0 else print("x is odd")

Output:

x is odd


7. and, or, and not with Conditionals

Logical operators allow evaluating multiple conditions in a single expression.

  • and: True if both conditions are true.
  • or: True if at least one condition is true.
  • not: Negates the condition.

Example:

  x = 10
  y = 20

  if x > 5 and y > 15:
      print("Both conditions are true")

  if x > 15 or y > 15:
      print("At least one condition is true")

  if not x < 5:
      print("x is not less than 5")

Output:

  • Both conditions are true
  • At least one condition is true
  • x is not less than 5

Summary Table

Keyword Use Case Example
if Check a single condition if x > 5: print("x is greater than 5")
else Specify an alternative action else: print("x is not greater than 5")
elif Check additional conditions elif x == 7: print("x is 7")
and, or, not Combine or modify conditions if x > 5 and y > 10: ...

Python conditional statements are versatile and enable complex decision-making in programs. With these examples and syntax, you can write efficient and clear conditional logic in your Python programs.

Previous Next