There is a "for" loop to get even values | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

There is a "for" loop to get even values

for i in range(0,20,a): print(i) when a=2, we can get event values. However,when a=4,6 or 8, the loop can not be ran to get event values, why?

2nd Jun 2018, 3:46 PM
leekuy
5 Answers
+ 1
When the incrementing number is 4 or 6, you won't be getting ALL of the even values anymore like you do with 2. Instead you will just get the multiples of 4 and 6. The multiples of 2 are all the even numbers, which is why that works
2nd Jun 2018, 4:17 PM
Zeke Williams
Zeke Williams - avatar
0
in for i in range(0,20,a): a refers to the number we want to increment for e.g. for i in range(0,8,2): #(here 2 denotes as i=i+2) print(i) Output- 0, 2 4 6 6(incrementing everytime by 2) Also when for i in range(0,8,4): #here i=i+4 print(i) Output- 0 4 so, when a=2, it will return all even numbers
2nd Jun 2018, 4:11 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
You are welcome
2nd Jun 2018, 4:18 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
Thank you very much,Zeke Williams.
3rd Jun 2018, 2:46 AM
leekuy
- 1
It should be when: a % 2 == 0
2nd Jun 2018, 3:55 PM
Zeke Williams
Zeke Williams - avatar