Can you divide 2 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

asked Jun 11, 2011 at 12:16

1

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

answered Jun 11, 2011 at 12:17

so12311so12311

4,0391 gold badge26 silver badges37 bronze badges

Multiply by 1.

result = 1. * a / b

or, using the float function

result = float(a) / b

answered Jun 11, 2011 at 12:45

rizariza

15.3k7 gold badges28 silver badges29 bronze badges

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.

answered Jun 11, 2011 at 12:29

Thomas WoutersThomas Wouters

127k23 gold badges146 silver badges122 bronze badges

x / y

quotient of x and y

x // y 

(floored) quotient of x and y

Can you divide 2 variables in python?

chown

50.9k16 gold badges132 silver badges169 bronze badges

answered Jun 11, 2011 at 12:20

nikagranikagra

8132 gold badges9 silver badges23 bronze badges

3

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

Try float(a)/b

answered Jun 19, 2016 at 9:58

0

In this article, you’ll learn about the division operators // and / in Python 2 and 3. You can check out the version in your Python script as shown here.

A short visual overview of the division operator in Python 2 and 3:

Can you divide 2 variables in python?
Figure: The differences and similarities of the division operators in Python 2 and 3.

Assuming two integer values stored in variables a and b, there are four different cases depending on which Python version and division operator you use:

  • Python 2: The single front-slash operator a/b performs integer division.
  • Python 2: The double front-slash operator a//b performs integer division.
  • Python 3: The single front-slash operator a/b performs float division.
  • Python 3: The double front-slash operator a//b performs integer division.

Let’s dive deeper into these cases with some examples next!

  • How to Divide Two Integers in Python 2?
    • Float Division in Python 2
  • How to Divide Two Integers in Python 3?
  • Summary

For Python 2, dividing two integers uses integer division. This is also known as “floor division” because it applies the floor function after division. For example, 7/2 in Python 2.x would result in the value 3. However, using “/” is deprecated — to perform floor division, use “//” that is available in Python 2.2 and later versions.

# Python 2.x
print(7/2)

Output:

3

Float Division in Python 2

To perform float division in Python 2, use the from __future__ import division statement and use the single front-slash a/b to perform float division as in Python 3. For example, 7/2 will now result in 3.5.

from __future__ import division
print(7/2)
# 3.5

Alternatively, you can multiply with the float 1.0 to “infect” the numerator so that one of the operands of the division operator is a float value and the whole division becomes a float division as well:

result = 1.0 * 7 / 2
print(result)
# 3.5

Alternatively, you can use the float() built-in function on the numerator or denominator to perform float division.

result = float(7) / 2
print(result)
# 3.5

result = 7 / float(2)
print(result)
# 3.5

How to Divide Two Integers in Python 3?

For Python 3, dividing two integers using normal float division. For example, 7/2 in Python 3.x would result in the floating point value 3.5.

print(7/2)

Output:

3.5

You can read more discussions about this here.

Summary

A very important lesson in Python to learn from the beginning is “Division in Python”. What it means to divide in Python, and the different uses of Python arguments or operators. Many people do not learn these subtle differences. When they do not learn, it costs them hours and days of debugging programs.

You can use the division in two different ways:

  • Integer division takes two numbers and divides them to give a result of a whole number. In Python 3, integer division (or floor division) uses the double front-slash // operator. In Python 2, integer division uses the single front-slash / operator.
  • Float division takes two numbers and divides them and results in a decimal value. In Python 3, the use of regular division uses the single front-slash / operator. This operator will result in a decimal value.

You can learn more about division in Python here.

Can you divide 2 variables in python?

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

Can you divide in Python?

In Python, there are two types of division operators: / : Divides the number on its left by the number on its right and returns a floating point value. // : Divides the number on its left by the number on its right, rounds down the answer, and returns a whole number.

Can you divide two strings in Python?

Python Split function Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.

How do you divide equations in Python?

In Python 3. x, slash operator ("/") does true division for all types including integers, and therefore, e.g. 3/2==1.5. The result is of a floating-point type even if both inputs are integers: 4 / 2 yields 2.0.