Python3 number (Number)

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

Data types are not allowed to change, which means that if you change the value of a numeric data type, the memory space will be reassigned.

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

var1 = 1
Var2 = 10

You can also use the del statement to remove references to some numeric objects.

The syntax of the

del statement is:

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

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

del var
del var_a, var_b

Python supports three different numeric types:

  • Int - Usually referred to as an integer or integer, is a positive or negative integer without a decimal point. The Python3 integer is unrestricted and can be used as a Long type, so Python3 does not have a Python2 Long type.
  • Float - The float is composed of an integer part and a fractional part, and the float type can also be represented by scientific notation (2.5e2 = 2.5 x 102< /sup> = 250)
  • Complex) - A complex number consists of a real part and an imaginary part, which can be represented by a + bj, or complex(a, b), the real part a and the imaginary part of the complex number b They are all floating point types.

We can use hexadecimal and octal to represent integers:

>>> number = 0xA0F # Hexadecimal
>>> number

>>> number=0o37 # Octal
>>> number
  • Python supports complex numbers. The complex number consists of a real part and an imaginary part, which can be represented by a + bj, or complex(a,b). The real part a and the imaginary part b of the complex number are both floating point type.

Python numeric type conversion

Sometimes, we need to convert the type of data built in, the conversion of the data type, you only need to use the data type as the function name.

  • int(x) Converts x to an integer.

  • float(x) Converts x to a floating point number.

  • complex(x) Converts x to a complex number with a real part of x and an imaginary part of 0.

  • complex(x, y) Converts x and y to a complex number with a real part of x and an imaginary part of y. x and y are numeric expressions.

The following example converts the floating point variable a to an integer:

>>> a = 1.0
>>> int(a)

Python Digital Operations

The Python interpreter works as a simple calculator, and you can type an expression into the interpreter that will output the value of the expression.

The syntax of expressions is straightforward: +, -, * and /, as in other languages >>> 8 / 5 # always returns a floating point number 1.6

Note: The results of floating point operations on different machines may vary.

In integer division, the division / always returns a floating point number. If you only want to get the result of an integer, discard the possible fractional part, you can use the operator // :

>>> 17 / 3 # Integer division returns to float
>>> 17 // 3 # integer division returns the result of rounding down
>>> 17 % 3 # % operator returns the remainder of the division
>>> 5 * 3 + 2

Note:// The resulting number is not necessarily an integer type, it is related to the data type of the denominator.

>>> 7//2
>>> 7.0//2
>>> 7//2.0

The equal sign = is used to assign values to variables. After the assignment, the interpreter will not display any results except the next prompt.

>>> width = 20
>>> height = 5*9
>>> width * height

Python can use ** Operation to power:

>>> 5 ** 2 # 5 Squared
>>> 2 ** 7 # 2 Squared 7th power

The variable must be "defined" before it is used (ie a variable is assigned to it), otherwise an error will occur:

>>> n # Try to access an undefined variable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined

Converting integers to floating point numbers when mixing different types of numbers:

>>> 3 * 3.75 / 1.5
>>> 7.0 / 2

In interactive mode, the result of the last output expression is assigned to the variable _ . E.g:

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
>>> price + _
>>> round(_, 2)

Here, the _ variable should be treated as a read-only variable by the user.

mathematical function

function return value (description)
abs(x) Returns the absolute value of the number, such as abs(-10) returns 10
ceil(x) Returns the number of the input integer, 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. Python 3 is deprecated and replaced with (x>y)-(x<y).
exp(x) returns e to the power of x (ex), such as math.exp(1) returns 2.718281828459045
fabs(x) Returns the absolute value of a number, such as math.fabs(-10) returns 10.0
floor(x) Returns a number of rounding integers, such as math.floor(4.9) returns 4
log(x) If 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 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 part and the decimal part of x. The numeric sign of the two parts is the same as x, and the integer part is represented by floating point.
pow(x, y) x**y The value after the operation.
round(x [,n]) returns the rounding value of the floating point number x. If the value of n is given, it represents the number of digits rounded to the decimal point.
sqrt(x) Returns the square root of the number x.

random number function

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

Python includes the following common random number functions:

choice(seq) Pick an element randomly from the elements of the sequence, such as random.choice(range(10)), and randomly pick an integer from 0 to 9.
randrange ([start,] stop [,step]) Get a random number from the specified increment in the specified range. The default value of the base is 1
random() Randomly generate the next real number, which is in the range [0,1).
seed([x]) Change the seed seed of the random number generator. If you don't understand the principle, you don't have to set the seed specifically, Python will help you choose the seed.
shuffle(lst) Randomly sort all elements of a sequence
uniform(x, y) randomly generates the next real number, which is in the range [x, y].

Trigonometric function

Python includes the following trigonometric functions:

acos(x) Returns the inverse cosine radians of x.
asin(x) Returns the arc sine radians of x.
atan(x) Returns the arc tangent value of x.
atan2(y, x) Returns the arctangent of the given X and Y coordinate values.
cos(x) Returns the cosine of the arc of x.
hypot(x, y) returns the Euclidean norm sqrt (x*x + y*y).
sin(x) The sine of the returned x radians.
tan(x) Returns the tangent of x radians.
degrees(x) Convert radians to angles, such as degrees(math.pi/2) , return 90.0
radians(x)Convert angles to radians

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.