公告
财富商城
积分规则
提问
发文
2019-03-12 10:16发布
一纸荒年 Trace。
Does anyone know if Python has an in-built function to work to print out even values. Like range() for example.
Thanks
>>> if 100 % 2 == 0 : print "even" ... even
Range has three parameters.
You can write range(0, 10, 2).
range(0, 10, 2)
I don't know if this is what you want to hear, but it's pretty trivial to filter out odd values with list comprehension.
evens = [x for x in range(100) if x%2 == 0]
or
evens = [x for x in range(100) if x&1 == 0]
You could also use the optional step size parameter for range to count up by 2.
range
#This is not suggestible way to code in Python, but it gives a better understanding numbers = range(1,10) even = [] for i in numbers: if i%2 == 0: even.append(i) print (even)
Just use a step of 2:
range(start, end, step)
Try:
range( 0, 10, 2 )
最多设置5个标签!
Range has three parameters.
You can write
range(0, 10, 2)
.I don't know if this is what you want to hear, but it's pretty trivial to filter out odd values with list comprehension.
or
You could also use the optional step size parameter for
range
to count up by 2.Just use a step of 2:
Try: