Strange python 'is' operator in arithmetic [duplicate] - python

This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Closed 8 years ago.
I tried this operation in python interactive mode :
>>> (1*1) is 1
True
>>> (377*35) is 13195
False
>>> 377*35
13195
>>> 377*35 is 377*35
False
>>> 1*1 is 1
True
Could anybody explain why ' (377*35) is 13195 ' is false?
Thanks in advance!

A is B checks that A and B refer to the same object. It does not check whether A equals B numerically.
The reason for the different behaviour in your examples is that ints with small values (typically between -1 and 99
inclusive) are "interned" by the interpreter -- whenever a result has such a value,
an existing short int with the same value is returned.
This explains why is returns True for your examples involving small numbers but not for those involving large numbers.

Related

or operator does not returns boolean type [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
enter image description here
i'm now working on if statements
this is my code
a = [1,2,3]
print(4 or 5 in a)
and the outcome is 4 not False
this code returns what i have expected
a = [1,2,3]
print((4 or 5) in a)
I can't understand how or operator works
why the return is integer..? not True or False
If there are multiple conditions in an if statement, if the first statement is correct, it doesn't check the second statement. Since 4 is true, it doesn't check if 5 is in the variable a or not. Therefore, it returns the value 4 and not False
In this case you have expression written incorrectly and as a result it evaluates the way it does. What you should have written was:
>>> a = [2, 1, 4]
>>> 4 in a or 5 in a
Out[18]: True
In your case the expression evaluates only the condition 4 or 5 which evaluates to 4 as the first truthy value.
>>> 4 or 5 in a
Out[27]: 4
>>> 4 or 5
Out[28]: 4
In fact, variable a is completely skipped here and not evaluated at all. We can do the same but using variable b that has never been defined in this session and we get 4 instead of UnboundLocalError:
>>> 4 or 5 in b
Out[31]: 4
Similarly we can test this behaviour. When you compare two truthy values the result is always the first one the interpreter encounters.
>>> 'a' or 'b'
Out[29]: 'a'
>>> 'd' or 'a'
Out[30]: 'd'
I suggest reading docs on boolean operators in Python to get a better understanding of what's happening (for example, here). It can get confusing.

Understanding the "and" operator - Why is "None and None" not False? [duplicate]

This question already has answers here:
Logical operators in Python [duplicate]
(4 answers)
Closed 2 years ago.
I though I understood Python the "and" operator. But after seeing
assert (None and None) is None
it was apparently that my understanding was not precise. Which was that None and None would be the same as bool(None) and bool(None).
Does anybody have definition of the "and" operator and can explain the logic.
According to the official documentation:
help('and')
[...] The expression "x and y" first evaluates x; if x is false,
its value is returned; otherwise, y is evaluated and the resulting
value is returned. [...]
and returns the first value if it is "falsey"*. Otherwise it returns the second one.
For example:
3 and 6 -> 6
0 and 7 -> 0
[] and 'abc' -> []
* a thing is falsey if bool(thing) is False

if x or y in LIST is always true even if it is not [duplicate]

This question already has answers here:
How to test the membership of multiple values in a list
(12 answers)
Closed 3 years ago.
I have an if statement which always returns as true even if it is not.
I have a global list :
NUMBERS_LIST = []
Numbers get added to it via an API call, that works fine.
When it does the following:
def func():
if 8 or 9 in NUMBER_LIST:
return true
elif 1 or 2 in NUMBER_LIST:
return true
But for some reason, it always returns true on the first if statement, even if NUMBER_LIST = [1]
I debugged my program and can see that NUMBER_LIST does contain 1, it's type is int.
I tried doing int(8), converting both types to str but that did not fix my issue.
When I debugged and step through the program, it doesn't really tell me much, I am using Pycharm.
or does not distribute. What you have written is not equivalent to
if 8 in NUMBER_LIST or 9 in NUMBER_LIST:
which is what you want, but to
if 8 or (9 in NUMBER_LIST):
Since 8 is a truthy value, the containment operation is never evaluated.

Python is operator [duplicate]

This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Two variables in Python have same id, but not lists or tuples
(5 answers)
Closed 5 years ago.
I understand that the "is" operator checks identity between two objects, but what is this?
a=25
b=25
a is b
True
Why is this true if
a = [1,2,3]
b = [1,2,3]
a is b
False
And from this post, https://www.quora.com/Python-is-operator, it says all immutable objects, like integers from the first example, will have the first example's result, True, but tuples are immutable and this happens:
a = (1,2)
b = (1,2)
a is b
False
Can someone explain please?
Thanks.

What does < or > mean in python while comparing strings [duplicate]

This question already has answers here:
How are strings compared?
(7 answers)
Closed 6 years ago.
I have written this code in python 3.5
x="absx"
o="abcdef"
and if i am doing this operation,
x<o
False #it return's False and i think it should return True
So what is '<' doing in case of strings and why is it not returning true.
how is it comparing x and o?
The < or > would result in lexicographic comparison of the two strings:
>>> x="absx"
>>> o="abcdef"
>>> x > o
True
Lexicographic ordering is same as dictionary ordering, and basically, the operators are checking for which string would come earlier (or later) in a dictionary order. The behavior is same for both Python 2 and 3.
The final result does not depend on the size of the string, Example:
>>> "a" < "aaaaa"
True
In example above "a" would come before "aaaaa" when written in dictionary order. To compare by length of strings, use the len() function on the strings.
Lexicographic comparison. In your case o would come after x.

Categories

Resources