0
Why are the output of this bonch of code like this?
numbers = [1, 2, 9, 8, 6] for i in range(0, 5): if i % 2 == 0: print(numbers[i]) ____________________ output: 1 9 6 I'd like to know why output is not 2, 8, 6 because reminders for these digits is zero. I can not understand how 1, 9 and 6 have reminder of zero while they divided to 2, So: 1 % 2 = 1 9 % 2 = 1 6 % 2 = 0 Right? Thanks all for your support and clear answer. I'm new in learning Python
11 ответов
+ 7
Mahdi Zabihi
The i is from range, not the list. It will be the index, not the number in the list.
your if should be
if numbers[i]%2 == 2:
to get 2,8,6
I feel you are still in the c mentality when looping.
your for loop should be
for i in numbers:
if i%2==0:
print(i)
+ 7
Mahdi Zabihi ,
From your above code using for loop you are checking the position is divisible by 2 or not...
So you are getting 1 9 6 as output...
As your code is like this...
For loop runs for values
0,1,2,3,4 ----->(These are ' i ' values)
1st ITERATION:
0%2=0 (so condition satisfied
It will print ---> numbers [i] as per your code...so number[0] as ' i ' value is ' 0 ' in first iteration .... which gives the output ' 1 ')
2nd ITERATION:
1%2!=0 (so condition FAILED)
3rd ITERATION:
2%2=0 (so condition satisfied
It will print ---> numbers [i] as per your code...so number[2] as ' i ' value is ' 2 ' in THIRD iteration .... which gives the output ' 9 ')
4th ITERATION:
3%2!=0 (so condition FAILED)
5th ITERATION:
4%2=0 (so condition satisfied
It will print ---> numbers [i] as per your code...so number[4] as ' i ' value is ' 4 ' in FIFTH iteration .... which gives the output ' 6 ')
+ 6
And if you need 2,8,6 as output means you have to modify the code as Bob_Li said...
+ 5
It is a shorten Riya's explanation.
numbers = [1, 2, 9, 8, 6]
for i in range(0, 5): # for i in range(5):
if i % 2 == 0:
print(number[i])
The results:
i = 0 >> 0 % 2 = 0 >> 0 is equal to 0, print(number[0]) >> 1, next i;
i = 1 >> 1 % 2 = 1 >> 1 is not equal to 0, next i;
i = 2 >> 2 % 2 = 0 >> 0 is equal to 0, print(number[2]) >> 9, next i;
i = 3 >> 3 % 2 = 1 >> 1 is not equal to 0, next i;
i = 4 >> 4 % 2 = 0 >> 0 is equal to 0, print(number[4]) >> 6, end of loop
It prints 1, 9, 6
With Bob_Li's code:
for i in numbers:
if i % 2 == 0:
print(i)
The result:
i = 1 >> 1 % 2 = 1 >> 1 is not equal to 0, next i;
i = 2 >> 2 % 2 = 0 >> 0 is equal to 0, print(2) >> 2, next i;
i = 9 >> 9 % 2 = 1 >> 1 is not equal to 0, next i;
i = 8 >> 8 % 2 = 0 >> 0 is equal to 0, print(8) >> 8, next i;
i = 6 >> 6 % 2 = 0 >> 0 is equal to 0, print(6) >> 6, end of loop
It prints 2, 8, 6
+ 4
for i in range(0, 5) will produce 0, 1, 2, 3, 4.
Actually range(0, 5) can be shorten to range(5). Unless you want to specify the starting number, such as range(3, 5), which produces 3 and 4.
If you want to loop through a list, just as Bob_Li said, for i in numbers.
+ 3
Thanks all guys😊🙏. I leant not only the code itself but more by ur gratful support.
+ 1
Bob_Li
if numbers[i] % 2 == 0 ;)
+ 1
First of all...
i has no remainder when it is 0 or 2 or 4
0%2=0
2%2=0
4%2=0
...and then it prints the numbers from the list that has index 0, 2, 4
That's it.
+ 1
It seems there might be a misunderstanding. The output you provided is actually correct based on the code you provided.
In the code you posted, you have a list of numbers `[1, 2, 9, 8, 6]`. The loop `for i in range(0, 5)` iterates over the indices `0, 1, 2, 3, 4`.
When `i` is `0`, `numbers[i]` refers to the first element of the list, which is `1`. Since `1 % 2` is `1`, the condition `if i % 2 == 0` evaluates to `True` and `1` is printed.
When `i` is `2`, `numbers[i]` refers to the third element of the list, which is `9`. Since `9 % 2` is `1`, the condition `if i % 2 == 0` evaluates to `True` and `9` is printed.
When `i` is `4`, `numbers[i]` refers to the fifth element of the list, which is `6`. Since `6 % 2` is `0`, the condition `if i % 2 == 0` evaluates to `True` and `6` is printed.
So the output is indeed `1, 9, 6`, which is consistent with the code you provided.
If you expected the output to be `2, 8, 6`, then there might be a misunderstanding in your logic or an error in the code that you're using
0
রহস্যময়— পৃথিবী
I see you replied to lots of questions in a short time.
However this is a place to learn coding, please don't give a ready made code easily, ok?
Give some hints or advice so they can learn by themselves.
0
🙏