If …. Else

Python provides a range of logical conditions for decision-making in your code. These conditions are fundamental in controlling the flow of your program. Here’s a breakdown of how to use these conditions effectively with if statements and logical operators.

Logical Conditions in Python

Python supports several logical conditions that are commonly used in programming:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions are primarily used in if statements and loops to control the flow of execution.

If Statements

An "if statement" evaluates a condition and executes the associated code block if the condition is true. Here's a basic example:

book1 = 5
book2 = 10
if book2 > book1:
    print("book2 is greater than book1")  

In this example, the variables book1 and book2 are compared. Since book2 is greater than book1, the output will be "book2 is greater than book1".

Indentation

Python uses indentation (whitespace at the beginning of a line) to define the scope of loops, conditionals, and functions. Proper indentation is crucial:

book1 = 5
book2 = 10
if book2 > book1:
    print("book2 is greater than book1")  

Incorrect indentation will lead to errors:

book1 = 5
book2 = 10
if book2 > book1:
print("book2 is greater than book1")  

Elif Statements

The elif keyword allows you to check multiple conditions sequentially. If the previous conditions are false, the elif condition is evaluated:

book1 = 5
book2 = 5
if book2 > book1:
    print("book2 is greater than book1")
elif book1 == book2:
    print("book1 and book2 are equal")  

In this case, since book1 is equal to book2, the output will be "book1 and book2 are equal".

Else Statements

The else keyword catches any cases not covered by the preceding if and elif statements:

book1 = 5
book2 = 10
if book2 > book1:
    print("book2 is greater than book1")  
elif book1 == book2:
    print("book1 and book2 are equal")
else:
    print("book1 is greater than book2")

Since book2 is greater than book1, the else block will not execute. If neither the if nor the elif conditions were true, the else block would run.

Short Hand If

For simple cases where you only need one statement, you can use a single line for your if statement:

book1 = 10
book2 = 5
if book1 > book2: print("book1 is greater than book2")  

Short Hand If...Else

When you have a single statement for both if and else, you can combine them into one line:

book1 = 10
book2 = 5
print("A") if book1 > book2 else print("B")  

This method is known as a ternary operator or conditional expression.

Logical Operators

Logical operators allow you to combine multiple conditions:

  • And: Checks if multiple conditions are true. Example:
  • book1 = 10
    book2 = 5
    book3 = 20
    if book1 > book2 and book3 > book1:
        print("Both conditions are true")  
    
  • Or: Checks if at least one of multiple conditions is true. Example:
  • book1 = 10
    book2 = 15
    book3 = 5
    if book1 > book2 or book1 > book3:
        print("At least one condition is true")  
    
  • Not: Reverses the result of a condition. Example:
  • book1 = 10
    book2 = 15
    if not book1 > book2:
        print("book1 is not greater than book2")  
    

Nested If Statements

You can nest if statements within other if statements to handle more complex conditions:

x = 25
if x > 10:
    print("Above ten,")  
    if x > 20:
        print("and also above 20!")  
    else:
        print("but not above 20.")

The Pass Statement

When an if statement is required syntactically but you have no code to execute, use the pass statement to avoid errors:

book1 = 10
book2 = 15
if book1 > book2:
    pass  

Practice Exercises

Here are some exercises to test your understanding of Python's if statements:

  1. Print "Hello World" if a is greater than b:
    a = 50
    b = 10
    if a > b:
        print("Hello World")  
    

Leave a Comment

Simple, privacy focused and free ad network for websites in need of new visitors. Free & easy backlink link building. Overseas domestic helper.