Python If … Else

The if statement in Python is used to execute a block of code only if a particular condition is true. If the condition is false, the code inside the if statement will be skipped and execution will continue with the next line of code after the if statement.

The basic syntax of an if statement is as follows:

if condition:
    # code to execute if condition is true

Here, condition is the expression that is being evaluated. If condition evaluates to True, the code inside the if block will be executed. If condition is False, the code inside the if block will be skipped.

Here’s an example:

x = 5
y = 10

if x < y:
    print("x is less than y")

In this example, x < y is the condition being evaluated. Since x is indeed less than y, the code inside the if block will be executed, and the output will be x is less than y.

You can also use else to specify a block of code to execute if the condition in the if statement is false. The basic syntax for an if-else statement is as follows:

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

Here’s an example that uses an if...else statement to check whether a number is even or odd:

num = 10

if num % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")

In this example, we’re using the modulo operator % to check whether num is divisible by 2 with no remainder. If the result is 0, then num is even, so the code inside the if block is executed and “The number is even” is printed to the console. If the result is not 0, then num is odd, so the code inside the else block is executed and “The number is odd” is printed to the console.

It’s important to note that the else block is optional. If you only want to execute code when the condition is True, you can omit the else block altogether. Here’s an example:

age = 18

if age >= 18:
    print("You are old enough to vote")

In this example, we’re using an if statement to check whether age is greater than or equal to 18. If the condition is True, then “You are old enough to vote” is printed to the console. If the condition is False, then nothing happens, because there is no else block to execute.

Elif Statement

The elif statement is used in Python to check for multiple conditions, after the initial if statement. It stands for “else if”, and it allows you to chain together multiple conditions, so that the program can choose which block of code to execute based on multiple criteria.

Here’s the basic syntax of an if...elif...else statement:

if condition1:
    # do something if condition1 is True
elif condition2:
    # do something else if condition2 is True
else:
    # do something else if neither condition1 nor condition2 is True

The condition1 is checked first, and if it is True, then the code inside the first if block is executed. If it is False, then the condition2 is checked, and if it is True, then the code inside the elif block is executed. If condition2 is also False, then the code inside the else block is executed.

Here’s an example that uses an if...elif...else statement to check the temperature and give a message accordingly:

temperature = 25

if temperature > 30:
    print("It's too hot!")
elif temperature > 20:
    print("It's just right.")
else:
    print("It's too cold!")

In this example, we’re using an if statement to check whether the temperature is greater than 30. If it is, then “It’s too hot!” is printed to the console. If the temperature is not greater than 30, then the elif statement is checked to see whether the temperature is greater than 20. If it is, then “It’s just right.” is printed to the console. If both condition1 and condition2 are False, then the code inside the else block is executed, and “It’s too cold!” is printed to the console.

Note that the elif statement is optional, and you can have as many elif statements as you need, depending on how many conditions you want to check.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial