This question already has answers here:
binary numbers?
(8 answers)
Closed 8 years ago.
I wonder why adding one or multiple leading zeros to an integer in Python leads to different results when using the bitshift-operators:
In: 10<<1
Out: 20
Adding a "0" in front of the integer:
In: 010<<1
Out: 16
Meanwhile, I found the answer is quite simple - but maybe it's worth sharing it:
According to this answer, adding a leading zero to an integer will cause Python to interpret it as an octal/base 8.
In: int("010",8)
Out: 8
Thus, left-shifting the octal (or decimal 8), i.e. multipicaton by 2**1, leads to 16
In: 8<<1
Out: 16
Related
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 3 years ago.
Pretty new to python, facing a problem that requires basically the opposite of the remainder "%" function. For example, if I wanted to divide 81.5 by 20, my output would be 4. My best attempt is as follows:
amount = 81.504
round(amount, 2)
num20s = amount / 20
int(num20s)
I've tried several different combinations of the above code, but nothing has worked so far. This is the closest I've gotten to what I want, but it won't work in edge cases, and for some reason still represents the number with a ".0" at the end, so that last line must not be doing anything.
Integer division operator in python is "//".
>>> amount = 81.504
>>> amount // 20
Out[3]: 4.0
>>> int(amount // 20)
Out[4]: 4
This question already has answers here:
Is integer division always equal to the floor of regular division?
(4 answers)
Closed 3 years ago.
I was making an integer to Boolean program and was dealing with some large numbers
The test case was - 15921396743627894741911
When I used
r/2 the output was 7.960698371813948e+21
int(r/2) gave me 7960698371813947736064
and r//2 gave me 7960698371813947370955
Why is the value for the last two cases so vastly different. Thank you
In Python 3, / does "true division", which returns a float, but floats have limited precision.
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 7 years ago.
I need to print double with precision equal exactly to 6, I found function round:
print(str(round(result, 6))
But in case result itself has less precision, the print function skips zeros at the end.
Gor example, the output of such code,
print(str(round(4.0, 6)))
is
4.0
But what I need is
4.000000
How can I reach this?
Try using a format string:
print("%.6f"%4.0) # 4.000000
Or alternatively:
print("{:.6f}".format(4.0))
See the Python documentation for details on format strings and more examples.
This question already has answers here:
How to return a number as a binary string with a set number of bits in python
(3 answers)
Closed 7 years ago.
I am trying to convert hex to binary in python. I am using:
hexNumber = "0x3a81"
print bin(int(hexNumber,16))
The return I am getting for hexNumber = 0x3a81 is: 0b11101010000001
I believe the correct conversion is 0011101010000001
The return I am getting for hexNumber = 0x53f6 is: 0b101001111110110
I believe the correct conversion is 0101001111110110
What does the b mean? If I am trying to slice the first 5 bits of the binary number, do I ignore the b or count it towards the string length?
The 0b is like the 0x on your hexNumber; it's an indication that the number is in a certain base, specifically base 2. If you want the binary digits, just slice that part off.
0b is the prefix for a binary number. It means that Python knows it's a number, and is just showing you it in binary.
This question already has answers here:
Most Pythonic way to print *at most* some number of decimal places [duplicate]
(3 answers)
Formatting floats without trailing zeros
(21 answers)
Closed 8 years ago.
I am using xlrd to read values from cells in an excel spreadsheet.
Whenever I detect a cell type = 2, then I know it is a number.
A number of 3 in cell will be returned as 3.0
And a number of 3.14 will be returned as 3.14
I will be converting numbers to text.
What function should I use to remove zeroes right of the decimal and the decimal?
The above 2 numbers should be 3 and 3.14
Use str.rstrip(), twice:
str_of_float.rstrip('0').rstrip('.')
This will remove trailing zeros, and if that leaves you with a trailing . it's removed as well.
Demo:
>>> '3.14'.rstrip('0').rstrip('.')
'3.14'
>>> '3.0'.rstrip('0').rstrip('.')
'3'
>>> '3000.0'.rstrip('0').rstrip('.')
'3000'
Don't be tempted to use .rstrip('.0'); it'll remove too many zeros:
>>> '3000.0'.rstrip('.0')
'3'
I always use format when printing values to strings. Using the format specs, it gives a good deal of control over what is printed.