Use of <> in Python. Is <> an operator? [duplicate] - python

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 !=

Related

When is it ok to check for literal values with "is" in Python? [duplicate]

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?

Why "~False" is -1 in Python? [duplicate]

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.

Python function in an if-statement [duplicate]

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):

string object behavior in python [duplicate]

This question already has answers here:
comparing two strings with 'is' -- not performing as expected
(3 answers)
Closed 3 years ago.
curious to know the reason for following
x='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
y='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
x is y
#output : True
x='a'*21
y='a'*21
x is y
#output : False
Q1: since string interning is done for string literals then why not for 'a'*21 ? is it not a string literal?
Q2: is it because expression 'a'*21 is evaluated at run-time and interning is done at runtime?
#
s1 = "strin"
s2 = "string"
s1+"g" is s2
# output : False
# Explaination : Above code involves a run-time concatenation, the result of which is not automatically interned
Q3: How to know if an expression is going to be evaluated at run-time or compile time?
is will return True if two variables point to the same object, == will return True if the objects referred to by the variables are equal. If you try:
s1 = "strin"
s2 = "string"
s1+"g" == s2
# True
x='a'*21
y='a'*21
x == y
True
In your first case, Python was intelligent enough to use one single object to refer to the literal string you described twice. This is generally not the case.

How to write if-then statement as a logical statement? [duplicate]

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

Categories

Resources