[python] How can I divide two integers stored in variables in Python?

I've got two numbers stored in varibles, a and b, and i'd like to see of the ratio of those two is an integer or not in python. However when I try

result = a./b

it gives me a

SyntaxError: invalid syntax

however if I say

result = a/b

it truncates the decimal portion. How can I get the full numbeer to test for integrality?

I was going to use

if (not isinstance(result, (int, long))): 
    then do something for non-integer numbers..

Thanks, and i'm using python 2.7.1

This question is related to python

The answer is


Use this line to get the division behavior you want:

from __future__ import division

Alternatively, you could use modulus:

if (a % b) == 0: #do something

The 1./2 syntax works because 1. is a float. It's the same as 1.0. The dot isn't a special operator that makes something a float. So, you need to either turn one (or both) of the operands into floats some other way -- for example by using float() on them, or by changing however they were calculated to use floats -- or turn on "true division", by using from __future__ import division at the top of the module.


x / y

quotient of x and y

x // y 

(floored) quotient of x and y


Multiply by 1.

result = 1. * a / b

or, using the float function

result = float(a) / b

if 'a' is already a decimal; adding '.' would make 3.4/b(for example) into 3.4./b

Try float(a)/b