PyCourse/值与相对应运算.ipynb

770 lines
16 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"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
}