+ 2
What is the output of this code? list = [2, 3, 4, 5, 6, 7] for x in list: if(x%2==1 and x>4): print(x) bre
What is the output of this code? list = [2, 3, 4, 5, 6, 7] for x in list: if(x%2==1 and x>4): print(x) break
19 Réponses
+ 8
I don't understand why the result is 5. The x is the first number right?
so it is ==1 but it is not >4, so the result it is not 1.
What is the correct way then?
Sorry for this question but i am a beginner and i always want a opinion of an expert.
Thank you for who will answer me
+ 3
5 is the Answer
+ 3
Please correct me if I am wrong,
I think it is calling for items which are greater than 4, which are basically 5,6 and seven and are not divisible to 2 and there will be remained amount;
so the six goes out and because we have the "break"at the end, it will end the output number list to the first option which is 5.
+ 1
1 is the answer
+ 1
5 because 2 will fit into 2 == 1 and it is more than 4
0
5
0
I suggest you to install thonny software to better understand the step-by-step output of the program.
0
list = [2, 3, 4, 5, 6, 7]
for x in list:
if(x%2==1 and x>4) :
print(x)
break
0
Just asking questions on this topic because I'm a beginner, but could it be because of the "break" command?
Could it be because 5 matches the loops conditions of x%2==1 and x>4?
Once it's found the first conditions it has found in the list that matches the conditions it breaks the loops??
0
5
0
5
0
Ans is
5
0
Why 5 is the answer
Why not it's 7 cause 7is also more than 4
And left remainder 1??
0
I had to search it up but apparently x%2==1 means the x, when divided by 2, gives the remainder of 1. (It's called the modulo operator, it will divide the number on the left hand by the number on the right and give the remainder.)
So the conditions which have to be fulfilled are, the number must
a) give the remainder of 1 when divided by 2, and
b) be bigger than 4.
I was also confused about the break code being put after the print(x) command but I messed around with my own code for a bit and it basically means after printing one number, it'll stop.
In this list the first number which fulfills both requirements is 5. Then the break stops it there. Therefore the output is 5!
0
The loop iterates over each element in the list.
For each element, it checks if the element is odd (x % 2 == 1) and greater than 4 (x > 4).
When it finds such an element (5), it prints it and then exits the loop immediately using the break statement. Therefore, only the first qualifying element is printed, which is 5.
0
x = -8
y = -4
print(x + y == 2)
print(not (x + y == 2))
print((x > 9) and (x != y))
print(not ((x > 9) or (x != y)))
- 2
You can try it on code playground
- 2
ans: 5
- 2
5