2.2. Python Operators#
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 |
|
Arithmetic |
|
Mathematical operations |
|
Comparison |
|
Compare values, return bool |
|
Logical |
|
Boolean logic |
|
Identity |
|
Test object identity |
|
Membership |
|
Test membership in sequence |
|
Bitwise |
|
Bit-level operations |
|
A more detailed overview of all the operators in Python:
No. |
Type |
Operator |
Meaning |
Example |
Result |
|---|---|---|---|---|---|
1 |
Assignment |
|
Assign |
|
|
|
Add and assign |
|
|
||
|
Subtract and assign |
|
|
||
|
Multiply and assign |
|
|
||
|
Divide and assign |
|
|
||
|
Floor divide and assign |
|
|
||
|
Modulus and assign |
|
|
||
|
Exponent and assign |
|
|
||
|
Assignment expression (walrus operator) |
|
assign sum(data) to total |
||
2 |
Arithmetic |
|
Addition |
|
|
|
Subtraction |
|
|
||
|
Multiplication |
|
|
||
|
Division |
|
|
||
|
Floor Division |
|
|
||
|
Modulo |
|
|
||
|
Exponentiation |
|
|
||
3 |
Comparison |
|
Equal to |
|
|
|
Not equal to |
|
|
||
|
Greater than |
|
|
||
|
Less than |
|
|
||
|
Greater than or equal |
|
|
||
|
Less than or equal |
|
|
||
Membership |
|
Member of |
|
|
|
|
Not member of |
|
|
||
Identity |
|
Same object |
|
Varies |
|
|
Different object |
|
Varies |
||
4 |
Logical |
|
Logical AND |
|
|
|
Logical OR |
|
|
||
|
Logical NOT |
|
|
||
5 |
Bitwise |
|
Bitwise AND |
|
|
|
Bitwise OR |
|
|
||
|
Bitwise XOR |
|
|
||
|
Bitwise NOT |
|
|
||
|
Left shift |
|
|
||
|
Right shift |
|
|
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 |
|
— |
|
Add and assign |
|
|
|
Subtract and assign |
|
|
|
Multiply and assign |
|
|
|
Divide and assign |
|
|
|
Floor divide and assign |
|
|
|
Modulus and assign |
|
|
|
Exponent and assign |
|
|
|
Walrus (assign in expr) |
|
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:
The plus sign,
+, performs addition.The minus sign,
-, is the operator that performs subtraction.The asterisk,
*, performs multiplication.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.The integer/floor division operator,
//, is called floor division because it always rounds down (toward the “floor”).The modulus operator
%returns the remainder after division.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 |
|---|---|---|---|---|
|
Binary |
|
|
|
|
Binary |
|
|
|
|
Unary |
Inverts the boolean value |
|
|
Truth table:
|
|
|
|
|
|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
innot in
Use the membership operators, in and not in, to check whether a value is in a collection.
Operator |
Type |
Example |
Description |
|---|---|---|---|
|
Membership |
|
Is value inside collection? |
|
Non-membership |
|
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 |
|---|---|---|
|
return |
|
|
return |
|
Key difference:
==checks if values are equal;ischecks if they are the same object (same memory address).
Useismainly for comparing againstNone,True, orFalse.
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 |
|
|
Returns 1 only when both bits are 1 (5=0101, 3=0011 → 0001) |
2 |
|
OR |
|
|
Returns 1 when at least one bit is 1 (5=0101, 3=0011 → 0111) |
3 |
|
XOR |
|
|
Returns 1 when bits are different (5=0101, 3=0011 → 0110) |
4 |
|
NOT |
|
|
Inverts all bits in two’s complement representation |
5 |
|
Left shift |
|
|
Shifts bits left, adding 0s on right; multiply by 2 |
6 |
|
Right shift |
|
|
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 |
|
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 |
|
Identity and membership operators |
|
11 |
|
Logical NOT |
|
12 |
|
Logical AND |
|
13 |
|
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 ** 2equals2 ** 9=512Comparison operators all have the same precedence and are evaluated left-to-right
Logical operators follow the order:
not→and→orWhen 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