0

nums=[1,"2","3","4"] print (4 not in nums)

How this could print true? anybody explain ?

25th May 2021, 6:20 AM
Durga M
Durga M - avatar
7 Antworten
+ 11
4 and "4" are different
25th May 2021, 6:26 AM
Oma Falk
Oma Falk - avatar
+ 1
Coding Kitty, because the "not" operator is used. 4, how is the integer value not in the "nums" array? Well no.
25th May 2021, 6:43 AM
Solo
Solo - avatar
+ 1
wait. ooh. I forgot. he used not
25th May 2021, 7:20 AM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 1
Because 4 is an integer. Integers and strings are different data types. Integers are whole numbers and inputted like this in many programming languages: 4, 999 , 1000000 to input an integer in Python, use: int(input()) Example: integer = int(input()) Strings are any characters (including numbers and integers) and usually inputted with single (') or double (") quotation marks. Examples are: "4", "1.007", "string" Example inputting in Python: string = ["str", "ing"] string = str(input()), input: string, result: 'string' Let me give you some examples on how different strings and integers: 4==4 Outputs True since they are equal and both an integer 5=="5" Outputs False since the first 5 is an integer and the second 5 is an string (inputted with quotation marks) "5"=='5' Outputs True since they are both strings (double vs single quotation marks, but still both quotation marks)
25th May 2021, 10:07 AM
Ivan Vincent
Ivan Vincent - avatar
+ 1
It's output could be True because of using 'not', it alter the bool value
26th May 2021, 5:26 AM
Anshul
0
@Frogged but it should print false though. he says it printed true even though there's no integer 4 in that list?
25th May 2021, 6:33 AM
Rellot's screwdriver
Rellot's screwdriver - avatar
0
And because you use not in a different data (4 vs "4"). I've explained their difference recently.
25th May 2021, 10:15 AM
Ivan Vincent
Ivan Vincent - avatar