This question already has answers here:
How are strings compared?
(7 answers)
Closed 5 months ago.
I came up with this comparison issue in python in binary values which is mentioned here in the image.
What are the possible reasons for returning "True" to the comparison between bin(1538)<bin(5138) since numerical value and binary digit length of the two binary values are contradictory to the output in python? Are there any specific comparison rules in python when it comes to the comparisons between binary values generated by bin() function (python default function) or are they similar as normal number comparisons?
because you are comparing two strings and the first string is "larger" than the second one...
try this as an example:
x = bin(1538)
print(x)
print(type(x))
y = bin(5138)
print(y)
print(type(y))
returns this:
0b11000000010
<class 'str'>
0b1010000010010
<class 'str'>
Related
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:])
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'>
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:
Python floating point number comparison
(3 answers)
Closed 5 years ago.
I write below program but My program cannot check the two number is equal or not.
There is no error,
I would be appreciate for any help.
import math
def IsCollinding(ball1,ball2):
distance=math.sqrt((((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2)))
print(type(distance))
print(type(ball1[2]+ball2[2]))
if(distance==ball1[2]+ball2[2]):
print("Is Coliding")
else:
print("Not Coliding")
print(distance)
print(ball1[2]+ball2[2])
ball1=[2,2,3]
ball2=[11,11,9.7279220614]
IsCollinding(ball1,ball2)
output:
<type 'float'>
<type 'float'>
Not Coliding
12.7279220614
12.7279220614
You can not really do this. Floats may appear equal, but are actually different due to floating point precision. However, you can cheat. We can call two numbers "equal" if the difference between the two is very small.
The function looks like this:
(x - y) < delta
Where delta is a small number. Implemented in Python:
def almost_equal(x, y, delta=1e-10):
return abs(x - y) < delta
I use abs here to get the absolute value of the difference. We avoid having to deal with negative numbers and the order of the x and y arguments this way.
Comparing two floats is one of the most common gotchas, and is something most of us run into at one point. Googling "comparing two floats python" should have returned plenty of informative results about this topic.
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