Introduction to the more special division and exponentiation operations in Python

  • 2020-05-05 11:25:55
  • OfStack

No matter what language it is, you have to do with addition, subtraction, multiplication and division, but in Python do you know what these symbols mean?

This is division. What about this //? *, this is multiplication, but what about **? Let's talk about each of them.

"//" operations

The division operator is known as /, but the result of the binary operator/depends on the operand itself, such as


20 / 3
6
20 / 3.0
6.666666666666667
20.0 / 3
6.666666666666667
20.0 / 3.0
6.666666666666667

In other words, when using the/operator, if one of the operands is a floating point number, the result is a floating point number, which we call a true division, but if both operands are integers, the result is an integer that is reduced to decimal places, which we call a divisible division. But if I have a situation where I want the result to be divisible whether the operand is an integer or a floating point number, then the // comes in handy, and that's what the // is for.

"//" begins with Python 2.2. In addition to "/", the division operator introduces a division operator, which is only used for divisible division. An example is


20 // 3
6
20 // 3.0
6.0
20.0 // 3
6.0
20.0 // 3.0
6.0
20 // 3.00
6.0

The result of "//" is divisible regardless of the operand, and if the operand is a floating-point number, we are only given a divisible result converted to a floating-point number.

"**" operation

This "**" is relatively simple, is the title of Python power operation, as shown below:


2 ** 0
1
2 ** 1
2
2 ** 10
1024
2 ** 20
1048576

The first operand is the base and the second is the exponent.

The ~


Related articles: