|
| Home |
|---|
Python operators are symbols and keywords used to perform operations on values and variables. They allow Python programs to perform mathematical calculations, compare values, make decisions, assign values, check membership and work with individual bits of data.
Operators are one of the most important foundations of Python programming. They are used in expressions, conditional statements, loops, functions, calculations and almost every Python program.
For example, the + operator can add numbers, while the
== operator can compare two values. The and,
or and not operators can be used to combine or
reverse conditions.
Python provides several types of operators. The main categories of Python operators are:
Python also supports unary operators, augmented assignment operators, the conditional expression and the matrix multiplication operator. Understanding these operators will help you write clearer and more effective Python programs.
Arithmetic operators are used to perform mathematical calculations in Python. They can be used with numeric values and variables to perform operations such as addition, subtraction, multiplication, division, floor division, modulus and exponentiation.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| // | Floor Division | a // b |
| % | Modulus | a % b |
| ** | Exponentiation | a ** b |
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
13
7
30
3.3333333333333335
3
1
1000
The division operator / returns a division result as a
floating-point number. The floor division operator //
returns the floor of the division result.
The modulus operator % returns the remainder after division,
while the exponentiation operator ** raises one number to
the power of another.
The three operators /, // and %
are particularly important when working with numbers.
| Operator | Purpose | Example | Result |
|---|---|---|---|
| / | Division | 10 / 3 | 3.3333333333333335 |
| // | Floor division | 10 // 3 | 3 |
| % | Remainder | 10 % 3 | 1 |
These operators are commonly used in calculations, loops, counters, data processing and programs that work with quantities or measurements.
Comparison operators compare two values and return a Boolean result:
True or False.
Comparison operators are commonly used in if statements,
loops and other conditional expressions.
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
a = 10
b = 5
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
False
True
True
False
True
False
Beginners often confuse the = and ==
operators. They perform completely different tasks.
| Operator | Purpose | Example |
|---|---|---|
| = | Assigns a value to a variable | x = 10 |
| == | Checks whether two values are equal | x == 10 |
For example, x = 10 assigns the value 10 to
x. On the other hand, x == 10 checks whether
the value of x is equal to 10.
Logical operators are used to combine or reverse Boolean conditions. They are especially useful when a program needs to make decisions based on more than one condition.
| Operator | Description |
|---|---|
| and | Returns a truthy result when both conditions are true |
| or | Returns a truthy result when at least one condition is true |
| not | Reverses the truth value of an expression |
age = 25
print(age > 18 and age < 60)
print(age > 30 or age < 40)
print(not(age > 18))
True
True
False
Logical operators are frequently used with if,
while and other conditional expressions.
Assignment operators are used to assign values to variables. Python
provides the basic assignment operator = as well as
augmented assignment operators that combine an operation with
assignment.
| Operator | Example | Description |
|---|---|---|
| = | x = 5 | Assign value |
| += | x += 3 | Add and assign |
| -= | x -= 3 | Subtract and assign |
| *= | x *= 3 | Multiply and assign |
| /= | x /= 3 | Divide and assign |
| //= | x //= 3 | Floor divide and assign |
| %= | x %= 3 | Modulus and assign |
| **= | x **= 3 | Exponentiate and assign |
x = 10
x += 5
print(x)
15
The expression x += 5 is a shorter way of expressing the
operation of adding 5 to the current value of x and
assigning the result back to x.
Membership operators are used to check whether a value exists in a
sequence or collection. The two membership operators in Python are
in and not in.
| Operator | Description |
|---|---|
| in | Returns True if a value is found in the object |
| not in | Returns True if a value is not found in the object |
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits)
print("orange" not in fruits)
True
True
Membership operators can also be used with strings, tuples, sets and other Python objects that support membership testing.
text = "Python programming"
print("Python" in text)
print("Java" not in text)
True
True
Identity operators are used to determine whether two variables refer to
the same object. Python provides two identity operators:
is and is not.
| Operator | Description |
|---|---|
| is | True if two variables refer to the same object |
| is not | True if two variables do not refer to the same object |
x = [1, 2, 3]
y = x
print(x is y)
True
In this example, y = x makes both variables refer to the
same list object. Therefore, x is y returns
True.
The is operator and the == operator are not
interchangeable. The == operator compares values, while
is checks whether two variables refer to the same object.
| Operator | Checks |
|---|---|
| == | Whether two values are equal |
| is | Whether two variables refer to the same object |
A common Python practice is to use identity comparisons with
None, such as value is None.
Bitwise operators work with the individual bits of integer values. They are useful in areas such as low-level programming, binary data processing, flags and some specialized algorithms.
| Operator | Description |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left shift |
| >> | Right shift |
a = 5
b = 3
print(a & b)
print(a | b)
print(a ^ b)
1
7
6
Bitwise operations work on the binary representation of integer values.
For example, the binary representation of 5 is 101, while
3 is 011.
Unary operators work with a single value or operand. The most common
unary operators are the positive sign +, negative sign
- and bitwise NOT operator ~.
| Operator | Description | Example |
|---|---|---|
| + | Positive value | +x |
| - | Negative value | -x |
| ~ | Bitwise NOT | ~x |
x = 10
print(+x)
print(-x)
print(~x)
10
-10
-11
Python provides a conditional expression that allows you to select one of two values based on a condition. It is sometimes called a ternary operator.
The basic form is:
value_if_true if condition else value_if_false
age = 20
result = "Adult" if age >= 18 else "Minor"
print(result)
Adult
Conditional expressions are useful when a simple decision needs to
produce a value without writing a complete multi-line
if statement.
Python also provides the @ operator for matrix
multiplication. It is primarily used with objects that implement matrix
multiplication, particularly in scientific computing and data science
libraries such as NumPy.
For example, matrix multiplication can be performed using
@ with compatible matrix objects.
A @ B
The @ operator is different from the
* operator. For matrix objects, * generally
represents element-wise multiplication, while @ represents
matrix multiplication.
When an expression contains several operators, Python follows rules called operator precedence to determine which operation is performed first.
For example, multiplication has a higher precedence than addition. Therefore:
result = 10 + 5 * 2
print(result)
20
Python performs 5 * 2 before adding 10.
Parentheses can be used when you want to explicitly control the order of operations.
result = (10 + 5) * 2
print(result)
30
Using parentheses can make complex expressions easier to understand and can prevent mistakes caused by misunderstanding operator precedence.
Operators are frequently used in conditional statements. Comparison and logical operators allow a program to evaluate conditions and decide which code should execute.
age = 20
if age >= 18:
print("You are an adult.")
You are an adult.
In this example, the comparison operator >= checks
whether the value of age is greater than or equal to 18.
Operators are also commonly used in for and
while loops. Arithmetic, comparison and assignment
operators can be combined to control how a loop operates.
number = 1
while number <= 5:
print(number)
number += 1
1
2
3
4
5
Here, the comparison operator <= determines whether the
loop should continue, while the augmented assignment operator
+= increases the value of number.
Some operators can also be used with strings. For example, the
+ operator can concatenate strings, while the
* operator can repeat a string.
first_name = "Python"
language = "Programming"
print(first_name + " " + language)
print("Hi " * 3)
Python Programming
Hi Hi Hi
The behavior of an operator depends on the types of objects involved. This is one of the useful features of Python's operators.
When an expression contains multiple operators, using parentheses can make the intended calculation much clearer.
a = 10
b = 5
c = 2
result = a + b * c
print(result)
20
Because multiplication has higher precedence than addition, Python
calculates b * c before adding a.
When in doubt, use parentheses to make the order of operations explicit:
result = (a + b) * c
print(result)
30
Beginners often encounter errors because some Python operators look similar but perform different operations.
= with ==.
is with ==.
/ when floor division // is required.
% returns the remainder.
and when or is required, or vice versa.
is to compare ordinary values instead of comparing
object identity.
The result of an operator can depend on the types of values involved.
For example, the + operator can add numbers and can also
concatenate strings.
print(10 + 5)
print("Python" + " Tutorial")
15
Python Tutorial
Python determines the appropriate operation based on the objects being
used. However, not every operator is valid for every data type. Using
an unsupported combination of values can result in a
TypeError.
| Category | Operators | Main Use |
|---|---|---|
| Arithmetic | +, -, *, /, //, %, ** | Mathematical calculations |
| Comparison | ==, !=, >, <, >=, <= | Compare values |
| Logical | and, or, not | Combine conditions |
| Assignment | =, +=, -=, *=, /=, //=, %=, **= | Assign and update values |
| Membership | in, not in | Check whether a value exists |
| Identity | is, is not | Compare object identity |
| Bitwise | &, |, ^, ~, <<, >> | Work with bits |
| Conditional | if ... else | Choose between two values |
| Matrix multiplication | @ | Matrix multiplication |
Python operators are symbols and keywords used to perform operations on values and variables. They are used for calculations, comparisons, logical decisions, assignments and data checks.
The main categories include arithmetic, comparison, logical, assignment, membership, identity and bitwise operators. Python also supports unary operators, conditional expressions and the matrix multiplication operator.
The = operator assigns a value to a variable, while
== checks whether two values are equal.
The // operator performs floor division. It returns the
floor of the division result.
The % operator is the modulus operator. It returns the
remainder after division.
The == operator compares values, while is
checks whether two variables refer to the same object.
Python has three logical operators: and, or
and not. They are used to combine or reverse conditions.
Python operators allow programmers to perform calculations, compare values, assign and update variables, combine conditions, test membership, compare object identity and manipulate binary data.
The most commonly used Python operators include arithmetic operators
such as + and *, comparison operators such as
== and >, logical operators such as
and and or, and assignment operators such as
= and +=.
Understanding Python operators is essential for writing if statements, loops, functions, calculations and data processing programs. Beginners should first become comfortable with arithmetic, comparison, logical and assignment operators before moving on to identity and bitwise operations.
Continue learning Python with these related tutorials:
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net