Today a friend asked me a simple question: “What will be the output?” T = (1, 2, 3, [4, 5]) try: T[3] += [6] except Exception as e: print(e) print(T) I was confident. Tuple => immutable List => mutable So it should just add 6 to the list… right? Well… Here’s the actual output: ~ 'tuple' object does not support item assignment ~ (1, 2, 3, [4, 5, 6]) Wait… 🤯 If an exception was raised, how did the list change? What’s really happening? T[3] += [6] is not one operation. Python roughly does this: 1. The Fetch: Python gets the list at T[3]. 2. The Mutation: It calls .__iadd__([6]). Because lists are mutable, this modifies the list in-place. The list is now [4, 5, 6]. 3. The Assignment: It tries to assign that list back to T[3]. Step 2 succeeds. ✔️ Step 3 fails. ❌ So the tuple stays immutable But the list inside it was already modified The Real Insight Tuple immutability means: 👉 You cannot change what the tuple references 👉 But you can mutate the object it references (if it’s mutable) Immutability in Python is about bindings and references — not deep contents. And a great reminder that understanding how Python executes operations under the hood makes you a stronger developer. Have you ever been surprised by Python’s behavior like this? #Python #SoftwareEngineering #ProgrammingConcepts #CleanCode #LearningInPublic
Python Tuples and Immutability: A Surprising Twist
More Relevant Posts
-
𝐖𝐡𝐲 𝐱 𝐢𝐧 𝐬𝐞𝐭 𝐟𝐞𝐞𝐥𝐬 𝐢𝐧𝐬𝐭𝐚𝐧𝐭… 𝐛𝐮𝐭 𝐱 𝐢𝐧 𝐥𝐢𝐬𝐭 𝐝𝐨𝐞𝐬𝐧’𝐭 👇 𝑻𝒘𝒐 𝒍𝒊𝒏𝒆𝒔 𝒕𝒉𝒂𝒕 𝒍𝒐𝒐𝒌 𝒕𝒉𝒆 𝒔𝒂𝒎𝒆: ---> x in my_list ---> x in my_set Both answer a simple question: “Is x present?” But their time complexity is very different. 🧾 𝐋𝐢𝐬𝐭𝐬 → 𝐥𝐢𝐧𝐞𝐚𝐫 𝐬𝐞𝐚𝐫𝐜𝐡 (𝐎(𝐧)) 𝑨 𝒍𝒊𝒔𝒕 𝒊𝒔 𝒍𝒊𝒌𝒆 𝒄𝒉𝒆𝒄𝒌𝒊𝒏𝒈 𝒂 𝑵𝒐𝒕𝒆𝒃𝒐𝒐𝒌: You start at page 1… then page 2… then page 3… Worst case: you check every element. nums = [3, 8, 2, 10, 7] 7 in nums # Python checks one-by-one So membership in a list is 𝑶(𝒏) — time grows with size. ⚡ 𝐒𝐞𝐭𝐬 → 𝐡𝐚𝐬𝐡𝐢𝐧𝐠 (𝐚𝐯𝐞𝐫𝐚𝐠𝐞 𝐎(1)) 𝑨 𝒔𝒆𝒕 𝒊𝒔 𝒎𝒐𝒓𝒆 𝒍𝒊𝒌𝒆 𝒂 𝒍𝒐𝒄𝒌𝒆𝒓 𝒔𝒚𝒔𝒕𝒆𝒎. Instead of scanning all elements, Python: Computes a hash of the value Jumps directly to its bucket nums = {3, 8, 2, 10, 7} 7 in nums # direct lookup No scanning. Just direct access → 𝐚𝐯𝐞𝐫𝐚𝐠𝐞 𝐎(1). 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐝𝐞𝐭𝐚𝐢𝐥 Set lookup is O(1) on average, not magic. If many values collide into the same bucket (rare but possible), it can degrade — but Python handles hashing well, so in practice it stays near constant time. 𝐏𝐫𝐚𝐜𝐭𝐢𝐜𝐚𝐥 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Use a list when: -order matters -duplicates matter -small dataset Use a set when: -you need fast membership checks -uniqueness matters -solving frequency / existence problems 𝑾𝒉𝒂𝒕 𝒔𝒎𝒂𝒍𝒍 𝑷𝒚𝒕𝒉𝒐𝒏 𝒅𝒆𝒕𝒂𝒊𝒍 𝒔𝒂𝒗𝒆𝒅 𝒚𝒐𝒖 𝒕𝒉𝒆 𝒎𝒐𝒔𝒕 𝒅𝒆𝒃𝒖𝒈𝒈𝒊𝒏𝒈 𝒕𝒊𝒎𝒆? #Python #DataStructures #Algorithms #DSA #CodingTips #LearningInPublic #YogeshLearns
To view or add a comment, sign in
-
One-Line Sorting with Custom Lambda Key (Sort Integers by The Number of 1 Bits) 💯 || O(N log N) || Just tackled a fun bit-manipulation and sorting problem on LeetCode (Problem 1356), and I wanted to share a super clean, Pythonic way to solve it! 🐍 🎯 The Problem: Sort an array of integers based on the number of 1s in their binary representation. If two numbers have the same number of 1s, sort them by their decimal value. 💡 The Approach: Instead of writing a complex custom comparator, Python’s built-in sorting handles multi-level conditions beautifully. By passing a list [primary_condition, secondary_condition] to the key argument, Python sorts by the first element and automatically falls back to the second if there's a tie. Here is the one-liner: Python class Solution: def sortByBits(self, arr: List[int]) -> List[int]: return sorted(arr, key=lambda x: [bin(x).count('1'), x]) 📊 Complexity: Time: O(N log N) — Python's Timsort algorithm does the heavy lifting, and counting bits for 32-bit integers takes O(1) time. Space: O(N) — Using sorted() creates a new list. Pro-Tip: If you want to optimize for space, you can swap sorted(arr, ...) for arr.sort(...) to sort the array in-place! What is your favorite Python built-in function or one-liner tip? Let me know in the comments! 👇 #Python #LeetCode #Algorithms #CodingInterviews #SoftwareEngineering #DataStructures
To view or add a comment, sign in
-
Ever explained Duck Typing in Python to someone and watched their face go from 😃 → 🤯 in 3 seconds? Here’s how I tried explaining it to a friend: Friend: “How does Python know if something is a duck?” Me: Python doesn’t care if it’s a duck 🦆, a robot 🤖, or a developer pretending to work on Friday afternoon 😅 If it walks like a duck and quacks like a duck, Python just says: "Cool… must be a duck." Example 👇 class Duck: def quack(self): print("Quack!") class Person: def quack(self): print("I can imitate a duck!") def make_it_quack(obj): obj.quack() make_it_quack(Duck()) make_it_quack(Person()) Python: "Both quack? Perfect. I’m not asking for ID." Meanwhile in some other languages: "Excuse me sir, please submit 4 forms, 2 interfaces, and a type certificate before quacking." 🧾 That’s the beauty of Python — behavior matters more than type. So remember: In Python, nobody asks what you are. They only check what you can do. And honestly… that’s a life lesson too. 😄 #Python #DuckTyping #ProgrammingHumor #LearnToCode #PythonDeveloper #CodingLife #SoftwareEngineering #TechHumor #DeveloperLife
To view or add a comment, sign in
-
𝐍𝐮𝐦𝐏𝐲 𝐁𝐨𝐨𝐥𝐞𝐚𝐧 𝐓𝐫𝐚𝐩 I’m still at the very beginning of my Python journey. But even with my tiny amount of experience, I already hit a subtle NumPy trap that can easily sneak into real code. Python is full of surprises — even at the very beginning It happens when you create an untyped NumPy array and fill it with a function that should return booleans… …but sometimes returns 𝙉𝙤𝙣𝙚 when processing fails. At first, you expect a clean boolean array — because the function normally returns 𝙏𝙧𝙪𝙚 or 𝙁𝙖𝙡𝙨𝙚. But NumPy has other plans. Here’s the trap 👇 🟥 𝟏) 𝐔𝐧𝐭𝐲𝐩𝐞𝐝 𝐚𝐫𝐫𝐚𝐲 + 𝐚 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐭𝐡𝐚𝐭 “𝐬𝐡𝐨𝐮𝐥𝐝” 𝐫𝐞𝐭𝐮𝐫𝐧 𝐛𝐨𝐨𝐥𝐞𝐚𝐧𝐬 𝒂𝒓𝒓 = 𝒏𝒑.𝒆𝒎𝒑𝒕𝒚(10) # 𝒏𝒐 𝒅𝒕𝒚𝒑𝒆 𝒂𝒓𝒓[𝒊] = 𝒎𝒚_𝒇𝒖𝒏𝒄() # 𝑻𝒓𝒖𝒆 / 𝑭𝒂𝒍𝒔𝒆 ... 𝒐𝒓 𝑵𝒐𝒏𝒆 You expect a clean boolean array because the function usually returns 𝙏𝙧𝙪𝙚/𝙁𝙖𝙡𝙨𝙚. But if even one value is 𝙉𝙤𝙣𝙚, NumPy must pick a type that can hold all values. 🟦 𝟐) 𝐍𝐮𝐦𝐏𝐲 𝐬𝐢𝐥𝐞𝐧𝐭𝐥𝐲 𝐬𝐰𝐢𝐭𝐜𝐡𝐞𝐬 𝐭𝐨 𝐝𝐭𝐲𝐩𝐞=𝐨𝐛𝐣𝐞𝐜𝐭 𝒂𝒓𝒓𝒂𝒚([𝑻𝒓𝒖𝒆, 𝑭𝒂𝒍𝒔𝒆, 𝑵𝒐𝒏𝒆, ...], 𝒅𝒕𝒚𝒑𝒆=𝒐𝒃𝒋𝒆𝒄𝒕) Impact: no vectorization logical operations break masks behave unpredictably performance collapses You think you have a NumPy boolean array. You actually have a Python object array. 🟩 𝟑) 𝐓𝐡𝐞 𝐬𝐢𝐥𝐞𝐧𝐭 𝐜𝐨𝐧𝐯𝐞𝐫𝐬𝐢𝐨𝐧 𝐭𝐫𝐚𝐩 Trying to fix it: 𝒂𝒓𝒓 = 𝒏𝒑.𝒆𝒎𝒑𝒕𝒚(10, 𝒅𝒕𝒚𝒑𝒆=𝒃𝒐𝒐𝒍) 𝒂𝒓𝒓[𝒊] = 𝒎𝒚_𝒇𝒖𝒏𝒄() NumPy converts: 𝑵𝒐𝒏𝒆 → 𝑭𝒂𝒍𝒔𝒆 (silently) Impact: 👉you lose the meaning of “no result” 👉your data becomes wrong 👉the bug becomes invisible ⭐ 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Same code. Same function. Two completely different arrays. NumPy’s dtype inference can hide subtle bugs — and I found this one with almost no Python experience. 𝐂𝐮𝐫𝐢𝐨𝐮𝐬 𝐭𝐨 𝐤𝐧𝐨𝐰: 👉 Have you ever run into this behavior? 👉 Or another NumPy dtype surprise? #python #numpy #datascience #cleanCode #devTips #programming
To view or add a comment, sign in
-
-
Let’s keep it simple (and a little fun today 😄) Which of the following is a valid list comprehension in Python? A) [x for x in range(5) if x % 2 == 0] B) for x in range(5): if x % 2 == 0 C) x = [range(5) if x % 2 == 0] D) list(x for x in range(5) if x % 2 == 0) 🧠 The correct answer is: 👉 A [x for x in range(5) if x % 2 == 0] This is the classic Pythonic structure: [expression for item in iterable if condition] ✨ Option D technically works and returns a list, but A is the clean, direct list comprehension syntax. Small concept. Big readability difference. Clean code isn’t about writing more… It’s about writing smarter 🔥 #Python #AI #LearningInPublic #30DayChallenge #DataAnalytics #CleanCode
To view or add a comment, sign in
-
📘 Day 3 — Lists, If Conditions, For Loops… and Why Indentation Matters! Today’s learning felt like the moment Python started behaving like a real analysis tool. I covered: 🔹 Lists → storing multiple values 🔹 If Conditions → applying logic 🔹 For Loops → automating repetition 🔹 Indentation → the rule that makes everything work First — Lists Just like a column in Excel, Python lets us store multiple values together: marks = [65, 72, 58, 90, 48] Second — If Condition This allows Python to make decisions based on rules: if mark > 60: print("Pass") else: print("Fail") Third — For Loop Instead of checking each value manually, Python can go through the whole list: for mark in marks: if mark > 60: print("Pass") else: print("Fail") Now the Most Important Part — Indentation In many tools, spacing is just for readability. But in Python, indentation defines the structure of the code. Python uses indentation to understand: ✔ What belongs inside the loop ✔ What belongs inside the condition ✔ Where logic starts and ends Notice how everything inside the loop is indented: for mark in marks: if mark > 60: print("Pass") If indentation is wrong, Python throws an error — even if the logic is correct. So the key rule I learned today: 👉 Same logic block = Same indentation (usually 4 spaces) Today felt like moving from “writing code” to “teaching Python how to think through data.” #PythonLearning #DataAnalyticsJourney #codebasics #OnlineCredibility
To view or add a comment, sign in
-
-
Day 17 – Strings, Lists & Practical Logic Building in Python Today’s session was a mix of real-world logic building and deeper practice with strings and lists in Python. I started with a simple electricity bill calculation program using conditional statements. It helped me understand how tier-based logic works in real-life scenarios and how to structure conditions properly using if-elif-else. Strings – More Practice Revised and practiced important string methods: upper() and lower() for case conversion isupper() and islower() for validation capitalize() vs title() replace() with control over number of replacements This reinforced how strings are immutable and how every operation returns a new modified string. Lists – Slicing & Methods in Depth Worked extensively on list operations and understood the difference between modifying and non-modifying methods. Slicing Practice: Normal slicing Step slicing Reverse slicing Skipping elements using step values List Methods Explored: append() → add single element extend() → add multiple elements from iterable insert() → add element at specific index remove() → remove first occurrence pop() → remove by index (returns removed value) clear() → empty the list index() → find position of element sort() → modifies original list sorted() → returns new sorted list reverse() → reverse list order join() → join list elements into a string I also observed: How insert() behaves with negative and out-of-range indexes Difference between sort() and sorted() How extend() works differently with list, tuple, and set Key Takeaways Understanding method behavior is more important than just memorizing syntax Some methods modify the original list, others return new values Real learning happens when testing edge cases Logic building is improving step by step Day 17 was more about strengthening fundamentals and building clarity in how Python handles data structures. #Python #PythonLists #PythonStrings #ProgrammingBasics #DataStructures #CodingPractice #DailyLearning #ProblemSolving #LearnToCode #SoftwareDevelopment
To view or add a comment, sign in
-
Day 63 of LeetCode Grind ⚡🔥 Two Easy problems today — but these are the exact problems that separate engineers who know their data structures from those who don't. Every array/string problem in interviews traces back to one of these two patterns. 1️⃣ Contains Duplicate (217) Pattern: HashSet for O(1) membership check <<< python return len(nums) != len(set(nums)) >>> * A set discards duplicates. If the set is smaller than the original array, a duplicate exists. One line. O(n) time, O(n) space. The deeper lesson: whenever you're asking "have I seen this before?" — reach for a set. 2️⃣ Valid Anagram (242) Pattern: Frequency Counter <<< python return Counter(s) == Counter(t) >>> * Two strings are anagrams ↔ identical character frequencies. * Python's Counter makes this one line, but the real skill is knowing why it works: we're hashing character → count and comparing maps. Follow-up: Works for Unicode too — Python dicts handle any hashable key, not just ASCII. ✨ Reflection: These two problems teach the two most reused patterns in all of array/string DSA: * Set → "Does this exist?" * HashMap/Counter → "How many times does this exist?" Master these two and you've unlocked the mental model behind 40% of Easy/Medium problems. #LeetCode #Day63 #Python #HashSet #HashMap #DataStructures #ProblemSolving #100DaysOfCode #BackToBasics
To view or add a comment, sign in
-
-
In Python, everything is an object. That one fact broke my mental model this week. I'm transitioning from Software engineering to AI engineering. Week 1, Day 7 🗓️ Here's what clicked 😲. Since everything in Python is an object, every object has a unique memory address. Dict uses this to store and find values in O(1). It hashes the key, jumps directly to the slot. No scanning. No searching. But this only works if the key never changes. A mutable key changes its hash, changes its slot, and your value becomes permanently unreachable. Silent corruption 🤯 . So dict keys must be immutable. And immutable means hashable. Hashable objects carry two methods that work as a pair: __hash__() generates the slot number & __eq__() confirms the exact key at that slot. Override one without the other and Python punishes you: ``` class BrokenDog: def __eq__(self, other): return self.name == other.name # forgot __hash__ hash(BrokenDog("Rex")) # ❌ TypeError: unhashable type ``` But Python's built-in types like dict and str already override __eq__() to compare contents and not addresses. That's why two dicts with identical keys and values return True on `==`. One more thing that surprised me. Since every object in Python carries this overhead of creation, hashing, and storage, even a simple `int` is not free. That's exactly why NumPy exists to escape Python's object model and work directly with raw memory for numerical computation. For more details, refer: https://lnkd.in/gZCGFRBB What's the Python concept that surprised you most when you first learned it? #AIEngineering #Python #LearningInPublic #CareerTransition #SoftwareEngineering
To view or add a comment, sign in
-
Just published my first open-source Python package — llmclean If you've ever piped raw LLM output directly into json.loads() and watched it explode, this is for you. Three utilities, zero dependencies: • strip_fences — removes the ```json ``` wrappers LLMs love to add • enforce_json — extracts valid JSON even when the model returns True instead of true, trailing commas, unquoted keys, or buries the JSON in prose • trim_repetition — cuts the looping tail when a model repeats itself Every function fails gracefully — if cleaning fails, you get the original back. It never throws. use with : pip install llmclean GitHub: https://lnkd.in/gQt4auQ2 PyPI : https://lnkd.in/guNpmtnm Built this because I kept copy-pasting the same cleaning logic across projects. Figured someone else probably was too. #Python #OpenSource #LLM #AI #Dev
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development