0

how to print all evens in ten ,what syntax should be used

evens=[0,2,4,6,8]

25th Nov 2018, 2:37 AM
Kjer
5 Antworten
+ 2
To me the easiest one would be evens = list(range(0, 10, 2)] range(0, 10, 2) produces the numbers between 0 (inclusive) and 10 (exclusive) in steps of 2. The step argument ensures that we only get the evens.
25th Nov 2018, 3:52 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
for i in range(10): if i % 2==0: print(i) if you want to add them to a list use listName.append(i) in place of print
25th Nov 2018, 3:04 AM
James
James - avatar
+ 1
list = [] even = [list.append(i)for i in range(10) if i % 2 == 0] print(list)
25th Nov 2018, 3:38 AM
James
James - avatar
0
evens=[i*2 for i in range(10) if i % 2 == 0] print(evens) Why it runs error ?
25th Nov 2018, 3:22 AM
Kjer
0
thxs
25th Nov 2018, 3:40 AM
Kjer