Introduction to Python if Statements

Introduction to Python if Statements

In Python, the if statement is used to make decisions based on certain conditions. It allows the program to execute different blocks of code based on whether a condition is true or false. if statements are fundamental to control flow and enable the program to perform different actions depending on the situation.

Basic Syntax

The basic syntax of an if statement in Python is as follows:

if condition:
    # Code block to be executed if condition is true

The condition is a logical expression that evaluates to either True or False. If the condition is True, the code block indented under the if statement is executed. If the condition is False, the code block is skipped.

Indentation

In Python, indentation plays a crucial role in defining code blocks. Indentation is used to group statements together and indicates which statements are part of the same code block. The standard convention is to use four spaces or one tab for indentation. It is important to maintain consistent indentation throughout your code to ensure its proper execution.

Logical Operators and Comparison

Conditions in if statements often involve logical operators and comparison. Here are some commonly used logical operators and comparison operators in Python:

  • Logical Operators:

    • and: Returns True if both conditions are true.

    • or: Returns True if at least one condition is true.

    • not: Negates the result of the condition.

  • Comparison Operators:

    • ==: Checks if two values are equal.

    • !=: Checks if two values are not equal.

    • <: Checks if the left value is less than the right value.

    • >: Checks if the left value is greater than the right value.

    • <=: Checks if the left value is less than or equal to the right value.

    • >=: Checks if the left value is greater than or equal to the right value.

Examples

Example 1: Checking if a Number is Positive

# Check if a number is positive
number = 7

if number > 0:
    print("The number is positive.")

In this example, the condition number > 0 checks if the value of number is greater than zero. If the condition is True, the program executes the code block and prints "The number is positive."

Example 2: Checking if a Number is Even

# Check if a number is even
number = 10

if number % 2 == 0:
    print("The number is even.")

In this example, the condition number % 2 == 0 checks if the remainder of number divided by 2 is equal to zero. If the condition is True, the program executes the code block and prints "The number is even."

Example 3: Checking User Input with Nested if Statements

# Check if a user's age is above 18 and provide different messages based on their gender
age = int(input("Enter your age: "))
gender = input("Enter your gender (M/F): ")

if age >= 18:
    if gender == "M":
        print("You are a male adult.")
    elif gender == "F":
        print("You are a female adult.")
else:
    print("You are not an adult yet.")

In this example, the program prompts the user to enter their age and gender. The outer if statement checks if the user's age is greater than or equal to 18. If the condition is True, the program proceeds to the nested `

ifstatements. Depending on the user's gender, different messages are printed. If the age condition isFalse, the program executes the code block under the else` statement and prints "You are not an adult yet."

Conclusion

if statements in Python allow us to make decisions and execute specific blocks of code based on the evaluation of a condition. By understanding how to use if statements, you can control the flow of your program and make it respond to different situations. Indentation, logical operators, and comparison operators are important components of if statements that help define conditions and code blocks. Additionally, the use of elif allows for multiple conditions to be checked in sequence. With practice and further exploration, you will be able to use if statements effectively, including nesting them and using logical and comparison operators, to create more complex decision-making processes in your Python programs.