Three: Python object type Detail one: number (top)

Source: Internet
Author: User
Tags floor division mathematical functions truncated

One: Python's number type:

a) integers and floating-point numbers

b) plural

c) Decimal number of fixed precision

d) Rational Score

E) Collection

f) Boolean type

g) Infinite integer precision

h) Various digital built-in functions and modules

Second: Various types of digital explanations

1, numeric constant: Python provides: integers (positive and negative integers) and floating-point numbers (digits with fractional parts). Python also allows us to use hexadecimal, octal, and binary constants to represent integers, and allows integers to have infinite precision.

(¥) built-in math tools and extensions: Python provides a range of tools for working with digital objects:

A) expression operator:+,-,*,/,>> (right shift), * * (Power),&, etc.

b) built-in mathematical functions: POW (), ABS (), round (), int (), Hex (), bin (), etc.

c) Common modules: random, math, etc.

(¥) Operator priority: Usually we do not have to pay attention to the priority level, you can use multiple parentheses to prioritize, so not only can completely forget the priority of things, and can increase the readability of the program.

(¥) hybrid type auto-upgrade: Python first converts the manipulated object to the type of the most complex operand, and then the number operation. Python divides the complexity of numeric types by: ' integers are simpler than floating-point numbers, and floating-point numbers are simpler than complex numbers '.

(¥) Number format display problem: Auto-echo of results in interactive prompt mode displays more digits than the printed statement. If you do not want to see more digits, you can use print (). STR and REPR display formats: repr The default interactive mode echo, as if they were code, and STR (that is, the print statement) into a more user-friendly format. Both of these functions transform arbitrary objects into their string representations. As shown below:

        

1 >>> num = 1/3.02 >>>repr (num)3'0.3333333333331  '4 >>> str (num)5'0.333333333'

(¥) comparison problem: Python supports continuous comparisons, eg: (1 < 2 < 3) equal (1 < 2 and 2< 3)

Three: The division is detailed:

A) '/': in python3.0 and above,/now always performs true division, regardless of the type of operand, returns a floating-point result containing any remainder. Representing traditional division in python2.6: EG:10/4 = = 2

b) '//': In python3.0 and above,//Perform floor division, which truncates the remainder and returns an integer for the integer operand, or returns a floating-point number if any one of the operands is a floating-point type.

>>> >>> 10/4 2.5 >>> 4 2 >>> 10/4.0 2.5 >>> 10//4.0 2.0

As an alternative, we can use a __future__import to open the python3.0 '/' in python2.6, instead of forcing it with a floating-point conversion, for both previous and later versions of '/' Compatibility:

C:\python26\python >>> from__future__import >>> 10/4 2.5

c) Floor Division vs Truncation Division:

The '//' operator is often called truncated division, but more accurately it is the floor division, which truncates the result down to its lower level, the nearest integer under the real result. The direct effect is rounded down, not strictly truncated, and this is valid for negative numbers. Trunc (): A function in the math module that truncates decimals and returns an integer.

>>>Import Math
>>>math.floor (2.5)
2
>>>math.floor ( -2.5)
-3
>>>math.trunc (2.5)
2
>>>math.trunc ( -2.5)
-2

Four: Complex explanation:

The complex number is represented as two floating-point numbers (both real and imaginary) and the suffix of J or J is added to the imaginary part. The complex number allows us to decompose its real and imaginary parts as attributes, and to support general mathematical expressions, and can be processed through the tools in the standard Cmath module (the standard mathematical module of the plural edition).

5:16 binary, octal, binary count:

Remember that these constants are just an alternative to specifying the value of an integer object. Hexadecimal number (0x40), octal number (0o377), binary number (0b1100). But Python is displayed by default in decimal numbers, but it provides built-in functions that allow us to convert integers to other binary numeric strings :

>>> Oct (+), Hex (+), bin ('0o100','0x40') ,'0b1000000')

Another way: The built-in int () transforms a string of numbers into an integer and can determine the binary of the transformed number by defining the second parameter:

 >>> Int ( " 64  " ), int ( " 100   ", 8), int ("  40   ", +), int ("  1000000   ", 2) #   Octal 100, Hex  (64,64,64,64) #   decimal display  >>> Int ( " 0x40  ," Int ( " 0b1000000   ", 2 64,64) 

Another way: the Evla () function takes a string as a python code. So it has a similar effect (but often allows it to be slow, it actually compiles and runs the string as a fragment of the program, and it assumes that you trust the source of the string. A smart user might be able to submit a string to delete a file on the machine):

>>> eval (' up'), eval ('0o100'), eval ('  0x40'), eval ('0b1000000') (64,64,64,64)

Finally: the ability to use string formatting method calls and expressions to convert an integer into a string of octal and hexadecimal numbers:

' {0:o},{1:x},{2:b} '. Format (64,64,64)   # string Format method '100,40,1000000'>> >'%o,%x' % (64,255)  # expression method '100,ff  '

Three: Python object type Detail one: number (top)

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.