Python learning notes (4) Numbers (2) and python learning notes

Source: Internet
Author: User
Tags decimal to binary

Python learning notes (4) Numbers (2) and python learning notes

Python

Division in Python2

 1 >>> 3 / 6       2 0 3 >>> 3.0 / 6 4 0.5 5 >>> 3.0 / 6.0 6 0.5 7 >>> 5 / 2     8 2 9 >>> 6.0 / 310 2.011 >>> 10.0 / 312 3.333333333333333513 >>> 0.2 +0.914 1.115 >>> 0.2 +0.1       16 0.30000000000000004
Because the computer will convert the input decimal to binary. When the floating point number is converted to binary, the problem will arise: 0.1 is converted to binary, 0.000110011 is not exactly equal to the decimal 0.1 after conversion, generally, we round the result to two places for calculation.

In Python2, the Division is used to take the integer part, so 3/6 =/2 = 2 is obtained.

The from _ future _ import division module can be referenced.

1 >>> from __future__ import division2 >>> 3 / 63 0.54 >>>

Remainder in Python

Two Methods of Remainder

1.%

2. divmod () to obtain the quotient and remainder

1 >>> 6% 52 13 >>> 6% 44 25 >>> divmod (6, 4) 6 (1, 2) quotient and remainder 7 >>> divmod (6, 5) 8 (1, 1)

 

  

Related Article

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.