Operators in Python: Types with Examples

Operators in Python are special symbols or keywords that perform operations on values and variables. Python defines seven operator types: arithmetic, comparison, logical, bitwise, assignment, membership and identity. Understanding what are operators in Python is the single most foundational skill before writing any real program.

  • Python has seven operator types, each serving a distinct purpose in expressions and control flow.
  • Logical operators and, or and not return actual values, not just True or False, which surprises most beginners.
  • Bitwise operators work directly on binary representations, making them essential for networking, cryptography and low-level system tasks.
  • Membership operators in and not in let you check if a value exists inside any iterable in a single, readable line.
  • Operator precedence determines evaluation order; misunderstanding it is one of the most common sources of logic bugs in student code.

Arithmetic and Comparison Operators

Arithmetic operators handle the maths. Every operation in Python that involves numbers starts here. If you are just getting started, make sure you can write and run a Python program before experimenting with the examples below.

Arithmetic Operators

Operator Name Example Result
+ Addition 10 + 3 13
– Subtraction 10 – 3 7
* Multiplication 10 * 3 30
/ Division 10 / 3 3.3333…
// Floor Division 10 // 3 3
% Modulus 10 % 3 1
** Exponentiation 2 ** 8 256

A practical tip: the modulus operator % is used constantly in real code to check even or odd numbers, paginate results, or cycle through a list. In competitive programming, you will see n % 2 == 0 in almost every second solution on platforms like Codeforces or LeetCode, which are popular among students at IITs, NITs and engineering colleges across India.

One common mistake students make is assuming / returns an integer. In Python 3, 10 / 2 gives 5.0, not 5. If you want integer division, use //. Python’s official documentation at python.org covers this behaviour explicitly.

Comparison Operators

Comparison operators compare two values and return a Boolean: either True or False. They are the engine behind every if statement and loop condition you will ever write. Knowing what are operators in Python at this level helps you write cleaner conditional logic from day one.

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7 > 4 True
< Less than 3 < 9 True
>= Greater than or equal 5 >= 5 True
<= Less than or equal 4 <= 6 True

Do not confuse = (assignment) with == (comparison). Writing if score = 90: instead of if score == 90: is a syntax error in Python, and it is one of the top beginner mistakes flagged in Stack Overflow’s Developer Survey year after year.

Assignment Operators in Python

Assignment operators store or update values in variables. The basic assignment operator is =, but Python also provides compound assignment operators that combine an arithmetic operation with assignment in a single step. These operators in Python are used in every loop, counter and accumulator pattern you will write.

Operator Name Example Equivalent To
= Assign x = 10 x = 10
+= Add and assign x += 5 x = x + 5
-= Subtract and assign x -= 3 x = x – 3
*= Multiply and assign x *= 2 x = x * 2
/= Divide and assign x /= 4 x = x / 4
//= Floor divide and assign x //= 3 x = x // 3
**= Exponent and assign x **= 2 x = x ** 2
%= Modulus and assign x %= 3 x = x % 3

Compound assignment operators are especially useful in loops. For example, total += marks inside a for loop accumulates a running sum without writing total = total + marks every time. You will see this pattern constantly in data processing scripts and student grade calculators.

Logical Operators: and, or, not

Logical operators combine or invert Boolean expressions. Python uses the plain English words and, or and not instead of symbols like && or || used in languages like C or Java. That makes Python code read almost like a sentence, which is one reason Python operators are easier to learn than operators in most other languages.

How and and or Work in Python

and returns the first falsy value it encounters, or the last value if all are truthy. or returns the first truthy value, or the last value if all are falsy. This is called short-circuit evaluation, and understanding it separates beginners from intermediate programmers.

  • and example: True and False returns False. 5 and 0 returns 0.
  • or example: False or "Delhi" returns "Delhi". Python returns the actual value, not just True.
  • not example: not True returns False. not 0 returns True.

The and and or pattern is used heavily in default-value assignments. You will see lines like city = user_input or "Mumbai" in production code, which means use the user’s input if it is not empty, otherwise default to Mumbai. It is clean, idiomatic Python that Indian developers working at product companies like Zepto, Swiggy and Razorpay use daily.

Combining Logical Operators

You can chain them: age >= 18 and age <= 60 checks whether a value falls in a range. Python also lets you write this more naturally as 18 <= age <= 60, which most other languages do not support. If you are building data pipelines or automation scripts, these patterns come up constantly. See our guide on Python for automation, web scraping and APIs for real-world examples.

Bitwise Operators

Bitwise operators work directly on the binary (bit-level) representation of integers. They are used in cryptography, network programming, compression algorithms, and ethical hacking tools. If you are preparing for a cybersecurity role or exploring the CEH v13 AI certification, understanding bitwise operations is non-negotiable.

Python Bitwise Operators Table

Operator Name Example (a=12, b=10) Binary Result Decimal
& Bitwise AND 12 & 10 1000 8
| Bitwise OR 12 | 10 1110 14
^ Bitwise XOR 12 ^ 10 0110 6
~ Bitwise NOT ~12 …11110011 -13
<< Left Shift 12 << 1 11000 24
>> Right Shift 12 >> 1 0110 6

Here is why this matters in practice: left shift by 1 is equivalent to multiplying by 2, and right shift by 1 is equivalent to dividing by 2 (integer). That is why game engines, image processing libraries and network packet parsers use shifts instead of multiplication for speed.

XOR has a neat property: a ^ a always equals 0, and a ^ 0 always equals a. Cryptographers use this to build simple cipher functions. According to the Stack Overflow Developer Survey 2024, Python remains the most-used programming language overall, which means bitwise operations written in Python are powering real infrastructure worldwide. The TIOBE Index (April 2024) also ranks Python as the number one programming language globally. Additionally, according to JetBrains State of Developer Ecosystem 2023, 53% of developers use Python as their primary language, up from 48% the previous year, confirming its dominance in education and industry alike.

A Note on Bitwise NOT

~x returns -(x + 1) due to how Python handles two’s complement integers. So ~12 gives -13, not 3. This trips up almost every student the first time. Write it out in binary once, and it will stick.

Membership and Identity Operators

These two operator types are often taught together because they both test relationships, but they test very different things. Both are important when answering what are operators in Python at an intermediate level.

Membership Operators in Python with Example

Membership operators check whether a value exists inside a sequence like a list, tuple, string or dictionary. There are exactly two: in and not in.

  • in: "Python" in ["Java", "Python", "C++"] returns True
  • not in: 99 not in [10, 20, 30] returns True
  • With strings: "AI" in "AI Engineer" returns True
  • With dictionaries: "name" in {"name": "Priya", "age": 22} checks keys, not values

Membership operators make Python code extremely readable. Instead of writing a loop to search a list, you write one line. If you are working with Pandas and NumPy explained in data science projects, the in operator works with Series and Index objects too, which speeds up data filtering tasks significantly.

Identity Operators

Identity operators check whether two variables point to the same object in memory. They do not compare values; they compare memory addresses.

  • is: a is b returns True if both variables reference the exact same object
  • is not: a is not b returns True if they reference different objects

The classic mistake: using is to compare integers or strings when you should use ==. x = 1000; y = 1000; x is y might return False because Python creates separate objects for large integers outside the range -5 to 256. These subtle behaviours are worth knowing because you will encounter them in code reviews and technical interviews at companies like Infosys, Wipro and TCS, which collectively hire tens of thousands of Python developers in India each year.

Operator Precedence in Python

When an expression contains multiple operators, Python evaluates them in a fixed order. Higher precedence operators run first. Understanding operator precedence is essential for writing correct Python expressions.

Precedence (High to Low) Operators
1 (Highest) ** (Exponentiation)
2 ~, unary +, unary –
3 *, /, //, %
4 +, –
5 <<, >>
6 &
7 ^
8 |
9 ==, !=, >, <, >=, <=, is, is not, in, not in
10 not
11 and
12 (Lowest) or

The rule of thumb: use parentheses whenever you are not 100% sure. 2 + 3 * 4 gives 14, not 20, because multiplication has higher precedence than addition. Add parentheses to make intent explicit: (2 + 3) * 4 gives 20. Readable code always beats clever code.

Understanding the different types of operators in Python also feeds directly into stronger coding practices when you are building anything serious, whether that is a web scraper, an AI model, or a security tool. Students studying BSc Computer Science subjects and scope will encounter operator-heavy code in every semester from data structures to algorithms to operating systems.

If you are aiming to become an AI engineer, Python operator fluency is one of the first things you need locked in before touching libraries like TensorFlow or PyTorch. The same applies if you are exploring software development tools to learn first and want to understand how debuggers and interpreters evaluate expressions.

Operators are the building blocks of every Python expression. Master them now, and every topic you study next, from automation to AI to cybersecurity, will feel easier.

Here are three things you can do this week to cement this knowledge:

  1. Open a Python shell and test each operator type from this article with your own numbers. Change the values and predict the output before running it.
  2. Write a small script that uses at least one arithmetic, one logical and one membership operator together, such as checking if a student’s marks qualify for a scholarship.
  3. Explore 3.0 University online certification courses in Cybersecurity, Ethical Hacking, AI and Blockchain. Strong Python fundamentals are the direct foundation for secure coding, AI development and tools covered in the CEH v13 AI course.

Frequently Asked Questions

What are operators in Python?

Operators in Python are symbols or keywords that perform operations on one or more values. Python includes seven types: arithmetic, comparison, logical, bitwise, assignment, membership and identity. Each type serves a specific role, from basic maths to checking object identity in memory.

What are assignment operators in Python?

Assignment operators in Python store or update values in variables. The basic operator is =. Compound assignment operators like +=, -=, *= and /= combine an arithmetic operation with assignment in one step. For example, x += 5 is equivalent to x = x + 5, making loops and accumulators more concise.

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

== checks whether two values are equal. is checks whether two variables point to the exact same object in memory. For example, two lists with identical contents will return True for == but False for is because they are stored as separate objects. Always use == for value comparison and is only when checking object identity, such as if x is None.

How do and and or work in Python?

In Python, and returns the first falsy value or the last value if all are truthy. or returns the first truthy value or the last value if all are falsy. This short-circuit behaviour means they do not always return a plain Boolean, which is why city = user_input or "Bangalore" is valid, idiomatic Python.

What are bitwise operators in Python?

Python bitwise operators work on the binary representation of integers. The six operators are AND (&), OR (|), XOR (^), NOT (~), left shift (<<) and right shift (>>). They are used in cryptography, network programming and low-level system operations where direct bit manipulation is needed.

What are membership operators in Python?

Membership operators check whether a value exists inside an iterable. Python has two: in returns True if the value is found, and not in returns True if it is not. They work with lists, tuples, strings, sets and dictionary keys, making search checks concise and readable.

What is operator precedence in Python?

Operator precedence determines which operations Python evaluates first in a complex expression. Exponentiation runs before multiplication, which runs before addition, which runs before comparison operators, which run before logical operators. When in doubt, use parentheses to make evaluation order explicit and avoid hard-to-find logic bugs.

Last updated: June 2025. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles