Explain the number type variables and methods of python

  • 2020-05-17 05:50:51
  • OfStack

preface

The python data type is not allowed to be changed, which means that if you change the value of the Number data type, memory space will be reallocated. Without further ado, let's take a look at the details.

The following instance will be created when the variable is assigned:


var1 = 1
var2 = 10

You can also delete 1 Number object reference using the del statement.

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 numeric types:

Integer (Int) - often referred to as an integer or an integer, positive or negative integer, with no decimal point.

Long integer (long) - an integer of infinite size that ends with an uppercase or lowercase L, e.g., 51924361L.

Floating point (float) - floating point is made up of integer and decimal parts. Floating point can also be represented by scientific notation
(2.5e2 = 2.5 x 10^2 = 250)

Complex number (complex) - complex number is composed of real part and imaginary part, which can be expressed by a + bj, or complex(a,b),
The real and imaginary parts of the complex Numbers a and b are floating point.

Python Number type conversion:

int(x [,base]) converts x to 1 integer

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

float(x) converts x to a floating point number

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

str(x) converts the object x to a string

repr(x) converts the object x into an expression string

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

tuple(s) converts the sequence s to a tuple

list(s) converts the sequence s into a list

chr(x) converts an integer to a character

unichr(x) converts an integer to the Unicode character

ord(x) converts 1 character to its integer value

hex(x) converts an integer into a base 106 string

oct(x) converts an integer into an 8-base string

Python mathematical function:

Function return value (description)

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

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

cmp(x, y) if x < y returns negative 1, if x == y returns 0, if x > y returns 1

exp(x) returns the x power of e (ex), such as math.exp (1) returns 2.718281828459045

fabs(x) returns the absolute value of the number, as math.fabs (-10) returns 10.0

floor(x) returns the rounded value of the number, as math.floor (4.9) returns 4

log(x) such as math.log (math.e) returns 1.0, math.log (100,10) returns 2.0

log10(x) returns the logarithm of x in base 10, such as math.log10 (100) returns 2.0

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

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

modf(x) returns the integer and fractional parts of x, both with the same numeric symbol as x, and the integer parts are represented as floating point.

pow(x, y) x**y.

round(x [,n]) returns the rounding value of the floating point number x by 4 rounds and 5 entries. If an n value is given, it represents the number rounded to the decimal point.

sqrt(x) returns the square root of the number x, which can be negative, and the return type is real, such as math.sqrt (4) returns 2+0j

Note:

1. Many mathematical functions of python cannot be directly accessed, so it is necessary to import math module and call this method through static objects.
Since we are not sure which method is required, it is better to introduce the math module when using the python mathematical function in the future.

2. Directly accessible mathematical functions:


abs(x)   Returns the absolute value of a number, such as abs(-10)  return  10
cmp(x, y)  if  x < y  return  -1,  if  x == y  return  0,  if  x > y  return  1
max(x1, x2,...)  Returns the maximum value of a given parameter, which can be a sequence. 
min(x1, x2,...)  Returns the minimum value of a given parameter, which can be a sequence. 
round(x [,n])  Return floating point number x the 4 Give up 5 Input value, as given n Value, which represents the number of digits rounded to the decimal point. 

Example:


#!/usr/bin/python
#coding:uft-8
import math #  The import  math  The module 

print "max(80, 100, 1000) : ", max(80, 100, 1000)
print "min(80, 100, 1000) : ", min(80, 100, 1000)
print "round(80.23456, 2) : ", round(80.23456, 2)
print "math.exp(-45.17) : ", math.exp(-45.17)
print "math.pow(100, 2) : ", math.pow(100, 2)

Python random number function:

The function description

choice(seq) selects one element at random from the elements of the sequence, such as random.choice (range(10)), and selects one integer at random from 0 to 9.

randrange ([start,] stop [,step]) gets a random number from a collection that is within a specified range and increments by the specified cardinality. The cardinality default is 1

random() randomly generates the next real number, which is in the range [0,1].

seed([x]) changes the seed seed of the random number generator.

shuffle(lst) randomly sorts all the elements of the sequence

uniform(x, y) randomly generates the next real number, which is in the range of [x,y].

Note:

1. The random number function of python cannot be directly accessed, so it needs to import random module and then call this method through random static object.

Example:


#!/usr/bin/python
# -*- coding: UTF-8 -*-
import random

print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])
#  The output  100 <= number < 1000  Between the even 
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)
#  To generate the first 1 A random number 
print "random() : ", random.random()
#  Generated with 1 A random number 
random.seed( 10 )
print "Random number with seed 10 : ", random.random()
list = [20, 16, 10, 5];
random.shuffle(list)
print " Randomly sorted list  : ", list
print "uniform(5, 10)  The random number of  : ", random.uniform(5, 10)

Python3 angular function:

The function description

acos(x) returns the arccosine radians of x.

asin(x) returns the arc sine of x.

atan(x) returns the arctangent radians of x.

atan2(y, x) returns the arctangent of the given X and Y coordinate values.

cos(x) returns the cosine of the radians of x.

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

sin(x) returns the sine of x radians.

tan(x) returns the tangent value of x radians.

degrees(x) converts radians to degrees, such as degrees(math.pi /2), returning 90.0

radians(x) converts the Angle to radians

Note:

1. The Angle function of Python3 cannot be directly accessed. It needs to import the math module and then call the method through the static object of math.

Example:


#!/usr/bin/python
#coding: UTF-8
import math

print "degrees(3) : ", math.degrees(3)
print "radians(-3) : ", math.radians(-3)
print "sin(3) : ", math.sin(3)
print "cos(3) : ", math.cos(3)
print "tan(3) : ", math.tan(3)
print "acos(0.64) : ", math.acos(0.64)
print "asin(0.64) : ", math.asin(0.64)
print "atan(0.64) : ", math.atan(0.64)
print "atan2(-0.50,-0.50) : ", math.atan2(-0.50,-0.50)
print "hypot(0, 2) : ", math.hypot(0, 2)

Python mathematical constants:

A constant describing
pi mathematical constant pi
e mathematical constants e, e is the constant of nature (constant of nature).

Note:

1. The mathematical constants of Python cannot be directly accessed, so the math module needs to be imported and then accessed through the static objects of math.

Example:


#!/usr/bin/python
#coding: UTF-8
import math

print math.pi
print math.e

conclusion

The above is the whole content of this article, I hope the content of this article can help you to learn or use python, if you have any questions, you can leave a message to communicate.


Related articles: