Skip to content

Commit 3ba685c

Browse files
authored
2017.2.27 small adds on lesson 2
add some contents
1 parent 1753069 commit 3ba685c

File tree

1 file changed

+128
-29
lines changed

1 file changed

+128
-29
lines changed

python_basic/python_basic_lesson_02.ipynb

Lines changed: 128 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"source": [
77
"# Lesson 2\n",
88
"\n",
9-
"Python Basic, Lesson 2, v1.0.0, 2016.12 by David.Yi\n",
9+
"Python Basic, Lesson 2, v1.0.0, 2016.12 by David.Yi \n",
10+
"Python Basic, Lesson 2, v1.0.1, 2017.02 modified by Yimeng.Zhang \n",
1011
"\n",
1112
"\n",
1213
"### 上次内容要点\n",
@@ -30,7 +31,7 @@
3031
"* 随机数介绍\n",
3132
"* 举例\n",
3233
" * 中文分词介绍\n",
33-
" * 小程序联系"
34+
" * 小程序练习(猜拳游戏)"
3435
]
3536
},
3637
{
@@ -241,7 +242,7 @@
241242
"\n",
242243
"#### 列表\n",
243244
"\n",
244-
"列表 list 是 python 内置的一种数据类型。list是一种有序的集合,可以随时添加和删除其中的元素。\n",
245+
"列表 list 是 python 内置的一种数据类型。list是一种可变、有序的集合,可以随时添加和删除其中的元素。\n",
245246
"列表在一般的 python 程序中是最常用的数据类型。\n",
246247
"\n",
247248
"列表的基本概念:\n",
@@ -287,7 +288,7 @@
287288
},
288289
{
289290
"cell_type": "code",
290-
"execution_count": 17,
291+
"execution_count": 18,
291292
"metadata": {
292293
"collapsed": false
293294
},
@@ -296,17 +297,24 @@
296297
"name": "stdout",
297298
"output_type": "stream",
298299
"text": [
299-
"['bird']\n"
300+
"['bird']\n",
301+
"['bird', 'snake']\n",
302+
"['sheep', 'bird', 'snake']\n"
300303
]
301304
}
302305
],
303306
"source": [
304307
"# 列表初始化\n",
305308
"a = []\n",
306309
"\n",
307-
"# 列表追加元素\n",
310+
"# 列表末尾追加元素\n",
308311
"a.append('bird')\n",
312+
"print(a)\n",
309313
"a.append('snake')\n",
314+
"print(a)\n",
315+
"\n",
316+
"# 列表指定位置插入元素\n",
317+
"a.insert(0,'sheep')\n",
310318
"print(a)"
311319
]
312320
},
@@ -321,13 +329,12 @@
321329
"name": "stdout",
322330
"output_type": "stream",
323331
"text": [
324-
"['bird']\n"
332+
"['sheep', 'snake']\n"
325333
]
326334
}
327335
],
328336
"source": [
329337
"# 列表删除指定序号的元素\n",
330-
"\n",
331338
"a.pop(1)\n",
332339
"print(a)"
333340
]
@@ -426,6 +433,28 @@
426433
"print(a)"
427434
]
428435
},
436+
{
437+
"cell_type": "code",
438+
"execution_count": 24,
439+
"metadata": {
440+
"collapsed": false
441+
},
442+
"outputs": [
443+
{
444+
"name": "stdout",
445+
"output_type": "stream",
446+
"text": [
447+
"2\n"
448+
]
449+
}
450+
],
451+
"source": [
452+
"# 统计某个元素在列表中出现次数\n",
453+
"\n",
454+
"a = ['a','b','b','c']\n",
455+
"print(a.count('b'))"
456+
]
457+
},
429458
{
430459
"cell_type": "code",
431460
"execution_count": 24,
@@ -559,6 +588,13 @@
559588
"\n",
560589
"字典中的 key 值不可以重复(定义后也没有办法重复)\n",
561590
"\n",
591+
"字典的几个特点:\n",
592+
"1. 查找和插入的速度极快,不会随着key的增加而变慢; \n",
593+
"2. 需要占用大量的内存,内存浪费多。 \n",
594+
"而list相反:\n",
595+
"1. 查找和插入的时间随着元素的增加而增加; \n",
596+
"2. 占用空间小,浪费内存很少。 \n",
597+
"\n",
562598
"* 创建字典\n",
563599
"* 访问字典中的 key-value\n",
564600
"* 修改字典中的 key-value\n",
@@ -717,7 +753,7 @@
717753
},
718754
{
719755
"cell_type": "code",
720-
"execution_count": 5,
756+
"execution_count": 3,
721757
"metadata": {
722758
"collapsed": false
723759
},
@@ -739,7 +775,7 @@
739775
},
740776
{
741777
"cell_type": "code",
742-
"execution_count": 11,
778+
"execution_count": 4,
743779
"metadata": {
744780
"collapsed": false
745781
},
@@ -760,7 +796,7 @@
760796
},
761797
{
762798
"cell_type": "code",
763-
"execution_count": 7,
799+
"execution_count": 8,
764800
"metadata": {
765801
"collapsed": false
766802
},
@@ -770,10 +806,10 @@
770806
"evalue": "'tuple' object has no attribute 'append'",
771807
"output_type": "error",
772808
"traceback": [
773-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
774-
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
775-
"\u001b[0;32m<ipython-input-7-98c1182e5bbc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Someone'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
776-
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
809+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
810+
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
811+
"\u001b[1;32m<ipython-input-8-e2bbceae4065>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# 元组创建后是不能修改的\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Someone'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
812+
"\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
777813
]
778814
}
779815
],
@@ -785,7 +821,30 @@
785821
},
786822
{
787823
"cell_type": "code",
788-
"execution_count": 8,
824+
"execution_count": 9,
825+
"metadata": {
826+
"collapsed": false
827+
},
828+
"outputs": [
829+
{
830+
"ename": "TypeError",
831+
"evalue": "'tuple' object does not support item assignment",
832+
"output_type": "error",
833+
"traceback": [
834+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
835+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
836+
"\u001b[1;32m<ipython-input-9-a9cd518cf863>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'aaa'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
837+
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
838+
]
839+
}
840+
],
841+
"source": [
842+
"t[1] = 'aaa'"
843+
]
844+
},
845+
{
846+
"cell_type": "code",
847+
"execution_count": 22,
789848
"metadata": {
790849
"collapsed": false
791850
},
@@ -808,7 +867,7 @@
808867
},
809868
{
810869
"cell_type": "code",
811-
"execution_count": 9,
870+
"execution_count": 23,
812871
"metadata": {
813872
"collapsed": false
814873
},
@@ -817,17 +876,45 @@
817876
"name": "stdout",
818877
"output_type": "stream",
819878
"text": [
879+
"(['A', 'B', 'C'], 100, 200)\n",
820880
"['A', 'B', 'C', 'D']\n",
821881
"(['A', 'B', 'C', 'D'], 100, 200)\n"
822882
]
823883
}
824884
],
825885
"source": [
826-
"# 变通的实现可变元组内容\n",
827-
"\n",
886+
"# 变通的实现\"可变\"元组内容\n",
887+
"print(t)\n",
828888
"l.append('D')\n",
829889
"print(l)\n",
830-
"print(t)"
890+
"print(t) # tuple的每个元素,指向永远不变,但指向的元素本身是可变的"
891+
]
892+
},
893+
{
894+
"cell_type": "code",
895+
"execution_count": 20,
896+
"metadata": {
897+
"collapsed": false
898+
},
899+
"outputs": [
900+
{
901+
"name": "stdout",
902+
"output_type": "stream",
903+
"text": [
904+
"<class 'int'>\n",
905+
"<class 'tuple'>\n"
906+
]
907+
}
908+
],
909+
"source": [
910+
"# 创建只有1个元素的元组\n",
911+
"\n",
912+
"l = (1)\n",
913+
"print(type(l)) # l成了一个整数,因为这里的括号有歧义,被认作数学计算里的小括号\n",
914+
"\n",
915+
"# 1个元素的元组必须加逗号来消除歧义\n",
916+
"l = (1,)\n",
917+
"print(type(l))"
831918
]
832919
},
833920
{
@@ -1185,11 +1272,22 @@
11851272
},
11861273
{
11871274
"cell_type": "code",
1188-
"execution_count": null,
1275+
"execution_count": 14,
11891276
"metadata": {
1190-
"collapsed": true
1277+
"collapsed": false
11911278
},
1192-
"outputs": [],
1279+
"outputs": [
1280+
{
1281+
"name": "stdout",
1282+
"output_type": "stream",
1283+
"text": [
1284+
"0:剪刀 1:石头 2:布\n",
1285+
"你出了:2\n",
1286+
"电脑出了 剪刀\n",
1287+
"computer win\n"
1288+
]
1289+
}
1290+
],
11931291
"source": [
11941292
"# 简单的剪刀石头布\n",
11951293
"\n",
@@ -1199,7 +1297,7 @@
11991297
"SECOND = 1 \n",
12001298
"BOTH = 2 \n",
12011299
"\n",
1202-
"t1 = ('scissors', 'stone', 'cloth')\n",
1300+
"t1 = ('剪刀', '石头', '')\n",
12031301
"t2 = ('human win', 'computer win', 'draw')\n",
12041302
"\n",
12051303
"def which_win(i1, i2):\n",
@@ -1218,17 +1316,18 @@
12181316
" if i1 == i2:\n",
12191317
" return BOTH\n",
12201318
"\n",
1221-
"print('0:scissors 1:stone 2:cloth')\n",
1222-
"human = int(input('please input:'))\n",
1319+
"print('0:剪刀 1:石头 2:')\n",
1320+
"human = int(input('你出了:'))\n",
12231321
"\n",
12241322
"c_index = random.randint(0,2)\n",
12251323
"computer = t1[c_index]\n",
12261324
"\n",
1227-
"print(c_index, computer)\n",
1325+
"print(\"电脑出了\",computer)\n",
12281326
"\n",
12291327
"print(t2[which_win(human, c_index)])\n",
12301328
"\n",
1231-
"# 如何优化改进?"
1329+
"# 如何优化改进?\n",
1330+
"# 如何设计记录5局3胜的功能?"
12321331
]
12331332
}
12341333
],
@@ -1249,7 +1348,7 @@
12491348
"name": "python",
12501349
"nbconvert_exporter": "python",
12511350
"pygments_lexer": "ipython3",
1252-
"version": "3.5.2"
1351+
"version": "3.5.1"
12531352
}
12541353
},
12551354
"nbformat": 4,

0 commit comments

Comments
 (0)