In earlier versions, Python division is a small algorithm skill.

Source: Internet
Author: User
Tags floor division

In earlier versions, Python division is a small algorithm skill.

The first thing to talk about is the division operation in python. in python 2.5, there are two Division operations, namely the true division and the floor division. When division is performed in the form of x/y, if both x and y are integers, the result is truncated and the integer part of the operation is obtained, for example, the calculation result of 2/3 is 0. If one of x and y is a floating point number, the so-called true division will be implemented. For example, the result of 2.0/3 is 0.66666666666666663. Another division is in the form of x // y. Here we use the so-called floor Division to obtain the maximum integer not greater than the result. This operation is irrelevant to the operand. For example, the result of 2 // 3 is 0, the result of-2 // 3 is-1, and the result of-2.0 // 3 is-1.0.

In the future python 3.0, x/y will only execute the true division, regardless of the operand; x // y will execute the floor division. If you need to use python 2.5 in this way, you need to add the declaration of from _ future _ import division before the code. For example:

Copy codeThe Code is as follows:
From _ future _ import division
A = 2/3
From _ future _ import division a = 2/3

The result of variable a is 0.66666666666666663 instead of 3.

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.