noli noli - 5 months ago 14
Python Question

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

Answer

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