Python's system of deciding for you what type to give all your variables is nice and all, but is there a way to force it to give a variable a certain data type? For instance, if you give it the following:

n = 2
m = 3
print n/m

It'll print out 0, because all the variables are ints, and it truncates the fraction off. Any way to get around this?

Recommended Answers

All 2 Replies

n = float(2)
m = float(3)
print n/m

Or

n = 2.0
m = 3.0
print n/m

Hope that helps.

Actually, the next version of Python (3.0) will use '/' for floating point divisions and '//' for integer divisions.

Pascal for instance has '/' for floating point divisions and 'div' for integer divisions since inception 30+ years ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.