Automatically translated.View original post

🐍 Daily Python Problem: Even Numb Filter

Today's problem is to write a filter _ even _ numbers function that receives a list of numbers and returns only "even numbers"! 🎯

📝 Problem & Task:

Write a new return list function that contains only Even Numbers (even numbers) from the original list.

💡 2 gods to solve Filter List in Python!

1.Basic (Use Loop + If) - Practice Logic! 🧱

• Create an empty list (even _ numbers = [])

• Loop. Look at the numbers one by one.

• Use if num% 2 = = 0 (Modulo: divide 2 and leave a remainder of 0) to "filter" even numbers.

• If it is even, use .append () to enter the new list.

• This method is basic.

2.Pythonic (Use List Comprehension) - Short, Pro, Fast! ✨

• This is how professional Python Developer uses it most often!

• We can write the entire Loop and Condition in one line!

• return [num for num in numbers if num% 2 = = 0]

• This method not only short code, but also runs much faster and easier to read for Python.

💖 Choose the style you like! But know both ways will make your code a lot more flexible! Keep going!

# python # data # ai # data aanalyst # learnonlemon8

2025/11/2 Edited to

... Read moreถ้าคุณเพิ่งเริ่มเขียน Python แล้วเจอโจทย์แนว “กรองข้อมูลจากลิสต์” บอกเลยว่าเรื่องเลขคู่เป็นจุดเริ่มที่ดีมาก เพราะได้ฝึกทั้งการวนลูป เงื่อนไข และทำความคุ้นเคยกับ List Comprehension ไปพร้อมกัน สิ่งที่ควรรู้ก่อนเขียนฟังก์ชันคือการใช้เครื่องหมาย Modulo (%) ค่ะ - num % 2 == 0 แปลว่า “หาร 2 แล้วเหลือเศษ 0” → เป็นเลขคู่ - num % 2 != 0 → เป็นเลขคี่ ตัวอย่างการใช้งานที่เจอบ่อย (และช่วยเช็กโค้ดตัวเองได้เร็ว): - Input: [1, 2, 3, 4, 5, 6] → Output: [2, 4, 6] - Input: [1, 3, 5] → Output: [] (ไม่มีเลขคู่ก็ต้องคืนค่าเป็นลิสต์ว่าง) - Input: [] → Output: [] (ลิสต์ว่างก็ต้องไม่พัง) ทิปที่อยากแชร์จากการทำโจทย์แนวนี้บ่อยๆ คือ “ตั้งชื่อฟังก์ชันให้สื่อความ” และ “ทำให้รองรับเคสแปลกๆ” นิดนึง เช่น ถ้าลิสต์มีเลขติดลบก็ยังใช้ได้เหมือนเดิม: - Input: [-4, -3, -2, -1, 0, 1] → Output: [-4, -2, 0] ถ้าต้องการให้โค้ดอ่านง่ายขึ้น เวลาใช้ List Comprehension ฉันมักจะเว้นวรรคให้ชัด และตั้งชื่อตัวแปรให้สื่อว่ามันเป็น “numbers” จริงๆ เช่น: - [num for num in numbers if num % 2 == 0] ข้อดีคือสั้นและตรงมาก เหมาะกับการเขียนงานจริงหรือทำ data prep เล็กๆ อีกมุมที่หลายคนพลาดคือเรื่อง “ชนิดข้อมูล” ถ้าข้อมูลมาจาก input() หรือไฟล์ บางทีมันอาจเป็นสตริง เช่น "2" ซึ่งเอาไป % 2 จะ error ได้ ถ้าเจอสถานการณ์นี้ ให้แปลงเป็น int ก่อน (เช่น map(int, ...)) หรือกรองเฉพาะตัวที่เป็น int/float ที่หารลงตัวตามที่ต้องการ สุดท้าย ถ้าอยากต่อยอดจากการกรองเลขคู่ ลองเพิ่มพารามิเตอร์เป็นเงื่อนไขได้เลย เช่น filter เฉพาะเลขที่มากกว่า 10 และเป็นเลขคู่ หรือกรองเลขคู่แล้วเอาไปคูณ 2 ต่อในบรรทัดเดียว แบบนี้จะได้ฝึกคิดแบบ Pythonic มากขึ้นค่ะ

Related posts

How to Learn Coding Fast Even as a Beginner
#codingforbeginners #programming #studymotivations #webdevelopment #learntocode
Aysha

Aysha

803 likes

how I got started learning Python 🐍💻👾
I’ve been learning Python this summer to prepare for my research lab and courses in the fall. Programming isn’t required for my major, so I’m taking the time to learn it alone. The biggest tip I can give is specific goals from the beginning! Speaking from experience, this will save a lot of time an
reagan

reagan

46 likes

reconnect with God when you feel spiritually numb
how to reconnect with God when you feel spiritually numb #faithstruggles #spiritualhealing #godisnear #numb
ervana

ervana

1418 likes

The Just 3 Rule for Mental Health That Sticks 💭❤️🧠✨
Let me be sooo real with you… Getting fit is hard. Staying consistent? Way harder. It’s not about having the “perfect” body or that one magical workout. It’s about your brain. Your mood. Your mindset. Because honestly? If your mental health is trash, your discipline will be too. And that’s exactly
Chalie_Baker

Chalie_Baker

642 likes

Living While Numb
Some days it’s not sadness, it’s the silence inside. Showing up, smiling, doing what you have to do… while feeling completely disconnected. This is for the ones who keep going even when it feels like nothing is reaching them anymore. Healing doesn’t always look like progress—it can look like just s
HerDailyFight

HerDailyFight

8 likes

A white spoon filled with brown granules rests on a dark, textured background. The image displays text stating, 'Addiction often begins as a way to numb the pain. Yet those who struggle with it are most shamed by the very people who traumatized them.' It also includes 'Daily Note #47' and '@theclairamemag' branding.
Daily Note No.47
Emotional awareness and clarity on addiction. Many people who struggle with it are shamed by the very people who traumatized them. —— If you like exploring the mind through psychology, wellness, and culture, with both information and reflection, follow for daily facts/notes, weekly articles, an
theclairame-digital library

theclairame-digital library

1 like

The Day I Went Numb
There was a time when I felt like I stopped living — not physically, but emotionally. My body kept moving, showing up, surviving… but inside, something had gone quiet. Grief didn’t just visit — it settled in. Pain stopped being something I feared and slowly became something familiar. Numbness ca
HerDailyFight

HerDailyFight

2 likes

My Skin Care Routine 🫧
𝑀𝒪𝑅𝒩𝐼𝒩𝒢 𝑅𝒪𝒰𝒯𝐼𝒩𝐸 🌞 1.Cleanser Anua Heartleaf Quercetinol Pore Deep Cleansing Foam • Use a small amount on damp skin, gently massage for 60 seconds, then rinse with lukewarm water. This clears overnight sweat, oil, and product buildup. 2. Toner / Essence • Numbuzin No.3 Super Glowing Essence
Bianca ༯ Alexandria

Bianca ༯ Alexandria

181 likes

The Pill I Didn’t Want to Need — and Why I Take It
This is your reminder that taking medication doesn’t make you weak, broken, or “less than.” Sometimes it just makes life livable. I’ve been on Zoloft (sertraline) for 17 years. Seventeen. And if I’m being honest, I wish I didn’t need to take something for the rest of my life just to function. I’
Moonlit Forest Lens

Moonlit Forest Lens

672 likes

How to sit with God when you’re numb
Let’s tell the truth. Some of you aren’t “busy.” You’re numb. You scroll. You work. You serve. You show up. But when it’s time to sit with God? It’s quiet inside. And that scares you. So you rush the prayer. You skim the scripture. You turn worship into background noise.
Britney | Healing by TCC

Britney | Healing by TCC

67 likes

The Pivot Year- A great daily read!
I entered the year going through a difficult season that had been ongoing for several years. It was one of those seasons that felt like it was never going to end. My brother purchased me this book and there was so much encouragement in it. So much hope for the future. So many insights to apply. I t
A Better Life

A Better Life

16 likes

Quietly Giving Up Inside
Depression isn’t always loud. Sometimes it’s quiet exhaustion that slowly takes pieces of you over time. It’s waking up already emotionally drained, feeling disconnected from your own life, and carrying thoughts so dark that you stop sharing them because you know most people wouldn’t understand the
HerDailyFight

HerDailyFight

1 like

A magnifying glass over financial charts and graphs, with text overlay 'STATISTIC 101 Correlation & PYTHON CODING', indicating a focus on statistical analysis and programming.
A mathematical formula for the correlation coefficient 'r' is displayed, accompanied by the text 'TAKE-AWAY for today' and a yellow checkmark.
A scatter plot illustrates a strong positive correlation, with data points generally increasing together and a red line showing the upward trend.
🎓Correlation is Not Causality, Not Even Close
📝What is correlation Correlation is a statistical measure that quantifies the degree and direction of the linear relationship between two or more variables. It provides valuable insights into how variables move in relation to one another. The correlation coefficient ranges from -1 to +1.
Capital&Crypto

Capital&Crypto

13 likes

Comfortably numb ✨
For Anyone Who’s Become Comfortably Numb Spirit gave me a song tonight. Comfortably Numb by Pink Floyd. And the second I heard it, I felt it in my bones— this post was meant to be written. Because maybe you didn’t go numb because you stopped loving them. You went numb because you love t
Laura

Laura

7 likes

A top-down view of numerous open books with aged, sepia-toned pages filled with text, scattered across a surface. The word "Storytime" is overlaid in blue script, with "Everyone has a story to tell" beneath it. Lemon8 logos with usernames "@collywobbles_18" and "@xxlazypatatoxxgam" are visible in the corners.
Short Stories pt2
#lemon8diarychallenge NOT MY STORY Cell 3.47 was situated on the third floor of B wing in Stocken Gate prison, slap bang in the heart of London’s east end. Known as The Gate, the prison had a reputation for being a tough place to do time. The inmates behind the doors at The Gate endured long
faith

faith

6 likes

See more