{
"cells": [
{
"cell_type": "markdown",
"id": "28b67313",
"metadata": {},
"source": [
"# 第 2 章 数值类型"
]
},
{
"cell_type": "markdown",
"id": "c72093e8",
"metadata": {},
"source": [
"## 教学目的和要求"
]
},
{
"cell_type": "markdown",
"id": "7f2d77e0",
"metadata": {},
"source": [
"1. 掌握三种数值类型 \n",
"2. 掌握数值类型的方法与类型转换 \n",
"3. 掌握数值运算 \n",
"4. 常用数值运 \n",
"5. math模块 用 其应用\n"
]
},
{
"cell_type": "markdown",
"id": "08de86ad",
"metadata": {},
"source": [
"## 重点难点"
]
},
{
"cell_type": "markdown",
"id": "16cd6672",
"metadata": {},
"source": [
"重点: \n",
"1.\t整型、浮点型的应用与类型转换 \n",
"2.\t数学运算函数 \n",
" \n",
"难点: \n",
"1.\t数据类型转换 \n",
"2.\t表达式与运算优先级 "
]
},
{
"cell_type": "markdown",
"id": "39b28d68",
"metadata": {},
"source": [
"## 教学安排"
]
},
{
"cell_type": "markdown",
"id": "dd04ce95",
"metadata": {},
"source": [
"### 预习及预习检验(25分钟)"
]
},
{
"cell_type": "markdown",
"id": "94ae71ba",
"metadata": {},
"source": [
"1. 本章数字教程 5分钟 \n",
"2. 阅读教材 5分钟 \n",
"3. 数值类型翻转教学设计 15分钟 "
]
},
{
"cell_type": "markdown",
"id": "ddee5fbb",
"metadata": {},
"source": [
"### 课堂教学(2学时共90分钟)"
]
},
{
"cell_type": "markdown",
"id": "e5984bbd",
"metadata": {},
"source": [
"复习: (2分钟) \n",
"1. 输入函数的接收到的只能是字符串类型 \n",
"2. 字符串不能参与数值运算 \n",
"3. 引出数值类型转换 \n",
"\n",
"讲授: \n",
"1. 数值类型及其转换函数(15分钟) \n",
" 整型int() \n",
" 浮点型float() \n",
" 复数类型complex() \n",
"2. 赋值运算(3分钟) \n",
"3. 数值运算(10分钟) \n",
"4. 常用数值运算函数(15分钟) \n",
"5. 数值计算案例教学(15分钟) \n",
"6. math库(20分钟) \n",
"7. 常用运算(20分钟)"
]
},
{
"cell_type": "markdown",
"id": "ba5109fd",
"metadata": {},
"source": [
"### 本章小节"
]
},
{
"cell_type": "markdown",
"id": "7d386ee9",
"metadata": {},
"source": [
"1. 所有客观对象都要抽象成数据,才能在计算机中进行存储和处理 \n",
"2. 参与数值运算的对象必须转为数值类型,主要是应用int()和float()函数转为整型或浮点类型 \n",
"3. 数据类型、运算符和表达式是程序设计的基本概念,必须熟练运用\n",
"4. math库中的函数不需要背记,可依赖IDE的提示使用 \n",
"5. 常用运算要注意运算的优先级 "
]
},
{
"cell_type": "markdown",
"id": "7c37122f",
"metadata": {},
"source": [
"## 作业"
]
},
{
"cell_type": "markdown",
"id": "25f78d29",
"metadata": {},
"source": [
"Python第二章作业(初级)(7关约30-90分钟)"
]
},
{
"cell_type": "markdown",
"id": "6cd4d2a9",
"metadata": {},
"source": [
"## 实验项目(建议90分钟)"
]
},
{
"cell_type": "markdown",
"id": "99bc426e",
"metadata": {},
"source": [
"1. 矩形面积运算(project)(6关约20分钟)\n",
"2. 地球数据计算闯关(project)(5关约20分钟)\n",
"3. 计算弓形的面积(project)(2关约10分钟)\n",
"4. 计算几何形状的表面积与体积(project)(7关约20分钟)\n",
"5. 学费计算(project)(2关约10分钟)\n",
"6. 弧田术(project)(6关约20分钟)"
]
},
{
"cell_type": "markdown",
"id": "7a17792a",
"metadata": {},
"source": [
"## 备注"
]
},
{
"cell_type": "markdown",
"id": "ce941d88",
"metadata": {},
"source": [
"1. 演示编写程序,强调输入的数据必须转数值型才能参与数值运算 \n",
"2. math库不用多讲,了解会用即可 \n",
"3. 常用运算中可放在本章讲也可放到下一章讲"
]
},
{
"cell_type": "markdown",
"id": "befed3fe",
"metadata": {},
"source": [
"## 问题总结"
]
},
{
"cell_type": "markdown",
"id": "b85dcda8",
"metadata": {},
"source": [
"1. eval()函数效率不高且存在安全性问题,建议学生不要滥用 \n",
"2. 明确根据要求转整型或浮点型 \n",
"3. 复杂数值公式转表达式可重点讲一下 \n",
"4. 本章部分内容可采取翻转教学,由学生课上完成翻转教学项目,培养学生自学能力。 "
]
},
{
"cell_type": "markdown",
"id": "2cef6716",
"metadata": {},
"source": [
"# 本章教学内容案例"
]
},
{
"cell_type": "markdown",
"id": "3809bfdb",
"metadata": {},
"source": [
"## 2.1 数值类型"
]
},
{
"cell_type": "markdown",
"id": "c7916196",
"metadata": {},
"source": [
"整数(int) \n",
"浮点数(float) \n",
"复数(complex) "
]
},
{
"cell_type": "markdown",
"id": "16af2104",
"metadata": {},
"source": [
"### 2.1.1 整数(int) "
]
},
{
"cell_type": "markdown",
"id": "26916ccf",
"metadata": {},
"source": [
"整数常被称为整型或整数类型 \n",
"不包含小数点的数字 \n",
"十进制: 0 、123、-45 \n",
"二进制:0b1101、0B1100 \n",
"八进制:0o17、0O777 \n",
"十六进制:0xff、0X123456789ABCDEF \n"
]
},
{
"cell_type": "markdown",
"id": "9b69fc9c",
"metadata": {},
"source": [
"整数特点 \n",
"大小无限制,可精确表示超大数 "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "29f30cd1",
"metadata": {},
"outputs": [],
"source": [
"import math # 导入 math 库\n",
"# 计算100的阶乘,158位整数\n",
"\n",
"print(math.factorial(100)) \n"
]
},
{
"cell_type": "markdown",
"id": "b8c54e58",
"metadata": {},
"source": [
"输出:\n",
"93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"
]
},
{
"cell_type": "markdown",
"id": "e42be386",
"metadata": {},
"source": [
"整数子类--bool值 \n",
"\n",
"True和False \n",
"以整数“1”和“0”为值参与数值运算 "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1532aef2",
"metadata": {},
"outputs": [],
"source": [
"print(False + 2) # 2\n",
"print(True * 2 + 3) # 5\n",
"print((67 > 60) * 5 + 8) # 13"
]
},
{
"cell_type": "markdown",
"id": "113bba3c",
"metadata": {},
"source": [
"### 2.1.2 浮点数(float, 实数)"
]
},
{
"cell_type": "markdown",
"id": "b0432aed",
"metadata": {},
"source": [
"十进制表示\n",
"由整数部分、小数点与小数部分组成\n",
"\n",
"123.45、23.0、0.78、 12. (12.0)、.15 (0.15)\n",
"\n",
"科学记数法表示 <a>e<n>(n为整数)\n",
" \n",
"0.48e-5、2e3、2.53e3\n",
"2.53e3(2.53 × 103 = 2530.0)\n",
"等价于a×10n \n"
]
},
{
"cell_type": "markdown",
"id": "11516316",
"metadata": {},
"source": [
"浮点数无法保证完全精确,输出时只保留17位有效数字\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4c08eaf8",
"metadata": {},
"outputs": [],
"source": [
"print(43.02 * 7) "
]
},
{
"cell_type": "markdown",
"id": "87f5f227",
"metadata": {},
"source": [
"预期输出:301.14\n",
"实际结果:301.14000000000004"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "55867055",
"metadata": {},
"outputs": [],
"source": [
"print(5.02 / 0.1) "
]
},
{
"cell_type": "markdown",
"id": "2ecb0e37",
"metadata": {},
"source": [
"预期输出:50.20\n",
"实际结果:50.199999999999996"
]
},
{
"cell_type": "markdown",
"id": "d4c6295e",
"metadata": {},
"source": [
"浮点数表示的数字最大值约为1.7× 10308 "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5fd84cdc",
"metadata": {},
"outputs": [],
"source": [
"print(pow(809.0, 106)) # 输出809的106次幂的值\n",
"# 输出 1.748007496839708e+308"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "dcc7f9ad",
"metadata": {},
"outputs": [],
"source": [
"print(pow(810.0, 106)) # 输出810的106次幂的值\n",
"# OverflowError: (34, 'Result too large')"
]
},
{
"cell_type": "markdown",
"id": "77711cac",
"metadata": {},
"source": [
"### 2.1.3 复数 \n",
"由实数部分和虚数部分构成 \n",
"实部虚部都是浮点数 \n",
"用a + bj 或 complex(a, b)表示 "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "14cadedc",
"metadata": {},
"outputs": [],
"source": [
"print((3.0 + 4.0j).real) # 输出实部 3.0\n",
"print((3.0 + 4j).imag) # 输出虚部 4.0\n",
"print(abs(3.0 + 4.0j)) # 输出复数的模 5.0"
]
},
{
"cell_type": "markdown",
"id": "66191fc9",
"metadata": {},
"source": [
"## 2.2 数值类型转换"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9aa86eb1",
"metadata": {},
"outputs": [],
"source": [
"side = input() # 输入 '5'\n",
"perimeter = side * 4 # 等价于 '5' * 4\n",
"print(perimeter) # 输出 '5555'"
]
},
{
"cell_type": "markdown",
"id": "614d7cfe",
"metadata": {},
"source": [
"### 2.2.1 字符串转整数 (int)"
]
},
{
"cell_type": "markdown",
"id": "0ed1f332",
"metadata": {},
"source": [
"int('x') 可将整数字符串'x'转为整数 x"
]
},
{
"cell_type": "markdown",
"id": "eb518ba2",
"metadata": {},
"source": [
"### 输入整数,表示正方形的边长, 计算并输出其周长"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d4c1d37",
"metadata": {},
"outputs": [],
"source": [
"side = input() # 输入'5'\n",
"perimeter = int(side) * 4 # 字符串转整数 (int),变成数值 5 * 4\n",
"print(perimeter) # 输出 20"
]
},
{
"cell_type": "markdown",
"id": "15854d82",
"metadata": {},
"source": [
"### 输入整数,表示正方形的边长, 计算并输出其周长"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "51d2d5b9",
"metadata": {},
"outputs": [],
"source": [
"side = int(input()) # 输入'5',字符串转整数 (int),int('x') 可将整数字符串'5'转为整数 5\n",
"perimeter = side * 4 # 相当于5 * 4\n",
"print(perimeter) # 输出20"
]
},
{
"cell_type": "markdown",
"id": "35da18dd",
"metadata": {},
"source": [
"### int('x') 可将整数字符串'x'转为整数 x,无法将浮点数字符串转为整数"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bb7a457b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" 6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"24\n"
]
}
],
"source": [
"side = int(input()) # 输入浮点数,表示正方形的边长, 计算并输出其周长,输入'3.5'\n",
"# int() 无法将浮点数字符串转为整数\n",
"perimeter = side * 4 \n",
"print(perimeter)\n",
"# ValueError: invalid literal for int() with base 10: '3.5'"
]
},
{
"cell_type": "markdown",
"id": "a553e6df",
"metadata": {},
"source": [
"### 2.2.1 字符串转浮点数 (float)"
]
},
{
"cell_type": "markdown",
"id": "f582f5c1",
"metadata": {},
"source": [
"float('x') 可将浮点数字符串'x'转为浮点数 x \n",
"float('x') 可将整数'x'转为浮点数 x "
]
},
{
"cell_type": "markdown",
"id": "bd8c7605",
"metadata": {},
"source": [
"输入浮点数,表示正方形的边长, 计算并输出其周长"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4c27c55b",
"metadata": {},
"outputs": [],
"source": [
"side = float(input()) # float('x')可将浮点数字符串'x'转为浮点数 x,输入3.5\n",
"perimeter = side * 4 # 3.5 * 4\n",
"print(perimeter) # 输出 14.0"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b08bbc3d",
"metadata": {},
"outputs": [],
"source": [
"side = float(input()) # float('x')可将整数字符串'x'转为浮点数 x.0,输入 3\n",
"perimeter = side * 4 # 3.0 * 4\n",
"print(perimeter) # 输出 12.0"
]
},
{
"cell_type": "markdown",
"id": "d86fd4f6",
"metadata": {},
"source": [
"eval('x')可将整数字符串'x'转为整数 x \n",
"eval('x')可将浮点数字符串'x'转为浮点数 x "
]
},
{
"cell_type": "markdown",
"id": "0c97b94a",
"metadata": {},
"source": [
"side = eval(input()) # eval('x')可将整数字符串'x'转为整数 x,输入3\n",
"perimeter = side * 4 # 3 * 4\n",
"print(perimeter) # 12"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "be8f2775",
"metadata": {},
"outputs": [],
"source": [
"side = eval(input()) # eval('x')可将浮点数字符串'x'转为浮点数 x,输入3.0\n",
"perimeter = side * 4 # 3.0 * 4\n",
"print(perimeter) # 12.0"
]
},
{
"cell_type": "markdown",
"id": "6440c5af",
"metadata": {},
"source": [
"float('x')去除字符串开头和结尾的空白字符, 包括:空格、\\n、\\t 等"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4993f247",
"metadata": {},
"outputs": [],
"source": [
"print(float(3)) # 输出 0.678\n",
"print(float(' 0.678\\n')) # 输出 3.0"
]
},
{
"cell_type": "markdown",
"id": "80a8336b",
"metadata": {},
"source": [
"int('x')去除字符串开头和结尾的空白字符, 包括:空格、\\n、\\t 等 \n",
"int(x.b)可截取浮点数x.b的整数部分 x"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a39b2c36",
"metadata": {},
"outputs": [],
"source": [
"print(int(3.99)) # 输出 3\n",
"print(int(' 678\\n')) # 输出 678 "
]
},
{
"cell_type": "markdown",
"id": "1837af43",
"metadata": {},
"source": [
"int('x', base=10))将其他进制字符串转为十进制整数 \n",
"base 为整数,此参数缺省时为十进制 \n",
"其他进制字符串转整数 (int) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99357952",
"metadata": {},
"outputs": [],
"source": [
"print(int('20')) # 输出 20\n",
"print(int('20', base=10)) # 输出 20\n",
"print(int('20', 10)) # 输出 20"
]
},
{
"cell_type": "markdown",
"id": "c8e2f7bc",
"metadata": {},
"source": [
"其他进制字符串转整数 (int) \n",
"int('x', base=10) ) \n",
"2、8、10、16代表二进制、八进制、十进制和十六进制 "
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8056fd8d",
"metadata": {},
"outputs": [],
"source": [
"print(int('11111111', base=2)) # 输出 255\n",
"print(int('11111111', 2)) # 输出 255\n",
"print(int('17', 8)) # 输出 15\n",
"print(int('FF', 16) ) # 输出 255"
]
},
{
"cell_type": "markdown",
"id": "03e00504",
"metadata": {},
"source": [
"实例 2.1 计算矩形面积 \n",
"矩形的面积等于其长与宽的乘积,用户输入长和宽的值,按输入要求编程计算矩形的面积,输入要求如下: \n",
"(1) 输入两个正整数,输出结果为整数int() \n",
"(2) 输入两个浮点数,输出结果为浮点数float() \n",
"(3) 输入两个正数,要求输出的数据类型与输入的数据类型保持一致eval() \n",
"输入:输入长和宽的数值 \n",
"处理:利用短形面积公式计算面积 \n",
"输出:输出面积 "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "128abdb0",
"metadata": {},
"outputs": [],
"source": [
"# 输入整数表示的矩形的长和宽, 计算并输出矩形的面积\u000b",
"\n",
"width = int(input()) # 用int()函数将输入转成整数,例如输入:3\u000b",
"\n",
"length = int(input()) # 用int()函数将输入转成整数,例如输入:4\u000b",
"\n",
"area = width * length # 利用面积公式计算面积\u000b",
"\n",
"print(area) # 输出:12"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1fd6eea",
"metadata": {},
"outputs": [],
"source": [
"# 输入浮点数表示的矩形的长和宽, 计算并输出矩形的面积\u000b",
"\n",
"\n",
"width = float(input()) # float()转成浮点数,输入:2.456\u000b",
"\n",
"length = float(input()) # float()转成浮点数,输入:3.58\u000b",
"\n",
"area = width * length # 计算面积\u000b",
"\n",
"print(area) # 输出:8.79248\u000b",
"\n",
"print(round(area, 2)) # 保留2位小数,输出:8.79"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87d22ad0",
"metadata": {},
"outputs": [],
"source": [
"# 输入两个正数,要求输出的数据类型与输入的数据类型保持一致eval(), 计算并输出矩形的面积\u000b",
"\n",
"\n",
"width = eval(input()) # 用eval()函数将输入转成数值型\u000b",
"\n",
"length = eval(input()) # 用eval()函数将输入转成数值型\u000b",
"\n",
"area = width * length # 利用面积公式计算面积\u000b",
"\n",
"print(area) \n",
"# 输入3,4时,输出:12;\n",
"# 输入3.0,4.0时,输出12.0"
]
},
{
"cell_type": "markdown",
"id": "023e5482",
"metadata": {},
"source": [
"数值运算\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "5815ac55",
"metadata": {},
"source": [
"\n",
"\n",
"数学运算符:+、 -、 *、 /、 //、 %、 **\n",
"a = 8\u000b",
"b = 5\u000b",
"print(a + b) # 结果为 13\u000b",
"print(a - b) # 结果为 3\u000b",
"print(a * b) # 结果为 40\u000b",
"print(a / b) # 精确除法,浮点数1.6\u000b",
"print(a ** b) # a 的 b 次幂,32768\n",
"加、减、乘、除、整除、取模、 幂\n"
]
},
{
"cell_type": "markdown",
"id": "824fb411",
"metadata": {},
"source": [
"\n",
"x1 = -b + b 2 - 4 a c 0.5 2 a \u000b",
"x2 = -b - b 2 - 4 a c 0.5 2 a \n",
"\n",
"数学运算符:+、 -、 *、 /、 //、 %、 **\n",
"数学公式: \n",
"表达式: \n"
]
},
{
"cell_type": "markdown",
"id": "4cf9bff7",
"metadata": {},
"source": [
"整除(//): 整除,结果为整数,向下取整算法\n",
"\n",
"当有浮点数参与运算的时候,结果为浮点型的整数\n",
"\n",
"print(11 // 4)\n",
"print(99 // 10)\n",
"print(99.0 // 10)\n",
"\n",
"9\n",
"2.75\n",
"print(-11 // 4)\n",
"-2.75\n",
"9.0\n",
"2\n",
"-3\n",
"除(/): 除法,结果为浮点数\n",
"print(11 / 4)\n",
"2.75\n"
]
},
{
"cell_type": "markdown",
"id": "aaddfa33",
"metadata": {},
"source": [
"取模(%): 两个数整除的余数\n",
"\n",
"模非零时,其符号与除数相同\n",
"\n",
"a % b = a - (a // b) * b\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "70d2d262",
"metadata": {},
"source": [
"判断整除\n",
"\n",
"判断奇偶\n",
"\n",
"a % 4 == 0 and a % 100 != 0\n",
"\n",
"\n",
"a % 2 == 0\n",
"\n",
"\u000b",
"\n",
"使结果落在某区间\n",
"a % 7\n",
"a % 26\n",
"结果: 0, 1, 2, 3, 4, 5, 6\n",
"结果: 0, 1, 2……24, 25\n"
]
},
{
"cell_type": "markdown",
"id": "f02ae525",
"metadata": {},
"source": [
"幂(**): 两个星号表示幂运算 a ** b \n",
"\n",
"\n",
"print((1 / 2) ** 2)\n",
"\n",
"\n",
"\n",
"\u000b",
"\u000b",
"\n",
"print(3 ** 2)\n",
"print(3 ** (1 / 2))\n",
"print((-4) ** (1 / 2))\n",
"9\n",
"1.732\n",
"0.25\n",
"(1.2246467991473532e-16+2j)\n",
"a ** b 中 b 小于 1 时为开方,负数开偶次方结果为复数 \n"
]
},
{
"cell_type": "markdown",
"id": "5389bfbe",
"metadata": {},
"source": [
"实例 2.2 一元二次方程求解\n",
"现有一元二次方程:ax2 + bx + c = 0,当a,b,c的值分别为5,8,3时,编程求其实根。\n",
"a, b, c = 5, 8, 3 # 同步赋值,5,8,3分别赋值给a,b,c\u000b",
"x1 = (-b + (b * b - 4 * a * c) ** (1 / 2)) / (2 * a)\u000b",
"x2 = (-b - (b * b - 4 * a * c) ** (1 / 2)) / (2 * a)\u000b",
"# x2 = (-b - (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)\u000b",
"print(x1,x2) # 在一行内输出-0.6 -1.0,输出结果用空格分隔\n",
"此题中,判别式b2-4ac= 8 * 8 – 4 * 5 * 3 = 4 > 0\n",
"该方程有两个不相等的实数解\n",
"可利用一元二次方程的求根公式进行计算\n"
]
},
{
"cell_type": "markdown",
"id": "0842d418",
"metadata": {},
"source": [
"实例 2.2 一元二次方程求解\n",
"现有一元二次方程:ax2 + bx + c = 0,当a,b,c的值分别为5,8,3时,编程求其实根。\n",
"a, b, c = eval(input())\u000b",
"if b * b - 4 * a * c > 0:\u000b",
" x1 = (-b + (b * b - 4 * a * c) ** (1 / 2)) / (2 * a)\u000b",
" x2 = (-b - (b * b - 4 * a * c) ** 0.5) / (2 * a)\u000b",
" print(x1, x2)\n",
"a,b,c的值由用户在一行内输入的3个逗号分隔的数字\n"
]
},
{
"cell_type": "markdown",
"id": "ddcff58f",
"metadata": {},
"source": [
"实例 2.2 一元二次方程求解\n",
"现有一元二次方程:ax2 + bx + c = 0,当a,b,c的值分别为5,8,3时,编程求其实根。\n",
"a, b, c = eval(input())\u000b",
"if b * b - 4 * a * c > 0:\u000b",
" x1 = (-b + (b * b - 4 * a * c) ** (1 / 2)) / (2 * a)\u000b",
" x2 = (-b - (b * b - 4 * a * c) ** 0.5) / (2 * a)\u000b",
" print(x1, x2)\u000b",
"elif b * b - 4 * a * c == 0:\u000b",
" print(-b / (2 * a))\u000b",
"else:\u000b",
" x1 = (-b + (b * b - 4 * a * c) ** (1 / 2)) / (2 * a)\u000b",
" x2 = (-b - (b * b - 4 * a * c) ** 0.5) / (2 * a)\u000b",
" print(x1, x2)\u000b",
"\n"
]
},
{
"cell_type": "markdown",
"id": "3f4c8ae6",
"metadata": {},
"source": [
"常用数学运算函数\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "b06d8a71",
"metadata": {},
"source": [
"绝对值abs(x)\n",
"print(abs(-3)) 3\n",
"print(abs(-3.45)) 3.45\n",
"print(abs(3 + 4j)) 5.0\n",
"x 为整数或浮点数时返回其绝对值\n",
"x 为复数时返回复数的模\n"
]
},
{
"cell_type": "markdown",
"id": "68b86162",
"metadata": {},
"source": [
"返回 x的y次幂\n",
"print(pow(2, 3)) 8\n",
"print(pow(2, 1/2)) 1.4142135623730951\n",
"print(pow(-2, 1/2))\n",
"print(pow(1999, 1998, 1997)) 4 \n",
"幂 pow(x, y)\n",
"(8.659560562354934e-17+1.4142135623730951j)\n",
"x 的 y 次幂计算结果再对z 取模,效率高\n",
"pow(x, y[, z])\n",
"1999 ** 1998 % 1997 \n"
]
},
{
"cell_type": "markdown",
"id": "ed60b0da",
"metadata": {},
"source": [
"max(arg1,arg2,…) 从多个参数中返回其最大值\n",
"print(max(80, 100, 1000)) 1000\n",
"max(iterable) 从一个可迭代对象中返回其最大值\n",
"print(max([49, 25, 88])) 88\n",
"最大值 max()\n"
]
},
{
"cell_type": "markdown",
"id": "800a9885",
"metadata": {},
"source": [
"min(arg1,arg2,…) 从多个参数中返回其最小值\n",
"print(min(80, 100, 1000)) 80\n",
"min(iterable) 从一个可迭代对象中返回其最小值\n",
"print(min((49, 25, 88))) 25\n",
"最小值 min()\n"
]
},
{
"cell_type": "markdown",
"id": "f089ad30",
"metadata": {},
"source": [
"sum(iterable)\n",
"print(sum(range(11))) 55\n",
"print(sum([1, 2, 3, 4])) 10\n",
"将元素为数值的可迭代对象中的元素累加\n",
"加和 sum()\n"
]
},
{
"cell_type": "markdown",
"id": "ff48e1f6",
"metadata": {},
"source": [
"print(sum(range(11), start=10)) 65\n",
"print(sum([1, 2, 3, 4], 10)) 20\n",
"将元素为数值的可迭代对象中的元素累加到start上\n",
"sum(iterable, start=0)\n",
"加和 sum()\n"
]
},
{
"cell_type": "markdown",
"id": "136b6e69",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"round(number, n)浮点数number保留n 位小数最短表示 \n",
"print(round(3.14)) 3\n",
"print(round(3.84)) 4\n",
"print(round(3.125001, 2)) 3.13\n",
"print(round(3.125, 2)) 3.12\n",
"print(round(3.115, 2)) 3.12\n",
"四舍\n",
"print(round(3.84, 1)) 3.8\n",
"print(round(3.86, 1)) 3.9\n",
"五后非零就进一\n",
"五前为偶应舍去\n",
"五前为奇要进一\n",
"六入\n",
"五考虑\n"
]
},
{
"cell_type": "markdown",
"id": "35648ffd",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"结果小数部分的末位为0 或n 超过小数位数时,返回该数的最短表示\n",
"print(round(3.0000, 2))\n",
"期望输出3.00\n",
"print(round(3.14, 4))\n",
"期望输出3.1400\n",
"实际输出其浮点数的最短表示3.0\n",
"实际输出3.14\n"
]
},
{
"cell_type": "markdown",
"id": "167c610e",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"多数浮点数无法精确转为二进制,会导致部分数字取舍与期望不符\n",
"\n",
"print(round(3.1425, 3))\n",
"import decimal\u000b",
"\u000b",
"print(decimal.Decimal(2.675))\u000b",
"print(decimal.Decimal(3.1425)) \n",
"2.67499999999999982236431605997495353221893310546875\u000b",
"3.1425000000000000710542735760100185871124267578125\n",
"print(round(2.675, 2))\n",
"3.142\n",
"2.68\n",
"3.143\n",
"2.67\n"
]
},
{
"cell_type": "markdown",
"id": "df98a4fe",
"metadata": {},
"source": [
"Math模块及应用\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "bb145b12",
"metadata": {},
"source": [
"\n",
"import math\n",
"导入 math 模块,引用时函数名前要加 'math.'\n",
"https://docs.python.org/3/library/math.html\n",
"\n",
"print(math.pi) # pi为圆周率常量\u000b",
"3.141592653589793\n",
"print(math.sqrt(2)) # sqrt为开平方函数\n",
"1.4142135623730951\n"
]
},
{
"cell_type": "markdown",
"id": "92f438b6",
"metadata": {},
"source": [
"\n",
"from math import pi,sqrt\n",
"导入math中的常数pi和sqrt()函数或全部函数\n",
"from math import *\n",
"print(pi)\n",
"\n",
"3.141592653589793\n",
"print(sqrt(2))\n",
"1.4142135623730951\n"
]
},
{
"cell_type": "markdown",
"id": "7a2c01b3",
"metadata": {},
"source": [
"\n",
"math\n",
"数论与表示函数 24 个\n",
"幂与对数函数 8 个\n",
"三角函数 9 个\n",
"双曲函数 6 个\n",
"角度转换函数 2 个\n",
"特殊函数 4 个\n",
"常量 5 个\n",
"https://docs.python.org/zh-cn/3.9/library/math.html\n"
]
},
{
"cell_type": "markdown",
"id": "f21b7d40",
"metadata": {},
"source": [
"\n",
"math.fabs(x) 以浮点数形式返回x的绝对值\n",
"import math\n",
" \u000b",
"print(math.fabs(-6)) # 6.0\n"
]
},
{
"cell_type": "markdown",
"id": "0bcc9bf2",
"metadata": {},
"source": [
"\n",
"from math import fabs\n",
" \u000b",
"print(fabs(-6)) # 6.0\n",
"math.fabs(x) 以浮点数形式返回x的绝对值\n"
]
},
{
"cell_type": "markdown",
"id": "cbd34e4b",
"metadata": {},
"source": [
"\n",
"math.factorial(x):返回x的阶乘\n",
"print(math.factorial(6)) 720\n",
"要求x为非负整数,x为负数或浮点数时返回错误\n",
"print(math.factorial(-6))\n",
"ValueError: factorial() not defined for negative values\n",
"print(math.factorial(6.5))\n",
"ValueError: factorial() only accepts integral values\n"
]
},
{
"cell_type": "markdown",
"id": "a2b574b9",
"metadata": {},
"source": [
"\n",
"math.fsum()\n",
"返回浮点数迭代求和的精确值\n",
"print(sum([.1, .1,.1, .1, .1, .1, .1, .1, .1, .1, .1])) \n",
"1.0999999999999999\n",
"import math\n",
"\n",
"print(math.fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])) \n",
"1.0\n"
]
},
{
"cell_type": "markdown",
"id": "d862c5b8",
"metadata": {},
"source": [
"\n",
"math.gcd(*integers)\n",
"返回给定的整数参数的最大公约数\n",
"print(math.gcd(88, 44, 22)) 22\n",
"返回值是能同时整除所有参数的最大正整数\n",
"print(math.gcd(88, 44, 0)) 44\n",
"如果所有参数为零或无参数,则返回值为0\n",
"print(math.gcd(0, 0)) 0\n"
]
},
{
"cell_type": "markdown",
"id": "fbd99b16",
"metadata": {},
"source": [
"\n",
"math.lcm(*integers)\n",
"返回给定的整数参数的最小公倍数\n",
"print(math.lcm(5, 44, 22)) 220\n",
"如果参数之一为0,返回值为0\n",
"print(math.lcm(22, 44, 0)) 0\n",
"无参数时,返回值为1\n",
"print(math.lcm(0, 0)) 0\n",
"print(math.lcm()) 1\n"
]
},
{
"cell_type": "markdown",
"id": "6154a8eb",
"metadata": {},
"source": [
"math.floor(x)\u000b",
"返回不大于x的最大整数\u000b",
"print(math.floor(9.99)) # 输出9\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "13658e02",
"metadata": {},
"source": [
"math.ceil(x)\u000b",
"返回不小于x的最小整数\u000b",
"print(math.ceil(9.01)) # 输出10\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "2bd2c2f3",
"metadata": {},
"source": [
"math. sqrt(x) # 返回非负整数 x 的平方根\n",
"\n",
"import math\u000b",
"\u000b",
"print(math.sqrt(5)) # 5的平方根2.23606797749979\n",
"math. isqrt(x) # 返回非负整数 x 的整数平方根\n",
"print(math.isqrt(5)) # 非负整数 5 的整数平方根2\n"
]
},
{
"cell_type": "markdown",
"id": "71076c73",
"metadata": {},
"source": [
"实例2.3 换披萨\n",
"程序员小明在披萨店订购了一个直径为12 英寸的披萨,送货前客服打电话来说“对不起,12 英寸的卖完了,为了补偿您,我们给你2 个直径7 英寸披萨好吗?”\n",
"这个更换方案是否合理取决于最终吃到的披萨的多少?\n",
"披萨本身是近似于圆柱体的不规则的形状,不同直径披萨的用料、厚度、工艺等都可能不同,所以想用计算机计算,需要做一些近似和假设:\n",
"1.披萨是规则的圆柱形\n",
"2.厚度均匀\n",
"3.材料均匀\n",
"4.不同尺寸的披萨用料比例相同\n",
"5.不同尺寸的披萨厚度相同\n",
"这样可以将披萨抽象为一个圆柱体,前面假设的条件下,此问题简化为大、小披萨的面积计算。圆的面积计算公式:s = πr2\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "7f6fbb74",
"metadata": {},
"source": [
"实例2.3 换披萨\n",
"程序员小明在披萨店订购了一个直径为12 英寸的披萨,送货前客服打电话来说“对不起,12 英寸的卖完了,为了补偿您,我们给你2 个直径7 英寸披萨好吗?”\n",
"\n",
"import math # 导入math库,后面应用库中的pi值和ceil()函数\u000b",
"\u000b",
"PI = math.pi # 圆周率π的值\u000b",
"# 计算12寸披萨面积与 2 个 7 寸披萨面积之差\u000b",
"profit = PI * 12 * 12 / 4 - 2 * PI * 7 * 7 / 4\u000b",
"print(profit) # 输出面积差值36.128315516282626\n"
]
},
{
"cell_type": "markdown",
"id": "3fe0caa5",
"metadata": {},
"source": [
"实例2.3 换披萨"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6a92fd1f",
"metadata": {},
"outputs": [],
"source": [
"import math # 导入math库,后面应用库中的pi值和ceil()函数\n",
"\n",
"\n",
"diameter_of_big = int(input()) # 披萨直径为整数\n",
"diameter_of_small = int(input()) # 披萨直径为整数\n",
"\n",
"area_of_big = math.pi * (diameter_of_big / 2) ** 2 # 大寸披萨的面积\n",
"area_of_small = math.pi * (diameter_of_small / 2) ** 2 # 小寸披萨的面积\n",
"n = area_of_big / area_of_small # 大披萨与小披萨面积比值\n",
"num = math.ceil(n) # num值为不小于n的最小整数\n",
"profit = area_of_small * math.ceil(num) - area_of_big # 更换后多得的披萨的面积\n",
"\n",
"print(num) # 输出合理的更换数量\n",
"print(f'{num}个{diameter_of_small}寸披萨与1个{diameter_of_big}寸披萨面积差值为{round(profit, 2)}平方英寸')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f6d5008",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}