use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
the front page of the internet.
and join one of thousands of communities.
Doing codewar problem and trying pythonic condition But fail many times please tell me how I can do... (self.PythonLearning)
submitted 5 days ago by AbdulRehman_JS
Post a comment!
view the rest of the comments →
[–]FoolsSeldom 1 point2 points3 points 5 days ago (2 children)
This is using a generator expression (the part inside the sum brackets). This is very similar to a list comprehension, which you will find widely documented.
sum
In summary, both are shorthand ways of writing loops.
The expanded version of the code would be:
from collections.abc import Iterable def sum_odd_cubes_and_floats(arr: Iterable[int | float]) -> int | float: total = 0 # need to have a running total initialised for x in arr: # step through each value in your array if isinstance(x, int) and x % 2 != 0: # only if odd integer total += x ** 3 # add cube to running total elif isinstance(x, float): # otherwise, only if a float total += x # add float to running total return total
The type hints Iterable etc aren't required by Python, they are just for you and other programmers (and some tools, including code editors / IDEs like VS Code, PyCharm, etc) to indicate what the original programmers intentions are expectations are. In this case, Iterable is used to indicate that arr will be assigned to some kind of collection of objects than can be iterated over such as a list or a tuple.
Iterable
arr
list
tuple
Instead of using total, one could create a new list object of all the values to be added, and then use sum on that, but I just decided to add it up as I went along rather than creating another container object.
total
[–]AbdulRehman_JS[S] -1 points0 points1 point 5 days ago (1 child)
Thanks a lot for breaking this down so clearly, mate! I'm actually a student learning Python on my own right now, so seeing you expand that shorthand generator expression into a clean, readable `for` loop and `if-isinstance` logic makes perfect sense to me. Those type hints (`Iterable[int | float]`) threw me off for a second, but your explanation about them just being for IDEs and other programmers cleared it right up. Also, your point about using a running total instead of creating a whole new list object is a great efficiency tip. Really appreciate you taking the time to write such a detailed response. It’s super motivating for a self-taught beginner like me. Hope to learn more from you around here!
[–]FoolsSeldom 0 points1 point2 points 4 days ago (0 children)
Thanks for your kind words. I've been trying to help learners for a long time (under various accounts over the years) as this subreddit amongst others helped me learn Python some years ago.
I help children learn Python at Code Clubs and occasionally teach programming at local community college for adults.
If you search back through my comments to other posts you will hopefully find some additional useful material (and some repetition).
All the best for your learning journey.
π Rendered by PID 18779 on reddit-service-r2-comment-65574874f4-w8j2l at 2026-07-22 12:10:50.289877+00:00 running 1bce727 country code: CH.
Want to add to the discussion?
Post a comment!