To divide two integers and save them as prime numbers?

Asked 2 years ago, Updated 2 years ago, 80 views

I thought 0.5 would come out if I said 1/2, but 0 came out If you divide two integers, you just cut off the decimal point

When a and b are int

c = a/b

How do we get c to float as a result?

a = 1
b = 2
print a/b

python floating-point integer division

2022-09-22 22:31

1 Answers

division import modules.

from__future__import division #Not required for Python 3 or higher

print 1/2

If you do not write __future___, you can write as follows. However, this method can cause TypeError, so use it carefully.

a = 1
b = 2
float(a)/float(b)

You can use it with this

* Python 3 saves the result of int/int as float. So you don't have to import anything, and you don't have to change the type.


2022-09-22 22:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.