CoCalc Public Fileseven numbers.ipynbOpen with one click!
Author: Julia Burnside
Views : 28
Compute Environment: Ubuntu 20.04 (Default)

Use a list comprehension to create a list containing all even numbers from 1 to 100 (including 100).

This first function uses a range from 0<(i)<1010<(i)<101 and then, using the mod operator %\% evaluates each value to ensure it is an even number (i%2==0)(i\%2==0) before printing that value.

This second function uses a range from 2<(i)<1012<(i)<101 and then, using an incrementation of 22 in the forloopfor loop, prints ever second value of (i)(i)

In [5]:
print("Even numbers using mod (%):", '\n') for i in range(1,101): if (i%2==0): print(i, '\n') print("Even numbers using increment of 2", '\n') for i in range(2,101, 2): print(i, '\n')
Even numbers using mod (%): 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 Even numbers using increment of 2 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
In [ ]: