🚀 Python Secret #2: The Ghost of Dictionaries 👻 Ever seen this error? data = {"a": 1} print(data["b"]) # KeyError 💀 👉 Missing key = crash. But what if… you could control what happens when a key is missing? 😈 --- 🧠 Meet the hidden method: "__missing__" Most developers don’t know this exists. If you create a custom dictionary and define "__missing__", Python will call it automatically when a key is not found. --- 🔥 Example: class MyDict(dict): def __missing__(self, key): return f"Key '{key}' not found 😏" data = MyDict({"a": 1}) print(data["a"]) # 1 print(data["b"]) # Key 'b' not found 😳 👉 No error. No crash. Full control. --- 💡 Real Power Use Cases: ✔️ Default values without "get()" ✔️ Dynamic data generation ✔️ Smart fallback systems ✔️ API response handling --- 💀 Pro Example: class SquareDict(dict): def __missing__(self, key): return key * key nums = SquareDict() print(nums[4]) # 16 🔥 print(nums[10]) # 100 🚀 👉 Missing key = calculated on the fly. --- 🧠 Insight: “Dictionaries don’t fail… unless you let them 😈” --- 💬 Did you know about "__missing__"? Follow for more Python secrets 🐍 Day 2/30 — Let’s go deeper 🚀 #Python #Coding #Programming #Developers #PythonTips #LearnToCode #Tech #AI #100DaysOfCode
Python Dictionary Error Handling with __missing__
More Relevant Posts
-
I made Python talk to me, and it actually responded 😅 At first, I was just writing code. No interaction. No feedback. Just, output. Then I discovered something simple but powerful: The input() function Let me explain this like I’m talking to a baby Imagine you have a small robot You ask it: “Tell me anything…” The robot pauses… waits… then listens to you. After you talk, it replies: “Hmm… what you said… Really?” That’s exactly what this code does: Python anything = input("Tell me anything...") print("Hmm...", anything, "... Really?") What is happening here? • input() → Python asks you a question • It waits for your answer • It stores what you typed • print() → Python responds to you I used to think python just runs commands Now I see python can actually interact with users. Why this matters in Data Analysis As I move deeper into: Excel, SQL, Tableau and Python I’m realizing that: • You can collect user input • Make your analysis interactive • Build smarter tools Not just static reports, but dynamic systems Python is not just a tool, it’s something you can actually “talk to.” If you're learning python, what was the first thing you made Python do for you? 😅 #Python #DataAnalytics #LearningInPublic #SQL #Excel #Tableau #Programming #TechJourney #BeginnerInTech #DataScience #CareerGrowth
To view or add a comment, sign in
-
-
Treating NumPy arrays like fancy Python lists, you’re leaving significant performance on the table. For senior devs and ML engineers, the difference between Basic and Advanced indexing isn't just syntax it's a fundamental shift in memory management. 1. The Trailing Comma Trap Consider these two operations on an array x: view = x[(1, 2, 3)] copy = x[(1, 2, 3),] To a junior dev, they look nearly identical. To the NumPy engine, they are worlds apart: Basic Indexing (x) returns a view. It manipulates internal strides and offsets without touching a single byte of raw data. This is time and memory. Advanced indexing (x[(1, 2, 3), ]) triggers a copy. Because you provided a tuple containing a sequence, NumPy allocates new RAM and physically moves data Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view. 2. The Mechanics of ndarray An ndarray is a contiguous block of memory. Its power comes from vectorization delegating loops to optimized C/C++ and SIMD instructions. Avoid: [abs(val) for val in large_array] (Slow Python interpreter overhead). Prefer: np.abs(large_array) (Fast, vectorized execution). 3. Practical Senior-Level Tip: np.newaxis Stop using .reshape() blindly. When you need to turn a row into a column for broadcasting (e.g., B[:, np.newaxis]), you are creating a view by adding a new dimension of length 1. it’s a zero-cost abstraction that keeps your data contiguous and your cache lines happy. The Rule of Thumb: If you don't need a copy, don't use a comma. Keep your indexing basic to keep your pipelines efficient. happy learning #Python #NumPy #DataEngineering #PerformanceOptimization #MachineLearning #SoftwareArchitecture
To view or add a comment, sign in
-
Most data analysts know Python. But not everyone uses it effectively. This image covers some advanced Pandas techniques, and honestly, these are the kind of things that make a real difference in day-to-day work. Not because they’re “advanced", but because they make your code cleaner, faster, and easier to maintain What stood out to me is Instead of writing long, step-by-step transformations, you can chain operations for cleaner pipelines, use vectorized calculations instead of loops, and combine multiple aggregations in a single step. Also, small things matter more than we think: 🔺 selecting only required columns 🔺 handling missing data thoughtfully 🔺 using proper joins instead of manual merges These don’t sound fancy, but they save a lot of time in real projects. 𝐈'𝐦 𝐡𝐨𝐬𝐭𝐢𝐧𝐠 𝐚 𝐰𝐞𝐛𝐢𝐧𝐚𝐫 𝐨𝐧 𝐀𝐩𝐫𝐢𝐥 26. 𝐌𝐨𝐫𝐞 𝐝𝐞𝐭𝐚𝐢𝐥𝐬 𝐡𝐞𝐫𝐞: 👇 https://lnkd.in/gXQZCDV8 Visual Credits: Sohan Sethi 𝑾𝒂𝒏𝒕 𝒕𝒐 𝒄𝒐𝒏𝒏𝒆𝒄𝒕 𝒘𝒊𝒕𝒉 𝒎𝒆? 𝘍𝒊𝒏𝒅 𝒎𝒆 𝒉𝒆𝒓𝒆 --> https://lnkd.in/dTK-FtG3 Follow Shreya Khandelwal for more such content. ************************************************************************ #Python #DataScience #Pandas #Analytics
To view or add a comment, sign in
-
-
Same condition. Same variables. Different result… depending on how you write it. 🤯 This is where Python stops being “easy” and starts being precise. 🧠 Today’s concept: Truthiness, Short-Circuiting & Operator Precedence Three small ideas. Massive impact. # 1. Truthiness (Not just True/False) data = [] if data: print("Has data") else: print("Empty ❌") 👉 Empty values ([], {}, "", 0, None) are False 👉 Everything else is True # 2. Short-Circuiting (Python stops early) def check(): print("Checking...") return True result = False and check() print(result) 👉 Output: False 👉 check() NEVER runs Because: False and anything → already False Python doesn’t evaluate further # 3. OR short-circuit behavior def fallback(): print("Fallback executed") return "Default" value = "Data" or fallback() print(value) 👉 Output: "Data" 👉 fallback() NEVER runs Because: True or anything → already True # 4. Operator Precedence (Silent bugs ⚠️) a = True b = False c = False result = a or b and c print(result) 👉 Output: True Because Python reads it as: a or (b and c) NOT: (a or b) and c ⚠️ Real-world bug pattern # Looks correct, but isn't if user == "admin" or "manager": print("Access granted") 👉 ALWAYS True ❌ Correct way: if user == "admin" or user == "manager": 💡 Advanced takeaway: and → returns first False or last True value or → returns first True value Conditions don’t always return True/False—they return actual values #Python #AdvancedPython #CodingJourney #LearnInPublic #100DaysOfCode #SoftwareEngineering #Debugging #TechSkills
To view or add a comment, sign in
-
Day-23 — Build a RAG Pipeline From Scratch 💡 Build a RAG pipeline in ~30 lines of Python Stop letting your AI guess — make it answer from your data. from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings, ChatOpenAI from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.chains import RetrievalQA from langchain.schema import Document docs = [ Document(page_content="Q3 revenue was $2.4M, up 18%."), Document(page_content="Top product: Automation Suite at $890K."), Document(page_content="Churn dropped from 6.1% to 4.2% in Q3."), ] chunks = RecursiveCharacterTextSplitter( chunk_size=200, chunk_overlap=20 ).split_documents(docs) db = Chroma.from_documents(chunks, OpenAIEmbeddings()) qa = RetrievalQA.from_chain_type( llm=ChatOpenAI(model="gpt-4o"), retriever=db.as_retriever() ) print(qa.invoke("How much did churn improve?")["result"]) Swap in your PDFs, docs, SQL data — same pattern. Drop a 🔥 if you want the SQL version. #Python #RAG #LangChain #ChromaDB #AIEngineering #OpenAI
To view or add a comment, sign in
-
Day 0 - #100DaysOfCode Where I am currently: Python: ✦✦✦✧✧✧ (3/6) I’ve been practicing NumPy and Pandas through isolated problems for ~2 months: - https://lnkd.in/gHU9AkWt - https://lnkd.in/g7Zy6_-h For visualization, I haven’t practiced separately. My knowledge comes from references and usage in projects: - https://lnkd.in/g7A56DqJ I’ve already gone through ML theory once and made notes, so now I just revisit them whenever I need to refresh something. I’ve completed one guided ML project. I relied heavily on guidance and spent too much time going deep into EDA, which slowed my progress. In this project: - No data cleaning (dataset was already clean) - Performed EDA: feature comparisons, correlations, histograms, boxplots - Guided feature selection based on trends and correlations - Reframed problem: good (7–8) vs bad (3–6) wine classification - Trained models: KNN, Naive Bayes, Random Forest, Logistic Regression - Evaluated using precision and recall - No deployment - Conclusion: Models performed similarly. Accuracy was limited due to class imbalance, making exact prediction difficult. Current Project: Predicting response time from NYC 311 service requests (2020+ dataset) - Using ~200k rows for simplicity - Currently in data cleaning phase Rules I follow: Not allowed: - Blindly follow tutorials - Ask "what should I do next?" - Change the problem midway Allowed: - Ask specific questions - Get stuck - Verify reasoning - Ask for code improvements only if I already understand and can implement the logic at some level (I must fully understand any improved version I use) Goal: Make decisions independently and keep the project as unguided as possible.
To view or add a comment, sign in
-
Python isn't about being clever; it's about being concise. 👉 Here are 10 one-liners that actually save time in production. 1. Flatten a Nested List: [item for sublist in nested for item in sublist] – A list comprehension that turns a 2D list into a flat 1D list. 2. Swap Variables: a, b = b, a – Pythonic variable swapping using tuple unpacking (no temp variable needed). 3. Read File into Lines: open("f.txt").read().splitlines() – Efficiently reads a file and removes trailing newline characters. 4. Count Frequencies: from collections import Counter; Counter(data) – Quickly generates a dictionary of element counts. 5. Reverse Anything: value[::-1] – Uses slicing to reverse strings, lists, or tuples in one go. 6. Ternary Operator: x = "Yes" if condition else "No" – Compact inline conditional assignments. 7. Chained Comparisons: if 0 < x < 10: – Readable range checks that mirror mathematical notation. 8. List to String: ", ".join(map(str, values)) – Joins a list of items (even non-strings) into a single formatted string. 9. Pretty Print: from pprint import pprint; pprint(data) – Formats complex dictionaries or JSON into a readable structure. 10. Easter Eggs: import antigravity – A fun hidden feature that opens a classic XKCD comic about Python. #Python #CodingTips #DataEngineering #SoftwareEngineering #DataEngineer
To view or add a comment, sign in
-
-
𝗪𝗵𝘆 𝗘𝘃𝗲𝗿𝘆 𝗣𝘆𝘁𝗵𝗼𝗻 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗡𝗲𝗲𝗱𝘀 𝘁𝗼 𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗮𝗻𝗱𝗮𝘀 Raw Python loops on tabular data are slow, unreadable, and honestly just painful to maintain. The moment your dataset grows beyond a few hundred rows, you feel it — both in runtime and in your code quality. 𝗣𝗮𝗻𝗱𝗮𝘀 solves this. It gives you a complete, expressive toolkit for data manipulation, cleaning, reshaping, and analysis — all built on top of NumPy with deep integration into the entire Python data science ecosystem. Here are 3 things that make Pandas genuinely powerful: - 𝗩𝗲𝗰𝘁𝗼𝗿𝗶𝘇𝗲𝗱 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 — instead of writing loops, you perform arithmetic and logic across entire columns at once. df['A'] + df['B'] beats manual iteration every single time — faster execution, cleaner code. - 𝗙𝗹𝗲𝘅𝗶𝗯𝗹𝗲 𝗗𝗮𝘁𝗮 𝗖𝗹𝗲𝗮𝗻𝗶𝗻𝗴 — .isna(), .fillna(), .dropna(), .drop_duplicates(), and .astype() handle all the messy real-world data problems without custom functions or boilerplate code. - 𝗣𝗼𝘄𝗲𝗿𝗳𝘂𝗹 𝗚𝗿𝗼𝘂𝗽𝗶𝗻𝗴 & 𝗠𝗲𝗿𝗴𝗶𝗻𝗴 — .groupby() lets you split, apply, and combine data in one line. pd.merge() brings SQL-style joins directly into your Python workflow. Conclusion:- Pandas is not just a library — it is the foundation of practical data work in Python. Once you move from raw loops to vectorized operations, method chaining, and expressive querying, you stop wrestling with your data and start actually understanding it. If you are serious about Python, Pandas is non-negotiable. Special thanks to my mentor Mian Ahmad Basit for the continued guidance. #MuhammadAbdullahWaseem #Nexskill #Pandas #PythonDeveloper #Ceasefire #IslamabadTalks
To view or add a comment, sign in
-
Revisiting Python dictionaries today I have used dicts for years. But watching this video made me realise I was only using about 30% of what they can do. Here is how I am now thinking about dictionaries in data pipelines: As schema maps: column_map = {'CUST_ID': 'customer_id', 'ACCT_BAL': 'account_balance'} One dictionary. Update one place when the source changes. Everything downstream adapts. As pipeline config: Every script I write now starts with a config dict at the top. Source path, target path, partition column, batch size. No hardcoded values buried inside logic. As error collectors: Accumulate errors by rule name during validation. Write the entire dict to a log table at the end. Instant audit trail. As lookup tables: Small reference data loaded once into a dictionary and reused hundreds of times — much faster than hitting the database on every row. An immediate thing to note: 'Dictionaries are optimised for lookup. If you are searching through a list for a value, ask yourself if a dictionary would be better.' I had a list-based lookup I immediately refactored after that. How do you use dictionaries in your work? ---- #Python #DataEngineering #LearningInPublic #CleanCode #ETL
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