+ 1

Can anyone explain this to me please. I'm a beginner

The code is: for i in range(10): if not i % 2 == 0: print(i+1) Output: 2 4 6 8 10 The part i didn't get is this " i % 2 ==0" why did it print out even numbers, I understand that not is for saying it is false or something. But still I don't get it.

12th May 2018, 4:15 PM
Adrian Andrin
Adrian Andrin - avatar
3 Respostas
+ 7
Let's break down the code: for i in range(10): Lets just refer to i as "anything." So, for anything in the range of 1-10 Next: if not i % 2 == 0: So, if anything in the range of 1-10 divided by 2 has a remainder (%) of 0 That means you take 1, divide it by 2 and see if it has a remainder of 0. It doesn't, so it skips. Next is 2. It has a remainder of 0 when dividing it by 2, so it prints. So on, so on. The print(i+1) means print the number + 1, because if you don't have it the output would be odd numbers. Hope this helps! If you have any more questions, feel free to ask!! Happy coding :)
12th May 2018, 4:25 PM
DrChicken24
DrChicken24 - avatar
+ 2
Np :)
12th May 2018, 4:28 PM
DrChicken24
DrChicken24 - avatar
+ 1
Thank you so much 😊
12th May 2018, 4:27 PM
Adrian Andrin
Adrian Andrin - avatar