Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
Can anyone tell me why this code gives me True but 4 is not in the list??please tell me the concept
num=[10,9,8,6,7,5] num[0]=num[1]-5 print(4 in num)
14 Respostas
+ 7
because the first value in the list is reassigned to the value 4 before you check if 4 is in the list.
num[1] is the second value in the list: 9.
9 - 5 is 4
+ 3
with this resource, you can independently monitor your program step by step:
Visualize your code execution
(Python, Java, C, C++, JavaScript, Ruby)
https://pythontutor.com
+ 2
Hi! Becouse the third line of code takes the second element of the array-this is 9, subtracts 5 and inserts the result (9-5=4) into the zero element of the array. i.e., in fact, replaces 10 with 4
+ 2
Thanks Slick and ŠÆŃŠ¾ŃŠ»Š°Š² ŠŠµŃŠ½ŠøŠ³Š¾ŃŠ°(Yaroslav Vernigora) ...now i can understandš
+ 2
Or just use any modern IDE, they usually have this feature to single step through code and also to keep watch on certain variables you want.
+ 2
Somil Khandelwal becouse The āŖļøin āŖļø operator returns True if the specified substring is part of another string. Otherwise, it returns False .
+ 1
it all depends on where you write that and i dont see your code, soo...
+ 1
The in operator returns True if the specified substring is part of another string. Otherwise, it returns False .
+ 1
IDE's for Python:
PyCharm
Spyder
Thonny
Eclipse + PyDev
Sublime Text
Atom
Visual Studio Code
+ 1
num[1] which is 9 in your list
num[1]-5 means 9-5
that's why you got 4
+ 1
num[0]=num[1] so its true
+ 1
the second line num[0]=num[1]-5 replaces the 10 with a 4(9-5). When the 3rd line runs, 4 is in the list this True
0
But when i write
print(num)
It gives me[10,9,8,6,7,5]
Why it doesn't give me 4?
0
Bcos, num[0] has been replaced by num[1]-5. i.e. 10 has been replaced by 9-5=4. So, d 1st no in the list is now 4 not 10 again.