Loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Loops

list = [2,3,4,5,6,7] for x in list: if(x%2==1, x>4): print(x) break Can someone explain this?

22nd Apr 2021, 2:42 AM
suresh moru
suresh moru - avatar
4 Answers
+ 1
The for loop is used to iterate over objects in finite time. The current for loop is iterating over the list variable, so during each iteration the value of x is equal to an item in the list. This is why you can check over the entire list's elements for conditions. But it will be more readable if you remove the parentheses and join those two conditions with a logical operator, like this : if x%2==1 and/or x>4 : print(x) And I don't understand why you want to break up the loop when you are sure this is not going to run infinite.
22nd Apr 2021, 5:10 AM
Ervis Meta
Ervis Meta - avatar
+ 1
https://code.sololearn.com/cSs8ujY0gCGq/?ref=app I commented on each line. In short, the code is buggy.
22nd Apr 2021, 5:18 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
- 1
Ok, hang on.
22nd Apr 2021, 5:07 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
- 1
Kapil Unstoppable well, not so much ignore the line as will always see the line as true, and thus the line is ineffective.
22nd Apr 2021, 1:31 PM
Wilbur Jaywright
Wilbur Jaywright - avatar