🚀 Day 10/60 – Dictionaries in Python (Key-Value Magic 🔑) Lists store values… But what if you need labels with values? That’s where dictionaries shine 👇 🧠 What is a Dictionary? A dictionary stores data in key-value pairs. person = { "name": "Adeel", "age": 25, "city": "Lahore" } 🔍 Access Values print(person["name"]) # Adeel print(person["age"]) # 25 ➕ Add / Update Values person["job"] = "Data Engineer" # Add person["age"] = 26 # Update ❌ Remove Item person.pop("city") 🔁 Loop Through Dictionary for key, value in person.items(): print(key, value) ⚡ Real Example student = { "name": "Ali", "marks": 90 } if student["marks"] > 80: print("Top Student") ❌ Common Mistake print(person.name) # ❌ Error Correct: print(person["name"]) # ✅ 🔥 Pro Tip Keys must be unique & immutable (like strings, numbers, tuples) 🔥 Challenge for today Create a dictionary: 👉 Store your name, age, and favorite language 👉 Print each key and value Comment “DONE” when finished ✅ Follow Adeel Sajjad to stay consistent for 60 days 🚀 #Python #LearnPython #PythonProgramming #Coding #Programming
Python Dictionaries: Key-Value Pairs and Access
More Relevant Posts
-
🚀 Python Series – Day 10: Strings in Python (Text Handling Basics) Till now, we worked with numbers and collections. But what about text data? 🤔 👉 That’s where Strings come in! 🧠 What is a String? A string is a sequence of characters enclosed in quotes. ✔️ Can use single ' ' or double " " quotes 🔧 Example: name = "Mustaqeem" print(name) 🔁 Access Characters text = "Python" print(text[0]) # P print(text[-1]) # n ✂️ String Slicing text = "Python" print(text[0:3]) # Pyt print(text[2:]) # thon 🔄 String Methods msg = "hello world" print(msg.upper()) # HELLO WORLD print(msg.lower()) # hello world print(msg.title()) # Hello World ❌ Mutability Fails in String Strings are immutable — meaning you cannot change them directly. text = "Python" text[0] = "J" # ❌ Error 👉 This will give an error because strings cannot be modified. ✅ Correct Way (Create New String) text = "Python" new_text = "J" + text[1:] print(new_text) # Jython 🎯 Why Strings are Important? ✔️ Used in almost every program ✔️ Helps in user input & output ✔️ Important for data processing 🔥 Pro Tip: Whenever you want to modify a string 👉 create a new one instead of changing the original ⚡ Quick Challenge: What will be the output? text = "Python" print(text[1:4]) 👇 Comment your answer! 📌 Tomorrow: Dictionaries & Sets (Advanced Data Structures) Follow me to learn Python step-by-step from basics to advanced 🚀 #Python #DataScience #Coding #Programming #LearnPython #Beginners #Tech #MustaqeemSiddiqui
To view or add a comment, sign in
-
-
🐍 Lambda Function in Python (Simple Explanation) Lambda is just a **small one-line function**. No name, no long code… just quick work ✅ 👉 Instead of writing this: ```python def add(x, y): return x + y ``` 👉 You can write this: ```python add = lambda x, y: x + y print(add(3, 5)) # 8 ``` 💡 Where do we use it in real life? 🔹 1. Sorting (very useful) ```python students = [("Vinay", 25), ("Rahul", 20)] students.sort(key=lambda x: x[1]) # sort by age print(students) ``` 🔹 2. Filter (get only even numbers) ```python numbers = [1,2,3,4,5,6] even = list(filter(lambda x: x % 2 == 0, numbers)) print(even) # [2,4,6] ``` 🔹 3. Map (change data) ```python numbers = [1,2,3] square = list(map(lambda x: x*x, numbers)) print(square) # [1,4,9] ``` ✅ Use lambda when: • Code is small • Use only once • Want quick solution ❌ Don’t use when: • Code is big or complex 💡 Simple line to remember: “Short work → Lambda” 👉 Are you using lambda or still confused? #Python #Coding #LearnPython #Programming #Developers #PythonTips
To view or add a comment, sign in
-
-
🚀 Day 14/60 – Dictionary Comprehension (Level Up Your Python 🚀) Yesterday you learned list comprehension. Today, let’s level up 👇 🧠 What is Dictionary Comprehension? A quick way to create dictionaries in one clean line. ❌ Traditional Way numbers = [1, 2, 3, 4] squares = {} for num in numbers: squares[num] = num * num print(squares) ✅ Dictionary Comprehension Way numbers = [1, 2, 3, 4] squares = {num: num * num for num in numbers} print(squares) 👉 Cleaner. Faster. More Pythonic. 🔍 With Condition numbers = [1, 2, 3, 4, 5, 6] even_squares = {num: num * num for num in numbers if num % 2 == 0} print(even_squares) ⚡ Real Example names = ["adeel", "ali", "ahmed"] name_length = {name: len(name) for name in names} print(name_length) ❌ Common Mistake {num * num for num in numbers} # ❌ This creates a set Correct: {num: num * num for num in numbers} # ✅ Dictionary 🔥 Pro Tip Use dictionary comprehension when: ✅ You want clean transformation of data ❌ Avoid if logic becomes too complex 🔥 Challenge for today 👉 Create numbers from 1 to 5 👉 Create dictionary where: Key = number Value = cube of number Comment “DONE” when finished ✅ Follow Adeel Sajjad to stay consistent for 60 days 🚀 #Python #PythonProgramming #LearnPython #Coding #Programming #Developer #SoftwareEngineering
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
-
-
🚀 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
To view or add a comment, sign in
-
-
I thought Python was just doing calculations, until it gave me a “wrong” answer 😅 I was like: “How is this even possible??” Then I discovered something that changed everything Operators don’t just run, they follow rules. Let me explain this like I’m talking to a baby Imagine 3 kids solving math Kid 1: “Let’s go left to right” Kid 2: “No, start from the right” Kid 3: “Follow the rules first!” That’s exactly how Python behaves. What are Operators?Operators are just symbols like: ➕ ➖ ✖️ ➗ ** % They tell Python what to do with numbers. Python doesn’t just calculate randomly. It follows priority + binding rules. Two important rules I learned Modulo (%) → Left to Right For example: 20 % 6 % 4 = (20 % 6) % 4 = 2 % 4 = 2 Exponent (**) → Right to Left For Example: 2 ** 3 ** 2 = 2 ** (3 ** 2) = 2 ** 9 = 512 🤯 I used to think python is giving wrong answers Now I know that python is always correct, I just didn’t understand the rules. As I grow from excel to SQL and to Tableau and now python I’m learning that: Small mistakes = wrong insights Wrong insights = wrong decisions And in data, that’s dangerous Python is not confusing, it’s just very obedient to its rules. If you’re learning python, have you ever been surprised by a result like this? 😅 #Python #DataAnalytics #LearningInPublic #SQL #Excel #Tableau #Programming #TechJourney #BeginnerInTech #DataScience #CareerGrowth
To view or add a comment, sign in
-
-
I am learning dictionaries in Python, which allow me to store data in key-value pairs. This makes it easy to organize and retrieve information efficiently. For example, I can create a dictionary to store information about a person, like their name, age, and job. Each piece of data is accessed using a unique key instead of an index, unlike lists. I can also update, add, or remove items from a dictionary as needed. Here is an example of a dictionary in Python: person = { "name": "David", "age": 28, "job": "Data Engineer" } # Accessing values print(person["name"]) # Output: David # Adding a new key-value pair person["city"] = "Charlotte" # Updating a value person["age"] = 29 # Removing a key-value pair del person["job"] print(person)
To view or add a comment, sign in
-
🐍 Day 17–20 of My 30-Day Python Learning Challenge 🚀 Over the last few days, I focused on improving my Mini Project: Log File Analyzer by making it more practical and closer to real-world usage. 📌 What I Improved: ✅ Removed Stopwords Ignored common words like "the", "is", "and" to focus on meaningful data. stopwords = {"the", "is", "and", "in", "to", "of"} filtered_words = [w for w in words if w not in stopwords] --- ✅ Data Cleaning (Punctuation Removal) Handled messy real-world text by removing special characters. import string for p in string.punctuation: content = content.replace(p, "") --- ✅ Better Word Frequency Analysis Used efficient logic to count words. word_count[word] = word_count.get(word, 0) + 1 --- ✅ Top Frequent Words Extraction top_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:3] --- 📊 Key Learning: Small improvements like cleaning and filtering data significantly improve accuracy. 📈 Next Steps: • Visualize results using graphs • Add user input support • Build a simple UI using Streamlit 💡 This project helped me understand how Python is used in: • Data analysis • Text processing • Real-world problem solving #Python #MiniProject #DataCleaning #LearningInPublic #SoftwareDeveloper #ProjectBuilding
To view or add a comment, sign in
-
🌟 Today’s Learning: Clearing Nulls & Replacing Missing Values in Python 🐍📊 Data cleaning is one of the most important steps in data analysis. Today I learned how to handle null values and missing data using Python Pandas. ✅ Key Learnings: 🔹 Detect null values using isnull() 🔹 Count missing values with sum() 🔹 Remove null rows using dropna() 🔹 Replace missing values using fillna() 🔹 Use mean / median / mode for smart replacements 💻 Example Code: Python import pandas as pd # Load dataset df = pd.read_excel("data.xlsx") # Check null values df.isnull() # Count null values in each column df.isnull().sum() # Remove rows with null values df.dropna() # Fill missing values with 0 df.fillna(0) # Fill missing values with mean df["Sales"] = df["Sales"].fillna(df["Sales"].mean()) # Fill missing values with median df["Age"] = df["Age"].fillna(df["Age"].median()) # Drop rows with null values df.dropna(inplace=True) 📈 Clean data = Better insights = Better decisions! #Python #Pandas #DataCleaning #DataAnalysis #LinkedInLearning #MachineLearning #CodingJourney #DataScience
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