Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to divide in Python

Introduction

Welcome to this tutorial on division in Python! If you're learning programming or just starting to dive into the world of Python, this article is perfect for you. We will walk you through the basics of division, how to perform different types of division in Python, and give you some practical examples to help you get started. Let's begin!

What is Division?

Division is one of the four basic arithmetic operations, along with addition, subtraction, and multiplication. It's the process of splitting a given value into equal parts. For example, if you have ten apples and want to divide them equally among five friends, you would give each friend two apples. In mathematical terms, this can be represented as 10 ÷ 5 = 2.

In Python, there are two types of division: 1. True Division (also known as floating-point division): It produces a floating-point result by dividing two numbers. 2. Floor Division (also known as integer division): It disregards the remainder and only gives the integer part of the quotient.

Let's explore these types of division in detail and learn how to perform them in Python.

True Division

True division (or floating-point division) is the most common type of division you'll come across. It produces an exact result as a floating-point number, which can include decimal places.

In Python, we perform true division using the / (forward slash) operator. Here's the syntax:

result = dividend / divisor

Let's see some examples of true division in action.

Example 1:

# Performing true division
result = 10 / 5
print(result)  # Output: 2.0

In this example, we divided 10 by 5. The result is 2.0, which is a floating-point number.

Example 2:

# Performing true division
result = 9 / 4
print(result)  # Output: 2.25

Here, we divided 9 by 4. The result is 2.25, which is a floating-point number with decimal places.

Now, let's move on to the floor division.

Floor Division

Floor division (or integer division) is a type of division that disregards the remainder and only gives the integer part of the quotient. In other words, it rounds the result down to the nearest whole number.

In Python, we perform floor division using the // (double forward slash) operator. Here's the syntax:

result = dividend // divisor

Let's see some examples of floor division in action.

Example 1:

# Performing floor division
result = 10 // 5
print(result)  # Output: 2

In this example, we divided 10 by 5. The result is 2, which is an integer.

Example 2:

# Performing floor division
result = 9 // 4
print(result)  # Output: 2

Here, we divided 9 by 4. The result is 2, which is the integer part of the quotient, disregarding the remainder.

Remainder in Division

Sometimes, you might want to know the remainder after performing a division operation. In Python, you can obtain the remainder using the % (modulo) operator. Here's the syntax:

remainder = dividend % divisor

Let's see some examples of using the modulo operator to find the remainder.

Example 1:

# Finding the remainder
remainder = 10 % 5
print(remainder)  # Output: 0

In this example, we divided 10 by 5. The remainder is 0.

Example 2:

# Finding the remainder
remainder = 9 % 4
print(remainder)  # Output: 1

Here, we divided 9 by 4. The remainder is 1.

Combining Division and Remainder

Now that we know how to perform both true division and floor division, as well as finding the remainder, let's see an example of combining these operations.

Example:

# Performing division and finding the remainder
dividend = 17
divisor = 4

quotient = dividend // divisor
remainder = dividend % divisor

print("Quotient:", quotient)    # Output: Quotient: 4
print("Remainder:", remainder)  # Output: Remainder: 1

In this example, we divided 17 by 4. The floor division gave us a quotient of 4, and the remainder is 1.

Division by Zero

It's essential to understand that dividing by zero is undefined in mathematics, and Python will raise a ZeroDivisionError if you attempt to divide by zero. Let's see an example:

# Attempting to divide by zero
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Error: Division by zero")  # Output: Error: Division by zero

In this example, we tried to divide 10 by 0. Python raised a ZeroDivisionError, and we caught the error using a try-except block to display a custom error message.

Conclusion

In this tutorial, we explored how to perform division in Python, covering both true division and floor division. We also learned how to find the remainder after division using the modulo operator and how to handle division by zero.

By understanding these basic arithmetic operations in Python, you'll be well-equipped to tackle more complex programming problems. Keep practicing and experimenting with different numbers and operators to get a better understanding of how division works in Python. Happy coding!