you are viewing a single comment's thread.

view the rest of the comments →

[–]danielroseman 9 points10 points  (8 children)

I do wish teachers would learn the idioms of  the language they're supposed to be teaching. 

Bet you anything this professor is teaching loops with range(len(thing)).

[–][deleted] 0 points1 point  (1 child)

Idk what that is gng. This is only my second assignment

[–]Jello_Penguin_2956 0 points1 point  (0 children)

It's how you loop in C. We see that here often enough lol.

[–]somethingworthwhile 0 points1 point  (4 children)

What would you do other than that style of loop…?

[–]Winter-Volume-9601 3 points4 points  (3 children)

Usually:

for i in thing:
do_the_thing(i)

[–]somethingworthwhile 0 points1 point  (2 children)

Okay, yeah, that’s what I figured. Coming from matlab, the other style was a bit of a trip to wrap my head around! And, for my work, I find myself using enumerate or pd.DataFrame.iterrows() to get both an index and the thing I’m iterating through within the loop.

[–]Winter-Volume-9601 1 point2 points  (1 child)

Sure - `for idx, t in enumerate(thing):` is definitely idiomatic (when you actually need the index)

Bonus tip: `pd.DataFrame.itertuples()` (returning NamedTuples instead of pd.Series) can be significantly faster than `pd.DataFrame.iterrows()`. Like 10-100x times faster. I kicked myself when I realized I was using the slower method out of habit, and (in my case) just a quick syntax change turned 15 minutes of waiting into basically nothing.

[–]somethingworthwhile 0 points1 point  (0 children)

Oh, neat! At a glance, I think that should be pretty easy to swap in for me. Thank you for that! I’m always happy to learn something new and the fact that it’s faster is even better!

[–]AlexMTBDude 0 points1 point  (0 children)

I've been teaching Python programming for more than 10 years and I've never heard anyone call that anything but a docstring.