🧩 Wait… you can use ELSE with a FOR loop in Python? Sounds weird, right? Because we all learned that else only works with if. But Python quietly lets you use else with loops too🫡 Sample code 👉 : numbers = [1, 3, 5, 7] for num in numbers: if num == 4: print("Found 4!") break else: print("4 not found in list") 💡 The else part runs only if the loop finishes normally — no break inside. 🔹 If we found 4 → break stops the loop → else is skipped 🔹 If 4 isn’t found → loop completes → else executes Neat trick, right? Came across this interesting feature while exploring Python basics — thought of sharing for those who didn’t know! #Python #CodingTips #Developers #LearnPython #Programming
Using ELSE with FOR loop in Python: A neat trick
More Relevant Posts
-
PYTHON JOURNEY - Day 14 / 50 !! TOPIC : The for loop in python When you know how many times you want to repeat something — Python’s for loop is your go-to tool It’s perfect for looping through lists, strings, or any sequence. --- Syntax for variable in sequence: # code to execute --- Example 1 — Loop through a list fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) Output: apple banana cherry --- Example 2 — Using range() for i in range(1, 6): print("Number:", i) Output: Number: 1 Number: 2 Number: 3 Number: 4 Number: 5 --- Quick Tip: Use range(start, end) when you want to loop a fixed number of times. It’s often used for counting, patterns, and iterating over indexes. --- #Python #ForLoop #LearnPython #PythonBasics #Coding #Programming #LinkedInLearning
To view or add a comment, sign in
-
-
I've stopped using print() for debugging in Python. And you should too. Here's why: We've all been there. Littering our code with print() statements just to see what a variable holds. It's messy, tedious to write, and a pain to clean up later. What’s a simple alternative? IceCream - a library that makes debugging effortless and more readable. It automatically prints variable names and their values, saving you time and keystrokes. • Cleaner. • Faster to type. • More context with zero extra effort. • A game-changer for quick checks and exploring data frames. #Python #DataScience #DeveloperTools #Programming #CodingTips
To view or add a comment, sign in
-
-
Does removing Python's GIL actually make your code faster? I tested 5 different scenarios to find out. Spoiler: it depends more than you'd think. Python 3.14's free-threading (no-GIL) build is officially supported, but the performance story is nuanced. Here's what my benchmarks revealed: ✅ Pure Python CPU work (factorials, loops): 3-4x faster ✅ PyTorch DataLoaders with workers: noticeable speedup ❌ Pillow image processing: zero improvement ⚠️ Single-threaded code: 5-10% slower overhead The key insight? Libraries like NumPy and Pillow already release the GIL during C operations, so free-threading won't help them. It shines when you're doing heavy computation in pure Python code across multiple threads. GitHub: https://lnkd.in/gPeZYYrM Medium: https://lnkd.in/gkruWByZ #Python #Programming #MachineLearning #SoftwareEngineering
To view or add a comment, sign in
-
-
PYTHON JOURNEY - Day 13 / 50 !! TOPIC : While loop in python What if you want a piece of code to run again and again until a condition fails? That’s exactly what the while loop does --- Syntax while condition: # code to execute repeatedly The loop continues as long as the condition is True. --- Example: count = 1 while count <= 5: print("Count:", count) count += 1 Output: Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 --- Be Careful! If you forget to update the variable inside the loop, you’ll end up with an infinite loop 😅 --- Quick Tip: Use while when the number of iterations isn’t fixed, like reading input until the user quits. --- #Python #WhileLoop #LearnPython #PythonBasics #Coding #Programming #LinkedInLearning
To view or add a comment, sign in
-
-
Today, I practiced creating a simple Python calculator using loops and conditional statements. 💻 This small project helped me understand: 🔹 How to take user input in Python 🔹 How to use while loops for continuous execution 🔹 How to apply if-elif conditions for different operations (+, -, *, /) 🔹 And how to break the loop using a quit command (q) Here’s what the program does: It asks the user to enter two numbers Then takes an operator (+, -, *, /) Displays the result instantly Keeps running until you press q to quit This might look simple, but every small program builds a strong foundation. Step by step, I’m getting more comfortable with logic building in Python. 🚀 #Python #CodingJourney #Programming #LearningByDoing #Tech #Developer
To view or add a comment, sign in
-
-
🐍 Python 3.14 was out yesterday! It’s always fun when a new Python release drops. I probably won’t upgrade right away (waiting until the big libraries support it), but here are a few things that stood out: Real multi-core Python - free-threaded support means Python can finally use multiple CPU cores better. Faster Python - a new experimental interpreter that can make programs run quicker. Friendlier Python - the shell now has color and autocomplete, and error messages are clearer. New extras - template strings, built-in Zstandard compression, and new UUID formats. Nothing too flashy, but it feels like Python is becoming both easier to use and more powerful. Are you planning to try Python 3.14 soon, or will you wait it out? Read the official documentation in the comments to learn more about the release! #Python #Python314 #Programming #Tech
To view or add a comment, sign in
-
🚀 New Blog Post Alert! I’ve just published a new article on Python fundamentals — covering what Python is, its core data types, and how explicit type conversion works with real examples. 🐍 If you're starting your Python journey or brushing up on the basics, this guide is for you! 👉 Read here: https://lnkd.in/gpRrBVjc 💡 In this post, you’ll learn: What makes Python beginner-friendly Common data types (int, float, str, list, dict, etc.) The difference between implicit and explicit type conversion Simple examples you can run yourself Let’s keep learning and building with Python! 💻✨ #Python #Coding #Programming #PythonForBeginners #LearnToCode #Hashnode #TechBlog
To view or add a comment, sign in
-
PYTHON JOURNEY - Day 16 of 50 !! TOPIC : The else Clause in Loops Did you know? Python loops can have an else block — and it’s not what most people think! The else part in a loop runs only when the loop completes normally (without a break). --- Example 1 — With for Loop for i in range(1, 6): print(i) else: print("Loop completed successfully ") Output: 1 2 3 4 5 Loop completed successfully --- Example 2 — Using break for i in range(1, 6): if i == 3: break print(i) else: print("Loop completed successfully ") Output: 1 2 The else block didn’t execute because the loop was broken early. --- Quick Tip: Use the loop else to handle cases when a loop ends naturally, like searching for an item — if not found, execute the else. --- #Python #LearnPython #ForLoop #WhileLoop #PythonBasics #Coding #PythonTips #LinkedInLearning
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