What is happening in the code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is happening in the code?

An algorithm i found working... but also found out that '''(1 > 2) in [0]''' the Output is True https://code.sololearn.com/c06SdhxNIAgC/?ref=app

11th Aug 2019, 5:34 AM
Charles Atienza
Charles Atienza - avatar
3 Answers
+ 6
Generally, when working with conditional statements such as that, they will return a boolean value of either True or False. These values can also be thought of as numbers, with 1 being True and 0 being false. In your code, you have a for loop that loops through a range of 0 and the length of the array provided (which in this case is 5). What it will do is test for the value of num (with num being 1) being greater than 2 (which will always return False or 0) and see if that value is within an array containing the value of i, the variable being iterated through the loop. Following its execution, we get this: 1) 1 > 2 = 0, and 0 in [0] returns True 2) 1 > 2 = 0, and 0 in [1] returns False 3) 1 > 2 = 0, 0 in [2] returns False ... same thing for the next iterations
11th Aug 2019, 5:47 AM
Faisal
Faisal - avatar
+ 2
Charles Atienza It's not checking the nums array, but rather [i], which is an array that only consists of the current value of i.
11th Aug 2019, 5:58 AM
Faisal
Faisal - avatar
+ 1
In my understanding to your explanation, What is happening is that... (1 > 2) returns 0 since it is false then checks if 0 is inside the nums List? Is it correct?
11th Aug 2019, 5:57 AM
Charles Atienza
Charles Atienza - avatar