+ 1
for loop
What is the output of this code? #i know the answer is 5 but how and why please help list = [2, 3, 4, 5, 6, 7] for x in list: if(x%2==1 and x>4): print(x) break
3 Réponses
+ 4
Amol Chourasia because 5 % 2 == 1 and 5 > 4 but after this there is break so it will print only 5.
+ 3
list = [2, 3, 4, 5, 6, 7]
for x in list:
if(x%2==1 and x>4):
print(x)
break
it will check condition if (x%2==1 and x>4) if this condition will true then loop will break when the first element of list 2 then it will check condition if(2%2==1 and x>4) this is false then again loop will run then it will check 2nd element of list which is 3 then check if condition
If( 3%2==1and x>4) this is false becoz 3%2 Remainder is q but second condition 3>4 this is false so again loop will check of 4 this is also false after that it will check for next element which is 5
if (5%2==1 and x>4) here both condition are true then print statement will print 5 after that break soo loop will break here it won't check for next element.
.......Thankyou.......