Introduction to division and power operations in Python, and Division operations in python

Source: Internet
Author: User

Introduction to division and power operations in Python, and Division operations in python

No matter what language, you can't do without the addition, subtraction, multiplication, division algorithms. But in Python, do you know what operations these symbols represent?

"/" 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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.