🧠 Python Concept: sorted() with key Sort smarter, not harder 😎 ❌ Traditional Way students = [ {"name": "Alice", "marks": 85}, {"name": "Bob", "marks": 92}, {"name": "Charlie", "marks": 78} ] # Sorting manually (messy & long) students.sort(key=lambda x: x["marks"]) print(students) ✅ Pythonic Way students = [ {"name": "Alice", "marks": 85}, {"name": "Bob", "marks": 92}, {"name": "Charlie", "marks": 78} ] sorted_students = sorted(students, key=lambda x: x["marks"]) print(sorted_students) 🧒 Simple Explanation Think of key like a rule for sorting 📏 ➡️ “Sort based on THIS value” ➡️ Python handles the rest 💡 Why This Matters ✔ Clean & readable ✔ No custom loops needed ✔ Works with complex data ✔ Very common in real-world apps ⚡ Bonus Tip (Reverse Order) sorted_students = sorted(students, key=lambda x: x["marks"], reverse=True) 🐍 Don’t just sort — sort smart 🐍 Let Python do the heavy lifting #Python #PythonTips #CleanCode #LearnPython #Sort #SortingWithKeys #Programming #DeveloperLife #100DaysOfCode
Sorting with Python's sorted() function using key
More Relevant Posts
-
🚀 Python Series – Day 9: Lists & Tuples (Store Multiple Values Easily!) Till now, we were working with single values. But what if you want to store multiple values in one variable? 🤔 👉 That’s where Lists & Tuples come in! 📦 What is a List? A list is a collection of items stored in a single variable. ✔️ Ordered ✔️ Changeable (Mutable) ✔️ Allows duplicates 🔧 Example: fruits = ["apple", "banana", "mango"] print(fruits) 🔁 Accessing Elements print(fruits[0]) # apple print(fruits[1]) # banana ✏️ Modify List fruits[1] = "orange" print(fruits) ➕ Add Elements fruits.append("grapes") 📦 What is a Tuple? A tuple is similar to a list, but: ✔️ Ordered ❌ Not changeable (Immutable) ✔️ Faster than list 🔧 Example: numbers = (1, 2, 3, 4) print(numbers) 🎯 Key Difference 👉 List = Mutable (can change) 👉 Tuple = Immutable (cannot change) 🔥 Pro Tip: Use: List → when data changes frequently Tuple → when data should stay fixed ⚡ Quick Challenge: What will be the output? x = [1, 2, 3] x[0] = 10 print(x) 👇 Comment your answer! 📌 Tomorrow: Strings in Python (Text Handling Basics) 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
-
-
Python has easy ways to make your text, numbers, and dates look clear and professional. Here are 4 tricks you should try: 1. f-Strings :The easiest way to put variables straight into your text. Fast, readable, perfect for quick outputs. name = "Mayar" age = 22 print(f"My name is {name} and I am {age} years old.") 2. Alignment & Width :Keep tables, reports, or lists neat by aligning text and numbers. Left, center, or right—your choice! print("{:<10} | {:^10} | {:>10}".format("Name", "Age", "Score")) print("{:<10} | {:^10} | {:>10}".format("Mayar", 22, 95)) 3. Template Strings :Create reusable text templates and fill in values later. Makes your code cleaner and easier to manage. from string import Template t = Template("Hello $name, your score is $score.") print(t.substitute(name="Mayar", score=95)) 4. Date & Time Formatting :Show dates and times in a clear, readable way. Useful for reports, logs, or messages. from datetime import datetime now = datetime.now() print(f"Date: {now:%d-%m-%Y} Time: {now:%H:%M:%S}") CodeAcademy_om Kulsoom Shoukat Ali Sultan AL-Yahyai #Python #Coding #PythonTips #Developer #LearnPython #TechSkills #CodeBetter #DateTime
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
-
Assalam o Alaikum 👋 💡 Python Tip: Stop Writing Extra Code — Use "enumerate()"! If you’re learning Python, this small function can make your code cleaner and smarter 🚀 What is "enumerate()"? "enumerate()" is a built-in Python function that helps you loop through a list while keeping track of the index (position) of each item. 👉 Normally, you do this: You create a counter variable, update it manually, and then access elements. But with "enumerate()"… Python does it for you automatically Example: my_list = ['apple', 'banana', 'cherry'] for index, fruit in enumerate(my_list): print(index, fruit) Output: 0 apple 1 banana 2 cherry Why use "enumerate()"? No need to create a separate counter Cleaner & more readable code Less chance of mistakes Perfect for loops where position matters Pro Tip: You can even change the starting index! for index, fruit in enumerate(my_list, start=1): print(index, fruit) 👉 Now counting starts from 1 instead of 0 🚀 Real Use Cases: • Numbering items in a list • Working with indexed data • Tracking positions in loops • Displaying ordered results If you're learning Python, mastering small functions like this will level up your coding fast! 👉 Follow for more simple Python & AI tips #Python #PythonTips #CodingForBeginners #LearnPython #AIAutomation #TechLearning
To view or add a comment, sign in
-
-
I recently explored Python Lists and how they work in real-world scenarios, and here’s what I learned 👇 Python lists are one of the most powerful and beginner-friendly data structures. They allow us to store multiple values in a single variable and work with them efficiently. In my blog, I covered: • Creating and accessing lists • Indexing and slicing • Adding, removing, and updating elements • Important methods like append(), remove(), sort(), reverse() • Real-world examples like shopping lists, student marks, and to-do lists 🔑 Key Learnings: • Lists are mutable, which makes them flexible for real-time changes • Built-in methods simplify complex operations • Lists are widely used in real-world applications and problem-solving Read the full blog here: https://lnkd.in/g5kK2jPF #Python #DataStructures #Coding #Programming #LearningInPublic #Tech #Beginners A heartfelt thanks to Vishwanath Nyathani, Raghu Ram Aduri, Kanav Bansal, and Mayank Ghai, along with my mentors Harsha Mg for their continuous guidance and motivation.
To view or add a comment, sign in
-
🚀 Python Learning Series – 2: Variables, Data Types & Operators 🐍💻 After understanding the basics of Python in Series 1, the next important step is mastering Series 2, because this is the foundation of writing real programs. 📌 In Series 2, we learn: ✅ 🔹 Variables Variables are used to store values in memory. Example: name = "ABC" age = 25 ✅ 🔹 Rules of Variable Naming ✔ Must start with a letter or underscore ✔ Cannot start with a number ✔ No special symbols allowed ✅ 🔹 Python Data Types Python supports multiple data types such as: 📍 int (10, 20) 📍 float (12.5, 3.14) 📍 str ("Python") 📍 bool (True / False) 📍 list, tuple, set, dict ✅ 🔹 Type Checking & Type Casting We can check the type using: print(type(x)) And convert data types using: int(), float(), str() ✅ 🔹 Operators in Python Python provides different types of operators: ➕ Arithmetic (+, -, *, /, %) 🟰 Assignment (=, +=, -=) 🔍 Comparison (==, !=, >, <) 🧠 Logical (and, or, not) 📌 Membership (in, not in) 💡 Conclusion: Without understanding variables, data types, and operators, you cannot write proper Python programs. This chapter is the real base of coding! 📍 If you are a beginner, focus on practicing this chapter daily with small programs. #acsredutech #Python #PythonProgramming #LearnPython #Coding #ProgrammingForBeginners #DataTypes #Operators #ComputerEducation #SkillDevelopment #TechSkills #PythonCourse
To view or add a comment, sign in
-
-
🚀Today I explored another important concept in Python — Strings 💻 🔹 What is a String? A string is a sequence of characters used to store text data. Anything written inside quotes (' ' or " ") is considered a string in Python. 🔹 How Strings Work: 1️⃣ Each character has a position (index) 2️⃣ We can access characters using indexing 3️⃣ We can extract parts of a string using slicing 4️⃣ We can modify output using built-in methods 👉 Flow: Text → Access/Manipulate → Output 🔹 Operations I explored: ✔️ Indexing Accessing individual characters using position ✔️ Slicing Extracting a part of the string ✔️ String Methods Using built-in functions like upper(), lower(), replace() 🔹 Example 1: Indexing & Slicing text = "Python" print(text[0]) # P print(text[-1]) # n print(text[0:4]) # Pyth 🔹 Example 2: String Methods msg = "hello world" print(msg.upper()) print(msg.replace("world", "Python")) 🔹 Key Concepts I Learned: ✔️ Indexing (positive & negative) ✔️ Slicing ✔️ Built-in string methods ✔️ Immutability (strings cannot be changed directly) 🔹 Why Strings are Important: 💡 Used in user input 💡 Data processing 💡 Text manipulation in real-world applications 🔹 Real-life understanding: Strings are everywhere — from usernames and passwords to messages and data handling in applications Learning step by step and gaining deeper understanding every day 🚀 #Python #CodingJourney #Strings #Programming
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
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