Python numbers

高洛峰
Release: 2016-11-23 10:42:00
Original
1065 people have browsed it

Python numeric data type is used to store numerical values.

The data type is not allowed to be changed, which means that if the value of the numeric data type is changed, the memory space will be reallocated.

The following examples of numeric objects will be created when variables are assigned:

var1 = 1

var2 = 10

You can also use the del statement to delete some numeric object references. The syntax of

del statement is:

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

You can delete single or multiple items by using del statement Objects, for example:

del var

del var_a, var_b

Python supports four different numerical types:

Integer (Int) - often called an integer or integer , is a positive or negative integer without a decimal point.

Long integers - infinite-sized integers, with an uppercase or lowercase L at the end of the integer.

Floating point real values ​​- The floating point type consists of an integer part and a decimal part. The floating point type can also be expressed using scientific notation (2.5e2 = 2.5 x 102 = 250)

Complex numbers ( (complex numbers )) - The imaginary part of a complex number ends with the letter J or j. Such as: 2+3i

int

long

float

complex

10 51924361L 0.0 3.14j

100 -0x19 323L 15.20 45.j

-786 0122L -21.9 9.322e-36j

080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j

-0490 535633629843L -90. -.6545+0J

-0x260 -0523 18172735L -32.54e100 3e+26J

0x69 -4721885298529L 70.2-E12 4.53e-7j

Long type also A lowercase "L" can be used, but it is recommended that you use an uppercase "L" to avoid confusion with the number "1". Python uses "L" to display long integers.

Python also supports complex numbers. Complex numbers are composed of real parts and imaginary parts. They can be represented by a + bj, or complex(a,b). The real part a and the imaginary part b of complex numbers are both floating point types

Python Numeric type conversion

int(x [,base ]) Convert x to an integer

long(x [,base ]) Convert x to a long integer

float(x) Convert x convert to A floating point number

complex(real [,imag ]) Create a complex number

str(x) Convert object x to string

repr(x) Convert object x to expression string

eval(str) Used to evaluate a valid Python expression in a string and return an object

tuple(s)                 Convert the sequence s to a tuple    

list(s)                                . Convert an integer to a character

unichr(x) Convert an integer to a Unicode character

ord(x) Convert a character to its integer value

hex(x) Convert an integer to a hexadecimal Convert an integer to an octal string

oct(x) Convert an integer to an octal string

abs(x) Return The absolute value of the number, such as abs(-10) returns 10

ceil(x) Returns the upward integer of the number, such as math.ceil(4.1) returns 5

cmp(x, y) If x < y returns - 1, if x == y return 0, if x > y return 1

exp(x) Returns e to the power of x) Returns the rounded down integer of the number, such as math.floor(4.9) returns 4

log(x) For example, math.log(math.e) returns 1.0, math.log(100,10) returns 2.0

log10( x) Returns the logarithm of x based on 10, such as math.log10(100) returns 2.0

max(x1, x2,...) Returns the maximum value of the given parameter, which can be a sequence.

min(x1, x2,...) Returns the minimum value of the given parameter, which can be a sequence.

modf(x) Returns the integer part and decimal part of x. The numerical signs of the two parts are the same as x, and the integer part is expressed in floating point type.

pow(x, y) The value after x**y operation.

round(x [,n]) Returns the rounded value of the floating point number x. If the n value is given, it represents the number of digits rounded to the decimal point.

sqrt(x) Returns the square root of the number In games, security and other fields, it is often embedded into algorithms to improve algorithm efficiency and improve program security.

Python includes the following commonly used random number functions:

Function

Description

choice(seq) Randomly select an element from the elements of the sequence, such as random.choice(range(10)), from 0 to Pick an integer at random from 9.

randrange ([start,] stop [,step]) Get a random number from the set in the specified range, increasing by the specified base. The default value of the base is 1

random() Randomly generate the next real number, which is Within the range [0,1).

seed([x]) Change the seed of the random number generator. If you don't understand the principle, you don't have to set the seed specifically, Python will choose the seed for you.

shuffle(lst) Randomly sort all elements of the sequence

uniform(x, y) Randomly generate the next real number, which is in the range [x, y].

Python trigonometric functions

Python includes the following trigonometric functions:

Function

Description

acos(x) Returns the inverse cosine of x in radians.

asin(x) Returns the arcsine radians value of x.

atan(x) Returns the arc tangent of x in radians.

atan2(y, x) Returns the arc tangent of the given X and Y coordinate values.

cos(x) Returns the cosine of x in radians.

hypot(x, y) Returns the Euclidean norm sqrt(x*x + y*y).

sin(x) Returns the sine of x in radians.

tan(x) Returns the tangent of x in radians.

degrees(x) Convert radians to angles, such as math.degrees(math.tan(1.0)), return 30.0

radians(x) Convert angles to radians

Python math constants

constants

Description

pi Mathematical constant pi (pi, generally expressed as π)

e Mathematical constant e, e is a natural constant (natural constant).

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template