Why doesn't != work for string index comparison?
Question:
I tried to run this code with input "AC039"
code = input("Enter code: ").upper()
if code[0] != ('N' or 'A' or 'C' ):
print("The first character must be N, A or C")
else:
print("Pass!")
It gave me the output error result:
The first character must be N, A or C
However, if I input "AC039" into the below code using ‘not in’,
code = input("Enter code: ").upper()
if code[0] not in ["N", "A", "C"]:
print("The first character must be N, A or C")
else:
print("Pass!")
The resulting output is:
print("Pass!")
Why doesn’t "!=" work for the first set of code, since both code[0] and ‘A’ are string types?
I ran a check using type function on code[0] and it returned string type.
code = input("Enter code: ").upper()
print(type(code[0]))
print(type('A'))
returns:
<class 'str'>
<class 'str'>
Answers:
I think this is best explained with a rough implementation of what a or b or c or ...
does.
import typing as t
T = t.TypeVar("T")
def Or(*args: T) -> T:
arg: T
for arg in args[:-1]:
if bool(arg) is True:
return arg
return args[-1]
>>> Or("N", "A", "T") # equivalent to `"N" or "A" or "T"`
'N'
>>> Or("", None, 0) # equivalent to `"" or None or 0`
0
These two lines are identical.
if code[0] != ('N' or 'A' or 'C' ):
if code[0] != 'N':
Because the operator ‘or’ checks if the first operand is true, then it takes it; otherwise, it checks another one.
Examples:
>>> 1 or 2
1
>>> 0 or 3
3
>>> 0 or 2 or 3
2
>>> False or 2
2
>>> False or 0
0
As a result, you can apply this pattern to solve your problem.
if code[0] in ('N', 'A', 'C'):
or
if code[0] == 'N' or code[0] == 'A' or code[0] == 'C':
It’s because the "or" operator compares sequentially.
from the given input "AC039",
code[0] != ('N' or 'A' or 'C' ):
what’s happening from the above code is, comparing one by one.
(code[0] != 'N') -> True
(code[0] != 'A') -> False
(code[0] != 'C') -> True
since the first ‘N’ doesn’t match with ‘A’, it stops going through other comparisons, ‘A’ and ‘C’.
print('N' != ('N' or 'A' or 'C' )) -> False
print('A' != ('N' or 'A' or 'C' )) -> True
print('C' != ('N' or 'A' or 'C' )) -> True
However, if code[0] not in ["N", "A", "C"]:
works because "not in" checks whole elements in the array.
I hope it helps.
I tried to run this code with input "AC039"
code = input("Enter code: ").upper()
if code[0] != ('N' or 'A' or 'C' ):
print("The first character must be N, A or C")
else:
print("Pass!")
It gave me the output error result:
The first character must be N, A or C
However, if I input "AC039" into the below code using ‘not in’,
code = input("Enter code: ").upper()
if code[0] not in ["N", "A", "C"]:
print("The first character must be N, A or C")
else:
print("Pass!")
The resulting output is:
print("Pass!")
Why doesn’t "!=" work for the first set of code, since both code[0] and ‘A’ are string types?
I ran a check using type function on code[0] and it returned string type.
code = input("Enter code: ").upper()
print(type(code[0]))
print(type('A'))
returns:
<class 'str'>
<class 'str'>
I think this is best explained with a rough implementation of what a or b or c or ...
does.
import typing as t
T = t.TypeVar("T")
def Or(*args: T) -> T:
arg: T
for arg in args[:-1]:
if bool(arg) is True:
return arg
return args[-1]
>>> Or("N", "A", "T") # equivalent to `"N" or "A" or "T"`
'N'
>>> Or("", None, 0) # equivalent to `"" or None or 0`
0
These two lines are identical.
if code[0] != ('N' or 'A' or 'C' ):
if code[0] != 'N':
Because the operator ‘or’ checks if the first operand is true, then it takes it; otherwise, it checks another one.
Examples:
>>> 1 or 2
1
>>> 0 or 3
3
>>> 0 or 2 or 3
2
>>> False or 2
2
>>> False or 0
0
As a result, you can apply this pattern to solve your problem.
if code[0] in ('N', 'A', 'C'):
or
if code[0] == 'N' or code[0] == 'A' or code[0] == 'C':
It’s because the "or" operator compares sequentially.
from the given input "AC039",
code[0] != ('N' or 'A' or 'C' ):
what’s happening from the above code is, comparing one by one.
(code[0] != 'N') -> True
(code[0] != 'A') -> False
(code[0] != 'C') -> True
since the first ‘N’ doesn’t match with ‘A’, it stops going through other comparisons, ‘A’ and ‘C’.
print('N' != ('N' or 'A' or 'C' )) -> False
print('A' != ('N' or 'A' or 'C' )) -> True
print('C' != ('N' or 'A' or 'C' )) -> True
However, if code[0] not in ["N", "A", "C"]:
works because "not in" checks whole elements in the array.
I hope it helps.