YouTip LogoYouTip

Python Numbers

Python Number(Numeric)

Python2.x Python Number(Numeric)

Python Number data type is used to store numeric values.

Data types are immutable, which means if you change the value of a Number data type, it will reallocate memory space.

The following example creates a Number object when assigning a value to a variable:

var1 = 1
var2 = 10

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

The syntax of the del statement is:

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

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

del var
del var_a, var_b

Python supports four different numeric types:

  • Integer(int) - Commonly known as integer or whole number, it is a positive or negative integer without a decimal point.
  • Long integer(long integers) - Integers of unlimited size, the integer ends with an uppercase or lowercase L.
  • Floating point(floating point real values) - Floating point consists of an integer part and a fractional part, floating point can also be represented using scientific notation (2.5e2 = 2.5 x 10^2 = 250)
  • Complex numbers(complex numbers) - Complex numbers consist of a real part and an imaginary part, they can be represented as a + bj, or complex(a,b), the real part a and the imaginary part b are both floating points.
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
  • Long integers can also use lowercase "L", but it is still recommended that you use uppercase "L" to avoid confusion with the digit "1". Python uses "L" to display long integers.
  • Python also supports complex numbers, complex numbers consist of a real part and an imaginary part, they can be represented as a + bj, or complex(a,b), the real part a and the imaginary part b are both floating points.

  • int(x [,base ]) converts x to an 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 object x to a string
  • repr(x ) converts object x to an expression string
  • eval(str ) evaluates a valid Python expression in a string, and returns an object
  • tuple(s ) converts sequence s to a tuple
  • list(s ) converts sequence s to a list
  • chr(x ) converts an integer to a character
  • unichr(x ) converts an integer to a Unicode character
  • ord(x ) converts a character to its integer value
  • hex(x ) converts an integer to a hexadecimal string
  • oct(x ) converts an integer to an octal string

Python math module, cmath module

The commonly used mathematical functions in Python are mostly in the math module, cmath module.

The Python math module provides many mathematical functions for floating point numbers.

The Python cmath module includes some functions for complex number operations.

The functions in the cmath module are basically consistent with the math module functions, the difference is that the cmath module operates on complex numbers, and the math module operates on mathematical operations.

To use math or cmath functions, you must import them first:

import math

View the contents of the math package:

>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

The specific applications of each function will be introduced below.

View the contents of the cmath package:

>>> import cmath
>>> dir(cmath)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']

Example:

>>> import cmath
>>> cmath.sqrt(-1)
1j
>>> cmath.sqrt(9)
(3+0j)
>>> cmath.sin(1)
(0.8414709848078965+0j)
>>> cmath.log10(100)
(2+0j)

Python Mathematical Functions

Function Return Value (Description)
[abs(x)](#) Returns the absolute value of a number, e.g., abs(-10) returns 10
[ceil(x)](#) Returns the ceiling value of a number, e.g., math.ceil(4.1) returns 5
[cmp(x, y)](#) Returns -1 if x y
[exp(x)](#) Returns e raised to the power of x (e^x), e.g., math.exp(1) returns 2.718281828459045
[fabs(x)](#) Returns the absolute value of a number as a floating point, e.g., math.fabs(-10) returns 10.0
[floor(x)](#) Returns the floor value of a number, e.g., math.floor(4.9) returns 4
[log(x)](#) Returns the natural logarithm of x, e.g., math.log(math.e) returns 1.0, math.log(100,10) returns 2.0
[log10(x)](#) Returns the base-10 logarithm of x, e.g., math.log10(100) returns 2.0
[max(x1, x2,...)](#) Returns the largest of the input values, arguments can be a sequence.
[min(x1, x2,...)](#) Returns the smallest of the input values, arguments can be a sequence.
[modf(x)](#) Returns the fractional and integer parts of x, both parts have the same sign as x, the integer part is returned as a float.
[pow(x, y)](#) Returns the value of x raised to the power of y (x**y).
[round(x [,n])](#) Returns the rounded value of x, if n is provided, rounds to n decimal places.
[sqrt(x)](#) Returns the square root of x.

Python Random Number Functions

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

Python includes the following commonly used random number functions:

Function Description
[choice(seq)](#) Picks a random element from the sequence, e.g., random.choice(range(10)), picks a random integer from 0 to 9.
[randrange ([start,] stop [,step])](#) Gets a random number from the specified range, incrementing by the specified step, default step is 1
[random()](#) Generates a random float in the range [0,1).
[seed()](#) Changes the seed of the random number generator. If you do not understand the principle, you do not need to set the seed specifically, Python will choose the seed for you.
[shuffle(lst)](#) Randomly shuffles all elements in the sequence.
[uniform(x, y)](#) Generates a random float in the range [x,y].

Python Trigonometric Functions

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 in radians.
[atan(x)](#) Returns the arc tangent of x in radians.
[atan2(y, x)](#) Returns the arc tangent of y/x in radians.
[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)](#) Converts radians to degrees, e.g., degrees(math.
← Func Number AbsPython Pass Statement β†’