Python Basic Learning

Source: Internet
Author: User

However, it is important to note that the result contains a number of decimal digits that may be indeterminate:

>>> 0.2 + 0.1 0.30000000000000004

>>> 3 * 0.1 0.30000000000000004

All languages have this problem, there is nothing to worry about.

Python will try to find a way to represent the results as accurately as possible, but in some cases it is difficult, given the way the numbers are represented inside the computer. For now, ignore the extra decimal digits temporarily; In the second part of the project, you will learn how to handle extra decimal digits when needed.

The value of 0.2+0.1 in JS is also 0.30000000000000004.

In most cases, using numbers in Python is very simple. If the results are unexpected, check to see if Python interprets the numbers as you would expect them to be numeric or string.

The integers in Python 2 have a slightly different result in Python 2, dividing two integers:

>>> python2.7

>>> 3/2 1

Python returns a result of 1, not 1.5.

In Python 2, the result of integer division contains only the integer part, and the fractional part is deleted. Note that when you calculate an integer result, the method is not rounded, but the fractional part is deleted directly. in Python 2, to avoid this, it is important to ensure that at least one operand is a floating-point number, and that the result will also be a float:

>>> 3/2 1

>>> 3.0/2 1.5

>>> 3/2.0 1.5

>>> 3.0/2.0 1.5

This division behavior is often confusing when you switch from Python 3 to Python 2 or from Python 2 to Python 3 o'clock. When you use or write code that uses both floating-point numbers and integers, be aware of this unusual behavior

Python Basic Learning

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.