Python second-week number type

Source: Internet
Author: User
Tags abs arithmetic operators pow

Number Type

In Python, a number is not a true object type, but rather a grouping of similar types. Python supports not only the usual numeric types (integers and floating-point numbers), but also the ability to create numbers directly with constants and expressions that handle numbers, as well as more support for digital types through modules and third-party libraries. The complete tools for Python numeric types include:

    • Integers and floating-point numbers

    • Complex number (requires module support)

    • Decimal number for fixed precision

    • Rational Fractions

    • Collection

    • Boolean type

    • Infinite integer Precision

    • Various digital built-in functions and modules

Tools for working with numeric objects:

    • Expression operators: + 、-、 *,/,//, >>, * *, &

    • Built-in math functions: POW, ABS, round, int, hex, bin

    • Common modules: random, Math


Common built-in math functions:

The POW (x,y,z=none) asks for X's Y-side, or X's y-square to Z-remainder

ABS (n) to find the absolute value of n

Round (x, y) returns the rounding value of the floating-point number x, Y is the precision

>>> Round (3.1415926,2)

3.14

Int () Converts a floating-point number to an integer

Hex () Converts a number to a hexadecimal number

Bin () turns a number into a binary number


Built-in math module math, random, decimal, fractions

Math constants and functions commonly used in the math module

PI, E (constant) and sqrt (), trunc () functions


The random module is used to generate the stochastic number, and the common usage is as follows:

Random.random ()

Random.randint (n,m) #产生一个n-M random number

Random.choice (list) #从列表中随机选择一个列表元素


The fractions module is used to process fractions

From fractions import fraction

x = fraction (1,3) #创建一个分数对象, numerator is 1, denominator is 3

y = fraction (4,6)

X + y

Fraction (a)

To pay particular attention to the precision of decimals and fractions, it is more convenient to use the decimal module when absolute precision is required.

The / number in the operator has different meanings in python2 and Python3, in the Python2, the meaning of the/number is divisible, there is no remainder, in the Python3, there will be a number of surplus.


Mixed Operations Support operator precedence

you typically use parentheses to separate expressions to clearly represent the order of operations . There are two situations when a mixed type is mathematically operating:

The first is integer and floating-point arithmetic, the integers are converted to floating-point numbers, and the final result is floating-point numbers.

The second is that one of two types does not support arithmetic operators and will error.

The operator overloads are described later in the contents of the class, allowing the same operators to support more object types.


The problem of the number of floating-point numbers

Floating-point numbers are limited by the floating-point hardware limits of the platform. When we want to output a specified number of digits, we need to use the output format.

There are three types of output formats:

The first is the% format, which uses the form of%+ formatted characters to control the accuracy of the characters;

The second type is format formatted. Both of these methods are described in the formatting of the string.

The third is the use of external modules to provide support, the external module decimal can be temporarily set decimal precision, after the statement exits, decimal precision is restored.

From decimal Import Decimal

Decimal (' 0.1 ') #得到一个绝对精确的小数0.1

Decimal.getcontext (). Prec = 4 #设置Decimal小数对象计算出来的小数精度为4位, always in effect

Decimal.localcontext (). Prec = 4 #设置Decimal小数对象计算出来的小数精度为4位, temporarily valid, return to normal accuracy after one operation


Digital expansion

The NumPy Library provides advanced digital programming tools such as matrix data types, vector processing, and advanced computing libraries.

SCIPY Library, improve for graphical tools, drawing tools and statistical library.


This article is from the "No Flying World" blog, please be sure to keep this source http://hf1208.blog.51cto.com/8957433/1881622

Python second-week number type

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.