🚀 Python Series – Day 14: File Handling (Read & Write Files) Yesterday, we explored advanced concepts in functions. Today, let’s learn something super practical — how Python works with files 📂 🧠 What is File Handling? File handling allows you to: ✔️ Read data from files ✔️ Write data to files ✔️ Store information permanently 👉 Used in real-world projects like logs, data storage, reports, etc. 📂 Step 1: Open a File file = open("demo.txt", "r") 👉 Modes: "r" → Read "w" → Write (overwrites file) "a" → Append "x" → Create new file 📖 Step 2: Read a File file = open("demo.txt", "r") print(file.read()) file.close() ✍️ Step 3: Write to a File file = open("demo.txt", "w") file.write("Hello, Python!") file.close() ➕ Step 4: Append Data file = open("demo.txt", "a") file.write("\nLearning File Handling 🚀") file.close() 🔥 Best Practice (Important!) Use with statement (auto closes file): with open("demo.txt", "r") as file: data = file.read() print(data) 🎯 Why This is Important? ✔️ Used in data science (CSV, logs) ✔️ Used in real-world applications ✔️ Helps manage large data ⚠️ Pro Tip: Always close files OR use with 👉 Otherwise it may cause memory issues 📌 Tomorrow: Exception Handling (Handle Errors Like a Pro!) Follow me to master Python step-by-step 🚀 #Python #Coding #Programming #DataScience #LearnPython #100DaysOfCode #Tech #MustaqeemSiddiqui
Mustaqeem Siddiqui’s Post
More Relevant Posts
-
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
-
-
Your Python Code Consuming Too Much Memory? Today, I explored a fundamental concept in NumPy that many of us often overlook: manual data type (dtype) . While NumPy is naturally more efficient than standard Python arrays, the way we define our data plays a massive role in actual performance. I recently followed a lecture by Respected Sir Zafar Iqbal on this topic, and it changed how I look at memory management in Data Science/ML. Here are my three key takeaways from today's practice: 1. The "Default" Memory Waste When we create an array without specifying a data type, Python often assigns the maximum possible size, such as int64, by default. If your data consists of small numbers (like 1 to 100), using int64 is a waste of resources. By simply defining dtype=np.int8, you can perform the same operations while using significantly less memory. 2. The Out-of-Bounds Trap Every data type has a specific boundary. For instance, int8 can only store values between -128 and 127. If you try to store a number like 130 in an int8 array, you will encounter an "out of bounds" error. In such cases, moving to int16 or int32 provides the necessary range while still being more efficient than the 64-bit default. 3. The Cost of "Object" Flexibility NumPy allows us to mix different types, like strings, integers, and floats, by using dtype=object. While this offers flexibility, it comes at a price: you lose the famous speed advantage that makes NumPy so powerful. For high-performance computing, keeping your data homogeneous is essential. Pro Tip: When working with large datasets, always use the .nbytes attribute to check exactly how much memory your array is consuming. Making small adjustments to your data types can transform a heavy, slow program into a super-efficient one. I am curious to hear from other data professionals: Do you usually stick with the default settings, or do you prefer manual control over your memory usage? Let me know in the comments. #Python #DataScience #NumPy #CodingLife #LearningEveryday #MachineLearning #Efficiency
To view or add a comment, sign in
-
I used to think Data Structures were just a hard exam topic. Then StemLink lectures showed me how they actually work in Python — and everything changed. 🐍 Here's what I learned, broken down simply 👇 --- 🔷 The 4 main data structures in Python: 📋 Lists → ordered, mutable, most used mylist = [] mylist.append("item") # add to end mylist.sort() # sort in place 📖 Dictionaries → key-value pairs, O(1) lookup user = {"name": "Abiya", "age": 20} 🔸 Tuples → like lists but NOT mutable coords = (6.9, 79.8) # can't change this 🔹 Sets → unique values only, no duplicates tags = {"python", "dsa", "python"} # stores only once --- 🔷 How data lives inside structures: Everything stored inside these is called an Element. Elements are ordered in groups → accessed using Indexes. mylist[0] # first item mylist[-1] # last item mylist[0:3] # slice from 0 to 2 --- 🔷 How we move through data: Loops + Indexes work together to iterate through elements. for elem in mylist: # loop through every item print(elem) mylist[int] = x # modify by assignment --- 🔷 The big insight: Lists are the most popular data structure in Python. They connect everything — elements, loops, indexes, methods, and assignment — all in one place. Once you understand Lists deeply, the rest makes sense. These fundamentals go directly into the projects I build. Not just theory. Real code. Which Python data structure do you use the most? 👇 #Python #DSA #DataStructures #StemLink #IITColombo #LearnToCode #CS #StudentDeveloper #BuildInPublic #Programming
To view or add a comment, sign in
-
-
🚀 Exploring Python Lists – A Powerful Data Structure Recently, I learned how Python lists work in real-world scenarios, and it completely changed how I think about handling data in Python. 📌 Summary: Python lists allow us to store, manage, and manipulate multiple values efficiently. From basic operations to advanced techniques like list comprehensions, they make coding faster and more readable. 💡 Key Learnings: Lists are dynamic and can store different data types Methods like append(), remove(), and sort() make data handling easy List comprehensions help write clean and efficient code 🌍 Real-world use: Lists are widely used in applications like shopping carts, user data storage, and data analysis. 🔗 I’ve also written a detailed blog on this topic: 👉 https://lnkd.in/gT_FGa97 Excited to share my learning on Python Lists 🚀 Thanks to Mr.Vishwanath Nyathani, Mr.Raghu Ram Aduri, Mr.Kanav Bansal, Mr.Mayank Ghai, Mr.@Harsha M. Also inspired by Innomatics Research Labs learning resources #Python #Learning #Python #DataStructures #MachineLearning #AI #LearningInPublic #Coding #Tech
To view or add a comment, sign in
-
Day 9: Python Functions as First-Class Citizens ⚙️ Mastering neat, organized code is critical for Machine Learning pipelines. Today, I did a deep dive into Python Functions, focusing on how to organize code and how Python uses computer memory: Functional Programming: Functions behave like regular data (numbers or strings). I practiced storing them in variables, giving them as inputs to other functions, and having functions create new functions. This makes processing data in steps much easier. Decomposition & Abstraction: Moving past one giant block of code to build separate "boxes" for specific tasks (like separate sections for loading data, cleaning it, and training the AI model). I focused on writing clear instructions (docstrings) inside each one. Scoping & Frame Stack: Learned exactly how Python keeps track of where variables "live." A variable created inside a function is kept separate from variables outside, preventing accidental mistakes and data mix-ups. ⚡ Arbitrary Arguments (*args): Used *args to create super flexible functions that can accept any amount of inputs. This is crucial when you don't know exactly how much data you will get, ensuring the script doesn't crash. Moving from code that "works" to code that is neat, well-documented, and ready for production. 📈 #Python #LearningInPublic #ArtificialIntelligence #SoftwareEngineering #DataPipelines #Modularity #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Last month, I built and published my first Python package — Pristinizer I wanted to solve a simple but real problem in data science: 👉 Cleaning and understanding raw datasets takes way too much time. So I built Pristinizer, a lightweight Python package that helps streamline data cleaning + EDA in just a few lines of code. 🔍 What Pristinizer does: • Cleans messy datasets (duplicates, missing values, column formatting) • Generates structured dataset summaries • Visualizes missing data (heatmap, matrix, bar chart) ⚙️ Tech Stack: Python • pandas • matplotlib • seaborn 📦 Try it out: >> pip install pristinizer >> import pristinizer as ps df = ps.clean(df) ps.summarize(df) ps.missing_heatmap(df) 🧠 What I learned while building this: • Designing a clean and intuitive API • Structuring a real-world Python package • Publishing to PyPI • Writing proper documentation for users 📌 Next, I’m planning to add: • Outlier detection • Automated preprocessing pipelines • Advanced EDA reports Would love to hear your thoughts or feedback! #Python #DataScience #MachineLearning #OpenSource #Pandas #EDA #Projects
To view or add a comment, sign in
-
-
Ready to level up your Python data skills? Let's dive into NumPy arrays and why they are the backbone of Data Science and Machine Learning! 🚀 💡 Why choose NumPy over regular Python lists? NumPy arrays are specifically built for data science and are exceptionally fast and memory-efficient. They bypass standard interpreter limitations by using vectorised operations. This means you can apply mathematical operations across entire arrays simultaneously without writing slow, manual loops. 📐 Mastering Array Shape: The structure of a 3D NumPy array is defined by its shape, which tells you the exact depth (layers), rows, and columns. A critical rule is that NumPy requires a homogeneous shape, meaning every row must contain the exact same number of elements to prevent errors. 🔍 Multidimensional Indexing: Retrieving data from complex arrays is incredibly clean. While standard Python relies on clunky chain indexing (e.g., array[depth][row][column]), NumPy uses concise multidimensional indexing syntax like array[depth, row, column]. Relying on zero-based indexing, this allows you to efficiently pinpoint, extract, and even concatenate specific elements from deep within a 3D structure to build entirely new outputs. Have you made the switch to vectorised NumPy operations in your data projects? Let's discuss below! 👇 #Python #NumPy #DataScience #MachineLearning #CodingTips
To view or add a comment, sign in
-
-
🧠 Python Concept: get() method in dictionary Avoid key errors like a pro 😎 ❌ Traditional Way data = {"name": "Alice", "age": 25} print(data["city"]) 👉 KeyError (crashes if key not found) ❌ Old Safe Way if "city" in data: print(data["city"]) else: print("Not found") 👉 Too many lines ✅ Pythonic Way data = {"name": "Alice", "age": 25} print(data.get("city")) 👉 Output: None (no crash ✅) 🧒 Simple Explanation Think of get() like a safe search 🔍 ➡️ If key exists → returns value ➡️ If not → returns None (or default) 💡 Why This Matters ✔ Prevents crashes ✔ Cleaner code ✔ Useful in APIs & real data ✔ Handles missing keys easily ⚡ Bonus Example data = {"name": "Alice"} print(data.get("city", "Unknown")) 👉 Output: "Unknown" 🐍 Don’t let missing keys break your code 🐍 Use get() smartly #Python #PythonTips #CleanCode #LearnPython #Programming #DeveloperLife #100DaysOfCode
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
-
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
Can we read url instead of file name