Latest web development tutorials

Python Number (digital)

Python Number data type used to store values.

It is not allowed to change the data type, which means that if you change the value of the Number data type, will be re-allocated memory space.

The following examples are Number objects will be created when the variable assignment:

var1 = 1
var2 = 10

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

del statement syntax is:

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

You can delete single or multiple objects by using the del statement, for example:

del var
del var_a, var_b

Python supports four different types of values:

  • Integer (Int) - often referred to is an integer or an integer is a positive or negative integer, with no decimal point.
  • Long integer (long integers) - unlimited size integers, the integer last is an uppercase or lowercase L.
  • Float (floating point real values) - float by the integer part and the fractional part, floats can also be expressed (2.5e2 = 2.5 x 10 2 = 250) using scientific notation
  • Complex ((complex numbers)) - complex by the real and imaginary parts, you can use a + bj, or complex (a, b) that the real part and an imaginary part b is a floating-point type.
int long float complex
10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAECBFBAEl 32.3 + e18 .876j
-0490 535633629843L -90. -.6545 + 0J
-0x260 -052318172735L -32.54e100 3e + 26J
0x69 -4721885298529L 70.2-E12 4.53e-7j
  • You can also use a long integer lowercase "L", but it is recommended that you use an uppercase "L", to avoid the number "1" confusion. Python uses "L" to display the long integer.
  • Python also supports complex numbers, complex numbers by the real and imaginary parts, you can use a + bj, or complex (a, b) that the real part and an imaginary part b is a floating-point

Python Number Type Conversion

int(x [,base ])         将x转换为一个整数  
long(x [,base ])        将x转换为一个长整数  
float(x )               将x转换到一个浮点数  
complex(real [,imag ])  创建一个复数  
str(x )                 将对象 x 转换为字符串  
repr(x )                将对象 x 转换为表达式字符串  
eval(str )              用来计算在字符串中的有效Python表达式,并返回一个对象  
tuple(s )               将序列 s 转换为一个元组  
list(s )                将序列 s 转换为一个列表  
chr(x )                 将一个整数转换为一个字符  
unichr(x )              将一个整数转换为Unicode字符  
ord(x )                 将一个字符转换为它的整数值  
hex(x )                 将一个整数转换为一个十六进制字符串  
oct(x )                 将一个整数转换为一个八进制字符串  

Python Math Functions

function Return Value (description)
abs (x) Returns the absolute value, such as abs (-10) returns 10
ceil (x) Returns on the integers, such as math.ceil (4.1) returns 5
cmp (x, y) If x <y returns -1 if x == y Returns 0 if x> y returns 1
exp (x) Returns e raised to the power of x (e x), as math.exp (1) returns 2.718281828459045
fabs (x) Returns the absolute value, such as math.fabs (-10) returns 10.0
floor (x) Returns the rounded integer, such as math.floor (4.9) returns 4
log (x) As math.log (math.e) returns 1.0, math.log (100,10) returns 2.0
log10 (x) Returns the base 10 logarithm of x, such math.log10 (100) returns 2.0
max (x1, x2, ...) The maximum return for a given parameter, parameter sequence.
min (x1, x2, ...) Returns the minimum value for a given parameter, parameter sequence.
modf (x) Returns the integer part of x and the fractional part, numerical symbols and the two parts of the same x, the integer part in floating-point representation.
pow (x, y) Value after the operation x ** y.
round (x [, n]) Returns the rounded value of float x, as given n value represents the rounding to the number of decimal places.
sqrt (x) Returns the square root of x, numbers can be negative, the return type is a real number, such as math.sqrt (4) returns 2 + 0j

Python random number functions

Random number can be used in mathematics, gaming, security and other fields, it is also frequently embedded into the algorithm to improve the efficiency and increase the security of the program.

Python random number functions include the following common:

function description
choice (seq) From the sequence of elements in a randomly selected element, such as random.choice (range (10)), from 0-9 in a randomly selected integer.
randrange ([start,] stop [ , step]) To get a random number from within the specified range specified increments base collection, base defaults to 1
random () Randomly generated under a real number in [0,1) range.
seed ([x]) Changing the random number generator seed seed. If you do not understand the principle, you do not have to set special seed, Python will help you choose the seed.
shuffle (lst) All the elements of a sequence in random order
uniform (x, y) Randomly generated under a real number in [x, y] range.

Python trigonometric

Python includes the following trigonometric functions:

function description
acos (x) Returns the arc cosine of x in radians.
asin (x) Returns the arc sine of x radians.
atan (x) Returns the arctangent of x in radians.
atan2 (y, x) Returns the arctangent of X and Y coordinate values.
cos (x) Returns the cosine of x radians.
hypot (x, y) Back Euclidean norm sqrt (x * x + y * y).
sin (x) Returns the sine of x radians.
tan (x) Returns the tangent of x radians.
degrees (x) Converts radians to degrees, such as degrees (math.pi / 2), returned 90.0
radians (x) Convert degrees to radians

Python Mathematical Constants

constant description
pi Mathematical constant pi (pi, π generally represented)
e Mathematical constant e, e of the natural constants (the constants of nature).