What Does // Mean In Python

What Does // Mean In Python

The “//” operator in Python is known as the floor division operator.

It performs division similar to the “/” operator, but it rounds the result down to the nearest integer.

Here is an example of how this // operator works in Python.

x = 7 // 3
print(x) #outputs 2

Before digging deep into this floor division operation,

Let’s try to understand how many division operators are in Python.

And how they work.

Types of Division Operators in Python

In Python, there are three types of division operators:

  • The regular division operator (/)
  • Integer division or floor division operator (//)
  • Remainder operator or modulo operator (%)

Regular Division Operator (/)

The regular division operator (/) performs a division operation on the two operands.

And returns the result as a float value.

For example:

x = 8 / 5
print(x)

Output

1.6

The regular division operator divided 8 by 5.

And printed the answer as a float value.

Integer division or floor division operator (//)

The integer division or floor division operator (//) performs a division operation on the two operands.

And returns the result as an integer value.

This operator rounds down the answer to the nearest integer.

For example:

x = 23 // 2
print(x)

Output

11

In this example, 11 is the largest integer value that is less than or equal to the result of the division, which is 11.5

Remainder Operator or Modulo Operator (%)

The remainder or modulo operator (%) returns the remainder of a division operation.

For example:

x = 23 % 2
print(x)

Output

1

In this example, the remainder of the division of 23 by 2 is 1.

Here’s an example that uses all three division operators:

x = 7
y = 3

z = x / y
print(z)

z = x // y 
print(z)

z = x % y 
print(z)

Output

2.333333333333333
2
1

In this example, we assign the value 7 to the variable x and the value 3 to the variable y.

We then perform regular division, integer division, and the remainder operation on x and y and assign the results to the variable z.

Finally, we print the value of z for each operation.

What Does // Mean In Python

The // operator performs a division operation on two operands and after that returns the result rounded down to the nearest integer.

x = 7 // 3
print(x)

Output

2

In this example, the // operator performs a division operation on operands 7 and 3.

The result of this operation is 2.3333333333333335.

But the // operator returns 2, which is the largest integer that is less than or equal to the result.

The // operator is useful when you need to perform division and obtain an integer result.

For example, when you are dividing quantities that must be represented as whole numbers, such as when you are counting items.

The // operator can result in a different value than the regular division operator / when dealing with negative numbers.

For example:

x = -7 // 3
print(x)

Output

-3

In this example, the // operator returns -3, which is the largest integer that is less than or equal to the result of the division (-2.3333333333333335).

In general, the // operator is used when you need to obtain an integer result from a division operation.

Whereas the regular division operator / is used when you need a floating-point result.

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