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.
Related
This question already has answers here:
Real world use cases of bitwise operators [closed]
(41 answers)
Bitwise operation and usage
(17 answers)
Closed 12 months ago.
I understand that the bitwise and operator (&) is equivalent to a product of two bit values. When would I use it?
Please also help me understand what num&1 does in the code below:
def func(num):
n = 1 + func((3*num+1) if num&1 else (num>>1))
return n
As the comments mentioned, num&1 is a bitwise AND between num and 1.
Since 1 in binary is ...000000001, the AND will result True iff the least significant bit of num is 1, in other words, if it is odd (here some explanation of binary)
This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
So I have this code:
t=int(input())
while t:
s=int(input())
n=bin(s)
n=n[2:][::-1]
if n.count('1')==1:
pos=n.find('1')+1
print(pos)
else:
print('-1')
t-=1
I would like to know exactly what's going on in this line:
n=n[2:][::-1]
What does [::-1] means?
It takes the reverse of binary value of n excluding "0b" values in the beginning. For example if you enter 6 for value of n. The binary value would be 0b110 and reverse value excluding 0b would be 011.
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
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'