Introduction to division and power operations in Python

Source: Internet
Author: User
Tags integer division integer numbers
This article mainly introduces some special division and power operations in Python. what about division? "*" Is a multiplication operation. what about this? This article explains the differences between these operations. For more information, see what languages do not support addition, subtraction, multiplication, division, and division algorithms. But do you know what operations these symbols represent in Python?

"/" Is a division operation. what about? "*" Is a multiplication operation. what about this? Next we will introduce them one by one.

"//" Operation

The division operator is "/", which is a human operator, but the result of the binary operator "/" is determined by the operand itself, for example:

The code is as follows:


20/3
6
20/3 .0
6.666666666666667
20.0/3
6.666666666666667
20.0/3.0
6.666666666666667


That is to say, when the "/" operator is used, if one of the operands is a floating point number, the result is a floating point number. We call it a real division, but if both operands are integer numbers, the result is an integer that gives away decimal places. this is called Division. However, in this case, no matter whether the operand is an integer or a floating-point number, all the results I want are divisible, then "//" will be used, this "//" is used to solve this problem.

"//" Starts with Python2.2. in addition to "/", the division operator introduces a division operator, which is only used for division. The example is as follows:

The code is as follows:


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

Regardless of the operand, the result of "//" is an integer division. if the operand is a floating point number, the result returned to us is a integer converted to a floating point number.

"**" Operation

This "**" is relatively simple, namely the power operation of Python in the title. The example is as follows:

The code is as follows:


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


The first operand is the base number, and the second operand is the index.

End ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.