Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Operators in Python

Lesson 18 of 37 in the free Data Science notes on Siksha Sarovar, written by Rohit Jangra.

Operators in Python

Definition: An operator is a symbol that performs an operation on one or more operands(values / variables).Python supports a rich set of operators across several categories.

---

1. Arithmetic Operators

These perform basic mathematical operations.

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division (float)10 / 33.333...
//Floor Division10 // 33
%Modulus (Remainder)10 % 31
**Exponentiation10 ** 31000

Data Science Use Cases:

  • / and // — Normalization, calculating averages.
  • % — Checking even/odd, batch processing (every nth item).
  • ** — Squaring errors in loss functions.

---

2. Comparison (Relational) Operators

These compare two values and return a Boolean (True or False).

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

Common Mistake: Using = (assignment) instead of == (comparison).

---

3. Logical Operators

These combine multiple conditions and return a Boolean.

OperatorDescriptionExampleResult
andTrue if BOTH are True(5 > 3) and (10 > 7)True
orTrue if EITHER is True(5 > 3) or (10 < 7)True
notReverses the resultnot (5 > 3)False

Truth Table:

ABA and BA or Bnot A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue

---

4. Assignment Operators

These assign values to variables, often with a shorthand for combined operations.

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

---

5. Identity Operators

These check whether two variables refer to the same object in memory, not just equal values.

OperatorDescriptionExample
isReturns True if same objectx is y
is notReturns True if NOT same objectx is not y

Key Difference: == vs is:

  • == checks equality of values.
  • is checks identity of memory location.

---

6. Membership Operators

These check if a value exists within a sequence (list, string, tuple, etc.).

OperatorDescriptionExampleResult
inTrue if value is in sequence"a" in "apple"True
not inTrue if value is NOT in sequence"z" not in "apple"True

---

7. Bitwise Operators

These operate on binary representations of integers. While less common in everyday data science, they are used in image processing, cryptography, and optimization.

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

---

Operator Precedence (Order of Evaluation)

When multiple operators appear in an expression, Python evaluates them in a specific order:

PriorityOperatorDescription
1 (Highest)()Parentheses
2**Exponentiation
3~, +x, -xUnary operators
4*, /, //, %Multiplication, Division
5+, -Addition, Subtraction
6<<, >>Bitwise Shifts
7&Bitwise AND
8^Bitwise XOR
9``Bitwise OR
10==, !=, >, <, >=, <=Comparisons
11notLogical NOT
12andLogical AND
13 (Lowest)orLogical OR

Tip: When in doubt, use parentheses () to make the order explicit.

Summary

  • Python has 7 categories of operators: Arithmetic, Comparison, Logical, Assignment, Identity, Membership, and Bitwise.
  • Comparison operators return Boolean values, forming the basis of conditional logic.
  • Logical operators (and, or, not) combine conditions.
  • Identity (is) checks memory reference, while Equality (==) checks value.
  • Operator precedence determines the order of evaluation; use parentheses for clarity.