Can anyone tell me why this code gives me True but 4 is not in the list??please tell me the concept | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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)

4th Apr 2021, 4:29 PM
Ayushman Halder
Ayushman Halder - avatar
14 Answers
+ 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
4th Apr 2021, 4:33 PM
Slick
Slick - avatar
+ 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
4th Apr 2021, 4:46 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 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
4th Apr 2021, 4:40 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Thanks Slick and Ярослав Вернигора(Yaroslav Vernigora) ...now i can understand😅
4th Apr 2021, 4:43 PM
Ayushman Halder
Ayushman Halder - avatar
+ 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.
4th Apr 2021, 4:47 PM
Slick
Slick - avatar
+ 2
Somil Khandelwal becouse The ▪︎in ▪︎ operator returns True if the specified substring is part of another string. Otherwise, it returns False .
5th Apr 2021, 5:41 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
it all depends on where you write that and i dont see your code, soo...
4th Apr 2021, 4:38 PM
Slick
Slick - avatar
+ 1
The in operator returns True if the specified substring is part of another string. Otherwise, it returns False .
4th Apr 2021, 4:42 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
IDE's for Python: PyCharm Spyder Thonny Eclipse + PyDev Sublime Text Atom Visual Studio Code
4th Apr 2021, 5:00 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
num[1] which is 9 in your list num[1]-5 means 9-5 that's why you got 4
5th Apr 2021, 5:38 PM
Somil Khandelwal
+ 1
num[0]=num[1] so its true
5th Apr 2021, 5:39 PM
Somil Khandelwal
+ 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
6th Apr 2021, 8:26 AM
Tugume Daniel
Tugume Daniel - avatar
0
But when i write print(num) It gives me[10,9,8,6,7,5] Why it doesn't give me 4?
4th Apr 2021, 4:37 PM
Ayushman Halder
Ayushman Halder - avatar
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.
5th Apr 2021, 10:39 AM
Adibeh Esther
Adibeh Esther - avatar