When determining if a number is outside a range its best to use this operator?

I like Pointy's between function so I wrote a similar one that worked well for my scenario.

Show
/**
 * Checks if an integer is within ±x another integer.
 * @param {int} op - The integer in question
 * @param {int} target - The integer to compare to
 * @param {int} range - the range ±
 */
function nearInt(op, target, range) {
    return op < target + range && op > target - range;
}

so if you wanted to see if x was within ±10 of y:

var x = 100;
var y = 115;
nearInt(x,y,10) = false

I'm using it for detecting a long-press on mobile:

//make sure they haven't moved too much during long press.
if (!nearInt(Last.x,Start.x,5) || !nearInt(Last.y, Start.y,5)) clearTimeout(t);

Python has three Boolean operators, or logical operators:

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0,
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1, and
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
2. You can use them to check if certain conditions are met before deciding the execution path your programs will follow. In this tutorial, you’ll learn about the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator and how to use it in your code.

In this tutorial, you’ll learn how to:

  • Understand the logic behind Python’s
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 operator
  • Build and understand Boolean and non-Boolean expressions that use the
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 operator
  • Use the
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 operator in Boolean contexts to decide the course of action of your programs
  • Use the
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 operator in non-Boolean contexts to make your code more concise

You’ll also code a few practical examples that will help you understand how to use the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator to approach different problems in a Pythonic way. Even if you don’t use all the features of
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0, learning about them will allow you to write better and more accurate code.

Free Download: Get a sample chapter from Python Tricks: The Book that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.

Working With Boolean Logic in Python

Back in 1854, George Boole authored The Laws of Thought, which contains what’s known as Boolean algebra. This algebra relies on two values: true and false. It also defines a set of Boolean operations, also known as logical operations, denoted by the generic operators

expression1 and expression2
0,
expression1 and expression2
1, and
expression1 and expression2
2.

These Boolean values and operators are pretty helpful in programming. For example, you can construct arbitrarily complex Boolean expressions with the operators and determine their resulting truth value as true or false. You can use the truth value of Boolean expressions to decide the course of action of your programs.

In Python, the Boolean type

expression1 and expression2
3 is a subclass of and can take the values
expression1 and expression2
5 or
expression1 and expression2
6:

>>>

>>> issubclass(bool, int)
True
>>> help(bool)
Help on class bool in module builtins:

class bool(int)
    ...

>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>

>>> isinstance(True, int)
True
>>> isinstance(False, int)
True

>>> int(True)
1
>>> int(False)
0

As you can see in this code, Python implements

expression1 and expression2
3 as a subclass of
expression1 and expression2
4 with two possible values,
expression1 and expression2
5 and
expression1 and expression2
6. These values are in Python. They’re internally implemented as integer numbers with the value
>>> 5 > 3 and 5 == 3 + 2 and 5 != 3
True

>>> 5 < 3 and 5 == 3 and 5 != 3
False
1 for
expression1 and expression2
5 and
>>> 5 > 3 and 5 == 3 + 2 and 5 != 3
True

>>> 5 < 3 and 5 == 3 and 5 != 3
False
3 for
expression1 and expression2
6. Note that both
expression1 and expression2
5 and
expression1 and expression2
6 must be capitalized.

Along with the

expression1 and expression2
3 type, Python provides three Boolean operators, or logical operators, that allow you to combine Boolean expressions and objects into more elaborate expressions. Those operators are the following:

OperatorLogical OperationConjunction

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1Disjunction
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
2Negation

With these operators, you can connect several Boolean expressions and objects to build your own expressions. Unlike other languages, Python uses English words to denote Boolean operators. These words are keywords of the language, so you can’t use them as identifiers.

In this tutorial, you’ll learn about Python’s

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator. This operator implements the logical
expression1 and expression2
0 operation. You’ll learn how it works and how to use it either in a Boolean or non-Boolean context.

Remove ads

Getting Started With Python’s >>> 5 > 3 and 5 == 3 + 2 True >>> 5 < 3 and 5 == 5 False >>> 5 == 5 and 5 != 5 False >>> 5 < 3 and 5 != 5 False 0 Operator

Python’s

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator takes two operands, which can be Boolean expressions, objects, or a combination. With those operands, the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator builds more elaborate expressions. The operands in an
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression are commonly known as conditions. If both conditions are true, then the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression returns a true result. Otherwise, it returns a false result:

>>>

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False

These examples show that an

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression only returns
expression1 and expression2
5 when both operands in the expressions are true. Since the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator takes two operands to build an expression, it’s a binary operator.

The quick examples above show what’s known as the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator’s truth table:

>>> def true_func():
...     print("Running true_func()")
...     return True
...

>>> def false_func():
...     print("Running false_func()")
...     return False
...

>>> # Use logical and
>>> false_func() and true_func()
Running false_func()
False

>>> # Use bitwise and
>>> false_func() & true_func()
Running false_func()
Running true_func()
False
2
>>> def true_func():
...     print("Running true_func()")
...     return True
...

>>> def false_func():
...     print("Running false_func()")
...     return False
...

>>> # Use logical and
>>> false_func() and true_func()
Running false_func()
False

>>> # Use bitwise and
>>> false_func() & true_func()
Running false_func()
Running true_func()
False
3
>>> def true_func():
...     print("Running true_func()")
...     return True
...

>>> def false_func():
...     print("Running false_func()")
...     return False
...

>>> # Use logical and
>>> false_func() and true_func()
Running false_func()
False

>>> # Use bitwise and
>>> false_func() & true_func()
Running false_func()
Running true_func()
False
4TrueTrueTrueTrueFalseFalseFalseFalseFalseFalseTrueFalse

This table summarizes the resulting truth value of a Boolean expression like

>>> def true_func():
...     print("Running true_func()")
...     return True
...

>>> def false_func():
...     print("Running false_func()")
...     return False
...

>>> # Use logical and
>>> false_func() and true_func()
Running false_func()
False

>>> # Use bitwise and
>>> false_func() & true_func()
Running false_func()
Running true_func()
False
4. The result of the expression depends on the truth values of its operands. It’ll be true if both are true. Otherwise, it’ll be false. This is the general logic behind the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator. However, this operator can do more than that in Python.

In the following sections, you’ll learn how to use

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 for building your own expressions with different types of operands.

Using Python’s >>> 5 > 3 and 5 == 3 + 2 True >>> 5 < 3 and 5 == 5 False >>> 5 == 5 and 5 != 5 False >>> 5 < 3 and 5 != 5 False 0 Operator With Boolean Expressions

You’ll typically use logical operators to build compound Boolean expressions, which are combinations of variables and values that produce a Boolean value as a result. In other words, Boolean expressions return

expression1 and expression2
5 or
expression1 and expression2
6.

Comparisons and equality tests are common examples of this type of expression:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False

All these expressions return

expression1 and expression2
5 or
expression1 and expression2
6, which means they’re Boolean expressions. You can combine them using the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 keyword to create compound expressions that test two—or more–subexpressions at a time:

>>>

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False

Here, when you combine two

expression1 and expression2
5 expressions, you get
expression1 and expression2
5 as a result. Any other combination returns
expression1 and expression2
6. From these examples, you can conclude that the syntax for creating compound Boolean expressions with the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator is the following:

expression1 and expression2

If both subexpressions

>>> 2 and 3
3

>>> 5 and 0.0
0.0

>>> [] and 3
[]

>>> 0 and {}
0

>>> False and ""
False
8 and
>>> 2 and 3
3

>>> 5 and 0.0
0.0

>>> [] and 3
[]

>>> 0 and {}
0

>>> False and ""
False
9 evaluate to
expression1 and expression2
5, then the compound expression is
expression1 and expression2
5. If at least one subexpression evaluates to
expression1 and expression2
6, then the result is
expression1 and expression2
6.

There’s no limit to the number of

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operators you can use when you’re building a compound expression. This means that you can combine more than two subexpressions in a single expression using several
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operators:

>>>

>>> 5 > 3 and 5 == 3 + 2 and 5 != 3
True

>>> 5 < 3 and 5 == 3 and 5 != 3
False

Again, if all the subexpressions evaluate to

expression1 and expression2
5, then you get
expression1 and expression2
5. Otherwise, you get
expression1 and expression2
6. Especially as expressions get longer, you should keep in mind that Python evaluates the expressions sequentially from left to right.

Remove ads

Short-Circuiting the Evaluation

Python’s logical operators, such as

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 and
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1, use something called short-circuit evaluation, or lazy evaluation. In other words, Python evaluates the operand on the right only when it needs to.

To determine the final result of an

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression, Python starts by evaluating the left operand. If it’s false, then the whole expression is false. In this situation, there’s no need to evaluate the operand on the right. Python already knows the final result.

Having a false left operand automatically makes the whole expression false. It would be a waste of CPU time to evaluate the remaining operand. Python prevents this by short-circuiting the evaluation.

In contrast, the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator evaluates the operand on the right only if the first operand is true. In this case, the final result depends on the right operand’s truth value. If it’s true, then the whole expression is true. Otherwise, the expression is false.

To demonstrate the short-circuiting feature, take a look at the following examples:

>>>

>>> def true_func():
...     print("Running true_func()")
...     return True
...

>>> def false_func():
...     print("Running false_func()")
...     return False
...

>>> true_func() and false_func()  # Case 1
Running true_func()
Running false_func()
False

>>> false_func() and true_func()  # Case 2
Running false_func()
False

>>> false_func() and false_func()  # Case 3
Running false_func()
False

>>> true_func() and true_func()  # Case 4
Running true_func()
Running true_func()
True

Here’s how this code works:

  • Case 1: Python evaluates
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    03, which returns
    expression1 and expression2
    
    5. To determine the final result, Python evaluates
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    05 and gets
    expression1 and expression2
    
    6. You can confirm this by seeing both functions’ output.
  • Case 2: Python evaluates
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    05, which returns
    expression1 and expression2
    
    6. Python already knows that the final result is
    expression1 and expression2
    
    6, so it doesn’t evaluate
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    03.
  • Case 3: Python runs
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    05 and gets
    expression1 and expression2
    
    6 as a result. It doesn’t need to evaluate the repeated function a second time.
  • Case 4: Python evaluates
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    03 and gets
    expression1 and expression2
    
    5 as a result. It then evaluates the function again. Since both operands evaluate to
    expression1 and expression2
    
    5, the final result is
    expression1 and expression2
    
    5.

Python processes Boolean expressions from left to right. It stops when it no longer needs to evaluate any further operands or subexpressions to determine the final outcome. To sum up this concept, you should remember that if the left operand in an

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression is false, then the right operand won’t be evaluated.

Short-circuit evaluation can have a significant impact on your code’s performance. To take advantage of that, consider the following tips when you’re building

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expressions:

  • Place time-consuming expressions on the right of the
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 keyword. This way, the costly expression won’t run if the short-circuit rule takes effect.
  • Place the expression that is more likely to be false on the left of the
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 keyword. This way, it’s more likely that Python can determine if the whole expression is false by evaluating the left operand only.

Sometimes you may want to avoid lazy evaluation in a specific Boolean expression. You can do so by using the bitwise operators (

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
21,
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
22,
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
23). These operators also work in Boolean expressions, but they evaluate the operands eagerly:

>>>

>>> def true_func():
...     print("Running true_func()")
...     return True
...

>>> def false_func():
...     print("Running false_func()")
...     return False
...

>>> # Use logical and
>>> false_func() and true_func()
Running false_func()
False

>>> # Use bitwise and
>>> false_func() & true_func()
Running false_func()
Running true_func()
False

In the first expression, the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator works lazily, as expected. It evaluates the first function, and since the result is false, it doesn’t evaluate the second function. In the second expression, however, the bitwise AND operator (
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
21) calls both functions eagerly even though the first function returns
expression1 and expression2
6. Note that in both cases, the final result is
expression1 and expression2
6.

Even though this trick works, it’s generally discouraged. You should use bitwise operators to manipulate bits, and Boolean operators to work with Boolean values and expressions. For a deeper dive into bitwise operators, check out Bitwise Operators in Python.

Using Python’s >>> 5 > 3 and 5 == 3 + 2 True >>> 5 < 3 and 5 == 5 False >>> 5 == 5 and 5 != 5 False >>> 5 < 3 and 5 != 5 False 0 Operator With Common Objects

You can use the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator to combine two Python objects in a single expression. In that situation, Python internally uses to determine the truth value of the operands. As a result, you get a specific object rather than a Boolean value. You only get
expression1 and expression2
5 or
expression1 and expression2
6 if a given operand explicitly evaluates to
expression1 and expression2
5 or
expression1 and expression2
6:

>>>

>>> 2 and 3
3

>>> 5 and 0.0
0.0

>>> [] and 3
[]

>>> 0 and {}
0

>>> False and ""
False

In these examples, the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression returns the operand on the left if it evaluates to
expression1 and expression2
6. Otherwise, it returns the operand on the right. To produce these results, the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator uses Python’s internal rules to determine an object’s truth value. The Python documentation states these rules like this:

By default, an object is considered true unless its class defines either a method that returns

expression1 and expression2
6 or a method that returns zero, when called with the object. Here are most of the built-in objects considered false:

  • constants defined to be false:
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    41 and
    expression1 and expression2
    
    6.
  • zero of any numeric type:
    >>> 5 > 3 and 5 == 3 + 2 and 5 != 3
    True
    
    >>> 5 < 3 and 5 == 3 and 5 != 3
    False
    
    3,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    44,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    45,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    46,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    47
  • empty sequences and collections:
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    48,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    49,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    50,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    51,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    52,
    >>> True and True
    True
    
    >>> False and False
    False
    
    >>> True and False
    False
    
    >>> False and True
    False
    
    53

()

With these rules in mind, look again at the code above. In the first example, the integer number

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
54 is true (nonzero), so
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 returns the right operand,
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
56. In the second example,
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
57 is true, so
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 returns the right operand even though it evaluates to
expression1 and expression2
6.

The next example uses an empty list (

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
50) as the left operand. Since empty lists evaluate to false, the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression returns the empty list. The only case where you get
expression1 and expression2
5 or
expression1 and expression2
6 is the one where you use a Boolean object explicitly in the expression.

Note: If you need to get

expression1 and expression2
5 or
expression1 and expression2
6 from an
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression involving common objects rather than Boolean expressions, then you can use
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
30. This built-in function explicitly returns
expression1 and expression2
5 or
expression1 and expression2
6 depending on the truth value of the specific object you provide as an argument.

Here’s how you can summarize the behavior of the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator when you use it with common Python objects instead of Boolean expressions. Note that Python uses the truth value of each object to determine the final result:

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
71
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
72
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
73FalseFalse
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
71FalseTrue
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
71TrueTrue
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
72TrueFalse
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
72

In general, if the operands in an

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression are objects instead of Boolean expressions, then the operator returns the object on the left if it evaluates to
expression1 and expression2
6. Otherwise, it returns the object on the right, even when it evaluates to
expression1 and expression2
6.

Remove ads

Mixing Boolean Expressions and Objects

You can also combine Boolean expressions and common Python objects within an

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression. In that situation, the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression still returns the left operand if it’s false, or else it returns the right operand. The returned value could be
expression1 and expression2
5,
expression1 and expression2
6, or a regular object, depending on which part of the expression provides that result:

>>>

>>> 2 < 4 and 2
2
>>> 2 and 2 < 4
True

>>> 2 < 4 and []
[]
>>> [] and 2 < 4
[]

>>> 5 > 10 and {}
False
>>> {} and 5 > 10
{}

>>> 5 > 10 and 4
False
>>> 4 and 5 > 10
False

These examples use a combination of Boolean expressions and common objects. In each pair of examples, you see that you can get either a non-Boolean object or a Boolean value,

expression1 and expression2
5 or
expression1 and expression2
6. The result will depend on which part of the expression provides the final result.

Here’s a table that summarizes the behavior of the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator when you combine Boolean expressions and common Python objects:

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
88
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
89
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
90
expression1 and expression2
5True
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
89
expression1 and expression2
5False
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
89
expression1 and expression2
6False
expression1 and expression2
6
expression1 and expression2
6True
expression1 and expression2
6

To find out what’s returned, Python evaluates the Boolean expression on the left to get its Boolean value (

expression1 and expression2
5 or
expression1 and expression2
6). Then Python uses its internal rules to determine the truth value of the object on the right.

As an exercise to test your understanding, you could try to rewrite this table by swapping the order of the operands in the third column to

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
01. Try to predict what will be returned in each row.

Combining Python Logical Operators

As you’ve seen earlier in this tutorial, Python provides two additional logical operators: the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1 operator and the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
2 operator. You can use them along with the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator to create more complex compound expressions. If you want to make accurate and clear expressions with multiple logical operators, then you need to consider the of each operator. In other words, you need to consider the order in which Python executes them.

Python’s logical operators have low precedence when compared with other operators. However, sometimes it’s healthy to use a pair of parentheses (

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
49) to ensure a consistent and readable result:

>>>

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
0

These examples combine the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1 operator and the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator in a compound expression. Just like the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator, the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1 operator uses short-circuit evaluation. Unlike
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0, however, the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1 operator stops once it finds a true operand. You can see this in the first example. Since
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
57 is true, the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1 subexpression immediately returns
>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
57 without evaluating the rest of the expression.

In contrast, if you enclose the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1 subexpression in a pair of parentheses, then it works as a single true operand, and
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
16 gets evaluated as well. The final result is
expression1 and expression2
5.

The takeaway is that if you’re using multiple logical operators in a single expression, then you should consider using parentheses to make your intentions clear. This trick will also help you get the correct logical result.

Using Python’s >>> 5 > 3 and 5 == 3 + 2 True >>> 5 < 3 and 5 == 5 False >>> 5 == 5 and 5 != 5 False >>> 5 < 3 and 5 != 5 False 0 Operator in Boolean Contexts

Like all of Python’s Boolean operators, the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator is especially useful in Boolean contexts. Boolean contexts are where you’ll find most of the real-world use cases of Boolean operators.

Two main structures define Boolean contexts in Python:

  1. >>> 5 == 3 + 2
    True
    >>> 5 > 3
    True
    >>> 5 < 3
    False
    >>> 5 != 3
    True
    
    >>> [5, 3] == [5, 3]
    True
    
    >>> "hi" == "hello"
    False
    
    20 statements let you perform conditional execution and take different courses of action based on the result of some initial conditions.
  2. >>> 5 == 3 + 2
    True
    >>> 5 > 3
    True
    >>> 5 < 3
    False
    >>> 5 != 3
    True
    
    >>> [5, 3] == [5, 3]
    True
    
    >>> "hi" == "hello"
    False
    
    21 loops let you perform conditional iteration and run repetitive tasks while a given condition is true.

These two structures are part of what you’d call control flow statements. They help you decide your programs’ execution path.

You can use Python’s

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator to construct compound Boolean expressions in both
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 statements and
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
21 loops.

Remove ads

>>> 5 == 3 + 2 True >>> 5 > 3 True >>> 5 < 3 False >>> 5 != 3 True >>> [5, 3] == [5, 3] True >>> "hi" == "hello" False 20 Statements

Boolean expressions are commonly known as conditions because they typically imply the need for meeting a given requirement. They’re pretty useful in the context of conditional statements. In Python, this type of statement starts with the and continues with a condition. A conditional statement can additionally include

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
27 and
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
28 clauses.

Python conditional statements follow the logic of conditionals in English grammar. If the condition is true, then the

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 code block executes. Otherwise, the execution jumps to a different code block:

>>>

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
1

Since

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
30 holds a negative number, the condition
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
31 is true. The
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 code block runs, and you get the message
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
33 printed on your screen. If you change the value of
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
30 to a positive number, however, then the
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
27 block runs and Python prints
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
36. Finally, if you set
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
30 to zero, then the
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
28 code block executes. Go ahead and play with
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
30 to see what happens!

Now say you want to make sure that two conditions are met—meaning that they’re both true—before running a certain piece of code. To try this out, suppose you need to get the age of a user running your script, process that information, and display to the user their current life stage.

Fire up your favorite code editor or IDE and create the following script:

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
2

Here, you get the user’s age using and then convert it to an integer number with . The

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 clause checks if
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
43 is greater than or equal to
>>> 5 > 3 and 5 == 3 + 2 and 5 != 3
True

>>> 5 < 3 and 5 == 3 and 5 != 3
False
3. In the same clause, it checks if
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
43 is less than or equal to
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
46. To do this, you build an
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 compound Boolean expression.

The three

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
27 clauses check other intervals to determine the life stage associated with the user’s age.

If you run this script from your command line, then you get something like this:

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
3

Depending on the age you enter at the command line, the script takes one course of action or another. In this specific example, you provide an age of 25 years and get the message

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
49 printed to your screen.

>>> 5 == 3 + 2 True >>> 5 > 3 True >>> 5 < 3 False >>> 5 != 3 True >>> [5, 3] == [5, 3] True >>> "hi" == "hello" False 21 Loops

The

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
21 loop is the second construct that can use
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expressions to control a program’s execution flow. By using the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator in the
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
21 statement header, you can test several conditions and repeat the loop’s code block for as long as all conditions are met.

Say you’re prototyping a control system for a manufacturer. The system has a critical mechanism that should work with a pressure of 500 psi or lower. If the pressure goes over 500 psi while staying under 700 psi, then the system has to run a given series of standard safety actions. For pressures greater than 700 psi, there are a whole new set of safety actions that the system must run.

To approach this problem, you can use a

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
21 loop with an
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression. Here’s a script that simulates a possible solution:

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
4

Inside

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
57, you create an infinite
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
21 loop on line 8. If the system is stable and the pressure is below 500 psi, the conditional statement breaks out of the loop and the program finishes.

On line 12, the first nested

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
21 loop runs the standard safety actions while the system pressure stays between 500 psi and 700 psi. In each iteration, the loop gets a new pressure measurement to test the condition again in the next iteration. If the pressure grows beyond 700 psi, then the second loop on line 16 runs the critical safety actions.

Note: The implementation of

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
57 in the example above is intended to show how the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator can work in the context of a
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
21 loop.

However, this isn’t the most efficient implementation you can write. You can refactor

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
57 to use a single loop without using
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0:

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
5

In this alternative implementation, instead of using

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0, you use the chained expression
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
66, which does the same as
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
67 but is cleaner and more Pythonic. Another advantage is that you call
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
68 only once, which ends up being more efficient.

To run this script, open up your command line and enter the following command:

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
6

The output on your screen should be a little different from this sample output, but you can still get an idea of how the application works.

Remove ads

Using Python’s >>> 5 > 3 and 5 == 3 + 2 True >>> 5 < 3 and 5 == 5 False >>> 5 == 5 and 5 != 5 False >>> 5 < 3 and 5 != 5 False 0 Operator in Non-Boolean Contexts

The fact that

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 can return objects besides just
expression1 and expression2
5 and
expression1 and expression2
6 is an interesting feature. For example, this feature allows you to use the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator for conditional execution. Say you need to update a
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
74 variable if the first item in a given list is equal to a certain expected value. For this situation, you can use a conditional statement:

>>>

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
7

Here, the conditional checks if the list has at least one item. If so, it checks if the first item in the list is equal to the

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
75 string. If both checks pass, then
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
74 changes to
expression1 and expression2
5. You can simplify this code by taking advantage of the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator:

>>>

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
8

In this example, the highlighted line does all the work. It checks both conditions and makes the corresponding assignment in one go. This expression takes the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator out of the
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 statement you used in the previous example, which means that you’re not working in a Boolean context any longer.

The code in the example above is more concise than the equivalent conditional statement you saw before, but it’s less readable. To properly understand this expression, you’d need to be aware of how the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator works internally.

Putting Python’s >>> 5 > 3 and 5 == 3 + 2 True >>> 5 < 3 and 5 == 5 False >>> 5 == 5 and 5 != 5 False >>> 5 < 3 and 5 != 5 False 0 Operator Into Action

So far, you’ve learned how to use Python’s

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator for creating compound Boolean expressions and non-Boolean expressions. You’ve also learned how to use this logical operator in Boolean contexts like
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 statements and
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
21 loops.

In this section, you’ll build a few practical examples that’ll help you decide when to use the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator. With these examples, you’ll learn how to take advantage of
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 for writing better and more Pythonic code.

Flattening Nested >>> 5 == 3 + 2 True >>> 5 > 3 True >>> 5 < 3 False >>> 5 != 3 True >>> [5, 3] == [5, 3] True >>> "hi" == "hello" False 20 Statements

One principle from the states that “Flat is better than nested.” For example, while code that has two levels of nested

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 statements is normal and totally okay, your code really starts to look messy and complicated when you have more than two levels of nesting.

Say you need to test if a given number is positive. Then, once you confirm that it’s positive, you need to check if the number is lower than a given positive value. If it is, you can proceed with a specific calculation using the number at hand:

>>>

>>> True and True
True

>>> False and False
False

>>> True and False
False

>>> False and True
False
9

Cool! These two nested

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 statements solve your problem. You first check if the number is positive and then check if it’s lower than
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
91. In this small example, the call to
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
92 is a placeholder for your specific calculation, which runs only if both conditions are true.

Even though the code works, it’d be nice to make it more Pythonic by removing the nested

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20. How can you do that? Well, you can use the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator to combine both conditions in a single compound condition:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
0

Logical operators like the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator often provide an effective way to improve your code by removing nested conditional statements. Take advantage of them whenever possible.

In this specific example, you use

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 to create a compound expression that checks if a number is in a given range or interval. Python provides an even better way to perform this check by chaining expressions. For example, you can write the condition above as
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
97. That’s a topic for the following section.

Checking Numeric Ranges

With a close look at the example in the section below, you can conclude that Python’s

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator is a convenient tool for checking if a specific numeric value is inside a given interval or range. For example, the following expressions check if a number
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
99 is between
>>> 5 > 3 and 5 == 3 + 2 and 5 != 3
True

>>> 5 < 3 and 5 == 3 and 5 != 3
False
3 and
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
91, both inclusive:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
1

In the first expression, the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator first checks if
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
99 is greater than or equal to
>>> 5 > 3 and 5 == 3 + 2 and 5 != 3
True

>>> 5 < 3 and 5 == 3 and 5 != 3
False
3. Since the condition is true, the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator checks if
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
99 is lower than or equal to
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
91. The final result is true because the second condition is also true. This means that the number is within the desired interval.

In the second example, the first condition is true, but the second is false. The general result is false, which means the number isn’t in the target interval.

You can enclose this logic in a function and make it reusable:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
2

In this example,

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
08 takes
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
09 as an argument. It also takes
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
10 and
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
11, which define the target interval. Note that these arguments have , which means they’re optional arguments.

Your

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
08 function returns the result of evaluating an
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression that checks if
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
09 is between
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
10 and
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
11, both inclusive.

Note: Unintentionally writing

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expressions that always return
expression1 and expression2
6 is a common mistake. Suppose you want to write an expression that excludes values between
>>> 5 > 3 and 5 == 3 + 2 and 5 != 3
True

>>> 5 < 3 and 5 == 3 and 5 != 3
False
3 and
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
91 from a given computation.

To achieve this result, you start with two Boolean expressions:

  1. >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    21
  2. >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    22

With these two expressions as a starting point, you think of using

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 to combine them in a single compound expression. However, no number is lower than
>>> 5 > 3 and 5 == 3 + 2 and 5 != 3
True

>>> 5 < 3 and 5 == 3 and 5 != 3
False
3 and greater than
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
91 at the same time, so you end up with an always-false condition:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
3

In this case,

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 is the wrong logical operator to approach the problem at hand. You should use the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
1 operator instead. Go ahead and give it a try!

Even though using the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator allows you to check gracefully if a number is within a given interval, there’s a more Pythonic technique to approach the same problem. In mathematics, you can write 0 < x < 10 to denote that x is between 0 and 10.

In most programming languages, this expression doesn’t make sense. In Python, however, the expression works like a charm:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
4

In a different programming language, this expression would start by evaluating

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
29, which is true. The next step would be to compare the true Boolean with
>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
91, which doesn’t make much sense, so the expression fails. In Python, something different happens.

Python internally rewrites this type of expression to an equivalent

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression, such as
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
32. It then performs the actual evaluation. That’s why you get the correct result in the example above.

Just like you can chain several subexpressions with multiple

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operators, you can also chain them without explicitly using any
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operators:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
5

You can also use this Python trick to check if several values are equal:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
6

Chained comparison expressions are a nice feature, and you can write them in various ways. However, you should be careful. In some cases, the final expression can be challenging to read and understand, especially for programmers coming from languages in which this feature isn’t available.

Remove ads

Chaining Function Calls Conditionally

If you’ve ever worked with Bash on a Unix system, then you probably know about the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
35 construct. This is a handy technique that allows you to run several commands in a chain. Each command runs if and only if the previous command was successful:

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
7

These examples use Bash’s short-circuit AND operator (

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
36) to make the execution of the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
37 command dependent on the success of the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
38 command.

Since Python’s

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 also implements the idea of lazy evaluation, you can use it to emulate this Bash trick. For example, you can chain a series of function calls in a single
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 expression like the following:

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
8

In this case, Python calls

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
41. If the function’s return value evaluates to a true value, then Python calls
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
42, and so on. If one of the functions returns a false value, then Python won’t call the rest of the functions.

Here’s an example that uses some

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
43 functions to manipulate a text file:

>>>

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
9

Nice! In a single line of code, you run three functions conditionally without the need for an

>>> 5 == 3 + 2
True
>>> 5 > 3
True
>>> 5 < 3
False
>>> 5 != 3
True

>>> [5, 3] == [5, 3]
True

>>> "hi" == "hello"
False
20 statement. In this specific example, the only visible difference is that returns the number of bytes it wrote to the file. The interactive shell automatically displays that value to the screen. Keep in mind that this difference isn’t visible when you run the code as a script.

Conclusion

Python’s

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator allows you to construct compound Boolean expressions that you can use to decide the course of action of your programs. You can use the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator to solve several problems both in Boolean or non-Boolean contexts. Learning about how to use the
>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator properly can help you write more Pythonic code.

In this tutorial, you learned how to:

  • Work with Python’s
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 operator
  • Build Boolean and non-Boolean expressions with Python’s
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 operator
  • Decide the course of action of your programs using the
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 operator in Boolean contexts
  • Make your code more concise using the
    >>> 5 > 3 and 5 == 3 + 2
    True
    
    >>> 5 < 3 and 5 == 5
    False
    
    >>> 5 == 5 and 5 != 5
    False
    
    >>> 5 < 3 and 5 != 5
    False
    
    0 operator in non-Boolean contexts

Going through the practical examples in this tutorial can help you get a general idea of how to use the

>>> 5 > 3 and 5 == 3 + 2
True

>>> 5 < 3 and 5 == 5
False

>>> 5 == 5 and 5 != 5
False

>>> 5 < 3 and 5 != 5
False
0 operator to make decisions in your Python code.

Mark as Completed

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using the Python and Operator

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

When determining if a number is outside a range its best to use this operator?

Send Me Python Tricks »

About Leodanis Pozo Ramos

When determining if a number is outside a range its best to use this operator?
When determining if a number is outside a range its best to use this operator?

Leodanis is an industrial engineer who loves Python and software development. He's a self-taught Python developer with 6+ years of experience. He's an avid technical writer with a growing number of articles published on Real Python and other sites.

» More about Leodanis


Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

When determining if a number is outside a range its best to use this operator?

Aldren

When determining if a number is outside a range its best to use this operator?

Bartosz

When determining if a number is outside a range its best to use this operator?

Sadie

Master Real-World Python Skills With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

Tweet Share Share Email

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. and get answers to common questions in our support portal.

When determining if a number is outside a range it's best to use this operator?

When determining whether a number is outside a range, it's best to use this logical operator. This operator connects two Boolean expressions into one. One, and only one, of the expressions must be true for the overall expression to be true.

When determining whether a number is inside a range which logical operator is it best to use?

Q: When determining whether a number is inside a range, which logical operator is it best to use? A: Logical "and" Operator: Logical operator combines the results of one or more conditions.

Which of the following logical operators should be used to reverse the value of an expression?

The functions AND and OR may be used to test two or more logical expressions, while the NOT function is used to reverse the truth value of a logical expression. The XOR function returns TRUE when one, and only one, of the logical expressions is true.

Which operator determines whether two expressions are both true?

Using the OR operator, we can create a compound expression that is true when either of two conditions are true. Imagine a program that determines whether a student is eligible to enroll in AP CS A.