Last Updated on March 10, 2023 by Prepbytes

Python provides various ways to perform division operations. Division Operators enable you to divide two numbers and return the quotient; that is, the first number or number on the left is divided by the second number or number on the right, and the quotient is returned. There are two types of Division Operators namely, Float Division and Truncation Division. Here we will focus on Truncation Division Operator which is also known as floor division operator in python.

## Division Operator in Python

In Python, the division operator (/) is used to divide two numbers and return the result. For example, if we want to divide 10 by 3, we can use the following code:

**Code:**

result = 10 / 3 print(result)

**Output:**

`3.3333333333333335`

**Explanation:**

The output is a floating-point number because Python automatically converts the result of the division into a float. This behavior is different from other programming languages like C and Java, which return an integer value when two integers are divided.

## What is Truncation Division Operator in Python?

Python provides another division operator called truncation division operator (//), also known as floor division Operator in Python. The truncation division operator is used to divide two numbers and return the integer value of the result. The operator truncates the decimal part of the division result and returns the integer value.

For example, if we want to divide 10 by 3 using the truncation division operator in python, we can use the following code:

**Code:**

result = 10 // 3 print(result)

**Output:**

`3`

**Explanation:**

As we can see, the output is an integer value. This is because the truncation division operator returns the integer part of the division result and discards the decimal part.

### How Truncation Division Operator in Python Works

The truncation division operator or floor division operator in Python works by dividing two numbers and then discarding the decimal part of the result. If the result is positive, the operator rounds the result toward zero. If the result is negative, the operator rounds the result toward negative infinity.

For example, if we want to divide 10 by 3 using the floor division operator or truncation division operator, the operator will perform the following steps:

**Step 1:**Calculate the division result: 10 / 3 = 3.3333333333333335**Step 2:**Discard the decimal part of the result: 3.3333333333333335 -> 3**Step 3:**Return the integer value of the result: 3

Similarly, if we want to divide -10 by 3 using the truncation division operator or floor division operator in python, the operator will perform the following steps:

**Step 1:**Calculate the division result: -10 / 3 = -3.3333333333333335**Step 2:**Discard the decimal part of the result: -3.3333333333333335 -> -3**Step 3:**Round the result towards negative infinity: -3 -> -4**Step 4:**Return the integer value of the result: -4

### Examples of Truncation Division Operator in Python

Let’s look at some examples of using the truncation division operator in Python.

**Example 1 of Truncation Division Operator in Python**

This example shows the Truncation Division of Two Integers.

**Code:**

# Declaring Variables a = 15 b = 4 # Truncation Division Operator c = a // b print(c)

**Output:**

`3`

**Explanation:**

In this example, the // operator is used to perform truncation division of two integers a and b. The result is assigned to c which is equal to the integer part of the quotient obtained by dividing a by b. In this case, a divided by b is equal to 3.75, but since we are using truncation division, only the integer part 3 is returned.

**Example 2 of Truncation Division Operator in Python**

The code given below shows the Truncation Division between an Integer and a Float Variable.

**Code:**

x = 11.5 #float y = 2 #integer z = x // y print(z)

**Output:**

`5.0`

**Explanation:**

In this example, the // operator is used to perform truncation division of a float x and an integer y. The result is assigned to z which is equal to the integer part of the quotient obtained by dividing x by y. In this case, x divided by y is equal to 5.75, but since we are using truncation division, only the integer part 5 is returned.

**Example 3 of Truncation Division Operator in Python**

Let us understand the behavior of the Truncation Division Operator in Python while dividing a positive Integer with a Negative Integer.

**Code:**

p = -21 q = 4 r = p // q print(r)

**Output:**

`-6`

**Explanation:**

In this example, we are performing truncation division of two integers p and q, where p is negative. The result is assigned to r which is equal to the integer part of the quotient obtained by dividing p by q. In this case, p divided by q is equal to -5.25, but since we are using truncation division, only the integer part -6 is returned.

**Example 4 of Truncation Division Operator in Python**

This example shows that the Truncation Division Operator in Python can be used on complex expressions involving multiple variables.

**Code:**

a = 20 b = 3 c = 2 # Complex Expression d = (a + c) // (b + 1) print(d)

**Output:**

`5`

**Explanation:**

In the above code, the result of the expression (a + c) // (b + 1) is assigned to d. The expression adds a and c, then divides the sum by b + 1, and finally returns the integer part of the quotient. In this case, the expression evaluates to (20 + 2) // (3 + 1), which simplifies to 22 // 4, and the result is 5.

**Example 5 of Truncation Division Operator in Python**

This example illustrates how Truncation Division Operator in Python behaves when we try to divide any number by 0.

**Code:**

# Declaring Variables a = 10 b = 0 # Dividing by 0 result = a // b print(result)

**Output:**

`ZeroDivisionError: integer division or modulo by zero`

**Explanation:**

In the above code, we try to divide 10 by 0 using the truncation division operator. However, this is not possible, and Python raises a ZeroDivisionError which is shown in the output.

**Conclusion**

In conclusion, the truncation division operator (//) or floor division operator in python is used to perform division operations and return the integer part of the result. The operator discards the decimal part of the division result and returns the integer value. It is useful when we want to perform integer division and ignore the decimal part of the result. It is also important to note that the operator rounds the result toward zero, and in the case of negative numbers, it rounds toward negative infinity.

## Frequently Asked Questions (FAQs)

Here are some FAQs on Truncation Division Operator in Python.

**Ques 1. How is the truncation division operator in Python different from the regular division operator (/)?**

**Ans.** The regular division operator (/) returns a floating-point number that includes the decimal part of the result, while the truncation division operator (//) returns only the integer part of the result.

**Ques 2. What happens if we use the truncation division operator in Python to divide by zero?**

**Ans.** If we use the truncation division operator to divide by zero, Python raises a ZeroDivisionError.

**Ques 3. How does the truncation division operator round the result?**

**Ans.** The truncation division operator in Python rounds the result toward zero. If the result is positive, it rounds the result toward zero, and if the result is negative, it rounds the result toward negative infinity.

**Ques 4. In what situations is the truncation division operator useful?**

**Ans.** The truncation division operator is useful when we want to perform integer division and ignore the decimal part of the result. It is commonly used in various programming tasks, such as floor division or integer division.

**Ques 5. How does the truncation division operator differ from the integer division operator in Python 2?**

**Ans.** In Python 2, the integer division operator (/) returns only the integer part of the result, while the regular division operator (/) returns a floating-point number that includes the decimal part of the result. This is different from Python 3, where the truncation division operator (//) is used for integer division.

**Ques 6. Can we use the truncation division operator with complex numbers?**

**Ans.** No, we cannot use the truncation division operator in Python with complex numbers and you will get TypeError as a result.