pythonbytes
pythonbytes
10 Python Bytes
R.S.Sriharsha
Data Scientist III, Zepto
MTech DA'20, NIT Trichy
@swamysriharsha
Python Bytes #1: Mutability vs🔍
Immutability in Python 🔍
Why do some objects change "in place" while others seem to "create a
new version" when modified? It all comes down to mutability.
✅ Mutable objects can be changed after creation (e.g., list, dict, set).
✅ Immutable objects cannot be changed—any "modification" creates a
new object (e.g., int, str, tuple).
🔑 Key takeaway: Immutable objects like int and str create new objects
when "modified."
🔍
Python Bytes #2: Integer and String
Interning in Python 🔍
✅ Integer Interning: Python caches integers between -5 and 256 to
save memory and improve performance.
✅ String Interning: Simple strings (without special characters) are also
automatically cached.
👉 a = 1000; b = 1000:
These are two separate assignments. Python creates a new object for
each assignment since 1000 is outside the interning range (-5 to 256).
🔑 Key takeaway:
👉 Single Assignment (a = b = value) → Python reuses the same object.
👉 Separate Assignments (a = value; b = value) → Python creates new
objects for integers outside the interning range.
Python Bytes #4: Understanding 🔍
is vs == in Python 🔍
✅ In Python, you may have encountered both is and ==, but do you know
when to use each one? Understanding the distinction can help you avoid
bugs and write more precise code.
🔑 For immutable objects like integers, strings, and tuples, Python may
reuse memory for the same value (a technique called interning). This
leads to is and == behaving similarly, but only for small values.
Python Bytes #4: Understanding🔍
is vs == in Python 🔍
What’s happening in the below example?
👉 Even though a == b is True because their values are the same, Python
does not reuse memory for large integer values like 1000. Therefore, a
and b are different objects in memory, and is returns False.
🔄
Python Bytes #5: Variable Scoping in
Python: For-Loop vs List Comprehension 🔄
How does a loop variable behave in different contexts? Let’s dive into an
interesting difference between for-loops and list comprehensions in
Python.
🌀 For-Loop Behavior:
A for-loop leaks the loop variable into the surrounding scope. This
means that after the loop finishes, you can still access the loop variable.
🔑 Key Idea: The % operator finds the remainder after subtracting the
largest multiple of the divisor that is less than or equal to the dividend
(for positive divisors) or greater than or equal to the dividend (for
negative divisors).
2️⃣ -11 % 4:
👉 The largest multiple of 4 that is ≤ -11 is -12 (since 4 × -3 = -12).
👉 The remainder is -11 − (-12) = 1
3️⃣ 11 % -4
👉 The largest multiple of -4 that is ≥ 11 is 12 (since -4 × -3 = 12).
👉 The remainder is 11 − 12 = -1
4️⃣ -11 % -4
👉 The largest multiple of -4 that is ≥ -11 is -8 (since -4 × 2 = -8).
👉 The remainder is -11 − (-8) = -3
Python Bytes #8:
Modulus Operator % in Python –
🔍More Than Just Remainders 🔍
🔑 Pro Tip: Always consider edge cases like dividing by zero or when the
dividend is smaller than the divisor.
⚡
Python Bytes #9: Short-Circuiting in
Python’s Logical Operators ⚡
✅ In Python, logical operators like and and or use a technique called
short-circuiting, which means they stop evaluating as soon as the result
is determined. This can lead to more efficient code, as unnecessary
operations are skipped. Let’s break it down with examples!
👉 Using or: With the or operator, if the first condition is True, the second
condition is not evaluated because the result will always be True.
⚡
Python Bytes #9: Short-Circuiting in
Python’s Logical Operators ⚡
What’s happening in the below example?
👉 First condition: a > 10 is False because a is 5.
👉 Since and requires all conditions to be True, Python stops here and
doesn’t check the rest of the conditions (a < 100 and a % 5 != 0) because
it already knows the result will be False
🔑 Pro Tip: For calculations requiring high precision, consider using the
decimal module in Python, which allows for arbitrary precision arithmetic.