Python learns _02_ numbers and operations

Source: Internet
Author: User
Tags abs bitwise operators integer division pow

Python has a powerful scientific computing function, Python, because of the support of more powerful object-oriented and dynamic features, compared to the R language, MATLAB, mathmatic and other traditional scientific computing tools have a great advantage.

Number of Python

The numbers in Python provide scalar storage and direct access, and are immutable types, and changes to numbers actually create a new numeric object, and the original number object is recycled by the garbage collection mechanism when its reference count is 0 o'clock, and the DEL keyword allows you to delete a reference to an object.

Boolean type: Supported after python2.3, Boolean type has only two values: True and False.

Shaping, long shaping: the size of the shaping in Python depends on the data bandwidth of the system, such as the 32 system's maximum supported 2^32-1, similar to long shaping in the C language, while the long shaping in Python depends on the virtual memory running on the machine. Python's long shape is followed by an L logo, and after python2.2, Python's long shaping and shaping is gradually unified into one, and can be automatically converted.

Floating-point type: floating-point type in Python is similar to double in the C language, is a dual-precision floating-point type, each floating-point type is 8 bytes (64 bits), and complies with the IEEE754 number specification: 52m/11e/1s,52 bits is used to represent the bottom, 11 bits is used to represent the exponent, and a bit is used to represent the symbol. Depending on which machine is running, it may be different.

Plural type: Python has a plural type, a complex number is represented as REAL+IMAGJ, and the imaginary part must have a suffix J or j, the imaginary numbers cannot exist alone, and he always forms a plural with a real part with a value of 0.0. The real and imaginary parts of a complex number are floating-point types. The complex number contains two built-in properties and one built-in method: Num.real, Num.imag, Num.conjugate (), where the conjugate () method returns the conjugate complex number of the complex number.

Python operators

When you work with Python operators, you must ensure that two operands are of the same type, and if they are not the same type, they will be converted according to the following logic:

1. If one operand is a complex number, the other operand is also converted to the plural
2. Otherwise, if one operand is a floating-point type, the other operand is also converted to a floating-point type
3. Otherwise, if one operand is a long shape, the other operand is also converted to a long reshape
4. Otherwise, both are normal shaping, no need to convert

Python + 、-、 *,/,%, and other languages are the same, the power operator in Python is * *, and added a new integer division operation://. For scientific calculations, the division of Python is divided into several types:

1. Traditional division: When the two operands are integers, an integer division is performed, and a shape is returned, and if one of them is a floating-point number, the other operand is either converted to floating-point numbers according to the conversion logic, and a floating-time division is performed. This rule changes after Python3, in the Python3, regardless of whether the two operands are all shaped, the actual division will be performed, and the original integer division needs to use the floor except the operation://To complete.

2. True division: After Python3, the/operator performs true division, and in Python2, if you want to run real division, you need to do this by executing the from __future__import Division directive.

3. Floor except: operator//Perform a floor in addition to the operation, regardless of the type of operand, the fractional part is removed, returning the nearest number in the sequence of numbers that is smaller than the true quotient.

In the remainder operation, if the operand is a floating-point type, the quotient takes the difference of the product of the maximum shaping of the value less than or equal to the exact values (the book is a bit of a mouthful, not quite understanding):

And the exponentiation operator, as well as the precedence between the operators, is consistent with the habits in mathematics.

Python also has bitwise operators: ~, &, |, ^, <<, >>, and so on, to support binary operations.

Built-in functions for numerical operations

1. Factory functions: Because of Python's integration of classes and types, the numeric type conversion function in Python is the factory function of the corresponding class. When the float () function is called to convert an integer to a floating point type, the actual work is to create a float class's strength object.

2. Functions: ABS (), coerce (), Divmod (), POW (), round (), etc.:

ABS (): Returns the absolute value of the given parameter, or, if it is a complex number, the distance from the origin.

Coerce (): Converts the passed-in parameter into a uniform type according to the conversion logic, and returns a tuple of two numeric values that have been converted.

Divmod (): The combination of division and redundancy, returns a tuple, including quotient and remainder.

POW (): the POW () function is exponential and can accept a third parameter for the remainder operation, and the POW (x, y, z) efficiency is higher than the POW (x, y)%z, which is used in cryptographic operations.

Round (): Rounding according to the rules rounded. The rules of Python2 and Python3 seem a little different:

The left figure above uses Python3 's idle run result, the right figure uses python2.7.9 to execute the result in the script, because does not install the Python2 idle, temporarily does not do scrutiny.

In contrast to the round (), the Int () function directly truncates the fractional part, and the floor () function returns the shape closest to the original number but less than the original number.

Python provides conversion functions for shaping: Hex (), Oct (), Ord (), Chr (), UNICHR () return hexadecimal shaping, octal shaping, ASCII code for a given character, characters of a given ASCII code, characters of a given Unicode code, respectively

The boolean number in Python

The boolean number in Python is a subclass of shaping, and true and false correspond to the shape of the Boolean in 1 and 0,python with the following characteristics:

1. There are two values that never change: true and False
2. A Boolean is a subclass of shaping, but it can no longer be inherited to generate his subclasses
3. The object without the __nonzero__ () method is true by default
4. For any number or empty set with a value of zero: null list, empty tuple, empty dictionary, etc. boolean values are false
5. Mathematical operations can be performed, and corresponding integers 1 and 0 in the operation

A Boolean value that takes an object is actually the __nonzero () __ method that executes the object, if the object does not have a __nonzero () __ method, such as:

Class C:pass
c = C ()

C is an instance of Class C created by the factory function, and there is no __nonzero () __ method in Class C, so bool (c) Returns a true value, and bool (c) Also returns a true value since the class class does not have a __nonzero () __ method.

Decimal floating point Type

Before python2.4, because the floating-point type follows the IEEE754 specification, the display uses the binary 52-bit to represent the bottom, some simple decimal floating-point type, such as 0.1 in the binary floating-point type will display as 0.10000000001 or 0.9999999999, after the python2.4, ten in Floating-point type is a feature of Python, but there is still a similar problem when doing floating-point fetching operations, although joining the decimal class does not solve:

Related modules

For scientific computing modules NumPy, SCIPY contains many advanced digital scientific computing content, some other digital related modules, such as decimal, array, Math/cmath, operator, random and other modules to expand the digital computing.

A generator with multiple pseudo-random numbers in the random module:

Randint (): Returns random shaping between the two in two shaping parameters

Randrange (): Randomly returns an item in the range ([start,]stop[,step]) result, such as Randrange (1,10,2)

Uniform (): similar to Randint, but returns the floating-point number between them (open interval)

Random (): similar to uniform, but the upper limit is constant equal to 1.0, the lower limit is equal to 0.0

Choice (): Randomly returns an element of a given sequence.

Python learns _02_ numbers and operations

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.