How To Divide In Python?

How To Divide In Python

In this tutorial you will learn How To Divide In Python.

In Python, the division is performed using the “/” operator.

The basic syntax for division is as follows:

result = numerator / denominator

Here, the numerator and denominator are the two values that we want to divide.

The result of the division is assigned to the variable result.

The / Operator

For example, if we want to divide the value of x by the value of y.

And assign the result to a variable called z, we can write:

x = 10
y = 2
z = x / y
print(z)

Output

5.0

In this example, the value of x is 10 and the value of y is 2.

The value of z will be 5.0 because the result of dividing 10 by 2 is 5.

In Python 3, division always returns a float value.

Even if the numerator and denominator are integers.

For example, if we divide 10 by 3, the result will be 3.33333 instead of 3.

The // Operator

To perform integer division, we can use the “//” operator.

For example:

x = 10
y = 3
z = x // y
print(z)

Output

3

In this example, the value of z will be 3.

Because we’re performing integer division.

In Python 3, division always returns a float.

Even if the numerator and denominator are both integers.

If you want to perform division and get an integer result.

You can use the “//” operator as mentioned above.

Or you can convert the result of the division to an integer using the “int()” function.

For example:

x = 10
y = 3
z = int(x / y)
print(z)

Output

3

In this case, the value of z will be 3.

Because we’re using the “int()” function.

int() will convert the floating-point result of the division to an integer

If we try to divide a number by zero in Python, we will get a ZeroDivisionError.

For example:

x = 10
y = 0
z = x / y 
print(z) # This will raise a ZeroDivisionError

Output

ZeroDivisionError: division by zero

To avoid this error, we can check if the denominator is zero before performing the division.

For example:

x = 10
y = 0
if y != 0:
    z = x / y
else:
    z = None
print(z)

Output

None

In this example, we’re checking if the value of y is not equal to zero before performing the division.

If y is zero, we’re assigning z to None.

This indicates that the result of the division is undefined.

What is the modulus operator in Python?

If you want to perform division and get the remainder of the division.

You can use the “%” operator.

This is also known as the modulus.

For example:

x = 10
y = 3
z = x % y
print(z)

Output

1

In this case, the value of z will be 1.

Because the remainder of dividing 10 by 3 is 1.

The divmod() function

If you want to perform division and get both the quotient and the remainder.

You can use the “divmod()” function.

This function takes two arguments (the numerator and denominator).

And returns a tuple containing the quotient and remainder.

For example:

x = 10
y = 3
z = divmod(x, y)
print(z)

Output

(3, 1)

In this case, the value of z will be (3, 1).

Because the quotient of dividing 10 by 3 is 3 and the remainder is 1.

Why can’t I divide in Python?

Make sure that the variables you’re trying to divide are both numeric types.

Numeric type means integers or floats.

If one or both of the variables are strings or other non-numeric types.

You won’t be able to perform division on them.

Let’s check this example:

num1 = input("Enter number 1: ")
num2 = input("Enter number 2: ")
result = num1 / num2
print(result)

Output

Enter number 1: 2
Enter number 2: 3
TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’

By default, the input() function takes input in string format.

You have to convert it into an integer using the int() function.

num1 = input("Enter number 1: ")
num1 = int(num1)
num2 = input("Enter number 2: ")
num2 = int(num2)
result = num1 / num2
print(result)

Output

Enter number 1: 8
Enter number 2: 4
2.0

You can see, we converted the input into an integer.

And the error is gone.

Learn about all python operators here.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x