🐍Python Operators Guide: Arithmetic, Logical, & Precedence Explained 🐍

Operators are special symbols in Python that perform mathematical or logical tasks. They are the building blocks of any program! ✨

1. Arithmetic Operators ➕

Used for basic math calculations.

Operator Meaning Example Result
+ Addition 4 + 2 6
% Modulus (Remainder) 5 % 2 1
** Exponent (Power) 5 ** 2 25
/ Division 7 / 2 3.5
// Floor Division 7 // 2 3

2. Comparison Operators ⚖️

These return either True or False.

Symbol Meaning Example (5 ? 2) Result
== Equal to 5 == 2 False
!= Not equal to 5 != 2 True
> Greater than 5 > 2 True
>= Greater than or equal to 5 >= 2 True

3. Logical Operators 🧠

  • and: Returns True if both statements are true.
  • or: Returns True if at least one statement is true.
  • not: Reverses the result (True becomes False).

    logical-operators
    logical operators


4. Identity Operators 🆔

Used to check if two variables are the same object in memory.

x = 2
y = 2
print(x is y) # Output: True

python-reuses-small-integers-in-memory
python reuses small integers in memory

*
In Python 🐍, small integers from -5 to 256 are created and stored in memory when the interpreter starts 🧠
These numbers are cached (saved) in advance ⚡
So when you use any number in this range, Python reuses the same memory value instead of creating a new one 🔁



⚡ Precedence: Which one goes first?
operator-precedency-table
Operator precedence table

Solve:
Example 1 (Different Level): 10 + 5 * 2 ** 2

  1. Power (**): 2 ** 2 = 4
  2. Multiply (*): 5 * 4 = 20
  3. Add (+): 10 + 20 = 30

Example 2 (Same Level - Left to Right):  20 / 2 * 5 % 4

  •   1. Division: 20 / 2 = 10.0
  •   2. Multiply: 10.0 * 5 = 50.0
  •   3. Modulus: 50.0 % 4 = 2.0

📖 Vocabulary

Operand: The value or variable that an operator works on.

Precedence: The order in which operations are performed.

📚 Related Articles

Article No Article Title & Link
1 🐍 Getting Started with Python for Data Science – Fundamentals Guide 🚀
2 🐍 Python Collections (Arrays) Simplified: List, Tuple, Set, & Dictionary 🐍
3 🐍 Understanding Python Lists in Simple Way 📋
4 🐍 Understanding Python Tuples in Simple Way 📦
5 🐍 Understanding Python Sets in Simple Way 🎴
6 🐍 Understanding Python Range & Dictionaries 📘
7 🐍Python Operators Guide: Arithmetic, Logical, & Precedence Explained 🐍
8 🐍 Python Control Statements Complete Guide 🔄
9 🐍 Python Functions Explained in Simple Way ⚙️
10 🐍 Python Map, Filter, Lambda & Modules – Simple Notes
11 🐍 Python OOP Concepts – Simple Short Notes
12 🐍  NumPy, Pandas & Web Scraping – Simple Python Notes📊












Post a Comment

Previous Post Next Post