0

Could you please explain me this code? And how it is giving 258 as its output

for n in range(10): if (n%3==2): print (n,end="")

12th May 2020, 7:21 PM
Bhanu Tripathi
Bhanu Tripathi - avatar
6 ответов
- 1
Bhanu Tripathi You need to revise the how loop works.. There in for loop, for n in range(10) : Means loop iterates by taking n value in range 0 to 9 (10-1), for each iteration, so n=0,1,2,3,4,5,6,7,8,9 So only, When n=2 or 5 or 8, The if condition becomes true so print n value, otherwise don't... Note: add related to tags like python, for, or % Only so can you edit it...!
12th May 2020, 7:43 PM
Jayakrishna 🇮🇳
+ 1
2%3 results in remainder 2 Same for 5%3 and 8%3
12th May 2020, 7:23 PM
Abhay
Abhay - avatar
+ 1
Here 2 is not divisible by 3 hence the modulo value is 2 only..
13th May 2020, 2:43 AM
Uppalapati Padma
Uppalapati Padma - avatar
0
But I can't get (2%3) part..how can (2%3) be computed?
12th May 2020, 7:28 PM
Bhanu Tripathi
Bhanu Tripathi - avatar
0
3*0=0 and remainder is 2 you can't do 3*1 since it will be greater than 2 ,I think that's how division works
12th May 2020, 7:29 PM
Abhay
Abhay - avatar
0
I can't get you fully...
12th May 2020, 7:32 PM
Bhanu Tripathi
Bhanu Tripathi - avatar