Python Practice: Logical Thinking with Functions and List Comprehensions

🚀 DSA Practice: Logical Thinking with Functions, any(), and List Comprehensions Today I practiced some interesting Python problems that helped me strengthen my logical thinking and coding skills. Sharing them here 👇 🔹 1. Check Even or Odd Number 👉 Problem: Determine whether a number is even or odd 👉 Method: Using modulus operator % def is_even(number): return number % 2 == 0 print("even" if is_even(2) else "odd") 🔹 2. Check Divisibility Condition 👉 Problem: Check if a number is divisible by 5 but NOT divisible by 10 👉 Method: Using logical AND def is_divisible(num): return num % 5 == 0 and num % 10 != 0 print("satisfy" if is_divisible(20) else "not satisfy") 🔹 3. Student Pass/Fail (All Subjects) 👉 Problem: Check if a student fails in any subject (< 35) 👉 Method: Using any() with list comprehension def is_pass(marks: list): return any([m < 35 for m in marks]) print("fail" if is_pass([70, 86, 90]) else "pass") 🔹 4. Student Pass if Passed Any One Subject 👉 Problem: Check if a student passed at least one subject (≥ 35) 👉 Example Input: Maths = 20, Physics = 38, Chemistry = 25 👉 Output: Pass 👉 Method: Using any() with condition marks = [20, 38, 25] print("pass" if any(m >= 35 for m in marks) else "fail") 💡 Concepts Used ✔️ List Comprehension new_list = [expression for item in iterable] ✔️ Ternary Operator result = "true" if condition else "false" ✔️ any() Function Returns True if any element satisfies the condition 📌 Key Learning: Combining any(), list comprehensions, and conditional expressions makes code more clean, readable, and Pythonic. ✨ Consistency in small problems builds strong problem-solving skills! #Python #CodingPractice #100DaysOfCode #Programming #PythonBasics #Learning #Developers #LogicBuilding 10000 Coders

  • No alternative text description for this image

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories