Skip to main content

Command Palette

Search for a command to run...

Operators in Python: A Beginner’s Guide with Examples

Published
•4 min read•View as Markdown

🔹 Introduction

In Python, operators are special symbols that perform operations on variables and values. For example, + adds numbers, > compares values, and in checks membership in a collection.

As a beginner, mastering operators is essential because they are used in almost every program you’ll ever write.

🔹 Types of Operators in Python

Python provides several categories of operators:

1️⃣ Arithmetic Operators

Used for basic mathematical calculations.

OperatorDescriptionExampleOutput
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52.0
%Modulus (remainder)10 % 31
//Floor Division10 // 33
**Exponentiation2 ** 38

2️⃣ Comparison (Relational) Operators

Used to compare two values. Result is always True or False.

OperatorDescriptionExampleOutput
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than10 > 5True
<Less than2 < 5True
>=Greater than or equal to5 >= 5True
<=Less than or equal to3 <= 5True

3️⃣ Logical Operators

Used to combine conditional statements.

OperatorDescriptionExampleOutput
andTrue if both are True(5 > 3) and (10 > 5)True
orTrue if at least one is True(5 > 10) or (3 < 5)True
notReverses the resultnot(5 > 3)False

4️⃣ Assignment Operators

Used to assign values to variables.

OperatorExampleSame As
=x = 10Assign 10 to x
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5

5️⃣ Bitwise Operators (Optional but Good to Know)

Operate on binary numbers.

OperatorDescriptionExampleOutput
&AND5 & 31
``OR`5
^XOR5 ^ 36
~NOT~5-6
<<Left Shift5 << 110
>>Right Shift5 >> 12

6️⃣ Membership Operators

Used to check if a value exists in a sequence (like a list, tuple, or string).

OperatorDescriptionExampleOutput
inReturns True if value exists'a' in 'apple'True
not inReturns True if value doesn’t exist'z' not in 'apple'True

7️⃣ Identity Operators

Used to compare memory locations (whether two variables point to the same object).

OperatorDescriptionExampleOutput
isReturns True if both are same objectx is yDepends
is notReturns True if not same objectx is not yDepends
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y)   # True (values are equal)
print(x is y)   # False (different objects in memory)

📝 Conclusion

Operators in Python are the foundation of writing logic. They allow you to:

  • Perform math operations.

  • Compare values.

  • Make decisions with conditions.

  • Work with collections of data.

👉 Take some time to practice these operators in your Python environment. Try combining them in small programs (like a calculator).

🔮 Next Blog Sneak Peek

Tomorrow, we’ll explore Conditional Statements in Python (if, elif, else) — the heart of decision-making in code!

👉 “If you enjoyed this, share it with friends who are learning Python!” to make it more engaging?

💡 Thanks for reading! If you’d like to support me and my blogging journey, feel free to check out my online store where I share a variety of useful products:

Visit my store for amazing products