![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python 数学编程
DOING MATH WITH PYTHON
奇遇世界
Late in autumn
展开
-
10、极差
通过取最大和最小数字之间的差值来计算极差。极差告诉我们一组数字中的两个极端值之差。def find_range(numbers): lowest = min(numbers) highest = max(numbers) r = highest - lowest return lowest, highest, rif __name__ == '__main__': donations = [100, 60, 70, 900, 100, 200, ...原创 2020-06-15 13:13:23 · 752 阅读 · 0 评论 -
9、众数
众数,一组数字中出现频率最多的数。使用 c 指代 Counter 对象most_common() 返回一个列表,每个成员都是一个元组。每个元组中的第一个元素是最频繁出现的数字,第二个元素是出现的次数。from collections import Counterdef calculate_mode(numbers): c = Counter(numbers) # 参数为返回的元组个数,1 表示返回一个,2 表示返回两个,按频数高到低顺序返回 m...原创 2020-06-15 13:11:23 · 463 阅读 · 0 评论 -
8、中位数
对数字进行升序排列如果数字列表长度是奇数,则列表中间的数字是中位数;如果数字列表长度是偶数,则取两个中间数的均值作为中位数。def calculate_median(numbers): n = len(numbers) numbers.sort() if n % 2 == 0: m1 = n / 2 m2 = n / 2 + 1 m1 = int(m1) - 1 m2 = int(m2) - 1 ...原创 2020-06-15 13:02:06 · 658 阅读 · 0 评论 -
7、均值 mean
求一组数字的平均值def calculate_mean(numbers): s = sum(numbers) n = len(numbers) mean = s / n return meanif __name__ == '__main__': donations = [100, 60, 70, 900, 100, 200, 500, 500, 503, 600, 1000, 1200] mean = calculate_mean(donati..原创 2020-06-15 12:59:46 · 637 阅读 · 0 评论 -
6、抛物运动
初始速度:u与地面的角度:theta水平速度:ux = u * cos(theta)垂直速度:uy = u * sin(theta) - gt水平距离:sx = ux * t = u * cos(theta) * t垂直距离:sy = uy * t - 0.5 * g * t * t = u * sin(theta) * t - 0.5 * t * t最高点时,uy = 0, u * sin(theta) = gt, t = u * sin(theta) / g...原创 2020-06-15 12:57:43 · 1654 阅读 · 0 评论 -
5、牛顿万有引力定律
import matplotlib.pyplot as pltdef draw_graph(x, y): plt.plot(x, y, marker='o') plt.xlabel('Distance in meters') plt.ylabel('Gravitational force in new tons') plt.title('Gravitational force and distance') plt.show()def generate_F.原创 2020-06-15 12:24:50 · 576 阅读 · 0 评论 -
4、比较纽约市的月平均气温
import matplotlib.pyplot as pltdef create_graph(): nyc_temp_2000 = [31.3, 37.3, 47.2, 51.0, 63.5, 71.3, 72.3, 72.7, 66.0, 57.0, 45.3, 31.1] nyc_temp_2006 = [40.9, 35.7, 43.1, 55.7, 63.1, 71.0, 77.9, 75.8, 66.6, 56.2, 51.9, 43.6] nyc_temp_20.原创 2020-06-15 12:21:56 · 558 阅读 · 0 评论 -
3、求二次方程的根
def roots(a, b, c): D = (b * b - 4 * a * c) ** 0.5 x_1 = (-b + D) / (2 * a) x_2 = (-b - D) / (2 * a) print('x1: {0}'.format(x_1)) print('x2: {0}'.format(x_2))if __name__ == '__main__': a = input('Enter a: ') b = input('Ente.原创 2020-06-15 12:20:13 · 314 阅读 · 0 评论 -
2、华氏温度 F 与摄氏温度 C 的转换
摄氏温度:C = (F - 32) * (5 / 9)华氏温度:F = C * (9 / 5) + 32def print_menu(): print('1. C to F') print('2. F to C')def c_f(): C = float(input('Enter temperature in C: ')) F = C * (9 / 5) + 32 print('temperature in F: {0}'.format(F)).原创 2020-06-15 12:19:33 · 3910 阅读 · 0 评论 -
1、英里与千米的转换
1 英里大约等于 1.609 千米def print_menu(): print('1. Kilometers to Miles') print('2. Miles to kilometers')def km_miles(): km = float(input('Enter distance in kilometers: ')) miles = km / 1.609 print('Distance in miles: {0}'.format(miles原创 2020-06-15 12:17:36 · 6215 阅读 · 2 评论