free geoip
37

Python Logical Operators with Examples

When learning Python, one of the most important concepts is understanding logical operators. Logical operators allow you to combine conditional…

When learning Python, one of the most important concepts is understanding logical operators. Logical operators allow you to combine conditional statements and control the flow of your program. They are essential for decision-making, comparisons, and building more complex expressions. In this article, we will explore Python logical operators with examples, covering how and, or, and not work, and provide real-world use cases.

Python logical operators with examples

What Are Logical Operators in Python?

Logical operators are used to evaluate two or more conditions and return a Boolean result (True or False). Python supports three main logical operators:

  • and – returns True if both conditions are true.
  • or – returns True if at least one condition is true.
  • not – reverses the Boolean result of a condition.

Python Logical Operators Table

OperatorDescriptionExampleResult
andReturns True if both statements are True(5 > 3) and (10 > 7)True
orReturns True if one of the statements is True(5 > 3) or (10 < 7)True
notReverses the resultnot (5 > 3)False

Using the and Operator

The and operator is true only if both conditions are true. It is commonly used when multiple requirements must be satisfied before performing an action.

age = 20
has_id = True

if age >= 18 and has_id:
    print("Access granted")
else:
    print("Access denied")

In the example above, the output will be Access granted because both conditions (age >= 18 and has_id) are true.

Using the or Operator

The or operator returns true if at least one condition is true. It is helpful when you want your program to continue even if only one requirement is met.

is_student = False
has_discount_code = True

if is_student or has_discount_code:
    print("You get a discount!")
else:
    print("No discount available.")

Since has_discount_code is true, the program prints You get a discount!.

Using the not Operator

The not operator inverts the result of a condition. If the condition is true, not will return false, and vice versa.

logged_in = False

if not logged_in:
    print("Please log in first.")
else:
    print("Welcome back!")

The output is Please log in first. because logged_in is false, and not False becomes true.

Combining Logical Operators

You can combine multiple logical operators in a single expression to create complex conditions.

temperature = 25
is_raining = False
has_jacket = True

if (temperature > 20 and not is_raining) or has_jacket:
    print("You can go outside comfortably.")
else:
    print("Better to stay inside.")

In this example, the output will be You can go outside comfortably. because either the weather is nice or the person has a jacket.

Practical Examples of Logical Operators

1. User Authentication

username = "admin"
password = "1234"

entered_username = "admin"
entered_password = "1234"

if entered_username == username and entered_password == password:
    print("Login successful")
else:
    print("Login failed")

2. Eligibility Check

age = 16
parent_permission = True

if age >= 18 or parent_permission:
    print("You can join the event")
else:
    print("You are not eligible")

3. Security System

door_locked = True
window_closed = False

if door_locked and window_closed:
    print("House is secure")
else:
    print("House is not secure")

Best Practices for Using Logical Operators

  • Use parentheses () to make complex conditions easier to read.
  • Avoid deeply nested conditions; break them into smaller steps.
  • Test your conditions with different input values.

Conclusion

Logical operators in Python are essential for decision-making and controlling the flow of your programs. By mastering and, or, and not, you can build smarter conditions and more flexible code. We have seen how these operators work with simple examples and real-world applications. Practice these concepts to improve your programming logic and problem-solving skills.

For more details, you can also check the official Python documentation on logical operators at Python Boolean Operations.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *