How to Divide Two Numbers in Python

I remember when I first started coding in Python over a decade ago. One of the first things I had to figure out was basic arithmetic.

While dividing two numbers sounds like a simple task, Python offers a few different ways to handle it depending on what you need.

In this tutorial, I will show you exactly how to write a Python program to divide two numbers. I will also walk you through different division operators and how to handle common errors.

Division of Two Numbers in Python

Now, let me show you how to write a program to divide two numbers in Python using different methods.

In Python, you can divide two numbers using different operators and functions. The most common way is to use the / operator, which performs true division and returns a floating-point result. There is also the // operator for floor division, which returns the largest whole number less than or equal to the result.

Method 1. Use the / Operator for True Division in Python

The / operator is used for true division in Python, which means it returns the precise quotient of the division as a float.

Here is an example and the complete Python code.

# Input: two numbers
numerator = 10
denominator = 3

# Perform division
result = numerator / denominator

# Output the result
print("The result of true division is:", result)

Output:

The result of true division is: 3.3333333333333335

You can see the output in the screenshot below.

write a python program to divide two numbers

In this example, 10 / 3 returns 3.3333333333333335, which is the precise quotient of the division.

Method 2. Use Python’s // Operator for Floor Division

The // operator performs floor division in Python, which means it returns the largest whole number less than or equal to the result.

Here is an example, and you can also see the complete Python code.

# Program to divide two numbers using floor division

# Input: two numbers
numerator = 10
denominator = 3

# Perform division
result = numerator // denominator

# Output the result
print("The result of floor division is:", result)

Output:

The result of floor division is: 3

You can see the output in the screenshot below.

division of two numbers in python

In this example, 10 // 3 returns 3, which is the largest whole number less than or equal to 3.3333333333333335.

Method 3. Use the divmod() Function in Python

The divmod() function in Python returns a tuple containing the quotient and the remainder of the division.

Here is the complete Python code.

# Program to divide two numbers using divmod()

# Input: two numbers
numerator = 10
denominator = 3

# Perform division
quotient, remainder = divmod(numerator, denominator)

# Output the result
print("The quotient is:", quotient)
print("The remainder is:", remainder)

Output:

The quotient is: 3
The remainder is: 1

You can see the output in the screenshot below.

python program to divide two numbers

In this example, divmod(10, 3) returns (3, 1), where 3 is the quotient, and 1 is the remainder.

Method 4. Handle Division by Zero in Python

When dividing numbers, it’s important to handle the case where the denominator is zero, as division by zero is undefined and will raise a ZeroDivisionError in Python.

Here is the complete Python code.

# Program to safely divide two numbers

# Input: two numbers
numerator = 10
denominator = 0

# Perform division with error handling
try:
    result = numerator / denominator
    print("The result of division is:", result)
except ZeroDivisionError:
    print("Error: Cannot divide by zero!")

Output:

Error: Cannot divide by zero!

You can see the output in the screenshot below.

python program for division of two numbers

In this example, the program checks for division by zero and handles it gracefully by printing an error message.

Summary of Python Division Operators

To help you remember, here is a quick breakdown of how division works in Python:

  • / (Float Division): Returns a decimal (e.g., 7 / 2 = 3.5).
  • // (Floor Division): Returns the largest whole number less than or equal to the result (e.g., 7 // 2 = 3).
  • % (Modulo): Returns the remainder of the division (e.g., 7 % 2 = 1).
  • divmod(a, b): Returns both the quotient and remainder.

Conclusion

I hope this guide helps you understand how to divide two numbers in Python effectively.

Whether you are building a simple calculator or a complex data analysis tool, mastering these Python division techniques is essential.

You may also like the following tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.