How do I get binary value as an integer in python? [duplicate] - python

This question already has answers here:
Python: Strip off string quotes from binary number
(5 answers)
Closed 1 year ago.
b = 15
a = bin(b) # I want return as an integer not string
print(a, type(a)) # output is string, actually I want is integer
# output - 0b1111 <class 'str'>
So, I want to get bin() function return as an integer

The int function is used to convert to the integer. We need to pass the number and its base to convert it into an integer (since, the base for binary values is 2).
a = int('101',2)
print(a)

If you question is about converting for example 5 into bin in python, the bin function actually gives 0b101 as the result. So the simple trick to get 101 as an int is 👇
intnum=int(bin(number)[2:])

Related

How do I convert numbers with e to digits only? [duplicate]

This question already has answers here:
How to convert exponent in Python and get rid of the 'e+'?
(2 answers)
Closed 3 months ago.
I want to convert numbers like 1.28e+21 to a long digits only number but the following code doesn't make a difference.
n = 1.28e+21 b = 1.28*10**21 print(b)
b still has an e.
How do I get rid of e?
These numbers in exponential format are from type float in python.You can use int to convert it to an integer.
>>> n = int(1.28e+21)
>>> n
1280000000000000000000
You can also use decimal module like this:
>>> import decimal
>>> decimal.Decimal(1.28e+21)
Decimal('1280000000000000000000')
>>>

How to reverse the float.hex() method in Python [duplicate]

This question already has answers here:
Converting hex string representation to float in python
(2 answers)
Closed 2 years ago.
Is there a way to reverse the hex() method of a float in Python? For example,
n = 1280.03125
n_hex = n.hex()
print(n_hex) # result--> 0x1.4002000000000p+10
How can I convert 0x1.4002000000000p+10 back to 1280.03125? I know you can use int(num, base) to convert a number to integer but it doesn't support decimal.
Try float.fromhex(str):
>>> float.fromhex("0x1.4002000000000p+10")
1280.03125

How to compare user input to a randomly generated number? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
So I made a "Guess the Number" program with Python and tested it. Apparently, choosing the correct number still comes up as the incorrect else statement. How can I fix this?
As you can see the 3 I entered apparently isn't the same as the 3 my program came up:
You're comparing the return value of your call to input (the string in your variable usernum) with the return value of random.randint which is an integer in your variable EasyRN.
You'll need to convert either the integer into an string:
usernum = int(usernum)
Or the string into an integer:
EasyRN = str(EasyRN)
Afterwards, you can use == to compare them.
The result of input() is text (in Python, a str, short for the word "string" which is used in programming), while the output of random.randint() is a number (an int, short for "integer").
>>> type("3")
<class 'str'>
>>> type(3)
<class 'int'>
If you compare a str and an int they will never be equivalent, as it's an apples-to-oranges comparison.
Look at the int() function which converts a string to an integer.
>>> int("3")
3
>>> type(int("3"))
<class 'int'>

Python convert int to string preserving zeros [duplicate]

This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 6 years ago.
i'd like to convert a decimal to a string, where zeros at the end are preserved.
Using str method erases the last zeros.
Example:
number=0.20
Goal: "0.20"
e.g. using: str(number)="0.2" doesn't seem to work.
If you want 2 decimal places use:
number = 0.20
str_number = '%.2f' % number
Number before f indicates the desired number of places.
This can be done using string formatting.
"{0:.2f}".format(number)
Will return 0.20.
Doing your chosen method won't work because upon declaring number = 0.20 it omits the last zero right away. If you put that into your idle:
number = 0.20
number
0.2
So declaring number as str(number) is doing str(0.2).
Use the % operator with an appropriate format string:
'%1.2f' % number
=> '0.20'

Converting a number in exponential form to decimal form in python [duplicate]

This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 8 years ago.
I have a very silly question, suppose if i have a number 1.70000043572e-05 how should I convert it into float i.e. 0.0000170000043572.
You need to convert to a float and use str.format specifying the precision:
In [41]: print "{:f}".format(float("1.70000043572e-05"))
0.000017
# 16 digits
In [38]: print "{:.16f}".format(float("1.70000043572e-05"))
0.0000170000043572
Just calling float would give 1.70000043572e-05.
Using older style formatting:
In [45]: print( "%.16f" % float("1.70000043572e-05"))
0.0000170000043572
If you are just inputting the number into your script python will understand that as a float.
If it is a string then use the builtin float on it to do the conversion for example:
x = float("0.423423e4")
print "%.2f" % (x)
will output
4234.23

Categories

Resources