This question already has answers here:
Negation in Python
(4 answers)
Closed 2 years ago.
I have a function which returns true/false. Now I tried to use in a condicional, like this:
isOdd = lambda n: True if n%2 != 0 else False
if !isOdd(2):
print('Yey')
but I got SyntaxError: invalid syntax
how could I use ! in a function like this? it looks like this only works if:
if isOdd(2) == False:
print('Yey')
I don't believe Python uses ! for negation like C does. Try
if not isOdd(2):
Related
This question already has answers here:
The tilde operator in Python
(9 answers)
Closed 1 year ago.
pd_selftest = pd_selftest[pd_selftest['SICCD'] != 0]
pd_selftest = pd_selftest[~pd_selftest['SICCD'].isnull()]
I'd like to know what the function of the ~ is in the above code.
That's the bit-wise invert or not operator. So, it returns only those lines where the SICCID column is not null. I would probably use the word not in this case.
This question already has answers here:
Why isn't "is" comparison used in place of "==" for primitive types?
(1 answer)
Boolean identity == True vs is True
(7 answers)
Is there only one True and one False object in Python?
(2 answers)
Closed 2 years ago.
I want to check if a variable is True (not just its truthiness).
This is easily checked like this:
>>> a = 1
>>> isinstance(a, bool) and a
False
>>> a = True
>>> isinstance(a, bool) and a
True
Of course, that is totally fine. However, the same behaviour can be seen in the following code:
>>> a = 1
>>> a is True
False
>>> a = True
>>> a is True
True
In my opinion, the second snippet is a bit more readable (if you understand how is works). With Python 3.8, I get a SyntaxWarning when executing the second snippet. Would the second example work with all Python implementations? Is there a guarantee that bool objects of the same value are identical in this way? I believe I have read somewhere that this is not guaranteed for int objects. Is it ever ok to check literal values with is like this?
This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 2 years ago.
Why "~False" is -1 in Python?
I was using a boolean variable in Python. When I try to do not of that, it returns -1. I want to understand why so? why a variable a changing its data-type duo to this operation.
Trying to add more details
b0 = False
print(type(b0))
b0 = ~b0
print(type(b0))
>>bool
>>int
The tilde ~ is the bitwise 'not' operator, not the boolean 'not' operator. To do a not you probably want 'not False'.
The reason for it changing its data type is that it treats False as binary 0 and then flips it to -1.
This question already has answers here:
Python ? (conditional/ternary) operator for assignments [duplicate]
(2 answers)
Closed 7 years ago.
I Java it's possible to represent IF-THEN statement in the following form:
a = (x==10) ? true : false;
that is equivalent to
if (x==10)
a=true;
else
a=false;
Is it possible to do the same thing in Python?
a = True if x == 10 else False
or simply
a = x == 10
This question already has answers here:
What does `<>` mean in Python?
(5 answers)
Closed 9 years ago.
>>> 1<>1
False
>>> 1<>2
True
>>> 1<>3
True
>>> 1<>0
True
>>> 1<>1
False
What is the use of <> in Python?
Can someone help in explaining the above or the '<>' in general.
It's the inequality operator, synonymous to !=. From the documentation:
The forms <> and != are equivalent; for consistency with C, != is preferred; where != is mentioned below <> is also accepted. The <> spelling is considered obsolescent.
The <> spelling has been removed in Python 3.
it looks like it's the same as !=