Skip to content

Commit 30aec73

Browse files
committed
2016.12.22 almost finish lesson 4 and begin to edit lesson 5 notebook
1 parent 292e28a commit 30aec73

File tree

2 files changed

+138
-81
lines changed

2 files changed

+138
-81
lines changed

python_basic/python_basic_lesson_04.ipynb

Lines changed: 42 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@
829829
},
830830
"outputs": [],
831831
"source": [
832+
"# 等价的函数一般写法和匿名函数写法\n",
833+
"\n",
832834
"def add(x, y):\n",
833835
" return x + y\n",
834836
"\n",
@@ -837,34 +839,64 @@
837839
},
838840
{
839841
"cell_type": "code",
840-
"execution_count": null,
842+
"execution_count": 9,
841843
"metadata": {
842-
"collapsed": true
844+
"collapsed": false
843845
},
844-
"outputs": [],
846+
"outputs": [
847+
{
848+
"data": {
849+
"text/plain": [
850+
"5"
851+
]
852+
},
853+
"execution_count": 9,
854+
"metadata": {},
855+
"output_type": "execute_result"
856+
}
857+
],
845858
"source": [
846859
"a = lambda x, y=2 : x + y \n",
847860
"a(3)"
848861
]
849862
},
850863
{
851864
"cell_type": "code",
852-
"execution_count": null,
865+
"execution_count": 10,
853866
"metadata": {
854-
"collapsed": true
867+
"collapsed": false
855868
},
856-
"outputs": [],
869+
"outputs": [
870+
{
871+
"data": {
872+
"text/plain": [
873+
"8"
874+
]
875+
},
876+
"execution_count": 10,
877+
"metadata": {},
878+
"output_type": "execute_result"
879+
}
880+
],
857881
"source": [
858882
"a(3, 5)"
859883
]
860884
},
861885
{
862886
"cell_type": "code",
863-
"execution_count": null,
887+
"execution_count": 11,
864888
"metadata": {
865-
"collapsed": true
889+
"collapsed": false
866890
},
867-
"outputs": [],
891+
"outputs": [
892+
{
893+
"name": "stdout",
894+
"output_type": "stream",
895+
"text": [
896+
"44\n"
897+
]
898+
}
899+
],
868900
"source": [
869901
"a = lambda x : x * x +40\n",
870902
"\n",
@@ -880,77 +912,6 @@
880912
"* 计算一个正整数的因数\n",
881913
"* 写一个寻找列表中最大数的函数(不用列表排序方法)"
882914
]
883-
},
884-
{
885-
"cell_type": "code",
886-
"execution_count": null,
887-
"metadata": {
888-
"collapsed": true
889-
},
890-
"outputs": [],
891-
"source": [
892-
"# Find the factors of a number\n",
893-
"\n",
894-
"import time\n",
895-
"\n",
896-
"def print_factors(x):\n",
897-
" print(\"The factors of\",x,\"are:\")\n",
898-
" for i in range(1, x + 1):\n",
899-
" if x % i == 0:\n",
900-
" print(i)\n",
901-
"\n",
902-
"# take input from the user\n",
903-
"num = int(input(\"Enter a number: \"))\n",
904-
"\n",
905-
"t1 = time.time()\n",
906-
"print_factors(num)\n",
907-
"t2 = time.time()\n",
908-
"print(t2-t1)"
909-
]
910-
},
911-
{
912-
"cell_type": "code",
913-
"execution_count": null,
914-
"metadata": {
915-
"collapsed": true
916-
},
917-
"outputs": [],
918-
"source": [
919-
"# Find max number #1\n",
920-
"\n",
921-
"def find_max (l):\n",
922-
" max = 0\n",
923-
" for x in l:\n",
924-
" if x > max:\n",
925-
" max = x\n",
926-
" return max\n",
927-
"\n",
928-
"print(find_max([-20,1,6,7,20,5]))"
929-
]
930-
},
931-
{
932-
"cell_type": "code",
933-
"execution_count": null,
934-
"metadata": {
935-
"collapsed": true
936-
},
937-
"outputs": [],
938-
"source": [
939-
"# Find max number #2\n",
940-
"# 使用递归\n",
941-
"\n",
942-
"def find_max (l):\n",
943-
" if len(l) == 1:\n",
944-
" return l[0]\n",
945-
" v1 = l[0]\n",
946-
" v2 = find_max(l[1:])\n",
947-
" if v1 > v2:\n",
948-
" return v1\n",
949-
" else:\n",
950-
" return v2\n",
951-
"\n",
952-
"print(find_max([1,6,7,20,5]))"
953-
]
954915
}
955916
],
956917
"metadata": {
@@ -969,7 +930,7 @@
969930
"name": "python",
970931
"nbconvert_exporter": "python",
971932
"pygments_lexer": "ipython3",
972-
"version": "3.4.5"
933+
"version": "3.5.2"
973934
}
974935
},
975936
"nbformat": 4,
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"# Find the factors of a number\n",
12+
"\n",
13+
"import time\n",
14+
"\n",
15+
"def print_factors(x):\n",
16+
" print(\"The factors of\",x,\"are:\")\n",
17+
" for i in range(1, x + 1):\n",
18+
" if x % i == 0:\n",
19+
" print(i)\n",
20+
"\n",
21+
"# take input from the user\n",
22+
"num = int(input(\"Enter a number: \"))\n",
23+
"\n",
24+
"t1 = time.time()\n",
25+
"print_factors(num)\n",
26+
"t2 = time.time()\n",
27+
"print(t2-t1)"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {
34+
"collapsed": true
35+
},
36+
"outputs": [],
37+
"source": [
38+
"# Find max number #1\n",
39+
"\n",
40+
"def find_max (l):\n",
41+
" max = 0\n",
42+
" for x in l:\n",
43+
" if x > max:\n",
44+
" max = x\n",
45+
" return max\n",
46+
"\n",
47+
"print(find_max([-20,1,6,7,20,5]))"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {
54+
"collapsed": true
55+
},
56+
"outputs": [],
57+
"source": [
58+
"# Find max number #2\n",
59+
"# 使用递归\n",
60+
"\n",
61+
"def find_max (l):\n",
62+
" if len(l) == 1:\n",
63+
" return l[0]\n",
64+
" v1 = l[0]\n",
65+
" v2 = find_max(l[1:])\n",
66+
" if v1 > v2:\n",
67+
" return v1\n",
68+
" else:\n",
69+
" return v2\n",
70+
"\n",
71+
"print(find_max([1,6,7,20,5]))"
72+
]
73+
}
74+
],
75+
"metadata": {
76+
"kernelspec": {
77+
"display_name": "Python 3",
78+
"language": "python",
79+
"name": "python3"
80+
},
81+
"language_info": {
82+
"codemirror_mode": {
83+
"name": "ipython",
84+
"version": 3
85+
},
86+
"file_extension": ".py",
87+
"mimetype": "text/x-python",
88+
"name": "python",
89+
"nbconvert_exporter": "python",
90+
"pygments_lexer": "ipython3",
91+
"version": "3.5.2"
92+
}
93+
},
94+
"nbformat": 4,
95+
"nbformat_minor": 0
96+
}

0 commit comments

Comments
 (0)