Python Condtitions 


In this tutorial, we will explore the concept of conditional statements in Python, specifically the if, elif, and else statements.

Conditional statements allow you to perform different actions based on certain conditions.

We will cover the syntax and usage of these statements in Python.

 

 

if Statement


The ‘if’ statement is used to execute a block of code if a certain condition is true. The basic syntax of the ‘if’ statement is as follows:

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


Here's an example that demonstrates the usage of the ‘if’ statement:

age = 18

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


In the above 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 ‘if-else’ statement allows you to execute different blocks of code based on a condition. If the condition specified in the ‘if’ statement is true, the code block under the ‘if’ statement will be executed. Otherwise, the code block under the ‘else’ statement will be executed. The basic syntax of the ‘if-else’ statement is as follows:

if condition:
    # Code to be executed if the condition is true
else:
    # Code to be executed if the condition is false

Here's an example that demonstrates the usage of the if-else statement:

age = 16

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


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

 

if-elif-else Statement


The ‘if-elif-else’ statement allows you to test multiple conditions and execute different blocks of code based on the conditions. The ‘elif’ keyword is used to specify additional conditions to be checked. If the condition specified in the ‘if’ statement is true, the code block under the ‘if’ statement will be executed. If the condition in the ‘if’ statement is false and the condition in an ‘elif’ statement is true, the code block under that ‘elif’ statement will be executed. If none of the conditions are true, the code block under the ‘else’ statement will be executed. The basic syntax of the ‘if-elif-else’ statement is as follows:

if condition1:
    # Code to be executed if condition1 is true
elif condition2:
    # Code to be executed if condition1 is false and condition2 is true
else:
    # Code to be executed if both condition1 and condition2 are false

Here's an example that demonstrates the usage of the if-elif-else statement:

age = 25

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 the above example, if the ‘age’ variable is less than 18, the message "You are a minor" will be printed. If the ‘age’ variable is between 18 and 65 (inclusive), the message "You are an adult" will be printed. Otherwise, the message "You are a senior citizen" will be printed.

 

Nested if Statements


You can nest ‘if’ statements within other ‘if’ statements to create more complex conditional structures. This allows you to check for multiple conditions and perform different actions based on the combinations of those conditions. Here's an example:

x = 10

if x > 0:
    if x % 2 == 0:
        print("x is a positive even number.")
    else:
        print("x is a positive odd number.")
else:
    print("x is not a positive number.")


In the above example, if the x variable is greater than 0, the inner ‘if’ statement will be checked. If the x variable is even, the message "x is a positive even number" will be printed. If the x variable is odd, the message "x is a positive odd number" will be printed. If the x variable is not greater than 0, the message "x is not a positive number" will be printed.

 

Summary


In this tutorial, we covered the basics of conditional statements in Python, including the ‘if’, ‘elif’, and ‘else’ statements. These statements allow you to control the flow of your program based on certain conditions. With the knowledge gained from this tutorial, you should be able to effectively use conditional statements in your Python programs to handle different scenarios and make decisions based on specific conditions.

© 2022-2023 All rights reserved.