首页 > 代码库 > Python number

Python number

1. Basic

var = 1 #int (signed integers)
var = 1L #long (long integers )
var = 1.1 #floating-point
var = 3+3j #complex (complex numbers)

  

2. convert

var = 1
int(var)
long(var)
float(var)
complex(var) #real part is var, imaginary part is 0
complex(x, y) #real part is x, imaginary part is y

  

3. Mathematical function

import math

var = 1
abs(var) #the absolute value of var
ceil(var)
floor(var)
cmp(x, y) #If x<y, return -1. If x==y, return 0. If x>y, return 1.
exp(var) #e^var
fabs(float(var))
log(var)
log10(var)
max(x1, x2, ...)
min(x1, x2, ...)
modf(var) #This method returns the fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.
pow(x, y) #x^y
round(x [,n]) #x rounded to n digits from the decimal point.
sqrt(x)

  

4. Random number function

import random

random.choice(seq) #returns a random item from a list, tuple, or string

random.randrange([start,] stop [,step]) #returns a randomly selected element from range(start, stop, step).
#start -- Start point of the range. This would be included in the range.
#stop -- Stop point of the range. This would be excluded from the range.
#step -- Steps to be added in a number to decide a random number.

random.random() #A random float r, such that 0 <= r <=1

random.seed([x]) #The method seed() sets the integer starting value used in generating random numbers. Call this function before calling any other random module function.
#x -- his is the seed for the next random number. If omitted, then it takes system time to generate next random number.

random.shuffle(lst) #Randomizes the items of a list in place. Returns None.

random.uniform(x ,y) #A random float r, such that x<=r<=y.

  

5. Trigonometric functions

import math

acos(x)
asin(x)
atan(x)
atan2(y,x) #atan(y/x)
cos(x)
sin(x)
tan(x)
hypot(x, y) #Return the Euclidean norm, sqrt(x*x + y*y).
degrees(x) #Converts angle x from radians to degrees.
radians(x) #Converts angle x from degrees to radians.



pi
e

  

Python number