SoFunction
Updated on 2024-10-30

Python standard library of Math, Random module use details

Mathematics module

import math

ceil -- upper rounding

Rounding up a number (rounding to the nearest integer) takes the maximum of the two nearest neighboring integers.

import math
res = (4.1)
print(res)  # 5

floor -- round down

Rounding down a number (back one method) takes the smallest value of the two nearest neighboring integers.

import math
res = (-3.9)
print(res)  # -4

discard four, but treat five as whole (of decimal points)

Put the commonly used built-in function -- round.

pow -- power operation

Calculate the Nth power of a number.

import math

""" The main difference between the methods in the called math module and the built-in is that the built-in returns integers and the math module returns decimals """

# Mathematics modules
res = (2, 3)
print(res)  # 8.0

# Built-in functions
res = pow(2, 3)
print(res)  # 8

# A simpler way is to use ** for power operations #
res = 2 ** 3
print(res)  # 8

sqrt -- squaring

import math
res = (9)  # The result is floating point
print(res)  # 3.0

fabs -- absolute values

import math

""" The main difference between the methods in the called math module and the built-in is that the built-in returns integers and the math module returns decimals """

# Mathematics modules
res = (-12341234123)
print(res)  # 12341234123.0

# Built-in functions
res = abs(-12341234123)
print(res)  #12341234123

modf -- split integer decimals

Splits a value into two parts, decimal and integer, to form a tuple with a floating-point value.

import math
res = (100.666)
print(res)  # (0.6659999999999968, 100.0)

copysign -- positive and negative copies

Copies the positive and negative state of the second argument to the first argument. (Returns a floating-point type)

import math
res = (100, -200)
print(res)  # -100.0

fsum -- sequence sum

Summing the elements of a container (the result is a floating point number)

import math
lst = [1, 2, 3]
res = (lst)
print(res)  # 6.0

pi -- pi constant

import math
res = 
print(res)  # 3.141592653589793

factorial -- factor

import math

# Find the factors of 5
factor = (5)
print(factor)  # 120

randomized module

import random

random -- Get a decimal number between 0 and 1.

random Get a random number of decimals between 0 ~ 1 (left-closed right-open) 0 <= x < 1

import random
res = ()
print(res)  # 0.15195915170299268

randrange -- Get an integer in the specified range.

Syntax: rangrange(start, end[, step])

randint -- get the specified range of integers

Syntax: randint(a, b)

Less flexible than randrange, but end values are available

uniform -- get a random decimal number within a specified range (left-closed-right-open)

import random

# uniform Get a random decimal number within a specified range (left closed, right open)
res = (1, 3)
print(res)  # 2.81589512983781

# Because of the built-in mechanism (uniform can reverse values and achieve the same effect)
res = (3, 1)
print(res)  # 1.4023313207919326

choice -- randomize the values in the sequence (multiple choice)

import random

# Try using randrange to implement

lst = ['A', 'B', 'C', 'D', 'E']
res = (lst)
print(res)  # E

sample -- randomize the values in the sequence (multiple choice, return a list)

Syntax: sample(poplation, num)

import random

lst = ['A', 'B', 'C', 'D', 'E', 'F']

res = (lst, 1)
print(res)  # ['F']

res = (lst, 2)
print(res)  # ['C', 'A']

shuffle -- randomly scramble values in a sequence (original address operation)

import random

lst = ['A', 'B', 'C', 'D', 'E', 'F']

(lst)
print(lst)  # ['F', 'D', 'C', 'B', 'E', 'A']

Randomized CAPTCHA implementation

import random


def getVer():
    ver_code = ''
    # Four randomly selected characters
    for i in range(4):
        s_char = chr((97, 123))
        b_char = chr((65, 91))
        num = str((10))
        lst = [s_char, b_char, num]
        ver_code += (lst)
    return ver_code


ver = getVer()
print(ver)  # b4Vq

Above is the Python standard library of Math, Random module to use the details, more information about Python Math Random module please pay attention to my other related articles!