Day 20 of My Python Learning Journey Today I learned about Shallow Copy and Deep Copy in Python. 📌 Shallow Copy A shallow copy creates a new object, but the nested objects inside it are still referenced from the original object. So, changes in nested elements will affect both copies. Example: import copy list1 = [[1,2,3],[4,5,6]] shallow = copy.copy(list1) shallow[0][0] = 100 print(list1) print(shallow) 📌 Deep Copy A deep copy creates a completely independent copy of the original object including all nested objects. Changes in one object will not affect the other. Example: import copy list1 = [[1,2,3],[4,5,6]] deep = copy.deepcopy(list1) deep[0][0] = 100 print(list1) print(deep) ✅ Key Difference Shallow Copy → Copies only the outer object Deep Copy → Copies outer object + all nested objects Learning these concepts helps in understanding memory handling and object references in Python #Python #PythonLearning #Day20 #CodingJourney #Programming #LearningEveryday
Python Shallow vs Deep Copy
More Relevant Posts
-
One small Python concept today… but a very important one. Today’s lesson focused on how Python handles mutable objects like lists. In the example below: def add_item(lst): lst.append(100) a = [1, 2, 3] add_item(a) print(a) The result will be: [1, 2, 3, 100] Why? Because lists in Python are mutable. When we pass a list to a function and modify it using methods like append(), the change happens in-place — meaning the original list itself is modified. 💡 Key takeaway: Understanding the difference between mutable and immutable objects is essential for writing predictable and efficient Python code. Every day in this sprint reminds me that small concepts build strong foundations in data analytics and AI. On to the next challenge. 🚀 #Python #DataAnalytics #AI #MachineLearning #LearningJourney #Coding #TechSkills #AIAnalytics #PythonProgramming #LinkedInLearning
To view or add a comment, sign in
-
I’ve just published my first blog post on Medium, diving into a Python concept I recently explored: 👉 Mutable vs Immutable Objects Before this, I used to think variables simply “store values.” But in Python, they actually reference objects in memory. Having learned C previously really helped me grasp this concept more quickly, especially when it comes to understanding how memory and references work. In this post, I break down: • The difference between == and is • Mutable vs immutable objects (with clear examples) • Why l1 = l1 + [4] and l1 += [4] behave differently • How Python passes arguments to functions Writing about what I learn pushes me to go deeper and truly understand the concepts. If you’re learning Python or preparing for technical interviews, this is definitely a topic worth mastering. 📖 Read here: https://lnkd.in/guVnYN3Q #Python #SoftwareEngineering #Programming #LearningJourney #Tech
To view or add a comment, sign in
-
Cracking the Code: How Python Calculates Everything Ever wondered how Python decides which math problem to solve first. It all comes down to Expressions and Operators. If you are learning to code, understanding these rules will save you from a lot of bugs later on.. ==> 1. What is an Expression? An expression is just a combination of values, variables, and operators that results in a single value. Example: 1 + 2 evaluates to 3. ==> 2. Meet the Arithmetic Operators Python uses special symbols for math. Some are common, but others are unique: + , - , *: Addition, Subtraction and Multiplication. /(Classic Division): This always returns a float (decimal), like 4 / 2 = 2.0. // (Floor Division): Rounds the result down to the nearest whole number. % (Modulus): Returns the remainder (e.g., 5 % 2 = 1). ** (Exponentiation): Powers (e.g., 2 ** 3 = 8). ==> 3. Unary vs. Binary Operators Unary: Works with only one value (e.g., -5 or +3). Binary: Works with two values (e.g., 10 + 5). ==> 4. Who Goes First? (Operator Priority) Just like in school math, Python has a hierarchy. It follows these rules: 1. Parentheses (): Anything inside brackets is calculated first! 2. Exponentiation **: This has the highest priority. 3. Unary + and - 4. Multiplication, Division, and Modulus (*, /, %) 5. Binary Addition and Subtraction (`+`, `-`): These have the lowest priority. ==> 5. A Tricky Rule: Right-Sided Binding Most math moves from left to right, but the exponentiation operator (**) is different. It uses right-sided binding. Example: 2 ** 2 ** 3 is actually 2 ** (2 ** 3), which equals 256. Mastering these small details makes a huge difference when building complex logic #Python #Coding #MathInCode #ProgrammingBasics #SoftwareDevelopment #TechTips
To view or add a comment, sign in
-
🐍 Day 2 of Learning Python Continuing my Python learning journey and today’s focus was on understanding how Python actually executes code behind the scenes, along with practicing some fundamental concepts. 🔎 What I explored today: ⚙️ How Python Executes Code I learned that Python does not directly run the code we write. Instead, the Python interpreter first converts the source code into Bytecode, which is then executed by the Python Virtual Machine (PVM). Understanding this process helped me see how Python translates human-readable instructions into something a computer can execute. 🧠 Variables in Python After that, I practiced working with variables, which are used to store data in memory. Python makes this simple since we don’t need to explicitly declare the data type — the interpreter handles it dynamically. 💻 Taking User Input To practice further, I wrote a small program where the user enters their name and age, and the program prints a formatted message. Example concept used: input() for user input. int() to convert age into an integer. f-strings for clean and readable output formatting. This small exercise helped me understand data types, variable assignment, and interaction with users through the terminal. Every day I’m trying to strengthen the fundamentals because strong basics make advanced topics like automation, AI, and machine learning easier to approach later. Looking forward to exploring more Python concepts tomorrow. 🚀 #Python #PythonProgramming #LearningPython #CodingJourney #100DaysOfCode #SoftwareDevelopment #ProgrammingBasics #TechLearning #Developers #FutureEngineer #LearnInPublic #PythonBeginner #SDE
To view or add a comment, sign in
-
-
Quick Python Challenge What will be the output of this code? a = [1, 2] b = a b.append(3) print(len(a)) Options: A) 2 B) 3 C) 1 D) Error 💡 At first glance, many people think the answer is 2. But the correct answer is actually 3. Why? Because in Python: b = a does not create a new list. It simply makes b reference the same object in memory as a. So when we run: b.append(3) we are modifying the same list that both a and b point to. The list becomes: [1, 2, 3] So: len(a) = 3 📌 Key Insight: In Python, variables can reference the same mutable object, which means modifying one reference affects the other. 🔥 Lesson of the day: Understanding mutable objects and references is essential when writing reliable Python code—especially in data analysis and AI pipelines. 💬 Curious: Did you get it right on the first try? #Python #AI #DataAnalytics #LearningInPublic #30DayChallenge #PythonTips
To view or add a comment, sign in
-
Slow python ? Meet pybind11 ! It started with a simple curiosity while exploring autograd in Deep Learning frameworks. To better understand how gradients work behind the scenes, we implemented #micrograd by Andrej Karpathy a tiny educational autograd engine written in Python. After building it, a natural question emerged: If Python is slow, how are performance-heavy libraries still so fast? Digging deeper, we discovered that many high-performance Python libraries rely on C++ under the hood, with Python acting as a clean interface. This led us to explore pybind11, a lightweight way to bridge Python and C++ ! We then, - Implemented micrograd in C++ - Exposed it to Python using pybind11 - Generated .so modules using setup.py - Recreated a simplified Python plus C++ architecture This small experiment helped us understand how Python can leverage C++ for performance while maintaining developer productivity.. Also, pybind11 is one of the several alternatives like SWIG, Boost.Python, and Cython that can also be used for language bindings. This exploration was done in collaboration with Kavin Kumar, where we jointly worked on both the medium article and the C++ micrograd implementation. To check our implementation, Github: https://lnkd.in/gz6GBuNV For a deep dive, Explore our article: https://lnkd.in/g2y8KRta PS: Not AI Generated :) #Python #CPP #CPlusPlus #Pybind11 #MachineLearning #DeepLearning #Autograd #Micrograd #PythonPerformance
To view or add a comment, sign in
-
Understanding Insertion Sort in Python While exploring Data Structures and Algorithms in Python, I learned about Insertion Sort, one of the simplest and most intuitive sorting techniques. 💡 Real-Life Analogy Insertion Sort works similar to arranging playing cards in your hand. You take one card at a time and place it in the correct position among the already sorted cards. 📌 How Insertion Sort Works 1️⃣ The first element is considered already sorted. 2️⃣ Take the next element from the list. 3️⃣ Compare it with the elements before it. 4️⃣ Shift larger elements one position to the right. 5️⃣ Insert the element in its correct position. This process continues until the entire list becomes sorted. 📌 Python Implementation def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] # element to insert j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key numbers = [12, 11, 13, 5, 6] insertion_sort(numbers) print("Sorted List:", numbers) 📊 Example Input [12, 11, 13, 5, 6] Output [5, 6, 11, 12, 13] Time Complexity 🔹 Best Case → O(n) (When the list is already sorted) 🔹 Average Case → O(n²) 🔹 Worst Case → O(n²) ✅ Advantages ✔ Simple and beginner-friendly algorithm ✔ Efficient for small datasets ✔ Works well for partially sorted data ❌ Limitation Not efficient for very large datasets compared to advanced algorithms like Merge Sort or Quick Sort. 🎯 Conclusion Insertion Sort may be simple, but learning it helps us understand algorithm thinking, comparisons, and data movement, which are fundamental concepts in programming. #Python #Algorithms #InsertionSort #DataStructures #CodingJourney #LearnPython
To view or add a comment, sign in
-
-
🔹 Python Tip: sort() vs sorted() When working with lists in Python, we often need to sort data. There are two common ways to do this: list.sort() and sorted(). Let’s look at a simple example: lst1 = [20, 10, 50, 30, 40] Suppose we want to sort the elements in ascending order. 1️⃣ Using list.sort() lst1 = [20, 10, 50, 30, 40] lst1.sort() print(lst1) ➡️ Output: [10, 20, 30, 40, 50] Here, sort() modifies the original list directly. This is called in-place sorting, meaning the existing list itself gets reordered. 2️⃣ Using sorted() lst1 = [20, 10, 50, 30, 40] new_list = sorted(lst1) print(new_list) ➡️ Output: [10, 20, 30, 40, 50] The key difference is that sorted() does not change the original list. Instead, it returns a new sorted list, so we store it in another variable. So now we have: new_list = [10, 20, 30, 40, 50] lst1 = [20, 10, 50, 30, 40] 🔹 Summary • Use sort() when you want to modify the list itself and don’t need the original order. • Use sorted() when you want to keep the original data unchanged and create a new sorted sequence. • Another advantage of sorted() is that it works with other iterable types such as tuples, strings, and sets. #Python #Programming #Coding #DataScience #LearnPython
To view or add a comment, sign in
-
Python Discussion Both if and while in Python rely on a condition, but they behave very differently. Let’s break it down 👇 1️⃣ Execution Method 🔹 if statement Checks the condition once. If the condition is True, the code runs one time only. 🔹 while loop Keeps checking the condition and repeats execution as long as the condition is True. 2️⃣ Number of Executions if ➜ Executes 0 or 1 time while ➜ Executes multiple times until the condition becomes False 3️⃣ When to Use Each One (Use Case) ✅ Use if when you want to make a decision once. Example: age = 20 if age >= 18: print("You can enter") 🔁 Use while when you want the program to repeat an action until a condition changes. Example: count = 1 while count <= 5: print(count) count += 1 Output: 1 2 3 4 5 🎯 Simple way to remember: if → Decision while → Repetition 💡 In AI and Data Analytics, loops like while are useful when processing data until a certain condition is met, while if is used for logic and decision making inside algorithms. 💬 Quick question for Python learners: Can you think of a scenario where you would use both if and while together in the same program? #Python #AI #DataAnalytics #LearningInPublic #30DayChallenge #PythonBasics
To view or add a comment, sign in
-
🚀 Learn Python – Tuple Slicing If you want to learn how to extract portions of a tuple, understanding Slicing is essential. It's a fundamental skill for data manipulation in Python. I’ve created a structured learning section on my website that explains tuple slicing step-by-step with practical examples. 📚 Explore the tutorial: https://lnkd.in/g_DmQ7wa 🔹 What you will learn • Extracting sub-tuples using the start:end:step syntax • Using positive and negative indexing for flexible slicing • Reversing tuples easily with slicing tricks • Understanding slicing behavior and default values • Practical examples: Slicing from the beginning, end, or with steps This resource is designed to help developers learn Python with practical examples and structured lessons. Happy Learning! 🚀 #Python #Coding #DataStructures #PythonTips #Programming #LearnPython
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