Python Operators


Operators are a key part of writing Python code. They help you perform actions like calculations, comparisons, logical decisions and working with values in different ways. Even though they look simple, operators play a major role in how your program behaves. In this chapter, you’ll explore all the main types of operators used in Python, how each one works and why they matter in real programs.

What Are Operators in Python?

Operators are symbols or keywords that tell Python to perform an operation. Every operator works with one or more values, known as operands. When you write something like 10 + 5, the plus sign is the operator and the two numbers are operands.

Python groups operators into different categories based on what they do. Learning how they work helps you write cleaner and more accurate code.

Types of Operators in Python

Python supports several types of operators:

  • Arithmetic operators

  • Assignment operators

  • Comparison operators

  • Logical operators

  • Identity operators

  • Membership operators

  • Bitwise operators

Each type serves a different purpose in a program.

Arithmetic Operators

Arithmetic operators are used for basic math tasks like addition or subtraction.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b
// Floor division a // b
** Power a ** b

Example:

x = 15
y = 4
print(x + y)
print(x // y)
print(x ** y)

Arithmetic operators are common in calculators, billing systems, measurements and other numeric tasks.

Assignment Operators

Assignment operators store values in variables. The most basic one is =, but Python includes many shortcuts.

Operator Meaning Example
= Assign value x = 10
+= Add and assign x += 5
-= Subtract and assign x -= 3
*= Multiply and assign x *= 2
/= Divide and assign x /= 4
%= Modulus and assign x %= 2
//= Floor divide and assign x //= 3
**= Power and assign x **= 2

Example:

x = 10
x += 3
print(x)   # Output: 13

These operators reduce repetition and make code cleaner.

Comparison Operators

Comparison operators check the relationship between two values. They return either True or False.

Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater or equal a >= b
<= Less or equal a <= b

Example:

age = 18
print(age >= 18)

Comparison operators are essential for conditions, loops and decision-making.

Logical Operators

Logical operators connect multiple conditions.

Operator Meaning Example
and Both conditions must be true a > 10 and a < 20
or One of the conditions must be true a == 10 or a == 20
not Reverses the condition not a == 10

Example:

score = 85
print(score > 80 and score < 100)

Logical operators help in validation, filtering and decision trees.

Identity Operators

Identity operators check whether two variables point to the same object in memory.

Operator Meaning Example
is Same object a is b
is not Not the same object a is not b

Example:

x = [1, 2, 3]
y = x
print(x is y)

Identity checks are useful for understanding how variables behave with mutable data types like lists.

Membership Operators

Membership operators check whether a value exists in a sequence like a list, tuple or string.

Operator Meaning Example
in Value exists "a" in "apple"
not in Value does not exist "z" not in "apple"

Example:

fruits = ["apple", "banana", "mango"]
print("banana" in fruits)

You will use membership operators often when searching lists or validating choices.

Bitwise Operators

Bitwise operators work with binary numbers. While you may not use them daily, they are important in tasks like encryption, compression or low-level operations.

Operator Meaning Example
& AND a & b
` ` OR
^ XOR a ^ b
~ NOT ~a
<< Left shift a << 2
>> Right shift a >> 2

Example:

a = 5      # 0101
b = 3      # 0011
print(a & b)

Bitwise operators help when working with permissions, flags and performance-critical tasks.

Operator Precedence in Python

Operator precedence determines which operator Python evaluates first when multiple operators appear in one statement.

For example:

result = 10 + 5 * 2

Python performs multiplication first, then addition, giving 20.

General precedence order (highest to lowest):

  1. **

  2. *, /, //, %

  3. +, -

  4. Comparison operators

  5. Logical operators

Using parentheses helps make operations clearer:

result = (10 + 5) * 2

Operators in Real-World Python Programs

Operators are used everywhere in programming, such as:

  • Validations like checking password length

  • Loop conditions

  • Mathematical calculations

  • Input checks using logical operators

  • Searching a value in a list

  • Assigning values efficiently with +=

  • Managing permissions with bitwise operators

  • Comparing user choices in menus

Understanding them helps you write programs that are clear, readable and accurate.

Summary of the Tutorial

Operators allow Python to perform actions like calculations, comparisons, assignments and logical checks. You learned about arithmetic, assignment, comparison, logical, identity, membership and bitwise operators along with how each one works. Operator precedence determines the order in which Python evaluates expressions. Once you understand how operators behave, you can write cleaner code and make better decisions inside your programs.


Practice Questions

  1. What is the purpose of operators in Python, and how do they interact with operands?

  2. How do arithmetic operators differ from comparison operators in terms of usage?

  3. Why are logical operators important when writing conditions in Python programs?

  4. What is operator precedence, and why does it matter when evaluating expressions?

  5. How do identity operators differ from membership operators?

  6. Write a small expression using arithmetic operators that calculates the remainder of dividing 25 by 4.

  7. Create an example using += to update the value of a variable.

  8. Write a condition using comparison and logical operators to check whether a number is between 10 and 50.

  9. Use a membership operator to check if "apple" exists inside a list of fruits.

  10. Show a short example of how the is operator behaves when two variables reference the same list.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top