2.2. Python Operators#

Hide code cell source

import sys
from pathlib import Path

current = Path.cwd()
for parent in [current, *current.parents]:
    if (parent / '_config.yml').exists():
        project_root = parent  # ← Add project root, not chapters
        break
else:
    project_root = Path.cwd().parent.parent

sys.path.insert(0, str(project_root))

from shared import thinkpython, diagram, jupyturtle, download

# Register as top-level modules so direct imports work in subsequent cells
sys.modules['thinkpython'] = thinkpython
sys.modules['diagram'] = diagram
sys.modules['jupyturtle'] = jupyturtle
sys.modules['download'] = download

2.2.1. Operators#

In programming languages, operators are special symbols that perform computations or logical comparisons between values. They form the backbone of most expressions — whether you’re performing arithmetic, comparing data, assigning values, or testing relationships between objects. (For a detailed discussion of Expressions and Operators, see Python Reference/Expressions )

The table below summarizes all Python operators.

Category

Operators

Description

Example

Assignment

= += -= *= /= //= %= **=

Assign and update values

x = 5, x += 3

Arithmetic

+ - * / // % **

Mathematical operations

5 + 3, 5 // 2, 2 ** 3

Comparison

== != > < >= <=

Compare values, return bool

5 > 3, x == y

Logical

and or not

Boolean logic

True and False, not True

Identity

is is not

Test object identity

x is None, a is not b

Membership

in not in

Test membership in sequence

'a' in 'cat', 5 not in [1,2,3]

Bitwise

& | ^ ~ << >>

Bit-level operations

5 & 3, 5 << 1

A more detailed overview of all the operators in Python:

No.

Type

Operator

Meaning

Example

Result

1

Assignment

=

Assign

x = 5

x is 5

+=

Add and assign

x += 3

x = x + 3

-=

Subtract and assign

x -= 3

x = x - 3

*=

Multiply and assign

x *= 3

x = x * 3

/=

Divide and assign

x /= 3

x = x / 3

//=

Floor divide and assign

x //= 3

x = x // 3

%=

Modulus and assign

x %= 3

x = x % 3

**=

Exponent and assign

x **= 3

x = x ** 3

:=

Assignment expression (walrus operator)

total := sum(data)

assign sum(data) to total

2

Arithmetic

+

Addition

5 + 3

8

-

Subtraction

5 - 3

2

*

Multiplication

5 * 3

15

/

Division

5 / 2

2.5

//

Floor Division

5 // 2

2

%

Modulo

5 % 2

1

**

Exponentiation

5 ** 2

25

3

Comparison

==

Equal to

5 == 5

True

!=

Not equal to

5 != 3

True

>

Greater than

5 > 3

True

<

Less than

5 < 3

False

>=

Greater than or equal

5 >= 5

True

<=

Less than or equal

5 <= 3

False

Membership

in

Member of

'a' in 'cat'

True

not in

Not member of

'x' not in 'cat'

True

Identity

is

Same object

x is y

Varies

is not

Different object

x is not y

Varies

4

Logical

and

Logical AND

True and False

False

or

Logical OR

True or False

True

not

Logical NOT

not True

False

5

Bitwise

&

Bitwise AND

5 & 3

1

|

Bitwise OR

5 | 3

7

^

Bitwise XOR

5 ^ 3

6

~

Bitwise NOT

~5

-6

<<

Left shift

5 << 1

10

>>

Right shift

5 >> 1

2

2.2.2. Assignment Operators#

Assignment operators are used to assign or update the value of a variable. The basic assignment operator is =, which stores a value in a variable. Python also provides augmented assignment operators that combine an arithmetic operation with assignment, making code more concise.

Operator

Meaning

Example

Equivalent to

=

Assign

x = 5

+=

Add and assign

x += 3

x = x + 3

-=

Subtract and assign

x -= 3

x = x - 3

*=

Multiply and assign

x *= 3

x = x * 3

/=

Divide and assign

x /= 3

x = x / 3

//=

Floor divide and assign

x //= 3

x = x // 3

%=

Modulus and assign

x %= 3

x = x % 3

**=

Exponent and assign

x **= 3

x = x ** 3

:=

Walrus (assign in expr)

(n := 10)

assigns and returns value

x = 10          # basic assignment
print(x)

x += 3          # x = x + 3
print(x)        # 13

x -= 2          # x = x - 2
print(x)        # 11

x *= 4          # x = x * 4
print(x)        # 44

x //= 3         # x = x // 3
print(x)        # 14

x %= 5          # x = x % 5
print(x)        # 4

x **= 2         # x = x ** 2
print(x)        # 16

# Walrus operator := assigns and returns in one step
numbers = [1, 2, 3, 4, 5]
if (n := len(numbers)) > 3:
    print(f"List is long: {n} elements")  # List is long: 5 elements
10
13
11
44
14
4
16
List is long: 5 elements

2.2.3. Arithmetic Operators#

An arithmetic operator is a symbol that represents an arithmetic computation. For example:

  1. The plus sign, +, performs addition.

  2. The minus sign, -, is the operator that performs subtraction.

  3. The asterisk, *, performs multiplication.

  4. The forward slash, /, performs division. Note that in modern Python (Python 3+), the division operator / always returns a floating-point number, even if the result is a whole number.

  5. The integer/floor division operator, //, is called floor division because it always rounds down (toward the “floor”).

  6. The modulus operator % returns the remainder after division.

  7. The operator ** performs exponentiation; that is, it raises a number to a power. In other languages, such as R, MATLAB, Julia, and Excel, the caret ^ is used for exponentiation.

a = 10 + 3
b = 10 - 3
c = 10 * 3
d = 10 / 3
e = 10 // 3
f = 10 % 3
g = 10 ** 3

print(a, b, c, d, e, f, g, sep="\n")
13
7
30
3.3333333333333335
3
1
1000

2.2.3.1. Integer division and modulus#

Recall that the integer division operator, //, divides two numbers and rounds down to an integer. For example, suppose the run time of a movie is 105 minutes. You might want to know how long that is in hours.

Conventional division returns a floating-point number:

minutes = 105
minutes / 60
1.75

But we don’t normally write hours with decimal points. Integer division returns the integer number of hours, rounding down:

minutes = 105
hours = minutes // 60
hours
1

To get the remainder, you could subtract off one hour in minutes:

remainder = minutes - hours * 60
remainder
45

Or you could use the modulus operator, %, which divides two numbers and returns the remainder.

remainder = minutes % 60
remainder
45

The modulus operator is more useful than it might seem. For example, it can check whether one number is divisible by another – if x % y is zero, then x is divisible by y.

Also, it can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly, x % 100 yields the last two digits.

x = 123
x % 10
3
x % 100
23

Finally, the modulus operator can do “clock arithmetic”. For example, if an event starts at 11 AM and lasts three hours, we can use the modulus operator to figure out what time it ends.

start = 11
duration = 3
end = (start + duration) % 12
end
2

The event would end at 2 PM.

2.2.4. Comparison Operators#

Comparison operators are used to compare values in Python. They allow you to check relationships between variables, such as equality, inequality, and ordering. These operators return a Boolean value (True or False) based on the result of the comparison. Understanding comparison operators is essential for writing conditional statements and controlling program flow.

x = 5
y = 10

print(x == y)   # False, checks equality
print(x != y)   # True, checks inequality
print(x < y)    # True, less than
print(x > y)    # False, greater than
print(x <= y)   # True, less than or equal to
print(x >= y)   # False, greater than or equal to
False
True
True
False
True
False

2.2.5. Logical operators#

Logical operators (also called Boolean operators) combine or negate boolean expressions and return a True or False result. Python provides three logical operators:

Operator

Type

Description

Example

Result

and

Binary

True only if both operands are True

True and False

False

or

Binary

True if at least one operand is True

True or False

True

not

Unary

Inverts the boolean value

not True

False

Truth table:

a

b

a and b

a or b

not a

True

True

True

True

False

True

False

False

True

False

False

True

False

True

True

False

False

False

False

True

The and operator returns True only when both conditions are true. For example, the expression below is True only if x is greater than 0 and less than 10:

x = 5
x > 0 and x < 10
True

The following expression is True if either or both* of the conditions is true, that is, if the number is divisible by 2 or 3:

x % 2 == 0 or x % 3 == 0
False

Finally, the not operator negates a boolean expression, so the following expression is True if x > y is False.

not x > y
True

2.2.5.1. Truthy/Falsy#

Strictly speaking, the operands of a logical operator should be boolean expressions, but Python is not very strict: Every value has an inherent Boolean evaluation, which can either be considered True or False in a Boolean context. Values that evaluate to True are called truthy, and values that evaluate to False are called falsy.

  • Truthy values:

    • Any nonzero number

    • Non-empty sequences or collections: e.g., string, list, dictionary

    • Constant: True

  • Flasy values:

    • Empty sequences and collections

    • Zero value numbers: 0, 0.0, 0j

    • Constants: None, False

This flexibility can be useful, but there are subtleties that can be confusing. So, while you need to know what how it works, you might want to avoid using it.

print(bool('42'))        ### how can a string be True??? ==> It is in Python.
print(42 and True)       ### 42 is true and True if of course True; numbers are True if they are not zero.
print('42' and True)     ### True and True

print(bool(0))
s1 = "  "
s2 = ""
print(bool(s1))
print(bool(s2))
True
True
True
False
True
False

Instead of this:

if len(my_list) > 0:
    print("Has elements")

use this:

if my_list:
    print("Has elements")

Or, short-circuit evaluation : (Short-circuit evaluation is “the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true [Wikipedia Contributors, 2026].”)

Or, short-circuit evaluation : (Short-circuit evaluation is “the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true [Wikipedia Contributors, 2026].”)

result = "" or "default"
print(result)
default
if 3333:
    print("What? This is True")
What? This is True
if "":
    print("Runs")
else:
    print("Empty string is falsy")
Empty string is falsy
### Exercise: what's the output?

a = 6
b = 10
print( not (b == 6) )
True
### Exercise: what's the output?

a = 6
b = 10
print( a == 10 or b == 10 )
True

2.2.5.2. Membership Operators#

In Python, membership means checking whether a value is present in a container/collection type (such as list, tuple dictionary, set, string) object. Python provides two membership operators:

  • in

  • not in

Use the membership operators, in and not in, to check whether a value is in a collection.

Operator

Type

Example

Description

in

Membership

3 in [1,2,3]

Is value inside collection?

not in

Non-membership

4 not in [1,2,3]

Is value NOT inside collection?

Let’s check the membership of some elements in containers.

numbers = [2, 1, 3, 4, 7]

# Check if value is in the list
print(3 in numbers)        # True
print(10 in numbers)       # False
print(10 not in numbers)   # True

# Works with strings too
name = "Chen"
print("C" in name)         # True
print("z" in name)         # False

# Works with dictionaries (checks keys)
person = {"name": "Chen", "age": 25}
print("name" in person)    # True
print("Chen" in person)    # False (it's a value, not a key)
True
False
True
True
False
True
False

2.2.5.3. Identity Operators#

Identity operators test whether two variables refer to the same object in memory — not just whether they have equal values. This is a subtle but important distinction from ==.

Operator

Description

Example

is

return True if both point to the same object

a is None

is not

return True if they point to different objects

a is not None

Key difference: == checks if values are equal; is checks if they are the same object (same memory address).
Use is mainly for comparing against None, True, or False.

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)       # True  — same values
print(a is b)       # False — different objects in memory
print(a is c)       # True  — c points to the same object as a

# Most common use: check for None
x = None
print(x is None)    # True  (preferred over x == None)
print(x is not None)  # False
True
False
True
True
False

2.2.6. Bitwise Operations#

Bitwise operations are used for low-level programming tasks that require efficient memory manipulation of individual bits, such as optimizing arithmetic operations, flagging file permissions, and hashing.

For an example of Bitwise operations, let’s take a look at Bitwise AND(&). The bitwise AND operation returns 1 only if both bits are 1. Here we have numbers A (0011, decimal 3) and B (0101, or 5 in decimal). As seen in the Truth Table below, we have 1 (0001) as the result of this Bitwise AND (&) operation:

Truth Table:

A

B

A & B

0

0

0

0

1

0

1

0

0

1

1

1

Bitwise Operation Examples

No.

Operator

Name

Example

Result

Explanation

1

&

AND

5 & 3

1

Returns 1 only when both bits are 1 (5=0101, 3=0011 → 0001)

2

|

OR

5 | 3

7

Returns 1 when at least one bit is 1 (5=0101, 3=0011 → 0111)

3

^

XOR

5 ^ 3

6

Returns 1 when bits are different (5=0101, 3=0011 → 0110)

4

~

NOT

~5

-6

Inverts all bits in two’s complement representation

5

<<

Left shift

5 << 1

10

Shifts bits left, adding 0s on right; multiply by 2

6

>>

Right shift

5 >> 1

2

Shifts bits right, removing rightmost bits; divide by 2

The code looks like below:

a = 5   # binary 0101
b = 3   # binary 0011

bw1 = a & b   # 1  (binary 0001)
bw2 = a | b   # 7  (binary 0111)
bw3 = a ^ b   # 6  (binary 0110)
bw4 = ~a      # -6 (two's complement)
bw5 = a << 1  # 10 (binary 1010)
bw6 = a >> 1  # 2  (binary 10)

print(bw1, bw2, bw3, bw4, bw5, bw6, sep='\n')
1
7
6
-6
10
2

As an example, we can use the bitwise operation to check if a number is even because if a binary number’s last digit is 0, then it is an even number:

def is_even(n):
    return (n & 1) == 0

x = is_even(3)       ### 011
y = is_even(5)       ### 101
z = is_even(6)       ### 110
z2 = is_even(8)      ### 1000
print(x, y, z, z2)
False False True True

As another example, when Linux/UNIX systems check if a user has write permission as an owner:

110                        ### (rw-)
010                        ### (w)
---
010                        ### not zero → write exists 

Or, for getting a network address using bitwise AND (&):

IP:         192.168.010.025  -> 11000000.10101000.00001010.00011001
Subnet:     255.255.255.0    -> 11111111.11111111.11111111.00000000

11000000.10101000.00001010.00011001   (IP)
11111111.11111111.11111111.00000000   (MASK)
-----------------------------------
11000000.10101000.00001010.00000000   (NETWORK)

Network Address: 192.168.10.0

2.2.7. Operator Precedence#

Operator precedence determines the order in which operations are evaluated in an expression. Operations with higher precedence are performed before those with lower precedence. When in doubt, use the parentheses () to ensure you have the preferred precedence.

Notice that exponentiation happens before addition because exponentiation is the 2nd highest precedence. This actually follows the order of operations you might have learned in a math class: exponentiation happens before multiplication and division, which happen before addition and subtraction.

Below is a comprehensive list of operator precedence:

Precedence Level

Operator(s)

Description / Example

1 (Highest)

()

Parentheses — control order of evaluation

2

**

Exponentiation

3

+x, -x, ~x

Unary plus, unary minus, bitwise NOT

4

*, /, //, %

Multiplication, division, floor division, modulus

5

+, -

Addition, subtraction

6

<<, >>

Bitwise left and right shift

7

&

Bitwise AND

8

^, |

Bitwise XOR, bitwise OR

9

Comparison: <, <=, >, >=, !=, ==

Relational and equality checks

10

is, is not, in, not in

Identity and membership operators

11

not

Logical NOT

12

and

Logical AND

13

or

Logical OR

14 (Lowest)

Assignment: =, +=, -=, *=, /=, //=, %= , **=

Assignment and augmented assignment

Note:

  • Always use parentheses when precedence is unclear to improve code readability

  • Exponentiation (**) is evaluated right-to-left: 2 ** 3 ** 2 equals 2 ** 9 = 512

  • Comparison operators all have the same precedence and are evaluated left-to-right

  • Logical operators follow the order: notandor

  • When operators have the same precedence, they are typically evaluated left-to-right (left-associative), except for exponentiation which is right-associative.

In the following example, multiplication happens before addition, and exponentiation happens before multiplication.

num1 = 12 + 5 * 6
num2 = (12 + 5) * 6

x, y, z = 1, 2, 3

result = x + y * z ** 2
print(result)               ### output: 13
19
numbers = [2, 1, 3, 4, 7]

# Check if value is in the list
print(3 in numbers)        # True
print(10 in numbers)       # False
print(10 not in numbers)   # True

# Works with strings too
name = "Chen"
print("C" in name)         # True
print("z" in name)         # False

# Works with dictionaries (checks keys)
person = {"name": "Chen", "age": 25}
print("name" in person)    # True
print("Chen" in person)    # False (it's a value, not a key)
True
False
True
True
False
True
False