Skip to content

Python's equivalent of && (logical-and) in an if-statement

In Python, the logical "and" operator is represented by the and keyword. Here is an example of how you might use it in an if-statement:

Logical and operator in Python

python
x = 1
y = 2
if x > 0 and y > 0:
    print("Both x and y are greater than 0")

<div class="alert alert-info flex not-prose"> Watch a course Python - The Practical Guide</div>

It can also be used in a ternary expression like this:

Logical and operator in a ternary expression in Python

python
x = 1
y = 2
result = "Both are positive" if x > 0 and y > 0 else "One of them is not positive"
print(result)

Please note that and operator is bit different than & which is bitwise operator, and has different behaviour.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.