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
Python Late Binding in Loops: Avoiding a Common Pitfall
More Relevant Posts
-
Day 102 Backtracking patterns are starting to repeat. #Day102 🧩 90. Subsets II How today went: • Similar to the basic Subsets problem • Key difference: handling duplicates • Sorting the array is important • While iterating, skip duplicates to avoid repeating subsets What clicked: Backtracking becomes easier when you: • Recognize the base pattern • Add constraints like duplicate handling Same structure, new rule. That’s how patterns build. #LeetCode #DSA #Python #Backtracking #Recursion #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
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
To view or add a comment, sign in
-
-
Here's what's happening... Output: l 🐍 Did you get it right? The key here is chained indexing — name[0] gives you the string, then [1] digs into that string. Step by step: name[0] → "Ali" (first element of the tuple) "Ali"[1] → "l" (second character of the string, index starts at 0!) ✅ This is called chained indexing. Follow for more Python puzzles 🔥 #python #pythontips #learnpython #pythonprogramming #viral
To view or add a comment, sign in
-
-
🚀 Day 72 of #100DaysOfCode Today’s problem: Words Within Two Edits of Dictionary. At first, it looked like a simple string comparison… but checking all possibilities made it tricky. Had to focus on counting differences instead of overcomplicating transformations. 💡 What helped me: Compare character by character → count mismatches Stop early if differences exceed the limit. It’s more about controlling comparisons than trying every change. Slowly getting better at string problems. #DSA #LeetCode #CodingJourney #Python #Strings
To view or add a comment, sign in
-
-
Day 53/100 – #100DaysOfCode 🚀 Solved LeetCode #125 – Valid Palindrome (Python). Today I practiced string cleaning and validation to check whether a given string is a palindrome. Approach: 1) Traverse the string and keep only alphanumeric characters. 2) Convert all characters to lowercase. 3) Build a cleaned string. 4) Compare the string with its reverse. 5) If both are equal, return True; otherwise, return False. Time Complexity: O(n) Space Complexity: O(n) Learning how preprocessing simplifies string problems 💪 #LeetCode #Python #DSA #Strings #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Working with messy real-world datasets taught me one thing: The cleaning step takes longer than the actual analysis. So I spent the last few weeks building dfdoctor - an open-source Python library that audits your DataFrame, tells you what’s wrong, and helps you fix it systematically instead of manually. It helps you quickly understand what’s broken and what to fix first. The part I'm most proud of: 5 correlation methods (including Kendall τ and Phi-k) implemented from scratch in pure numpy - no scipy dependency anywhere. 164 tests. CI passing across Python 3.9–3.12. Try it: pip install dfdoctor https://lnkd.in/e-ChV6mE #Python #OpenSource #DataEngineering #Pandas #EDA #DataScience
To view or add a comment, sign in
-
-
Day 33/100 – #100DaysOfCode 🚀 Solved LeetCode #1480 – Running Sum of 1d Array (Python). Today I practiced prefix sum logic to compute the running sum of an array. Approach: 1) Initialize an empty list to store the running sum. 2) Maintain a variable sum = 0. 3) Traverse the array and keep adding each element to sum. 4) Append the updated sum to the result list. 5) Return the final running sum array. Time Complexity: O(n) Space Complexity: O(n) Understanding prefix sums helps solve many array problems efficiently 💪 #LeetCode #Python #DSA #Arrays #PrefixSum #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 7 of my Python learning journey: Today I focused on classic array and string patterns, and tried to keep solutions clean and efficient. What I solved: Two Sum using brute-force and hash map Valid Palindrome with two pointers Move Zeros with an in-place two-pointer approach Container With Most Water in O(n) Big takeaway: Correctness first, clarity second, optimization third. Small design choices, like in-place updates vs extra arrays, really affect code quality. GitHub link: https://lnkd.in/gGPw8_js #Python #ProblemSolving #Algorithms #TwoPointers #LearningInPublic #CleanCode
To view or add a comment, sign in
-
Beyond String Concatenation When I started, I used to concatenate strings the old-school way. It was messy, prone to errors, and hard to read The Problem: Using + requires manual type conversion (like str(21)) and gets confusing with all the extra quotes and spaces Solution: F-strings Introduced in Python 3.6, F-strings makes your code: ✅ Readable: You see the full sentence structure ✅ Fast: They are more efficient than older methods ✅ Flexible: You can perform math or call methods directly inside { } It’s a small concept, but it’s one of the easiest ways to make code look 10x more professional. #Python #30DaysOfCode #BCA #LearningInPublic #Day21 #JECRC Day 21/30
To view or add a comment, sign in
-
-
Day 50/100 – #100DaysOfCode 🚀 Solved LeetCode #28 – Find the Index of the First Occurrence in a String (Python). Today I practiced string matching using a brute-force approach to find the first occurrence of a substring. Approach: 1) Traverse the main string (haystack). 2) For each index, try to match the substring (needle). 3) Compare characters one by one. 4) If all characters match, return the starting index. 5) If mismatch occurs, break and move to the next index. 6) If no match is found, return -1. Time Complexity: O(n × m) Space Complexity: O(1) Understanding basic string matching techniques step by step 💪 #LeetCode #Python #DSA #Strings #ProblemSolving #100DaysOfCode
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