python - Compare a bunch of integers to a variable in an if-statement -
i want compare s
in for-loop bunch of numbers multiples of 5
, don't want write out of or
's in-between. there easier way write this?
for s in range(0,50): lst = [] if s == 5,10,15,20,25,30,35,40,45: lst.append(" ") else: lst.append(letter) #letter defined before
of course, it's not right syntax way i've written , comes bunch of errors.
this in
operator for1:
if s in (5, 10, 15, 20, 25, 30, 35, 40, 45):
in specific case however, see if number divisible 5
:
if not s % 5: # or if s % 5 == 0:
also, need move line outside of loop:
lst = []
otherwise, lst
redefined each iteration.
1in python 3.3 or greater, more efficient utilize set literal here instead of tuple literal:
if s in {5, 10, 15, 20, 25, 30, 35, 40, 45}:
the newer versions of python smart plenty recognize constants such this, evaluating them 1 time instead of each time encountered. thus, improve utilize set, has o(1)
(constant) lookup time, rather tuple, has o(n)
(linear) lookup time.
python if-statement comparison
No comments:
Post a Comment