noli noli -5 years ago 383
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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download