Close Menu
    Trending
    • How I Automated My Machine Learning Workflow with Just 10 Lines of Python
    • LLMs + Democracy = Accuracy. How to trust AI-generated answers | by Thuwarakesh Murallie | Jun, 2025
    • The Creator of Pepper X Feels Success in His Gut
    • How To Make AI Images Of Yourself (Free) | by VIJAI GOPAL VEERAMALLA | Jun, 2025
    • 8 Passive Income Ideas That Are Actually Worth Pursuing
    • From Dream to Reality: Crafting the 3Phases6Steps Framework with AI Collaboration | by Abhishek Jain | Jun, 2025
    • Your Competitors Are Winning with PR — You Just Don’t See It Yet
    • Papers Explained 381: KL Divergence VS MSE for Knowledge Distillation | by Ritvik Rastogi | Jun, 2025
    Finance StarGate
    • Home
    • Artificial Intelligence
    • AI Technology
    • Data Science
    • Machine Learning
    • Finance
    • Passive Income
    Finance StarGate
    Home»Machine Learning»Day 15 — Looping Smarter, Not Harder | by Ratnesh | May, 2025
    Machine Learning

    Day 15 — Looping Smarter, Not Harder | by Ratnesh | May, 2025

    FinanceStarGateBy FinanceStarGateMay 31, 2025Updated:May 31, 2025No Comments2 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Record comprehension is a brief and stylish technique to create a brand new checklist by looping by way of an present one.

    🧠 Primary Syntax:

    [expression for item in iterable if condition]
    • expression: what you need to do with every merchandise
    • merchandise: the variable that takes every worth from the iterable
    • iterable: the gathering you are looping by way of (like checklist, vary, string)
    • if situation (non-obligatory): filter the objects primarily based on a situation

    🔁 Record Comprehension Examples

    Please observe them. Learn the code, write it down, and perceive what it’s doing. That’s the way you’ll actually profit from this course — not simply by studying, however by doing.

    ✅ Instance 1: Including 1 to All Numbers

    nums = [1, 2, 3, 4]
    squares = [n + 1 for n in nums]
    print(squares)

    🟢 Output:

    [2, 3, 4, 5]

    ✅ Instance 2: Filter Even Numbers

    nums = [1, 2, 3, 4, 5, 6]
    evens = [n for n in nums if n % 2 == 0]
    print(evens)

    🟢 Output:

    [2, 4, 6]

    ✅ Instance 3: Convert to Uppercase

    phrases = ['hello', 'world', 'python']
    capitalized = [word.upper() for word in words]
    print(capitalized)

    🟢 Output:

    ['HELLO', 'WORLD', 'PYTHON']

    Can We Use if-else in Record Comprehension?

    Sure! You possibly can completely add an if-else situation — it is like saying:

    “If the situation is true, do that. In any other case, do this.”

    📌 Syntax with if-else:

    [do_this if condition else do_that for item in iterable]

    Discover: if-else goes earlier than the for loop — not after.

    ✅ Instance 4: Label Even and Odd Numbers

    nums = [1, 2, 3, 4, 5]
    labels = ['even' if n % 2 == 0 else 'odd' for n in nums]
    print(labels)

    Right here, we’re checking every quantity and labeling it primarily based on whether or not it’s even or odd.

    🟢 Output:

    ['odd', 'even', 'odd', 'even', 'odd']



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleThis $200 MacBook Air Handles Your Hustle Without Complaints
    Next Article Improve Your Productivity with Windows 11 Pro for Just $15
    FinanceStarGate

    Related Posts

    Machine Learning

    LLMs + Democracy = Accuracy. How to trust AI-generated answers | by Thuwarakesh Murallie | Jun, 2025

    June 6, 2025
    Machine Learning

    How To Make AI Images Of Yourself (Free) | by VIJAI GOPAL VEERAMALLA | Jun, 2025

    June 6, 2025
    Machine Learning

    From Dream to Reality: Crafting the 3Phases6Steps Framework with AI Collaboration | by Abhishek Jain | Jun, 2025

    June 6, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    The Challenges of FedAvg and How Researchers Are Fixing Them | by Sandeep Kumawat | Mar, 2025

    March 2, 2025

    The Secret Weapon for Entrepreneurs Who are Battling Burnout

    February 18, 2025

    Deep Learning for Echocardiogram Interpretation

    March 18, 2025

    Why Franchise Leads Ghost You — And How to Fix It

    March 31, 2025

    The Threat of AI to Biosecurity. An essay by Max Freedman | by Science Policy for All | Mar, 2025

    March 27, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    Most Popular

    Using Small Language Models (SLMs) to Solve Real-World Problems and Cut Costs (with a Food App Example) | by Adarsh Pandey | Mar, 2025

    March 1, 2025

    OpenAI Says It Will Stay Under Nonprofit Control

    May 6, 2025

    Democracy.exe: When Exponential Tech Crashes the Human Mind

    May 13, 2025
    Our Picks

    How to Position Your Financial Firm as an Industry Leader

    March 30, 2025

    Meta Layoffs Begin: Inside Meta’s Rankings of Low Performers

    February 11, 2025

    Enjoy Budget-Friendly Flexibility with This $80 Lenovo 2-in-1 Chromebook

    April 20, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 Financestargate.com All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.