Day 12 of My Data Science Journey — Python Lists: Methods, Comprehension & Shallow vs Deep Copy Today’s focus was on one of the most essential data structures in Python — Lists. From data storage to manipulation, lists are used everywhere in real-world applications and data science workflows. 𝐖𝐡𝐚𝐭 𝐈 𝐋𝐞𝐚𝐫𝐧𝐞𝐝: List Properties – Ordered, mutable, allows duplicates, and supports mixed data types Accessing Elements – Used indexing, negative indexing, slicing, and stride for flexible data access List Methods – append(), extend(), insert() for adding elements – remove(), pop() for deletion – sort(), reverse() for ordering – count(), index() for searching and analysis Shallow vs Deep Copy – Understood that direct assignment does not create a new copy – Used copy(), list(), slicing for safe duplication – Learned the importance of copying, especially with nested data List Comprehension – Wrote concise and efficient code using list comprehension – Combined loops and conditions in a single readable line Built-in Functions – Used sum(), len(), min(), max() for quick data insights Additional Useful Methods – clear(), sorted(), zip(), filter(), map(), any(), all() 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Understanding how lists work — especially copying and comprehension — is critical for writing efficient and bug-free Python code. Lists are not just a data structure; they are a core tool for solving real-world problems. Read the full breakdown with examples on Medium 👇 https://lnkd.in/gFp-nHzd #DataScienceJourney #Python #Lists #Programming
Python Lists: Methods, Comprehension & Shallow vs Deep Copy
More Relevant Posts
-
Day 10 of My Data Science Journey — Lambda Functions, Variable Scope & Python Errors Day 10 was all about writing cleaner code, understanding how variables behave, and learning how to identify and fix common errors in Python. 𝐖𝐡𝐚𝐭 𝐈 𝐋𝐞𝐚𝐫𝐧𝐞𝐝: Lambda Functions – Explored anonymous one-line functions for concise operations – Used lambda with single and multiple arguments – Applied conditional logic within lambda expressions – Built simple function factories for reusable logic Variable Scope – Understood the difference between local and global variables – Learned how scope affects variable accessibility inside functions – Explored common issues like NameError and UnboundLocalError – Used the global keyword to modify global variables when needed Types of Python Errors – SyntaxError — incorrect syntax before execution – NameError — undefined variables or functions – TypeError — invalid operations between data types – IndexError — accessing out-of-range elements – AttributeError — using invalid methods – ZeroDivisionError — division by zero – LogicalError — code runs but produces incorrect results 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Understanding errors is just as important as writing code. Logical errors, in particular, are the most challenging because they don’t break the program — they silently produce wrong results. Additionally, writing readable functions with clear structure and minimal complexity is essential for maintainable code. 10 days into the journey — building a strong foundation step by step. Read the full breakdown with examples on Medium 👇 https://lnkd.in/gzfEYCQ4 #DataScienceJourney #Python #Lambda #Programming #Learning #Developers
To view or add a comment, sign in
-
My Data Science Journey — Python Tuple, Set, Dictionary & the Collections Library Today’s focus was on Python’s core data structures — Tuples, Sets, and Dictionaries — along with the powerful collections module that enhances their functionality for real-world use cases. 𝐖𝐡𝐚𝐭 𝐈 𝐋𝐞𝐚𝐫𝐧𝐞𝐝: Tuple – Ordered, immutable, allows duplicates – Single element tuples require a trailing comma → ("cat",) – Supports packing and unpacking → x, y = 10, 30 – Cannot be modified after creation (TypeError by design) – Faster than lists in certain operations – Used in scenarios like geographic coordinates and fixed records – Can be used as dictionary keys (unlike lists) Set – Unordered, mutable, stores unique elements only – No indexing or slicing support – Empty set must be created using set() ({} creates a dict) – .remove() raises KeyError if element not found – .discard() removes safely without error – Supports operations like union, intersection, difference, symmetric_difference – Methods like issubset(), issuperset(), isdisjoint() help in set comparisons – frozenset provides an immutable version of a set – Offers O(1) average time complexity for membership checks Dictionary – Key-value pair structure, ordered, mutable, and keys must be unique – Built on hash tables for fast lookups – user["key"] → raises KeyError if missing – user.get("key", default) → safe access with fallback – Methods: keys(), values(), items() for iteration – pop(), popitem(), update(), clear(), del for modifications – Widely used in real-world data like APIs and JSON responses – Common pattern: list of dictionaries for structured datasets Collections Library – namedtuple → tuple with named fields for better readability – deque → efficient queue with O(1) operations on both ends – ChainMap → combines multiple dictionaries without merging copies – OrderedDict → maintains order with additional utilities like move_to_end() – UserDict, UserList, UserString → useful for customizing built-in behaviors with validation and extensions Performance Insight – List → O(n) – Tuple → O(n) – Set → O(1) (average lookup) – Dictionary → O(1) (average lookup) 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Understanding when to use each data structure — and how collections enhances them — is crucial for writing efficient, scalable, and clean Python code. Read the full breakdown with examples on Medium 👇 https://lnkd.in/gvv5ZBDM #DataScienceJourney #Python #Tuple #Set #Dictionary #Collections #Programming #DataStructures
To view or add a comment, sign in
-
Python's Pro Tips : 🚀 What I learned building my first real Python dashboard (no one talks about this) Everyone talks about: 👉 Pandas 👉 Plotly 👉 AI tools But the real breakthrough for me was NOT libraries… It was understanding how Python actually thinks: 💡 1. Indentation = Logic Not formatting. Not style. 👉 One wrong space = broken program. GOLDEN RULE 👉 Every block must follow: if something: code (4 spaces) deeper code (8 spaces)à if file: → 0 spaces inside it → 4 spaces inside if len(...) → 8 spaces 🔒 BRACKETS Every: ( → must close ) [ → must close ] { → must close } WHY 0 / 4 / 8 SPACES? Python doesn’t use { } like JS… 👉 it uses INDENTATION = STRUCTURE 🔥 YOUR 3 if STATEMENTS (EXPLAINED CLEAN) ✅ LEVEL 1 (0 spaces) if file: 👉 This is TOP LEVEL 👉 Means: “only run everything below IF file is uploaded” ✅ LEVEL 2 (4 spaces) if categorical_cols: 👉 This is INSIDE if file 👉 Means: “only run this IF file exists AND categorical columns exist” ✅ LEVEL 3 (8 spaces) if selected_click != "None": 👉 This is INSIDE both above 👉 Means: “only run if: file exists categorical exists user selected something” 🎯 SIMPLE WAY TO SEE IT Think like this: IF file exists: DO A IF categorical exists: DO B IF user clicked: DO C 📊 VISUAL TREE (VERY IMPORTANT) if file: ← level 0 df = ... if categorical_cols: ← level 4 selected_click = ... if selected_click != "None": ← level 8 filtered_df = ... 💡 2. Execution Order matters 👉 You can’t use a variable before it exists. 👉 “Create → then use” (simple, but critical) 💡 3. Scope is everything If something is inside: if file: 👉 it ONLY exists there. 💡 4. Structure > Syntax You can know all libraries… But without structure, nothing works. 💡 5. Build → Break → Fix → Repeat That’s the real learning loop. Not tutorials. 🔥 After struggling with these, I built: ✔ Dynamic dashboard (multi-KPI) ✔ Power BI–style filtering ✔ Multi-chart engine ✔ RCA (insight layer coming next) 📌 Biggest takeaway: Stop chasing tools. Learn how things flow. Everyone learns in different way for me is outside à So first understand the whole picture (Structure) then see what section goes where and each section have what scripts inside it and how they structured (Spacing…..) That’s when everything clicks. Have difficult undertesting it I can explain in Mental Model: “Python Rooms & Floors” (only the first section if the image _Having the right Spacing_ kept me confused for while) :-) Next step: 📡 Turning this into a 💎 A commercial-grade telecom intelligence platform. Let’s see where this goes 🚀 Thanks for your impressions, feedbacks & comments
To view or add a comment, sign in
-
-
ANATOMY OF NUMPY NumPy is often described as "fast arrays for Python," but architecturally, it is a typed contiguous memory layout with vectorized C operations and broadcasting rules that let you compute on millions of elements without writing a single loop. Understanding its anatomy means knowing why np.dot beats a Python for-loop by 100×, and when it silently returns the wrong answer. 1. THE NDARRAY The core object: a contiguous block of memory plus metadata. dtype: fixed data type (float32, int64, bool). Chosen once, applied to every element. Wrong dtype silently loses precision. shape: tuple of dimensions. A (3, 4) array has 12 elements. Operations must agree on the shape or broadcast. strides: bytes to jump when moving along each axis. Transposes and slices just update strides, never copy data. 2. BROADCASTING NumPy aligns shapes from the right. Size-1 dimensions stretch to match. Rule 1: If shapes have different dimensions, prepend 1s to the shorter one. Rule 2: size 1 stretches to match. Anything else must agree, or broadcasting fails. Why it matters: avoids allocating huge intermediate arrays. x[:, None] - y[None, :] computes pairwise diffs with no loop. 3. VECTORIZATION Every operation is implemented in C with SIMD. Python loops are 50-200× slower. Ufuncs: np.sin, np.exp, np.add. Elementwise with no Python overhead. Reductions: sum, mean, argmax. Accept an axis argument to reduce along specific dimensions. Linear algebra: np.dot, np.linalg.solve, @ operator. Calls BLAS/LAPACK under the hood. 4. THE TRAPS Four foot-guns that bite every data scientist at least once. Views vs copies: slicing returns a view. Modifying a slice modifies the original. Use .copy() when you need independence. Integer overflow: int32 silently wraps at ±2.1 billion. Sum large integer arrays as int64 or float64. Shape mismatch: (n,) is not the same as (n, 1). The first is 1D, the second is 2D. Many bugs live in this gap. Hidden copies: fancy indexing with a boolean or integer array always returns a copy, even for a read. 🔥 THE BOTTOM LINE: The anatomy of NumPy is a balance between structure (dtype, shape, strides), math (vectorized C and BLAS calls), and iteration (broadcasting to avoid explicit loops). Master these three, and every Python data library built on NumPy (pandas, scikit-learn, PyTorch) starts to make sense at a lower level. #Python #NumPy #DataScience #MachineLearning #MLEngineering #AI
To view or add a comment, sign in
-
-
How to "Slice the Cake" in Python? 🎂🐍 (Slicing & Indexing) Once you’ve learned how to store strings, the big question is: Do we always have to use the entire text? 🧐 The Answer: Absolutely not! Python gives us precision tools (Indexing & Slicing) that allow us to manipulate text data and extract exactly what we need. At Data Hub, we use this constantly during Data Cleaning. Whether you're extracting specific "Product Codes" from a long string or separating "Dates" to generate accurate reports, these tools are your best friends. 📊 1️⃣ Indexing (Finding the Address): Remember, Python starts counting from 0, not 1. If we have: word = "Python" Letter P is at index 0 Letter y is at index 1 Letter n is at index 5 (or -1 if you count from the end) 💡 Pro Tip: Negative indexing is a lifesaver when dealing with long strings where you only need the last few characters! 2️⃣ Slicing (Cutting the Data): To extract a specific "portion" of text, we use the slice operator [start : stop]. word[0:4] ➡️ Starts at index 0 and stops "before" index 4. Result: Pyth. word[:] ➡️ Leaving it empty selects the entire string from start to finish. word[-3:-1] ➡️ Starts 3 characters from the end and stops before the last one. Result: ho. 🧠 The Bottom Line: Index is the "Address" of the character, while Slicing is the "Scissors" that separates the data. Mastering these is your first step toward becoming a Data Analyst who handles data with speed and intelligence! 👌 💬 Weekly Challenge: If you have the variable: name = "DataHub" What should we write between the brackets [ : ] to extract only the word "Data"? Show me your answers in the comments! 👇 #Python #DataAnalysis #DataHub #PythonBasics #DataScience #LinkedInLearning #Programming #DataCleaning
To view or add a comment, sign in
-
-
Week 4 hit differently. Not just because the material stacked up fast, but because this week, things started clicking into a much bigger picture. Digital Skola SESSION 10 Python is more than a language it's a way of thinking We went deep into Python's core data structures. List for ordered, mutable collections. Dictionary for key-value pairs that map attributes to values. Tuple for ordered, immutable sequences when you want data that shouldn't change. Then came Conditional Statements if, if-else, if-elif-else, and Nested-if giving programs the ability to make decisions. And Loops for, while, break, continue, pass, nested loops giving programs the ability to repeat. Separately, they're just syntax. Together, they're the building blocks of logic. SESSION 11 Functions: write once, use everywhere A function is a reusable block of code built for a specific task. Once defined with def, it can accept parameters, process logic, and return a value called as many times as needed without rewriting anything. We also got into scope: local variables live inside the function, global variables are accessible everywhere, and the global keyword bridges the two. Then Lambda anonymous one-liner functions inherited from Lambda Calculus theory, 1930. Short, clean, direct. And Modules and Packages, showing how Python code organizes itself into reusable, importable units at scale. The benefit isn't just shorter code. It's code that's easier to read, easier to fix, and easier to hand off to someone else. SESSION 12 NumPy: where Python becomes serious about data NumPy (Numerical Python) is the foundational library for scientific computing, linear algebra, and data analysis in Python. Its core is the ndarray a multidimensional array that's fast, homogeneous, and built for numerical work. 1D arrays as vectors. 2D as matrices. 3D as tensors. And a full toolkit of operations: reshape, flatten, transpose, concatenate, stack, split, resize, append, insert, delete, unique, and slicing. Each one exists for a reason, and understanding when to use which one is where the real learning lives. This week made it clear: data science isn't just about knowing tools. It's about understanding what each tool is actually doing to your data. Group 3 Baby Python is still very much in the game. The material gets heavier every week and that's exactly the point. #DigitalSkola #LearningProgressReview #DataScience #Python #NumPy #Bootcamp #BabyPython
To view or add a comment, sign in
-
Python is one of the most powerful tools for data science and one of the easiest to start with. From data cleaning with Pandas to visualization with Matplotlib and Seaborn, Python provides everything you need to analyze data effectively. If you're starting your data journey, this is the best place to begin. Focus on the basics, practice consistently, and build real projects. Read the full post here: https://lnkd.in/eMZNG-XK #Python #DataScience #DataAnalytics #AI #Tech
To view or add a comment, sign in
-
I just published c5tree — the first sklearn-compatible C5.0 Decision Tree for Python! Here's how it started: I noticed C5.0 wasn't part of scikit-learn. R has had the C50 package for years. Python didn't have anything equivalent. So I built it. pip install c5tree --- I benchmarked c5tree against sklearn's DecisionTreeClassifier (CART) across 3 classic datasets using 5-fold cross-validation: Dataset | CART | C5.0 Iris | 95.3% | 96.0% Breast Cancer | 91.0% | 92.1% Wine | 89.3% | 90.5% C5.0 wins on accuracy across every single dataset. I will be experimenting more combining with advanced ensemble methods. --- Why does C5.0 outperform CART? CART uses Gini/Entropy and always makes binary splits. C5.0 uses Gain Ratio - which corrects the bias toward high-cardinality features - and supports multi-way splits on categorical data. C5.0 also handles missing values natively (no imputation needed) and uses Pessimistic Error Pruning to produce smaller, more interpretable trees. --- Key features of c5tree: - Gain Ratio splitting (less biased than Gini) - Multi-way categorical splits - Native missing value handling - Pessimistic Error Pruning - Full sklearn compatibility (Pipelines, GridSearchCV, cross_val_score) - Human-readable tree output --- Quick start: from c5tree import C5Classifier clf = C5Classifier(pruning=True, cf=0.25) clf.fit(X_train, y_train) print(clf.score(X_test, y_test)) --- This is my first open-source Python package and it fills a genuine gap in the Python ML ecosystem. If you find it useful, a ⭐ on GitHub goes a long way! 🔗 PyPI: https://lnkd.in/e-sjUdSG 🔗 GitHub: https://lnkd.in/eecNYs_z #Python #MachineLearning #OpenSource #DataScience #ScikitLearn #DecisionTree
To view or add a comment, sign in
-
-
📊𝗗𝗔𝗬 𝟲𝟭 – 𝗗𝗔𝗧𝗔 𝗦𝗖𝗜𝗘𝗡𝗖𝗘 & 𝗗𝗔𝗧𝗔 𝗔𝗡𝗔𝗟𝗬𝗧𝗜𝗖𝗦 𝗟𝗘𝗔𝗥𝗡𝗜𝗡𝗚 𝗝𝗢𝗨𝗥𝗡𝗘𝗬 🚀 Today, I dived deeper into Object-Oriented Programming in Python by exploring three powerful concepts: 🔹Method Overloading 🔹 Method Overriding 🔹 Operator Overloading These concepts are not just theoretical—they are the backbone of writing clean, scalable, and real-world production-level code. 🔸 Method Overloading – Flexibility in Function Design Unlike other languages, Python doesn’t support traditional method overloading. But we can achieve similar behavior using: ✔️ Default arguments ✔️ `args` (best practice) 💡 This allows us to write flexible functions that can handle multiple inputs dynamically—very useful in data processing and analytics pipelines where input size can vary. 🔸 Method Overriding – Real Power of Inheritance Method overriding allows a child class to redefine a method from the parent class. ✔️ Enables runtime polymorphism ✔️ Helps in customizing behavior without changing the original code ✔️ Widely used in real-world systems and frameworks 💡 Example: In a machine learning pipeline, different models (Linear Regression, Decision Tree, etc.) can override the same method like `train()` or `predict()` but behave differently based on the algorithm. 🔸 Operator Overloading – Making Objects Smarter This is where Python becomes powerful and intuitive ✨ ✔️ Allows operators like `+`, `-`, `==` to work with user-defined objects ✔️ Implemented using **magic (dunder) methods like `__add__`, `__sub__`, `__eq__` ✔️ Improves code readability and usability 💡 Real-world use: Libraries like NumPy and Pandas use operator overloading internally, allowing us to perform operations like: 👉 `df1 + df2` or `array1 + array2` seamlessly 🔥 Key Takeaways: ✔️ Python supports runtime polymorphism, not compile-time ✔️ Overloading is simulated, not directly supported ✔️ Overriding is essential for inheritance-based design ✔️ Operator overloading makes custom objects behave like built-in types 📌 Learning these concepts is helping me understand how large-scale applications and data systems are designed with efficiency and flexibility. #Day61 #Python #DataScience #DataAnalytics #OOP #Polymorphism #OperatorOverloading #LearningJourney #100DaysOfCode 🚀
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