0

How output is 3 ?

x = 0 for i in range (2,20,3): if i%2==0: x+=1 print (x) Please anybody explain how output is 3

28th Aug 2020, 1:07 PM
Allamprabhu Hiremath
4 Answers
+ 4
Allamprabhu Hiremath The range(2,20,3) == (2,5,8,11,14,17) For each number in the range that is divisible by 2 without remainder, add 1 to x. Since there are only 3 numbers in the range [2, 8, 14], x = 3. Happy Coding 😀
28th Aug 2020, 1:15 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
Understand the code is testing for even numbers in the range from 2 to 20, but the step is 3, that is, each time the loop reiterates, i which started at 2 is increased by 3, until it reaches 20. So 2,5,8,11,14,17,20 has just 3 even numbers. for idx in range (<<initialize>>, <<limit>>, <<step>>)
28th Aug 2020, 1:19 PM
shashuri Magrease
shashuri Magrease - avatar