Python Tutoring Online
 Home            

Python Operators – Arithmetic, Comparison, Logical and More

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.

Python Arithmetic Operators

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
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
//Floor Divisiona // b
%Modulusa % b
**Exponentiationa ** b

Example: Python Arithmetic Operators

Python
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)

Output


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.

Python Division, Floor Division and Modulus

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.

Python Comparison Operators

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

Example: Python Comparison Operators

Python
a = 10
b = 5

print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)

Output


False
True
True
False
True
False

Difference Between == and = in Python

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.

Python Logical Operators

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

Example: Python Logical Operators

Python
age = 25

print(age > 18 and age < 60)
print(age > 30 or age < 40)
print(not(age > 18))

Output


True
True
False

Logical operators are frequently used with if, while and other conditional expressions.

Python Assignment Operators

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 = 5Assign value
+=x += 3Add and assign
-=x -= 3Subtract and assign
*=x *= 3Multiply and assign
/=x /= 3Divide and assign
//=x //= 3Floor divide and assign
%=x %= 3Modulus and assign
**=x **= 3Exponentiate and assign

Example: Augmented Assignment Operators

Python
x = 10

x += 5

print(x)

Output


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.

Python Membership Operators

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

Example: Membership Operators with a List

Python
fruits = ["apple", "banana", "cherry"]

print("apple" in fruits)
print("orange" not in fruits)

Output


True
True

Membership operators can also be used with strings, tuples, sets and other Python objects that support membership testing.

Example: Using in with a String

Python
text = "Python programming"

print("Python" in text)
print("Java" not in text)

Output


True
True

Python Identity Operators

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

Example: Python Identity Operators

Python
x = [1, 2, 3]
y = x

print(x is y)

Output


True

In this example, y = x makes both variables refer to the same list object. Therefore, x is y returns True.

Difference Between is and == in Python

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.

Python Bitwise Operators

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

Example: Python Bitwise Operators

Python
a = 5
b = 3

print(a & b)
print(a | b)
print(a ^ b)

Output


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.

Python Unary Operators

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

Example: Unary Operators

Python
x = 10

print(+x)
print(-x)
print(~x)

Output


10
-10
-11

Python Conditional Expression

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

Example: Python Conditional Expression

Python
age = 20

result = "Adult" if age >= 18 else "Minor"

print(result)

Output


Adult

Conditional expressions are useful when a simple decision needs to produce a value without writing a complete multi-line if statement.

Python Matrix Multiplication Operator

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.

Python Operator Precedence

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:

Python
result = 10 + 5 * 2

print(result)

Output


20

Python performs 5 * 2 before adding 10.

Parentheses can be used when you want to explicitly control the order of operations.

Python
result = (10 + 5) * 2

print(result)

Output


30

Using parentheses can make complex expressions easier to understand and can prevent mistakes caused by misunderstanding operator precedence.

Using Operators in if Statements

Operators are frequently used in conditional statements. Comparison and logical operators allow a program to evaluate conditions and decide which code should execute.

Python
age = 20

if age >= 18:
    print("You are an adult.")

Output


You are an adult.

In this example, the comparison operator >= checks whether the value of age is greater than or equal to 18.

Using Operators in Python Loops

Operators are also commonly used in for and while loops. Arithmetic, comparison and assignment operators can be combined to control how a loop operates.

Python
number = 1

while number <= 5:
    print(number)
    number += 1

Output


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.

Python Operators with Strings

Some operators can also be used with strings. For example, the + operator can concatenate strings, while the * operator can repeat a string.

Python
first_name = "Python"
language = "Programming"

print(first_name + " " + language)
print("Hi " * 3)

Output


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.

Python Operator Precedence and Parentheses

When an expression contains multiple operators, using parentheses can make the intended calculation much clearer.

Python
a = 10
b = 5
c = 2

result = a + b * c

print(result)

Output


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:

Python
result = (a + b) * c

print(result)

Output


30

Common Beginner Mistakes with Python Operators

Beginners often encounter errors because some Python operators look similar but perform different operations.

Python Operators and Data Types

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.

Python
print(10 + 5)

print("Python" + " Tutorial")

Output


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.

Python Operators Quick Reference

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

Frequently Asked Questions About Python Operators

What are operators in Python?

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.

What are the main types of operators in Python?

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.

What is the difference between = and == in Python?

The = operator assigns a value to a variable, while == checks whether two values are equal.

What does // mean in Python?

The // operator performs floor division. It returns the floor of the division result.

What does % mean in Python?

The % operator is the modulus operator. It returns the remainder after division.

What is the difference between is and == in Python?

The == operator compares values, while is checks whether two variables refer to the same object.

What are logical operators in Python?

Python has three logical operators: and, or and not. They are used to combine or reverse conditions.

Summary of Python Operators

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.

Related Python Tutorials

Continue learning Python with these related tutorials:

Need Help Learning Python?

Having difficulty understanding Python operators, expressions, variables, conditions or loops? An experienced Python tutor can help you understand programming concepts through clear explanations and practical coding exercises.

One-to-one Python tutoring provides guided practice and personalized help with Python fundamentals, assignments, projects and programming problems.

Get Python Tutoring Help




Stuck Learning Python?

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.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net