Python's strongest King (5) -- Nunber (number), pythonnunber

Source: Internet
Author: User
Tags mathematical functions

Python's strongest King (5) -- Nunber (number), pythonnunber

1. Python Number (Number)

The Python Number data type is used to store numeric values.

The data type cannot be changed, which means that if the value of the Number data type is changed, the memory space will be re-allocated.

The Number object will be created when the following instances assign values to variables:

var1 = 1var2 = 10

You can also use the del statement to delete some Number object references.

The syntax of the del statement is:

del var1[,var2[,var3[....,varN]]]]

You can use the del statement to delete one or more objects, for example:

del vardel var_a, var_b

2. Python supports four different numeric types

INTEGER (Int)-it is usually called an integer or integer. It is a positive or negative integer without a decimal point.

Long integer-an integer of infinite size. The integer is an upper or lower case L.

Floating point real values: floating points are composed of integer and decimal parts. Float points can also be expressed by scientific notation (2.5e2 = 2.5x102 = 250)

Complex Number (complex numbers)-a complex number consists of a real number and a virtual number. It can be expressed by a + bj or complex (a, B, the real part a and virtual Part B of the plural are float.

Note:

Long integers can also use lowercase "L", but we recommend that you use uppercase "L" to avoid confusion with numbers "1. Python uses "L" to display long integers.

Python also supports the complex number. The complex number consists of the real number and the virtual number. It can be expressed by a + bj or complex (a, B). The real part a and virtual Part B of the complex number are float.

3. Python Number type conversion

Int (x [, base]) converts x to an integer long (x [, base]) and converts x to a long integer float (x) convert x to a floating point complex (real [, imag]) to create a plural str (x). Convert x to a string repr (x) convert object x to expression string eval (str) to calculate a valid Python expression in the string, and return an object tuple (s) to convert the sequence s to a list (s) converts the sequence s to a list chr (x) and an integer to a character unichr (x). converts an integer to a Unicode Character ord (x) convert a character to its integer hex (x) and convert an integer to a hexadecimal string oct (x). convert an integer to an octal string.

4. Common Python mathematical functions

Abs (x) returns the absolute value of a number, for example, abs (-10) returns 10

Fabs (x) returns the absolute value of a number, for example, math. fabs (-10) returns 10.0

Cmp (x, y) returns-1 if x <y, 0 if x = y, and 1 if x> y

Sqrt (x) returns the square root of the number x. The number can be a negative number and the return type is a real number. For example, math. sqrt (4) returns 2 + 0j

5. Python random number Function

Random numbers can be used in mathematics, games, security, and other fields. They are often embedded in algorithms to improve algorithm efficiency and program security.

Python contains the following common random number functions:

Function Description
Choice (seq) Randomly select an element from the element of the sequence, such as random. choice (range (10), and randomly select an integer from 0 to 9.
Randrange ([start,] stop [, step]) Obtains a random number from a set that increments by the specified base number within the specified range. The default value of the base number is 1.
Random () The next real number is randomly generated within the range of [0, 1.
Seed ([x]) Change the seed of the random number generator. If you do not understand the principle, you do not need to set seed. Python will help you select seed.
Shuffle (lst) Random sorting of all elements in a sequence
Uniform (x, y) The next real number is randomly generated, which is within the range of [x, y.

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.