First Knowledge Python (3) __python numbers

Source: Internet
Author: User
Tags arithmetic operators bitwise natural logarithm square root

Number Type

About deleting a numeric object

According to Python's law, you can't really delete a numeric object, you just don't use it anymore. If you actually want to delete a reference to a numeric object, use the DEL statement

Python supports four types of numbers

Integer: Usually referred to as just an integer or an integer, is a positive or negative integer, without a decimal.

Long integer: Very long integral type, at the end of the number has L, such as "51924361L"

Floating-point numbers: double-precision floating-point numbers, which usually have a decimal point and an optional suffix e (uppercase or lowercase, which indicates scientific notation). The positive (+) or negative (-) can be used between the E and the exponent to indicate the positive or negative (the positive word can omit the symbol)

Plural: A combination of a real number and an imaginary number form a complex number. A complex number is a pair of ordered floating-point numbers (x, y). represented as X + YJ, where x is the real part, and Y is the part of the imaginary number.

Here are a few concepts about complex numbers in the Python language:

L imaginary numbers cannot exist alone, and they are always combined with a real part with a value of 0.0 to form a complex number.

L complex numbers are composed of real and imaginary parts

l syntax to represent imaginary numbers: REAL+IMAGJ

l Real and imaginary parts are floating-point numbers

L The imaginary part must have a suffix J or J.

Special Number Type--Boolean type:

l have two values that never change to True or false.

The L Boolean is a subclass of an integral type, but it can no longer be inherited to produce its subclasses.

L The default value of the object without the __nonzero__ () method is True.

For any number or empty set (empty list, empty tuple, empty dictionary, and so on) with a value of zero, the Boolean value in Python

L is false.

L in mathematical operations, the Boolean value of true and false corresponds to 1 and 0, respectively.

Most standard library functions and built-in Boolean functions that previously returned integers are now returned as Booleans.

L True and false are not keywords now, but will be in future versions of Python.

Arithmetic operators

Python supports the monocular operator plus (+) and minus (-), binocular operators, +,-,*,/,%, and * *,

Represent addition, subtraction, multiplication, division, take-up, and power operations, respectively. A new divisible operator//is also added from Python2.2.

Floor removal

Starting with Python 2.2, a new operator//has been added in order to perform the floor except://Division Not

The number of numeric types that a tube operand always removes, and returns the nearest number in a sequence of numbers that is smaller than the true quotient.

>>> 1//2 # Floors result, returns integer # floor except, return integer

0

>>> 1.0//2.0 # Floors result, returns float # floor except, return floating-point number

0.0

>>>-1//2 # Move left on number line# returns an integer smaller than –0.5, which is-1

-1

* Bitwise operator (for integers only)--- this part does not understand

Reference: Bitwiseoperations in the Python language

Python integers support standard bit operations: negation (~), bitwise-AND (&), or (|) and XOR (^) and left (<<) and right-shift (>>). Python handles bit operations like this:

    • L The negative number is treated as a positive 2-step complement.
    • L Shift left and right n are equal to 2 of n power operations without overflow checking: 2**n.
    • For long integers, the bitwise operator uses a modified 2-complement form to allow the symbol bits to expand infinitely to the left.

numeric type Conversion Functions

type int (x): Converts x to a normal integer.

Type Long (x): Converts x to a long integer.

Type float (x): Convert x to a floating-point number.

Type complex (x): Converts x to complex and real x and imaginary part to zero.

Type complex (x, y): Converts x and y to a complex and real number part X and imaginary part Y. X and y are numeric expressions

type bool (x): Returns the Boolean value of the X object

A function of the mathematical calculation performed by python

Function

Return (description)

ABS (x)

The absolute value of x: The distance between x and 0 (positive).

Ceil (x)

Upper limit of x: Minimum integer not less than X

CMP (x, y)

-1 if x < y, 0 if x = = y, or 1 if x > y

EXP (x)

Index of x: EX

Fabs (x)

Absolute value of X

Floor (x)

The floor of x: The largest integer is not more than X

Log (x)

Natural logarithm of x, for x> 0 o'clock

LOG10 (x)

Logarithm of base 10, x>0.

Max (x1, x2,...)

Its largest parameter: The value closest to the positive infinity

Min (x1, x2,...)

Its minimum parameter: The value closest to the negative infinity

MODF (x)

An integer and fractional portion of the two-tuple of X. These two elements have the same x symbol. The integer part returns a floating-point number.

Pow (NUM1, num2, mod=1)

X**y value, take NUM1 num2, if you provide mod parameters, then calculate the result of the MoD to take the remainder operation

Round (x [, N])

X rounds the decimal point to an n-digit number. Python is far from the 0-point decision: Round (0.5) is 1.0 and round (0.5) is-1.0.

sqrt (x)

Square root of X (x>0)

For random and trigonometric functions, please refer to: http://www.yiibai.com/python/python_numbers.html

For a comparison of int (), floor () and round ():

    • The L function int () truncates the fractional part directly. (The return value is an integer)
    • The L function floor () Gets the integer closest to the original number but less than the original number. (return value is floating point)
    • The L function round () gets the integer nearest to the original number. (return value is floating point)
    • The example of L uses four positive and four negative numbers as the parameters of these three functions, and compares the returned results together (for comparison purposes, we convert the return value of the Int () function to a floating-point number).

Functions that are used only for integers

Hex (num) Converts a number to hexadecimal number and returns as a string

The OCT (num) converts numbers into octal numbers and returns them as strings

Chr (num) Converts the number of ASCII values to ASCII characters, which can range from only 0 <= num <= 255.

Ord acceptsan ASCII or Unicode character (a string of length 1) and returns the corresponding ASCII or Unicode value.

UNICHR (num) accepts Unicode code values and returns their corresponding Unicode characters. The range of code values accepted depends on whether your Python is built on ucs‐2 or ucs‐4.

First Knowledge Python (3) __python numbers

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.