Python Clarity Series – Episode 25 Topic: Mutable vs Immutable Function Behavior 📌 Why did my list change after function call? def modify(lst): lst.append(100) a = [1, 2] modify(a) print(a) Output: [1, 2, 100] 👉 Lists are mutable → changes reflect outside Now: def modify(x): x = x + 10 a = 5 modify(a) print(a) Output: 5 👉 Integers are immutable → no change outside 💡 Rule: Mutable → changes persist Immutable → changes don’t This confusion causes logic errors. #PythonBasics #FunctionConcepts #StudentClarity #python #clarity
Mutable vs Immutable Function Behavior in Python
More Relevant Posts
-
Python Series — Day 3 🧠 Let’s level it up a bit 👇 What will be the output of this code? def modify_list(lst): lst.append(4) a = [1, 2, 3] modify_list(a) print(a) Options: A. [1, 2, 3] B. [1, 2, 3, 4] C. Error D. None Think carefully 👀 (Hint: It’s not about functions… it’s about how Python handles data) Drop your answer 👇 Answer tomorrow 🚀 #Python #CodingChallenge #LearningInPublic #DataEngineering #Tech
To view or add a comment, sign in
-
-
Built Linear Regression from scratch using Python (no libraries) Wanted to understand what’s happening under the hood before moving to sklearn. So I implemented a simple model to predict marks based on hours studied using Gradient Descent. 🔹 What I did: Implemented the prediction function (y = wx + b) Calculated Mean Squared Error (MSE) manually Computed gradients and updated parameters over 1000 epochs 🔹 What I learned: How gradient descent updates weights step by step Why learning rate plays a critical role How loss decreases as the model learns 🔹 Result: The model successfully learned the relationship. Example: If a student studies 9 hours → predicted marks ≈ 89.3 🔗 Code: https://lnkd.in/gPHCenhB Next step: implementing this using NumPy and then sklearn. #MachineLearning #Python #LearningInPublic
To view or add a comment, sign in
-
-
Most implementations of the State pattern in Python look very “clean”. Lots of small classes. A base interface. One class per state. But if you’ve ever worked with one in a real project, you know the downside: transitions are scattered, behaviour is hard to see in one place, and adding new states often means touching multiple files. In today’s video, I rebuild the State pattern in a very different way. Instead of relying on inheritance, I make the state machine explicit as data and use decorators to define transitions. The result is a small, reusable engine where the entire flow becomes visible at a glance. If you’re interested in writing Python that’s easier to reason about and extend, this is a pattern worth understanding. 👉 Watch here: https://lnkd.in/e9Y3xGNF. #python #softwaredesign #designpatterns #statemachine #cleancode
To view or add a comment, sign in
-
-
Most implementations of the State pattern in Python look very “clean”. Lots of small classes. A base interface. One class per state. But if you’ve ever worked with one in a real project, you know the downside: transitions are scattered, behaviour is hard to see in one place, and adding new states often means touching multiple files. In today’s video, I rebuild the State pattern in a very different way. Instead of relying on inheritance, I make the state machine explicit as data and use decorators to define transitions. The result is a small, reusable engine where the entire flow becomes visible at a glance. If you’re interested in writing Python that’s easier to reason about and extend, this is a pattern worth understanding. 👉 Watch here: https://lnkd.in/eg22yEHR. #python #softwaredesign #designpatterns #statemachine #cleancode
To view or add a comment, sign in
-
-
Python Clarity Series – Episode 24 Topic: Late Binding in Loops (Functions) ⚠️ Advanced pitfall: Late binding in loops funcs = [] for i in range(3): funcs.append(lambda: i) for f in funcs: print(f()) Output: 2 2 2 ❗ 👉 Expected: 0 1 2 👉 Got: same value 💡 Reason: Lambda captures variable, not value. 💡 Fix: funcs.append(lambda i=i: i) 💡 Rule: Default arguments capture current value. This is a classic interview trap. #PythonAdvanced #CodingPitfalls #DeveloperLevel #python #clarity
To view or add a comment, sign in
-
-
Day 60/100 — #100DaysOfCodingChallenge 60 days in… consistency is slowly turning into a habit now. 🔹 Python (DSA) Solved Search a 2D Matrix — used binary search by treating the matrix like a flattened sorted array. It was a nice reminder of how powerful binary search can be when applied smartly. 🔹 SQL Did some light practice to keep concepts fresh and maintain the streak. #Python #SQL #DSA #LeetCode #Day60 #100DaysOfCode #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 55/100 – #100DaysOfCode 🚀 Solved LeetCode #205 – Isomorphic Strings (Python). Today I practiced hashmap (dictionary) usage to check whether two strings follow the same pattern. Approach: 1) Create two hashmaps to store character mappings in both directions. 2) Traverse both strings together using zip(). 3) Check if the current mapping is consistent in both maps. 4) If any mismatch is found, return False. 5) Otherwise, update the mappings and continue. 6) If all mappings are valid, return True. Time Complexity: O(n) Space Complexity: O(n) Understanding how bidirectional mapping ensures consistency 💪 #LeetCode #Python #DSA #HashMap #Strings #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 35 / #120DaysOfCode – LeetCode Challenge ✅ Problem Solved: • Minimum Distance Between Equal Elements 💻 Language: Python 📚 Key Learnings: • Used HashMap (defaultdict) to store indices of elements • Learned how to track positions efficiently • Applied distance calculation between indices • Improved understanding of index-based problems • Practiced optimizing solution using O(n) approach Consistency + Practice = Progress 📈 🔗 LeetCode Profile: https://lnkd.in/gbeMKcv5 #LeetCode #Python #DSA #HashMap #Algorithms #CodingJourney #Consistency #120DaysOfCode
To view or add a comment, sign in
-
-
Python didn’t throw an error… and that’s the problem x = 10 x = "10" Later: print(x + 5) This crashes. Not when the mistake happened… but later, when the variable was actually used. That’s the difference. Python lets you change types freely (dynamic typing), but errors only show up at runtime. In C++, this wouldn’t even compile. You’d catch it immediately. So the real question is: Do you want errors early… or flexibility first and consequences later? Day 3/30 #Python #C++ #LearningInPublic #30DaysOfCode
To view or add a comment, sign in
-
-
Python Challenge – Can you solve this? Today was all about deep-diving into Lists vs. Sets and I came across a common mistake that we can sometimes overlook. Let’s test your Python understanding👇 numbers = [1, 2, 3] numbers.append([4, 5]) print(len(numbers)) A) 3 B) 4 C) 5 D) Error It’s a classic interview question that tests if you truly understand how Python handles memory and lists. Day 15/30 #30DaysOfCode #DataStructures #Day15 #PythonQuiz
To view or add a comment, sign in
-
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