Python if keyword is an important control statement in Python and is used to make decisions and control the flow of programs.
The if keyword is used to create conditional statements in Python.
It evaluates a condition and executes a block of code if the condition is true.
Here’s the basic syntax of an if statement:
if condition: # code to execute if the condition is True
The condition can be any expression that evaluates to either True or False.
If the condition is True, the indented code block following the if statement is executed.
Otherwise, the code block is skipped, and program execution continues with the next statement.
Conditional Branching
The if keyword becomes truly powerful when combined with additional branches, such as else and elif (also known as “else if”).
These branches allow you to handle multiple conditions and perform different actions accordingly.
Let’s examine an example:
if age < 18: print("You are a minor.") elif age >= 18 and age < 65: print("You are an adult.") else: print("You are a senior citizen.")
In this case, the if statement evaluates the value of the age variable and prints a corresponding message.
If the age is less than 18, the program prints “You are a minor.”
If the age is between 18 (inclusive) and 65 (exclusive), it prints “You are an adult.”
Otherwise, if none of the previous conditions are met, it prints “You are a senior citizen.”
Combining Conditions
Python offers a wide range of logical operators, such as and, or, and not, which allow you to create complex conditions by combining multiple expressions.
This enables you to handle a wide range of scenarios.
Consider the following example:
temperature = 25 humidity = 80 if temperature > 30 and humidity > 60: print("It's a hot and humid day!")
In this case, the if statement checks if the temperature is greater than 30 degrees Celsius and the humidity is above 60.
If both conditions are True, it prints the message “It’s a hot and humid day!”.
Nested ‘if’ Statements
Sometimes, we may need to evaluate multiple conditions within a condition.
The ‘if’ statements can be nested within each other to handle multiple conditions and create more complex decision-making structures.
By using indentation properly, you can create nested if statements that provide different paths of execution based on the conditions.
Let’s consider an example where we determine the category of a number based on its value:
number = 10 if number > 0: if number % 2 == 0: print("Positive even number") else: print("Positive odd number") elif number == 0: print("Zero") else: print("Negative number")
Output:
Positive even number
In this example, the program checks if the number is greater than 0.
If so, the nested if statement will check if it is even or odd.
If the number is not greater than 0, it moves to the elif branch to check if it is equal to 0.
Finally, if none of the conditions are met, the else branch handles negative numbers.
Conclusion
The if keyword is an essential tool for making decisions and controlling the flow of your Python programs.
By understanding its syntax and various use cases, you can create dynamic, responsive, and flexible programs.
Whether you’re building a simple script or a complex application, mastering the if keyword is a crucial step in becoming a proficient Python programmer.