Skip to content

Operators

An operator is a symbol or keyword that tells Python to perform a specific operation on one or more values (also called operands). Operators allow you to perform tasks like adding numbers, comparing values, assigning results to variables, or evaluating logical conditions.

In Python, just like in other languages, there are a variety of operator types which we will teach you below.

These are the most basic operators and are used to perform mathematical operations like addition, subtraction, multiplication, or division. Python uses them to work with numbers directly.

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division (float result)
//Integer division
%Modulo (remainder)
**Power
Operators.py
x = 12
y = 24
print(x + y) # Result: 36
print(x - y) # Result: -12
print(x * y) # Result: 288
print(x / y) # Result: 0.5
print(x // y) # Result: 0
print(x % y) # Result: 12

These operators allow you to compare two values. The result of a comparison is always a boolean value: True if the condition is met, and False if not.

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
Operators.py
print(5 == 5) # Result: True
print(5 != 3) # Result: True
print(7 > 3) # Result: True
print(2 < 4) # Result: True
print(6 >= 6) # Result: True
print(3 <= 5) # Result: True

Assignment operators are symbols used in programming to assign a value to a variable. The most common assignment operator is the equals sign (=), but there are also compound assignment operators that combine an operation with assignment.

OperatorDescription
=Assignment
+=Addition and assignment
-=Subtraction and assignment
*=Multiplication and assignment
/=Division and assignment
%=Modulo and assignment
//=Integer division and assignment
**=Power and assignment
Operators.py
print(x = 5) # Assigns the value 5 to x
print(x += 3) # x = x + 3
print(x -= 2) # x = x - 2
print(x *= 4) # x = x * 4
print(x /= 2) # x = x / 2
print(x %= 3) # x = x % 3
print(x //= 2) # x = x // 2
print(x **= 3) # x = x ** 3

They are used to combine multiple conditions and get a boolean result. They are usually used in control structures like conditionals.

OperatorDescriptionExampleResult
andTrue if both conditions are trueTrue and FalseFalse
orTrue if at least one condition is trueTrue or FalseTrue
notInverts the value of the conditionnot TrueFalse