โœ๏ธ Python Syntax & Basic Constructs
Estimated reading: 3 minutes 46 views

๐Ÿ Python Operators โ€“ Types, Usage & Examples

Python offers a rich set of operators that allow developers to perform operations on variables and values. Operators are the foundation of most computations in programming, and mastering them is essential for writing efficient Python code.

Whether you’re working with arithmetic, making decisions, or comparing values, Python provides built-in operators for all types of tasks โ€” making your code more expressive and readable.


โž• Arithmetic Operators

These operators are used to perform mathematical operations.

OperatorDescriptionExampleOutput
+Addition3 + 25
-Subtraction3 - 21
*Multiplication3 * 26
/Division3 / 21.5
//Floor Division3 // 21
%Modulus3 % 21
**Exponentiation2 ** 38

๐Ÿง  Note: Floor division // returns the largest integer less than or equal to the result.


๐Ÿงฎ Assignment Operators

Used to assign values to variables, often combined with arithmetic.

OperatorExampleEquivalent
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
//=x //= 2x = x // 2
%=x %= 2x = x % 2
**=x **= 2x = x ** 2

๐Ÿงพ Comparison Operators

Used to compare values and return True or False.

OperatorDescriptionExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater or equal5 >= 5True
<=Less or equal5 <= 3False

๐Ÿค” Logical Operators

Used to combine conditional statements.

OperatorDescriptionExampleResult
andReturns True if both are TrueTrue and FalseFalse
orReturns True if one is TrueTrue or FalseTrue
notInverts the resultnot TrueFalse

๐Ÿงฉ Bitwise Operators

Used to perform operations on binary values.

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

๐Ÿง  Tip: Bitwise operations are useful in low-level programming and optimization techniques.


๐Ÿงช Identity Operators

Used to compare memory locations of two objects.

OperatorDescriptionExampleResult
isTrue if both refer to same objectx is yTrue/False
is notTrue if not same objectx is not yTrue/False
x = [1, 2]
y = x
z = [1, 2]

print(x is y)    # True
print(x is z)    # False (different objects)

๐Ÿ“ฆ Membership Operators

Used to test if a value exists in a sequence (list, tuple, string, etc).

OperatorDescriptionExampleResult
inTrue if found'a' in 'apple'True
not inTrue if not found3 not in [1,2]True

โš™๏ธ Operator Precedence

Python evaluates expressions based on operator precedence (also called order of operations).

๐Ÿ“ Example:

result = 3 + 4 * 2     # Output: 11, not 14

๐Ÿ“Œ Precedence (High to Low):

  1. () – Parentheses
  2. ** – Exponentiation
  3. +, -, *, /, //, % – Arithmetic
  4. ==, !=, >, <, >=, <= – Comparison
  5. not, and, or – Logical
  6. is, in, etc.

โœ… Summary

๐Ÿ”น Python operators allow efficient computation and logical decision-making.
๐Ÿ”น They’re categorized into arithmetic, assignment, comparison, logical, bitwise, identity, and membership.
๐Ÿ”น Operator precedence ensures that expressions are evaluated in the correct order.
๐Ÿ”น Knowing operators is crucial for writing clean, optimized, and bug-free Python code.


โ“ FAQs

Q1. What’s the difference between = and == in Python?
= is an assignment operator, while == is a comparison operator.

Q2. Can I override operators in Python?
Yes, using magic methods (like __add__, __eq__, etc.) in classes.

Q3. Whatโ€™s the result of True + True in Python?
2 โ€“ Because True is treated as 1.

Q4. What does is check in Python?
It checks whether two variables point to the same object in memory.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Operators

Or Copy Link

CONTENTS
Scroll to Top