Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Python if else Conditional Statement

In Python, the if, elif, and else keywords are used to create conditional statements. Conditional statements allow your program to make decisions and execute different code blocks based on whether a certain condition is true or false.

Here's a basic tutorial on using if, elif, and else in Python:

  • if statement:

The if statement is used to test a condition, and if the condition is true, it executes the code block following the if statement. The basic syntax is:

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

Example:

age = 18

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

In this example, if the age variable is greater than or equal to 18, the message "You are eligible to vote." will be printed.

  • if-else statement:

The else statement is used to execute a code block if the condition in the if statement is false. The basic syntax is:

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

Example:

age = 16

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

In this example, since the age variable is less than 18, the message "You are not eligible to vote." will be printed.

  • if-elif-else statement:

The elif (short for "else if") statement is used to test multiple conditions. It is used when you need to test more than two conditions. The basic syntax is:

if condition1:
    # code block to execute if condition1 is true
elif condition2:
    # code block to execute if condition1 is false and condition2 is true
else:
    # code block to execute if all conditions are false

Example:

score = 75

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

In this example, the program checks the value of the score variable and prints the corresponding grade. Since the score variable is 75, the message "Grade: C" will be printed.

You can add more elif statements as needed to test additional conditions. The else statement is optional, and it's used to handle cases where none of the conditions are true.

  1. How to use if-else in Python:

    • Description: Using the basic if-else statement in Python for conditional execution.
    • Code:
      x = 10
      if x > 5:
          print("x is greater than 5")
      else:
          print("x is less than or equal to 5")
      
  2. Conditional statements in Python with if and else:

    • Description: Demonstrating the use of if and else for conditional statements in Python.
    • Code:
      age = 20
      if age >= 18:
          print("You are an adult.")
      else:
          print("You are a minor.")
      
  3. Python if-else examples for beginners:

    • Description: Providing simple if-else examples for beginners.
    • Code:
      number = 7
      if number % 2 == 0:
          print("The number is even.")
      else:
          print("The number is odd.")
      
  4. Using elif in Python if-else statements:

    • Description: Introducing the elif statement for multiple conditions in Python.
    • Code:
      grade = 75
      if grade >= 90:
          print("A")
      elif grade >= 80:
          print("B")
      elif grade >= 70:
          print("C")
      else:
          print("F")
      
  5. Nested if-else statements in Python:

    • Description: Using nested if-else statements for more complex conditions.
    • Code:
      x = 5
      if x > 0:
          if x % 2 == 0:
              print("Positive and even")
          else:
              print("Positive and odd")
      else:
          print("Non-positive")
      
  6. Python ternary operator for if-else:

    • Description: Utilizing the ternary operator for concise if-else statements.
    • Code:
      age = 22
      result = "Adult" if age >= 18 else "Minor"
      print(result)
      
  7. Comparisons and logical operators in Python if-else:

    • Description: Demonstrating comparisons and logical operators in if-else statements.
    • Code:
      x = 12
      if x > 0 and x % 2 == 0:
          print("Positive and even")
      elif x > 0 and x % 2 != 0:
          print("Positive and odd")
      else:
          print("Non-positive")
      
  8. Example code for Python if-else conditional statements:

    • Description: Providing an example code snippet with if-else conditional statements.
    • Code:
      temperature = 25
      if temperature > 30:
          print("It's hot outside.")
      else:
          print("It's not too hot.")