Python Python Tutorial File Handling NumPy Tutorial NumPy Random NumPy ufunc Pandas Tutorial Pandas Cleaning Data Pandas Correlations Pandas Plotting SciPy Tutorial



Python If...Else

Python is a high-level programming language that is widely used for web development, data analysis, artificial intelligence, and scientific computing. One of the most important features of Python is its ability to make decisions based on certain conditions. This is achieved through the use of if...else statements.

The if...else statement is used to execute a block of code if a certain condition is true, and another block of code if the condition is false. The syntax for 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

The condition can be any expression that evaluates to a boolean value (True or False). If the condition is true, the code inside the if block is executed. If the condition is false, the code inside the else block is executed.

Let's take a look at some examples of if...else statements in Python:

Example 1:

x = 10

if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")

In this example, the condition is x > 5. Since x is equal to 10, which is greater than 5, the code inside the if block is executed, and the output is "x is greater than 5".

Example 2:

age = 18

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

In this example, the condition is age >= 18. Since age is equal to 18, which is greater than or equal to 18, the code inside the if block is executed, and the output is "You are eligible to vote".

Example 3:

num = 7

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

In this example, the condition is num % 2 == 0. Since num is equal to 7, which is not divisible by 2, the code inside the else block is executed, and the output is "The number is odd".

Python also allows for nested if...else statements, which means that you can have an if...else statement inside another if...else statement. This is useful when you need to test multiple conditions.

Example 4:

num = 10

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

In this example, the first condition checks if num is greater than 0. If it is, then the nested if...else statement checks if num is even or odd. If num is even, the output is "The number is positive and even". If num is odd, the output is "The number is positive and odd". If num is not greater than 0, the output is "The number is not positive".

Overall, the if...else statement is a powerful tool in Python that allows you to make decisions based on certain conditions. By using if...else statements, you can write more efficient and effective code that can handle a variety of situations.

References:

Activity