This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 4 years ago.
In the following code :
magicnumber = 256
for n in range (500):
if n is magicnumber:
print ("the magic number is " , n)
break
else:
print(n)
The loop breaks at 256, but if you set magicnumber to 257, it doesn't. Why ?
Because you are checking for identity with is (instead of checking for equality with ==).
As an implementation detail, Python keeps an array of integer objects for all integers
between -5 and 256, when you create an int in that range you actually just get
back a reference to the existing object.
So integers above 256 will still be equal, but not identical (unless they refer to the same object, you can compare that with id()).
Source: https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong
Related
This question already has answers here:
Check if input is positive integer [duplicate]
(2 answers)
Closed 3 years ago.
Sometimes upon receiving numeric parameters it is required to check whether the received numeric value is or not is a positive integer value.
How can we do this in python?
One simple way:
if isinstance(number, int) and number > 0:
# number is a positive integer
This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 4 years ago.
I am unable to figure out the output of the following program
big_num_1 = 1000
big_num_2 = 1000
small_num_1 = 1
small_num_2 = 1
big_num_1 is big_num_2 # False
small_num_1 is small_num_2 # True
What is happening above?
Why is one False and other True.
Source: https://luminousmen.com/post/python-interview-questions-senior
Because is compares the identity of two objects (that is, if they're exactly the same object.) You want to test for equality, and for that you must use the == operator:
big_num_1 == big_num_2
=> True
small_num_1 == small_num_2
=> True
In case you're wondering why this example worked:
small_num_1 is small_num_2
=> True
It's because Python caches small (between -5 and 256) int objects internally, so the objects used in the comparison were taken from the cache and were the same. big_num_1 and big_num_2 are greater than 256, so they're represented by two different objects, and the identity test fails.
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 4 years ago.
I'm playing with the 'is' operator in the interactive shell when I encountered an odd behavior with the below code:
It goes as expected at first:
>>> x = 11
>>> y = 11
>>> x is y
True
But when I tried this one:
>>> x = 987456
>>> y = 987456
>>> x is y
False
After further tries using id() function, I noticed that integers >256 points on the same object while others are not. I also noticed that this behavior only occurs in the python interactive shell. What's with this behavior?
is checks for memory address. Immutable objects that are wrappers around C type tends to have same memory address, whereas others don't. The difference here is the bytes required to store the integers.
This question already has answers here:
How do you check whether a number is divisible by another number?
(9 answers)
Closed 6 years ago.
Wondering what is the most efficient way to check if an integer can or cannot be divided by another number (could be float) in Python 2.7. Or more general, what is the most efficient way to check if an integer can be divided by n (n could be a float number) in Python 2.7.
My pain point is, if I try to get x/n, it is always an integer.
Try
if x % n == 0 :
hope this helps !
Here:
x = 25
y = 2.5 # Or something
if not x % y: # Works with float too
print 'can divide'
else:
print 'cannot divide'
This question already has answers here:
and / or operators return value [duplicate]
(4 answers)
Closed 6 years ago.
This looks like a short circuit way of writing code but I just can't understand it. Is there a specific way to read this kind of short circuit.
e.g:
n = n and int(n)
n = n or int(n)
and conditions return the last truthy value or the first falsy one. So if n is falsy, n will remain whatever value it was. If n is truthy, it will be cast to an int.