{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 创建简单的Python列表\n",
"\n",
"- 在数据两边加引号,将各个电影名转换为字符串\n",
"- 用逗号将列表项与下一项分开\n",
"- 在列表的两边加上开始和结束中括号\n",
"- 使用赋值操作符(=)将这个列表赋值至一个标识符\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Life of Brian\n"
]
}
],
"source": [
"movies = ['The Holy Grail','The
Life of Brian','The Meaning of Life']\n",
"# python 的变量标识符没有类型\n",
"\n",
"# 使用中括号记法访问列表数据\n",
"print(movies[1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 在末尾增加内容\n",
"- append方法\n",
" - 语法:\n",
" movies.appen(内容)\n",
" append(...) method of builtins.list instance\n",
" L.append(object) -> None -- append object to end\n",
"- extend方法:\n",
" - 语法:\n",
" movies.extend([list])\n",
" extend(...) method of builtins.list instance\n",
" L.extend(iterable) -> None -- extend list by appending elements
from the iterable\n",
"# 删除内容\n",
"- pop方法:\n",
"- del方法:\n",
"- remove方法:\n",
"\n",
"# 在特定的位置增加一个特定的数据项\n",
"- insert方法"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['The Holy Grail', 'The Life of
Brian', 'The Meaning of Life', 'Gilliam']\n",
"['The Holy Grail', 'The Life of
Brian', 'The Meaning of Life', 'Gilliam', 'Chapman',
'Idle']\n"
]
}
],
"source": [
"# 案例:append方法\n",
"movies.append('Gilliam')\n",
"print(movies)\n",
"# 案例:extend方法\n",
"movies.extend([\"Chapman\",'Idle'])\n",
"print(movies)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['The Holy Grail', 'The Life of
Brian']\n",
"The Life of Brian\n"
]
}
],
"source": [
"# pop方法\n",
"# pop(...) method of builtins.list
instance\n",
"# L.pop([index]) -> item --
remove and return item at index (default last).\n",
"# Raises IndexError if list is
empty or index is out of range.\n",
"movies = ['The Holy Grail','The
Life of Brian','The Meaning of Life']\n",
"movies.pop()\n",
"print(movies)\n",
"print(movies.pop)\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['The Holy Grail', 'The Meaning of
Life']\n"
]
}
],
"source": [
"# del 方法\n",
"movies = ['The Holy Grail','The
Life of Brian','The Meaning of Life']\n",
"del movies[1]\n",
"print(movies)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['The Holy Grail', 'The Meaning of
Life']\n"
]
}
],
"source": [
"# remove 方法\n",
"movies = ['The Holy Grail','The
Life of Brian','The Meaning of Life']\n",
"movies.remove('The Life of
Brian')\n",
"print(movies)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['The Holy Grail', 'The Life of
Brian', 'Idle', 'The Meaning of Life']\n"
]
}
],
"source": [
"# insert 方法\n",
"movies = ['The Holy Grail','The
Life of Brian','The Meaning of Life']\n",
"movies.insert(2,\"Idle\")\n",
"print(movies)\n",
"\n",
"# Help on built-in function
insert:\n",
"\n",
"# insert(...) method of
builtins.list instance\n",
"# L.insert(index, object) --
insert object before index"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 迭代\n",
"- 通过内置的for循环可以方便的做到迭代\n",
"- 或者while循环也可以,但是要稍微复杂一点\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Holy Grail\n",
"The Life of Brian\n",
"The Meaning of Life\n"
]
}
],
"source": [
"# 迭代的案例\n",
"movies = ['The Holy Grail','The
Life of Brian','The Meaning of Life']\n",
"for each_movie in movies:\n",
" print(each_movie)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"The Holy Grail\n",
"The Life of Brian\n",
"The Meaning of Life\n"
]
}
],
"source": [
"# 使用while迭代\n",
"movies = ['The Holy Grail','The
Life of Brian','The Meaning of Life']\n",
"i = 0\n",
"j = len(movies)\n",
"print(j)\n",
"while i < j:\n",
" print(movies[i])\n",
" i += 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 本节课其他内容\n",
"-
除非有充分的理由使用while循环,或者需要while循环提供额外的控制,for循环会负责从列表起始位置开始一直处理到列表末尾,如果使用for不可能出现大小差1的情况,但是使用while就要计算清楚了\n",
"-
python中单引号和双引号都可以用来创建字符串,如果字符串前面使用了某个引号,那么字符串后面就要用同样的引号,不能混用\n",
"- python代码区分大小写"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 在列表中存储列表\n"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"# 建立一个嵌套列表\n",
"movies = ['The Holy
Grail',1975,'Terry Jones & Terry Gilliam',91,\n",
" ['Graham Chapman',['Michael Palin','John
Cleese','Terry Gilliam','Eric Idle',\n",
" 'Terry Jones']]]\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Terry Gilliam\n",
"['The Holy Grail', 1975, 'Terry
Jones & Terry Gilliam', 91, ['Graham Chapman', ['Michael
Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry
Jones']]]\n"
]
}
],
"source": [
"print(movies[4][1][2])\n",
"print(movies)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Holy Grail\n",
"1975\n",
"Terry Jones & Terry
Gilliam\n",
"91\n",
"['Graham Chapman', ['Michael
Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry
Jones']]\n"
]
}
],
"source": [
"# 使用for循环遍历列表\n",
"for each_ca in movies:\n",
" print(each_ca)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Holy Grail\n",
"1975\n",
"Terry Jones & Terry
Gilliam\n",
"91\n",
"Graham Chapman\n",
"Michael Palin\n",
"John Cleese\n",
"Terry Gilliam\n",
"Eric Idle\n",
"Terry Jones\n"
]
}
],
"source": [
"# 在列表中查找列表\n",
"# isinstance方法\n",
"#
print(isinstance(movies[4],list))\n",
"\n",
"# # movies = ['The Holy
Grail',1975,'Terry Jones & Terry Gilliam',91,\n",
"# # ['Graham Chapman',['Michael
Palin','John Cleese','Terry Gilliam','Eric Idle',\n",
"# # 'Terry Jones']]]\n",
"for each_item in movies:\n",
" if isinstance(each_item,list):\n",
" for
each_each_item in each_item:\n",
" if
isinstance(each_each_item,list):\n",
" for each_each_each_item in
each_each_item:\n",
" print(each_each_each_item)\n",
" else:\n",
" print(each_each_item)\n",
" else:\n",
" print(each_item)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"# 用函数处理多层嵌套列表-递归\n",
"def p_movies(movies):\n",
" for each_movies in movies:\n",
" if
isinstance(each_movies,list):\n",
" p_movies(each_movies)\n",
" else:\n",
" print(each_movies)\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Holy Grail\n",
"1975\n",
"Terry Jones & Terry
Gilliam\n",
"91\n",
"Graham Chapman\n",
"Michael Palin\n",
"John Cleese\n",
"Terry Gilliam\n",
"Eric Idle\n",
"Terry Jones\n"
]
}
],
"source": [
"p_movies(movies)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 其他\n",
"-
标识符是指示数据对象的名字,标识符没有类型,不过标识符所指示的数据对象有类型\n"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\t\t\t\t\tThe Holy Grail\n",
"\t\t\t\t\t1975\n",
"\t\t\t\t\tTerry Jones & Terry
Gilliam\n",
"\t\t\t\t\t91\n",
"\t\t\t\t\t\tGraham
Chapman\n",
"\t\t\t\t\t\t\tMichael
Palin\n",
"\t\t\t\t\t\t\tJohn Cleese\n",
"\t\t\t\t\t\t\tTerry
Gilliam\n",
"\t\t\t\t\t\t\tEric Idle\n",
"\t\t\t\t\t\t\tTerry Jones\n"
]
}
],
"source": [
"def p_movies(movies,i):\n",
" for each_movies in movies:\n",
" if
isinstance(each_movies,list):\n",
" p_movies(each_movies,i+1)\n",
" else:\n",
" for i_j in
range(i):\n",
" print('\\t',end=\"\")\n",
" print(each_movies)\n",
" \n",
" \n",
"# print('\\t',end=\"\")\n",
"# print(each_movies)\n",
" \n",
"p_movies(movies,5)\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 使用可选参数\n",
"-
为了将一个函数的必要参数编程可选的参数,需要为这个参数提供一个缺省值,如果没有提供参数值,则会使用这个缺省值,如果提供了一个参数值,则会使用这个值而不是缺省值"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
"def p_movies(movies,w = False,i =
0):\n",
" for each_movies in movies:\n",
" if
isinstance(each_movies,list):\n",
" p_movies(each_movies,w,i+1)\n",
" else:\n",
" if w == False:\n",
" for i_j in range(i):\n",
" print('\\t',end=\"\")\n",
" print(each_movies)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Holy Grail\n",
"1975\n",
"Terry Jones & Terry
Gilliam\n",
"91\n",
"Graham Chapman\n",
"Michael Palin\n",
"John Cleese\n",
"Terry Gilliam\n",
"Eric Idle\n",
"Terry Jones\n",
"\t\t\tThe Holy Grail\n",
"\t\t\t1975\n",
"\t\t\tTerry Jones & Terry
Gilliam\n",
"\t\t\t91\n",
"\t\t\t\tGraham Chapman\n",
"\t\t\t\t\tMichael Palin\n",
"\t\t\t\t\tJohn Cleese\n",
"\t\t\t\t\tTerry Gilliam\n",
"\t\t\t\t\tEric Idle\n",
"\t\t\t\t\tTerry Jones\n"
]
}
],
"source": [
"p_movies(movies,True,3)\n",
"p_movies(movies,False,3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 其他\n",
"- 使用三重引号字符串可以在代码中加入一个多行注释\n",
"- 使用import语句可以将模块导入到其他程序中\n",
"-
使用module.function()形式调用模块的函数时,要用命名空间名限定函数\n",
"- 使用import语句的from module import
function形式可以从一个模块将函数专门导入到当前命名空间\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 文件与异常\n",
"- 程序外部的数据\n",
"-
python的open()BIF就是用来与文件交互,如果与for语句结合使用,可以非常容易的读取文件\n"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"男子:这是争论的合适空间吗?\n",
"其他人:我曾经告诉过你一次。\n",
"男子:这是争论的合适空间吗?\n",
"\n",
"其他人:我曾经告诉过你一次。\n",
"\n",
"男子:不,你没有!\n",
"\n",
"其他男人:是的,我有。\n",
"\n",
"男人:什么时候?\n",
"\n",
"其他人:刚才。\n",
"\n",
"男子:不,你没有!\n",
"\n",
"其他男人:是的,我做到了!\n",
"\n",
"男:你没有!\n",
"\n",
"其他男人:我告诉你,我做到了!\n",
"\n",
"男:你没有!\n",
"\n",
"其他人:哦,对不起,这是一个五分钟的争论,还是整整半个小时?\n",
"\n",
"男:啊!(取出钱包并付钱)只需五分钟。\n",
"\n",
"其他男人:只需五分钟。谢谢。\n",
"\n",
"其他人:无论如何,我做到了。\n",
"\n",
"男:你肯定没有!\n",
"\n",
"其他人:现在让我们清楚地说明一点:我绝对告诉你了!\n",
"\n",
"男:哦,不,你没有!\n",
"\n",
"其他男人:哦,是的,我做到了!\n",
"\n",
"男:哦,不,你没有!\n",
"\n",
"其他男人:哦,是的,我做到了!\n",
"\n",
"男:哦,看,这不是争论!\n",
"\n",
"(暂停)\n",
"\n",
"其他男人:是的!\n",
"\n",
"男人:不,不是!\n",
"\n",
"(暂停)\n",
"\n",
"男人:这只是矛盾!\n",
"\n",
"其他男人:不,不是!\n",
"\n",
"男人:是的!\n",
"\n",
"其他男人:不是!\n",
"\n",
"男子:你刚才和我发生了矛盾!\n",
"\n",
"其他男人:不,我没有!\n",
"\n",
"男人:你真不错!\n",
"\n",
"其他男人:不不不!\n",
"\n",
"男人:你刚才做到了!\n",
"\n",
"其他男人:胡说八道!\n",
"\n",
"男子:(被激怒)哦,这是徒劳的!\n",
"\n",
"(暂停)\n",
"\n",
"其他男人:不,不是!\n",
"\n",
"男子:是的!\n"
]
}
],
"source": [
"# 打开-处理-关闭\n",
"# 使用import os\n",
"# os.getcwd()
——用来查找当前的工作目录,open只能打开当前的工作目录下的文件\n",
"# os.chdir(目录)切换工作目录\n",
"\n",
"data1 =
open(\"sketch.txt\")\n",
"# # Do something with the
data\n",
"# # in the file\n",
"# the_file.close()\n",
"print(data1.readline(),end=\"\")\n",
"print(data1.readline(),end=\"\")\n",
"# 使用readline方法从文件获取一个数据行\n",
"data1.seek(0)\n",
"# 退回到其实位置\n",
"# 使用for语句处理每一行\n",
"data1.seek(0)\n",
"for each_hang in data1:\n",
" print(each_hang)\n",
"data1.close()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 进一步查看数据\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on built-in function
insert:\n",
"\n",
"insert(...) method of builtins.list
instance\n",
" L.insert(index, object) -- insert object before
index\n",
"\n"
]
}
],
"source": [
"help(movies.insert)"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}