Conditional Statements in Python

In Python, we can make decisions using conditional statements. These are instructions that help the program choose between different actions based on conditions. For example, if it’s sunny, you might go for a walk, but if it’s rainy, you might stay inside. In Python, we use if, else, and elif to make decisions.

What are Conditional Statements?

Conditional statements are used to check if certain conditions are true or false. Based on that, the program executes a certain block of code.

Syntax of Conditional Statements

The basic syntax for conditional statements looks like this:

if condition:
    # code to run if the condition is True
else:
    # code to run if the condition is False
  

What is Indentation and Why is it Important?

Indentation in Python refers to the spaces or tabs used at the beginning of a line of code to define its level in the structure of the program. It’s important because Python uses indentation to determine which lines of code are part of a certain block. In other words, it shows which lines belong to a specific statement, such as if, else, or elif.

In many other programming languages, curly braces {} are used to define blocks of code. However, Python uses indentation to mark these blocks. If the indentation is not consistent, Python will throw an IndentationError.

For example:

if condition:
    print("This is inside the if block.")
    print("This line is also inside the if block.")
else:
    print("This is inside the else block.")
  

In the code above, the two print() statements inside the if block are indented by four spaces, meaning they belong to that block. Similarly, the else block is indented to indicate that its code belongs to the else statement. If the indentation were wrong, Python wouldn't know which block the code belonged to, and it would give an error.

Syntax for Conditional Statements (Revisited)

The basic syntax for conditional statements includes a colon (:) after the if, elif, or else statement. This colon tells Python that the block of code that follows it will be part of that conditional statement.

Example of Using if and else

Here is a simple example to check if a number is greater than 10:

number = 12
if number > 10:
    print("The number is greater than 10.")
else:
    print("The number is not greater than 10.")
  

Output: The number is greater than 10.

Using elif for Multiple Conditions

If you want to check multiple conditions, you can use elif (short for "else if"). It allows you to test more than one condition in order. Here’s an example:

number = 7
if number > 10:
    print("The number is greater than 10.")
elif number == 7:
    print("The number is exactly 7.")
else:
    print("The number is less than or equal to 10.")
  

Output: The number is exactly 7.

Using Logical Operators

You can combine multiple conditions using logical operators like and, or, and not:

Example:

age = 20
has_ticket = True
if age >= 18 and has_ticket:
    print("You can enter the event.")
else:
    print("You cannot enter the event.")
  

Output: You can enter the event.

Summary

Conditional statements help the program make decisions. The if statement checks if a condition is True, and if so, runs the code inside it. The else block runs if the condition is False. The elif allows you to check additional conditions. You can also combine conditions using logical operators.

In Python, remember that indentation and the colon are essential for the correct structure of the code. The colon denotes the start of the code block, and proper indentation defines the block.

In the next lesson, we’ll explore how to use loops to repeat tasks in Python!

Test Your Knowledge: Conditional Statements

Now, let's check your understanding of conditional statements in Python! Answer the following questions:

1. What is the correct way to write a conditional statement in Python?

2. Which statement is used to check another condition if the first condition is False?

3. What is the output of the following code?

    x = 15
    if x > 10:
        print("Greater")
    else:
        print("Smaller")
  

4. What does the logical operator `or` do in conditional statements?

5. What will be the output of the following code?

    x = 7
    if x > 10:
        print("Greater than 10")
    elif x == 7:
        print("Equal to 7")
    else:
        print("Less than or equal to 10")
  

Answer the questions and check how well you've understood conditional statements in Python!