Python中数学运算和排列组合相关 运算

B站影视 2025-02-09 13:27 4

摘要:from itertools import permutationspaths = permutations([1, 2, 3]) # Generate all permutations of the list [1, 2, 3]for path in pat

执行基本算

术:

sum = 7 + 3 # Additiondifference = 7 - 3 # Subtractionproduct = 7 * 3 # Multiplicationquotient = 7 / 3 # Divisionremainder = 7 % 3 # Modulus (Remainder)power = 7 ** 3 # Exponentiation

与复数一起工作:

z = complex(2, 3) # Create a complex number 2 + 3jreal_part = z.real # Retrieve the real partimaginary_part = z.imag # Retrieve the imaginary partconjugate = z.conjugate # Get the conjugate

常用数学函数:

import mathroot = math.sqrt(16) # Square rootlogarithm = math.log(100, 10) # Logarithm base 10 of 100sine = math.sin(math.pi / 2) # Sine of 90 degrees (in radians)

简单方法生成给定集合的排列:

from itertools import permutationspaths = permutations([1, 2, 3]) # Generate all permutations of the list [1, 2, 3]for path in paths: print(path)

简单生成组合的方法:

from itertools import combinationscombos = combinations([1, 2, 3, 4], 2) # Generate all 2-element combinationsfor combo in combos: print(combo)

获取随机数:

import randomnum = random.randint(1, 100) # Generate a random integer between 1 and 100

当你需要处理分数时:

from fractions import Fractionf = Fraction(3, 4) # Create a fraction 3/4print(f + 1) # Add a fraction and an integer

获取平均值、中位数和标准差:

import statisticsdata = [1, 2, 3, 4, 5]mean = statistics.mean(data) # Averagemedian = statistics.median(data) # Medianstdev = statistics.stdev(data) # Standard Deviation

与三角学一起工作:

import mathangle_rad = math.radians(60) # Convert 60 degrees to radianscosine = math.cos(angle_rad) # Cosine of the angleimport mathinfinity = math.inf # Representing infinitynot_a_number = math.nan # Representing a non-number (NaN)

来源:自由坦荡的湖泊AI

相关推荐