0
nums=[1,"2","3","4"] print (4 not in nums)
How this could print true? anybody explain ?
7 Antworten
+ 11
4 and "4" are different
+ 1
Coding Kitty, because the "not" operator is used.
4, how is the integer value not in the "nums" array? Well no.
+ 1
wait. ooh. I forgot. he used not
+ 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)
+ 1
It's output could be True because of using 'not', it alter the bool value
0
@Frogged
but it should print false though. he says it printed true even though there's no integer 4 in that list?
0
And because you use not in a different data (4 vs "4"). I've explained their difference recently.