What Are Operators?
Operators are symbols or keywords that tell Python to perform an operation.
For example:
a = 10
b = 3
print(a + b)Here, + is an operator that adds two values.
Python has several types of operators, including:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Membership operators
- Identity operators
- Bitwise operators
Let's look at each one.
Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 3 |
13 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 10 * 3 |
30 |
/ |
Division | 10 / 3 |
3.333... |
% |
Modulus | 10 % 3 |
1 |
// |
Floor division | 10 // 3 |
3 |
** |
Exponentiation | 10 ** 3 |
1000 |
Addition +
The + operator adds two values.
a = 10
b = 5
print(a + b)
# Output:
# 15It can also be used with strings to join them together:
first_name = "John"
last_name = "Doe"
print(first_name + " " + last_name)
# Output:
# John DoeSubtraction -
The - operator subtracts one value from another.
a = 10
b = 3
print(a - b)
# Output:
# 7Multiplication *
The * operator multiplies values.
a = 10
b = 3
print(a * b)
# Output:
# 30Division /
The / operator performs division.
a = 10
b = 3
print(a / b)
# Output:
# 3.3333333333333335In Python, / returns a floating-point number, even when the result is a whole number:
print(10 / 2)
# Output:
# 5.0Modulus %
The % operator returns the remainder after division.
print(10 % 3)
# Output:
# 1This is useful when checking whether a number is even or odd:
print(10 % 2)
# Output:
# 0
print(7 % 2)
# Output:
# 1If the remainder is 0, the number is evenly divisible by 2.
Floor Division //
The // operator performs floor division.
print(10 // 3)
# Output:
# 3Regular division gives:
print(10 / 3)
# Output:
# 3.3333333333333335Floor division gives the largest integer less than or equal to the result:
print(10 // 3)
# Output:
# 3Negative Numbers with % and //
There is an important detail to know when working with negative numbers.
Python's % and // follow rules that can be surprising if you're coming from another programming language.
For example:
print(-7 % 3)
# Output:
# 2
print(-7 // 3)
# Output:
# -3Python's % result has the same sign as the divisor.
So:
-7 % 3gives:
2Floor division also has an important detail.
// rounds down toward negative infinity, rather than toward zero.
Since:
-7 / 3is approximately:
-2.333...floor division gives:
print(-7 // 3)
# Output:
# -3It does not give -2 because -3 is the next whole number when rounding down.
This behavior is especially useful to remember when working with negative numbers.
Exponentiation **
The ** operator raises a number to a power.
print(2 ** 3)
# Output:
# 8This means:
2 × 2 × 2 = 8Assignment Operators
Assignment operators are used to assign values to variables.
Basic Assignment =
The = operator assigns a value to a variable.
score = 10Here, 10 is assigned to score.
Compound Assignment Operators
Python also provides shorthand operators for updating an existing variable.
For example:
score = 10
score += 5
print(score)
# Output:
# 15This is equivalent to:
score = score + 5Here are the common assignment operators:
| Operator | Example | Equivalent to |
|---|---|---|
= |
x = 5 |
Assign 5 |
+= |
x += 2 |
x = x + 2 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 2 |
x = x * 2 |
/= |
x /= 2 |
x = x / 2 |
//= |
x //= 2 |
x = x // 2 |
%= |
x %= 2 |
x = x % 2 |
**= |
x **= 2 |
x = x ** 2 |
For example:
number = 10
number += 5
print(number)
number *= 2
print(number)
# Output:
# 15
#30Comparison Operators
Comparison operators compare two values.
They return either True or False.
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | x == 10 |
!= |
Not equal to | x != 10 |
> |
Greater than | x > 10 |
< |
Less than | x < 10 |
>= |
Greater than or equal to | x >= 10 |
<= |
Less than or equal to | x <= 10 |
For example:
age = 20
print(age == 20)
print(age > 18)
print(age < 18)
# Output:
# True
# True
# False= vs ==
One of the most common mistakes for beginners is confusing = and ==.
= assigns a value:
age = 20== compares two values:
age == 20The first changes or assigns a variable.
The second checks whether the values are equal.
You'll use == frequently when writing conditional statements.
What Happens If You Use = in a Condition?
For example:
age = 18
if age = 18:
print("You are 18")This raises a SyntaxError.
Python does not allow assignment with = in this position. You need to use the comparison operator:
if age == 18:
print("You are 18")This is one of the mistakes Python catches before the program runs.
Logical Operators
Logical operators are used to combine or reverse conditions.
Python has three logical operators:
andornot
and
and returns True when both conditions are true.
age = 25
print(age > 18 and age < 30)
# Output:
# TrueBoth conditions are true:
age > 18
age < 30or
or returns True when at least one condition is true.
age = 25
print(age < 18 or age > 20)
# Output:
# TrueThe second condition is true, so the overall result is True.
not
not reverses a Boolean value.
is_logged_in = True
print(not is_logged_in)
# Output:
# FalseWe'll use logical operators more when working with conditional statements.
Membership Operators
Python has two membership operators:
innot in
They check whether a value exists inside a sequence or collection.
For example, with a string:
message = "Hello"
print("H" in message)
print("x" in message)
# Output:
# True
# FalseYou can also use not in:
message = "Hello"
print("x" not in message)
# Output:
# TrueMembership operators are especially useful when working with strings, lists, and other collections.
We'll explore them more when we learn about lists and collections.
Identity Operators
Python also has two identity operators:
isis not
Identity operators check whether two references point to the same object.
For example:
a = None
print(a is None)Output:
Trueis is different from ==.
== checks whether two values are equal.
is checks whether two references refer to the same object.
A common use is checking for None:
value = None
if value is None:
print("No value")We'll explore objects and identity in more detail later.
Bitwise Operators
Bitwise operators work with the individual bits of integer values.
Python provides these bitwise operators:
| Operator | Name |
|---|---|
& |
AND |
| |
OR |
^ |
XOR |
~ |
NOT |
<< |
Left shift |
>> |
Right shift |
For example:
a = 5
b = 3
print(a & b)
# Output:
# 1Bitwise operators are commonly used in areas such as low-level programming, permissions, flags, and performance-sensitive code.
You don't need to memorize them when you're just starting with Python, but it's useful to know they exist.
Operator Precedence
When an expression contains multiple operators, Python follows a specific order when evaluating them.
For example:
result = 10 + 2 * 3
print(result)
# Output:
# 16Python performs multiplication before addition:
2 * 3 = 6
10 + 6 = 16You can use parentheses to make the order explicit:
result = (10 + 2) * 3
print(result)
# Output:
# 36A simplified order to remember is:
- Parentheses
- Exponentiation
** - Multiplication, division, floor division, and modulus
- Addition and subtraction
- Comparisons
notandor
When in doubt, use parentheses. They make your code easier to read and remove ambiguity.
Operators and Conditional Statements
Operators become especially useful when combined with conditional statements.
For example:
age = 20
if age >= 18:
print("You are an adult")Here:
age >= 18is a comparison that produces either True or False.
The if statement then uses that result to decide whether to run the code inside it.
We'll explore conditional statements in the next lesson.
Quick Reference
| Type | Operators | |
|---|---|---|
| Arithmetic | +, -, *, /, %, //, ** |
|
| Assignment | =, +=, -=, *=, /=, //=, %=, **= |
|
| Comparison | ==, !=, >, <, >=, <= |
|
| Logical | and, or, not |
|
| Membership | in, not in |
|
| Identity | is, is not |
|
| Bitwise | &, |, ^, ~, <<, >> |
👉 Next tutorial: Python Conditional Statements