上传文件至 /

This commit is contained in:
lifulin 2024-06-30 12:05:06 +08:00
parent 669c8aeb20
commit ffae83b2e5
5 changed files with 5427 additions and 0 deletions

112
事例.ipynb Normal file
View File

@ -0,0 +1,112 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5, 0)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divmod(15,3)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1+1"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"a = 1+1"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\nabc'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
"abc\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"print(input())"
]
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

769
值与相对应运算.ipynb Normal file
View File

@ -0,0 +1,769 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 计算机程序两大部分:\n",
"- 运算\n",
"- 流程控制"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"83\n",
"89\n",
"97\n",
"101\n",
"103\n",
"107\n",
"109\n"
]
}
],
"source": [
"def is_prime(n): # 定义 is_prime(),接收一个参数\n",
" if n < 2: # 开始使用接收到的那个参数(值)开始计算……\n",
" return False # 不再是返回给人,而是返回给调用它的代码……\n",
" if n == 2:\n",
" return True\n",
" for m in range(2, int(n**0.5)+1):\n",
" if (n % m) == 0:\n",
" return False\n",
" else:\n",
" return True\n",
"\n",
"for i in range(80, 110):\n",
" if is_prime(i): # 调用 is_prime() 函数,\n",
" print(i) # 如果返回值为 True则向屏幕输出"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 值\n",
"值是程序的基础步骤<br>\n",
"运算Evaluation\n",
"- 常量Literal\n",
"- 变量Variable"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n"
]
}
],
"source": [
"a = 1 + 2 * 3\n",
"a += 1\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 函数值的特殊形式None\n",
"在 Python 中每个函数都有返回值,即便你在定义一个函数的时候没有设定返回值,它也会加上默认的返回值 None……请注意 None 的大小写!)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n",
"None\n",
"None\n"
]
}
],
"source": [
"def f():\n",
" pass\n",
"print(f()) # 输出 f() 这个函数被调用后的返回值None\n",
"print(print(f())) # 这一行最外围的 print() 调用了一次 print(f()),所以输出一个 None\n",
" # 而后再输出这次调用的返回值,所以又输出一次 None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"当我们调用一个函数的时候,本质上来看,就相当于:\n",
"\n",
">我们把一个值交给某个函数,请函数根据它内部的运算和流程控制对其进行操作而后返回另外一个值。\n",
"\n",
"比如abs() 函数就会返回传递给它的值的绝对值int() 函数会将传递给它的值的小数部分砍掉float() 接到整数参数之后,会返回这个整数的浮点数形式:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.14159\n",
"3\n",
"3.0\n"
]
}
],
"source": [
"print(abs(-3.14159))\n",
"print(int(abs(-3.14159)))\n",
"print(float(int(abs(-3.14159))))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 值的类型\n",
"在编程语言中,总是包含最基本的三种数据类型:\n",
"\n",
"- 布尔值Boolean Value)\n",
"- 数字Numbers整数Int、浮点数Float、复数Complex Numbers\n",
"- 字符串Strings<br>\n",
"**运算的一个默认法则就是,通常情况下应该是相同类型的值才能相互运算。**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"11 + 10 - 9 * 8 / 7 // 6 % 5\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate str (not \"int\") to str",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m3.14\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\n",
"\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
]
}
],
"source": [
"'3.14' + 3"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'33'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(int(float(\"3.14\"))) + str(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"所以,在不得不对不同类型的值进行运算之前,总是要事先做 Type Casting类型转换。比如:\n",
"\n",
"将字符串转换为数字用 int()、float()<br>\n",
"将数字转换成字符串用 str()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"另外,即便是在数字之间进行计算的时候,有时也需要将整数转换成浮点数字,或者反之:\n",
"\n",
"将整数转换成浮点数字用 float()<br>\n",
"将浮点数字转换成整数用 int()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"有个函数type(),可以用来查看某个值属于什么类型:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'>\n",
"<class 'float'>\n",
"<class 'str'>\n",
"<class 'bool'>\n",
"<class 'range'>\n",
"<class 'list'>\n",
"<class 'tuple'>\n",
"<class 'set'>\n",
"<class 'dict'>\n"
]
}
],
"source": [
"print(type(3))\n",
"print(type(3.0))\n",
"print(type('3.14'))\n",
"print(type(True))\n",
"print(type(range(10)))\n",
"print(type([1,2,3]))\n",
"print(type((1,2,3)))\n",
"print(type({1,2,3}))\n",
"print(type({'a':1, 'b':2, 'c':3}))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 操作符\n",
"|运算符号|含义|\n",
"|-------|----|\n",
"|+|加|\n",
"|-|减|\n",
"|*|乘|\n",
"|**|幂|\n",
"|/|除|\n",
"|//|取商|\n",
"|%|取余|\n",
"# 优先级\n",
"先幂,再乘除,再加减"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 布尔运算优先级\n",
"not > and > or \n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"True and False or not True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 逻辑操作符\n",
"使用范围:数值之间\n",
"# 优先级\n",
"数值运算 > 逻辑操作符 > 布尔值操作符"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n = -95\n",
"n < 0 and (n + 1) % 2 == 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 字符串操作符\n",
"针对字符串,有三种操作:\n",
"\n",
"拼接:+ 和 ' '(后者是空格)<br>\n",
"拷贝:*<br>\n",
"逻辑运算in、not in以及<、<=、>、>=、!=、=="
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AwesomePython\n",
"AwesomePython\n",
"Python, Awesome! Awesome! Awesome! \n",
"False\n"
]
}
],
"source": [
"print('Awesome' + 'Python')\n",
"print('Awesome' 'Python')\n",
"print('Python, ' + 'Awesome! ' * 3)\n",
"print('o' in 'Awesome' and 'o' not in 'Python')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 特例Unicode 码比较"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'a' < 'b'"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"65\n",
"97\n",
"20320\n"
]
}
],
"source": [
"print('A' > 'a')\n",
"print(ord('A'))\n",
"print(ord('a'))\n",
"print(ord(\"你\"))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'PYTHON' > 'Python 3'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"你好\n"
]
}
],
"source": [
"print(\"python\" and \"你好\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 列表操作符\n",
"因为列表和字符串一样,都是有序容器(容器还有另外一种是无序容器),所以,它们可用的操作符其实相同:\n",
"\n",
"拼接:+ 和 ' '(后者是空格)<br>\n",
"拷贝:*<br>\n",
"逻辑运算in、not in以及<、<=、>、>=、!=、==<br>\n",
"\n",
"两个列表在比较时(前提是两个列表中的数据元素类型相同),遵循的还是跟字符串比较相同的规则:“一旦决出胜负马上停止”。<br>\n",
"类型不同比较会报错!"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"\n",
"a_list = [1, 2, 3, 4, 5]\n",
"b_list = [1, 2, 3, 5]\n",
"c_list = ['ann', 'bob', 'cindy', 'dude', 'eric']\n",
"a_list > b_list\n",
"10 not in a_list\n",
"'ann' in c_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 关于布尔值的一些补充"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"True or \"python\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`False`定义为空\n",
"那么`True`的定义便是有"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 关于值类型的一些补充"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"除了数字、布尔值、字符串,以及上一小节介绍的列表之外,还有若干数据类型,比如 `range()`(等差数列)、`tuple`(元组)、`set`(集合)、`dictionary`(字典),再比如 `Date Type`(日期)等等。\n",
"\n",
"它们都是基础数据类型的各种组合 —— 现实生活中,更多需要的是把基础类型组合起来构成的数据。比如,一个通讯簿,里面是一系列字符串分别对应着若干字符串和数字。\n",
"\n",
"``` python\n",
"entry[3662] = {\n",
" 'first_name': 'Michael',\n",
" 'last_name': 'Willington',\n",
" 'birth_day': '12/07/1992',\n",
" 'mobile': {\n",
" '+714612234', \n",
" '+716253923'\n",
" }\n",
" 'id': 3662,\n",
" ...\n",
"}\n",
"``` \n",
"\n",
"针对不同的类型,都有相对应的操作符,可以对其进行运算。\n",
"\n",
"这些类型之间有时也有不得不相互运算的需求,于是,在相互运算之前同样要 _Type Casting_比如将 List 转换为 Set或者反之"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
"for i in range(10):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('你好', '我不好')\n",
"1\n"
]
}
],
"source": [
"a = (\"你好\",\"我不好\")\n",
"print(a)\n",
"a = 1\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'我不好'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"你好\" and \"我不好\""
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'小明': 5, '小红': 3}\n"
]
}
],
"source": [
"dic = {\"小明\":5,\"小红\":3}\n",
"print(dic)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'list'>\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"{1, 2, 3, 4, 5, 6, 7}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"\n",
"a = [1, 2, 3, 4, 5, 6, 7] #数组Array #列表\n",
"print(type(a))\n",
"b = set(a)\n",
"c = list(b)\n",
"a\n",
"b\n",
"c"
]
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

691
入口.ipynb Normal file
View File

@ -0,0 +1,691 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**程序**是按照一定**顺序**完成任务的流程\n",
"\n",
"做蛋炒饭步骤:\n",
"- 准备米饭\n",
"- 准备鸡蛋\n",
"- 准备配料\n",
"- 炒蛋\n",
"- 炒配料\n",
"- 调味\n",
"- 出锅\n",
"\n",
"菜谱 = 程序 -- 自然语言 -->程序语言\n",
"编写者 是人\n",
"执行者 是人ß\n",
"\n",
"计算机程序和人做饭程序最关键差别是**布尔运算**\n",
"- 按照顺序执行任务\n",
"- 根据不同情况执行不同任务\n",
" 如果条件满足,则重复执行某一任务\n",
"\n",
"计算机强大便是在于他**可编程**\n",
"而可编程的核心在于**布尔运算**及其相应**流程控制**\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n"
]
}
],
"source": [
"print(1 == 2)\n",
"print(1 != 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "html"
}
},
"source": [
"# 逻辑运算符\n",
"| 比较操作符 | 意义 | 示例 | 布尔值 | \n",
"| --- | --- | --- | --- | \n",
"| == | 等于 | 1 == 2 | False | \n",
"| != | 不等于 | 1 != 2 | True | \n",
"| > | 大于 | 1 >2 | False | \n",
"| >= | 大于等于 | 1>= 1 | True | \n",
"| < | 小于 | 1< 2 | True | \n",
"| <= | 小于等于 | 1<=2 | True | \n",
"| in | 属于 | \"a\" in \"basic\" | True | "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 布尔运算操作符\n",
"操作对象:数字值或字符串值\n",
"操作运算符:与、或、非\n",
"| and | True | False |\n",
"|--------|--------|---------|\n",
"| True | True | False | \n",
"| False | False | False | \n",
"\n",
"<br>\n",
"\n",
"| or | True | False |\n",
"|--------|--------|---------|\n",
"| True | True | True | \n",
"| False | True | False | \n",
"\n",
"<br>\n",
"\n",
"| not | True | False |\n",
"|--------|--------|---------|\n",
"| | False | True | "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(True and False) yields: False\n",
"(True and True) yields: True\n",
"(False and True) yields: False\n",
"(True or False) yields: True\n",
"(False or True) yields: True\n",
"(False or False) yields: False\n",
"(not True) yields: False\n",
"(not False) yields: True\n"
]
}
],
"source": [
"print('(True and False) yields:', True and False)\n",
"print('(True and True) yields:', True and True)\n",
"print('(False and True) yields:', False and True)\n",
"print('(True or False) yields:', True or False)\n",
"print('(False or True) yields:', False or True)\n",
"print('(False or False) yields:', False or False)\n",
"print('(not True) yields:', not True)\n",
"print('(not False) yields:', not False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 流程控制"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"255 is odd.\n"
]
}
],
"source": [
"import random\n",
"r = random.randrange(1, 1000)\n",
"# 暂时忽略以上两句的原理,只需要了解其结果:\n",
"# 引入随机数而后每次执行的时候r 的值不同\n",
"\n",
"if r % 2 == 0:\n",
" print(r, 'is even.')\n",
"else:\n",
" print(r, 'is odd.')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1+1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"print(1+1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"|英文|中文|\n",
"|---|---|\n",
"|Assignment|赋值|\n",
"|Variable|变量|\n",
"|Literal|常量|\n",
"|Loop|循环|"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n"
]
}
],
"source": [
"for i in range(10):\n",
" if i % 2 != 0:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"打印100以为的质数<br>\n",
"质数定义一个数大于等于2且只有在被它自身或者1做为除数时余数为0<br>\n",
"设n为整数n>=2<br>\n",
"若n==2n是质数<br>\n",
"若n>2就把n作为被除数从2开始一直到n-1都作为除数逐一计算看看余数是否等于0<br>\n",
"&nbsp;&nbsp;&nbsp;&nbsp;如果是,那就不用接着算了,它不是质数;<br>\n",
"&nbsp;&nbsp;&nbsp;&nbsp;如果全部都试过了余数都不是0那么它是质数。<br>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"3\n",
"5\n",
"7\n",
"11\n",
"13\n",
"17\n",
"19\n",
"23\n",
"29\n",
"31\n",
"37\n",
"41\n",
"43\n",
"47\n",
"53\n",
"59\n",
"61\n",
"67\n",
"71\n",
"73\n",
"79\n",
"83\n",
"89\n",
"97\n"
]
}
],
"source": [
"for n in range(2, 100): #range(2,100)表示含左侧 2不含右侧 100是不是第三次看到这个说法了\n",
" if n == 2:\n",
" print(n)\n",
" continue\n",
" for i in range(2, n):\n",
" if (n % i) == 0:\n",
" break\n",
" else: # 这里目前你可能看不懂…… 但先关注结果吧。\n",
" print(n) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 算法\n",
"以上程序,都可以称之为算法,而以上算法也都可以优化。\n",
"比方说寻找质数的时候完全没有必要将终点设在99按照数学逻辑设置在√n就好\n",
"```python\n",
"for n in range(2, 100):\n",
" if n == 2:\n",
" print(n)\n",
" continue\n",
" for i in range(2, int(n ** 0.5)+1): #为什么要 +1 以后再说…… n 的 1/2 次方,相当于根号 n。\n",
" if (n % i) == 0:\n",
" break\n",
" else:\n",
" print(n) \n",
"\n",
"优化没有固定的方法,所有工具,其效率都取决于使用他的人"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 函数\n",
"- 函数名Function Name\n",
"- 参数Parameters\n",
"- 返回值Return Value\n",
"- 调用Call"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.1415926"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = abs(-3.1415926)\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pip install xxx"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def is_prime(n): # 定义 is_prime(),接收一个参数\n",
" if n < 2: # 开始使用接收到的那个参数(值)开始计算……\n",
" return False # 不再是返回给人,而是返回给调用它的代码……\n",
" if n == 2:\n",
" return True\n",
" for m in range(2, int(n**0.5)+1):\n",
" if (n % m) == 0:\n",
" return False\n",
" else:\n",
" return True\n",
"\n",
"for i in range(80, 110):\n",
" if is_prime(i): # 调用 is_prime() 函数,\n",
" print(i) # 如果返回值为 True则向屏幕输出 i"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 细节补充,整体概括"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 语句"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n",
"----------------------------------\n",
"1\n",
"3\n",
"5\n",
"7\n",
"9\n"
]
}
],
"source": [
"# 通常情况下,建议一行写一条语句\n",
"for i in range(10):\n",
" if i % 2 != 0:\n",
" print(i)\n",
"\n",
"print(\"----------------------------------\")\n",
"\n",
"#但是,有时为了简化,也会使用一行表达\n",
"print(\"\\n\".join(str(i) for i in range(10) if i % 2 != 0))\n",
"\n",
"#但是,要注意,这样子写虽然逼格上来了,但是对计算机来说,效率没有上面高"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 语句块"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n",
"11\n",
"13\n",
"15\n",
"17\n",
"19\n",
"Collected odd numbers: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n"
]
}
],
"source": [
"def collect_and_print_odd_numbers(n):\n",
" # 初始化一个空列表来存储奇数\n",
" odd_numbers = []\n",
"\n",
" # 循环从0到n包括n\n",
" for i in range(n + 1):\n",
" if i % 2 != 0:\n",
" # 如果i是奇数则将其添加到列表中\n",
" odd_numbers.append(i)\n",
" \n",
" # 打印奇数列表中的每个数字\n",
" print(\"\\n\".join(str(num) for num in odd_numbers))\n",
"\n",
" # 返回奇数列表\n",
" return odd_numbers\n",
"\n",
"# 调用函数收集并打印0到20之间的奇数\n",
"odd_numbers_list = collect_and_print_odd_numbers(20)\n",
"print(\"Collected odd numbers:\", odd_numbers_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#python的注释一共有两种方法\n",
"#一般来说,这种写法放在中间\n",
"\n",
"\"\"\"\n",
"三个引号中间,都是注释区域\n",
"\n",
"一般来说,这种注释方法写在开头\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"\n",
"\"\"\"\n",
"#"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"你好 = 1\n",
"print(你好)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n",
"11\n",
"13\n",
"15\n",
"17\n",
"19\n",
"Collected odd numbers: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n"
]
}
],
"source": [
"def collect_and_print_odd_numbers(n):\n",
"\n",
" \"\"\"\n",
" 打印并返回从0到给定数字n之间的所有奇数。\n",
"\n",
" 参数:\n",
" n (int): 要检查的最大数字。\n",
"\n",
" 返回:\n",
" list: 包含所有奇数的列表。\n",
" \"\"\"\n",
" \n",
" # 初始化一个空列表来存储奇数\n",
" odd_numbers = []\n",
"\n",
" # 循环从0到n包括n\n",
" for i in range(n + 1):\n",
" if i % 2 != 0:\n",
" # 如果i是奇数则将其添加到列表中\n",
" odd_numbers.append(i)\n",
" \n",
" # 打印奇数列表中的每个数字\n",
" print(\"\\n\".join(str(num) for num in odd_numbers))\n",
"\n",
" # 返回奇数列表\n",
" return odd_numbers\n",
"\n",
"# 调用函数收集并打印0到20之间的奇数\n",
"odd_numbers_list = collect_and_print_odd_numbers(20)\n",
"print(\"Collected odd numbers:\", odd_numbers_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 操作运算符"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"|运算符号|含义|\n",
"|-------|----|\n",
"|+|加|\n",
"|-|减|\n",
"|*|乘|\n",
"|**|幂|\n",
"|/|除|\n",
"|//|取商|\n",
"|%|取余|"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 加法示例\n",
"a = 5\n",
"b = 3\n",
"sum_result = a + b # 5 + 3 = 8\n",
"print(\"加法结果:\", sum_result)\n",
"\n",
"# 减法示例\n",
"sub_result = a - b # 5 - 3 = 2\n",
"print(\"减法结果:\", sub_result)\n",
"\n",
"# 乘法示例\n",
"mul_result = a * b # 5 * 3 = 15\n",
"print(\"乘法结果:\", mul_result)\n",
"\n",
"# 除法示例\n",
"div_result = a / b # 5 / 3 = 1.6666666666666667\n",
"print(\"除法结果:\", div_result)\n",
"\n",
"# 幂运算示例\n",
"pow_result = a ** b # 5 ** 3 = 125\n",
"print(\"幂运算结果:\", pow_result)\n",
"\n",
"# 取商示例\n",
"floordiv_result = a // b # 5 // 3 = 1\n",
"print(\"取商结果:\", floordiv_result)\n",
"\n",
"# 取余示例\n",
"mod_result = a % b # 5 % 3 = 2\n",
"print(\"取余结果:\", mod_result)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 赋值"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"2\n"
]
}
],
"source": [
"a = 1\n",
"a = a + 1\n",
"a += 1\n",
"print(a)\n",
"a = 2\n",
"print(a)"
]
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

2511
字符串.ipynb Normal file

File diff suppressed because it is too large Load Diff

1344
容器.md Normal file

File diff suppressed because it is too large Load Diff