Practice makes perfect


#1



Could you please explain which is a better way of creating odd numbers? and in what circumstances I would i apply one or the other?.


to_21 = range(1, 22)
odds = to_21[::2]

odds_1 = [i for i in range(1, 22) if i % 2 ==1]


#2

I assume the first one would be better simply because it uses less code, and presumably executes faster


#3

You can set an increment on range like so:

odds = range(1, 22, 2)

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.