This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 8 years ago.
When I write run the following code in Python:
m = 010001110110100101110110011001010010000001101101011001010010000001100001011011100010000001000001
print "m = ", m
I receive the output:
m = 7772840013437408857694157721741838884340237826753178459792706472536593002623651807233
What's happening? Is Python automatically converting from base 2 to base 10?
You're starting m with 0. Python assumes it's an Octal (see this)
What you're seeing is the decimal representation of the octal number.
If you want to work with that as binary numbers, I'd recommend setting it as an str and then parse it to int specifying that you're parsing something in base 2:
>>> m='0100011'
>>> int(m, 2)
35
Kind of, it interprets a number starting with 0 as octal, so it actually isn't binary in the first place.
In any case, you can make it output the integer as binary like so:
print "m = ", "{0:b}".format(m)
Related
This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 5 months ago.
I have an input a=int(1010). I want to find integer equivalence of binary 1010. If I use bin(a) then output will be 1111110010, but I want to get 10.
You need to tell python, that your integer input is in binary. You can either parse a string with a defined base, or ad a 0b-prefix to your code constants.
a = int("1010", base=2)
a = 0b1010
print(a) # result: 10
print(bin(a)) # result: 1010
This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 3 years ago.
I want number from input to print out with 2 decimal places. You can assume that the number will always be a float.
num = 20.0
Desired output - 20.00.
I've tried this code:
num = round(num, 2)
num = float('{0.2f}'.format(num))
print(num)
This should work
print('{0.2f}'.format(num))
When you turn this string back into a float with float() the formatting is lost.
No matter what you do to the float value, as long as it is still a float, it does not have any internal concept of decimal places.
If you want to display two decimal places, then that happens when you convert to text - which everything you print is, whether you asked for the conversion or not. You cannot make num "be" 20.00 as opposed to 20.0, because those aren't actually different things. (And keep in mind that the float simply cannot represent all decimal values exactly.)
Therefore, we use string formatting in the print call:
num = 20.0
print('{.2f}'.format(num))
# Or, using f-strings:
print(f'{num:.2f}')
This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here
This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Pad python floats
(4 answers)
Closed 8 years ago.
I'm sorry, I know this must be a duplicate, I can't find where else it's posted. Please feel free to link me to the original question and mark this as duplicate.
I would like to print a 3 digits of a number AFTER the decimal point in it.
For example:
number = 523.637382
I would like to print: 523.637
I have a feeling I can use something similar to this
print(str(number)[:7])
>>>523.637
However, this will not work if the number before the decimal is not 3 decimals.
Bonus points:
Would this be easy?
number = 500.220
#magic
>>>500.22
number = 500.2000003
#magic
>>>500.2
A (built-in) function that could do this is round:
>>> number = 523.637382
>>> rounded = round(number, 3) # 3 decimal places, for example
>>> rounded
523.637
This has already been answered for example here.
The good news, to answer the second part of your question, is that the round function automatically removes trailing zeroes. It's much harder to retain the zeros if you're defining a new variable: you need the decimal module; but it looks that that isn't necessary here.
>>> number = 523.60000001
>>> rounded = round(number, 3)
>>> rounded
523.6
print("%.3f" % number)
or, using the new-style formatting,
print("{0:.3f}".format(number))
If you're printing a str like above you can use string interpolation:
number = 33.33333
print("{0:.3f}".format(number))
#=> 33.333
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how to parse hex or decimal int in Python
i have a bunch of Hexadecimal colors in a database stored as strings.
e.g. '0xFFFF00'
when i get them from the database i need to convert this string into an actual hexadecimal number, so
0xFFFF00
how can i do this in python
This is one way to do it:
>>> s = '0xFFFF00'
>>> i = int(s, 16)
>>> print i
hex(int('0xFFFF00', 16))
Also this works
number = int('0xFFFF00',0)
print("%x follows %x" % (number+1, number))
0 argument tells interpreter to follow the Python rules of numbers to decide the used format of number so this expression will work right for all numbers.