8000 Chapter_6__Fruitful_functions · softhints/python@97ddc31 · GitHub
[go: up one dir, main page]

Skip to content

Commit 97ddc31

Browse files
committed
Chapter_6__Fruitful_functions
1 parent 06f086a commit 97ddc31

File tree

1 file changed

+176
-32
lines changed

1 file changed

+176
-32
lines changed

notebooks/Books/Think Python/Chapter_6__Fruitful_functions.ipynb

Lines changed: 176 additions & 32 deletions
+
{
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"outputs": [],
4747
"source": [
4848
"def print_str(s):\n",
49-
" print(s)"
49+
" print(s)\n",
50+
"print_str(1)"
5051
]
5152
},
5253
{
@@ -60,14 +61,51 @@
6061
"print(1)"
6162
]
6263
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": [
70+
"del print"
71+
]
72+
},
6373
{
6474
"cell_type": "code",
6575
"execution_count": null,
6676
"metadata": {},
6777
"outputs": [],
6878
"source": [
6979
"def double_int(i):\n",
70-
" return i * 2"
80+
" return i * 2\n",
81+
"double_int(2)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"x = print_str(1)"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"y = double_int(1)"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"print(x, y)"
71109
]
72110
},
73111
{
@@ -105,7 +143,7 @@
105143
"outputs": [],
106144
"source": [
107145
"import math \n",
108-
"math.sin(radians)"
146+
"math.sin(5)"
109147
]
110148
},
111149
{
@@ -344,7 +382,7 @@
344382
},
345383
{
346384
"cell_type": "code",
347-
"execution_count": 1,
385+
"execution_count": null,
348386
"metadata": {},
349387
"outputs": [],
350388
"source": [
@@ -354,24 +392,31 @@
354392
},
355393
{
356394
"cell_type": "code",
357-
"execution_count": 2,
395+
"execution_count": null,
358396
"metadata": {},
359-
"outputs": [
360-
{
361-
"data": {
362-
"text/plain": [
363-
"(0, 1, 3)"
364-
]
365-
},
366-
"execution_count": 2,
367-
"metadata": {},
368-
"output_type": "execute_result"
369-
}
370-
],
397+
"outputs": [],
371398
"source": [
372399
"area_y(5)"
373400
]
374401
},
402+
{
403+
"cell_type": "code",
404+
"execution_count": null,
405+
"metadata": {},
406+
"outputs": [],
407+
"source": [
408+
"x,y,z = area_y(5)"
409+
]
410+
},
411+
{
412+
"cell_type": "code",
413+
"execution_count": null,
414+
"metadata": {},
415+
"outputs": [],
416+
"source": [
417+
"y"
418+
]
419+
},
375420
{
376421
"cell_type": "markdown",
377422
"metadata": {},
@@ -1045,6 +1090,26 @@
10451090
" return result"
10461091
]
10471092
},
1093+
{
1094+
"cell_type": "code",
1095+
"execution_count": 7,
1096+
"metadata": {},
1097+
"outputs": [
1098+
{
1099+
"data": {
1100+
"text/plain": [
1101+
"720"
1102+
]
1103+
},
1104+
"execution_count": 7,
1105+
"metadata": {},
1106+
"output_type": "execute_result"
1107+
}
1108+
],
1109+
"source": [
1110+
"factorial(6)"
1111+
]
1112+
},
10481113
{
10491114
"cell_type": "markdown",
10501115
"metadata": {},
@@ -1223,7 +1288,7 @@
12231288
},
12241289
{
12251290
"cell_type": "code",
1226-
"execution_count": null,
1291+
"execution_count": 8,
12271292
"metadata": {},
12281293
"outputs": [],
12291294
"source": [
@@ -1250,28 +1315,62 @@
12501315
},
12511316
{
12521317
"cell_type": "code",
1253-
"execution_count": null,
1318+
"execution_count": 9,
12541319
"metadata": {},
1255-
"outputs": [],
1320+
"outputs": [
1321+
{
1322+
"name": "stdout",
1323+
"output_type": "stream",
1324+
"text": [
1325+
"0, 1, 1, 2, 3, 5, 8, 13, 21, 34, "
1326+
]
1327+
}
1328+
],
12561329
"source": [
12571330
"for i in range(0,10):\n",
12581331
" print(fibonacci(i), end=', ')"
12591332
]
12601333
},
12611334
{
12621335
"cell_type": "code",
1263-
"execution_count": null,
1336+
"execution_count": 10,
12641337
"metadata": {},
1265-
"outputs": [],
1338+
"outputs": [
1339+
{
1340+
"data": {
1341+
"text/plain": [
1342+
"21"
1343+
]
1344+
},
1345+
"execution_count": 10,
1346+
"metadata": {},
1347+
"output_type": "execute_result"
1348+
}
1349+
],
12661350
"source": [
12671351
"fibonacci(8.0)"
12681352
]
12691353
},
12701354
{
12711355
"cell_type": "code",
1272-
"execution_count": null,
1356+
"execution_count": 11,
12731357
"metadata": {},
1274-
"outputs": [],
1358+
"outputs": [
1359
1360+
"ename": "RecursionError",
1361+
"evalue": "maximum recursion depth exceeded in comparison",
1362+
"output_type": "error",
1363+
"traceback": [
1364+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1365+
"\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)",
1366+
"\u001b[0;32m<ipython-input-11-0b0c6f7f535e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfibonacci\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m8.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1367+
"\u001b[0;32m<ipython-input-8-0caf601ae00b>\u001b[0m in \u001b[0;36mfibonacci\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfibonacci\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mfibonacci\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1368+
"... last 1 frames repeated, from the frame below ...\n",
1369+
"\u001b[0;32m<ipython-input-8-0caf601ae00b>\u001b[0m in \u001b[0;36mfibonacci\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfibonacci\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mfibonacci\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1370+
"\u001b[0;31mRecursionError\u001b[0m: maximum recursion depth exceeded in comparison"
1371+
]
1372+
}
1373+
],
12751374
"source": [
12761375
"fibonacci(8.5)"
12771376
]
@@ -1345,7 +1444,7 @@
13451444
},
13461445
{
13471446
"cell_type": "code",
1348-
"execution_count": null,
1447+
"execution_count": 12,
13491448
"metadata": {},
13501449
"outputs": [],
13511450
"source": [
@@ -1375,18 +1474,36 @@
13751474
},
13761475
{
13771476
"cell_type": "code",
1378-
"execution_count": null,
1477+
"execution_count": 13,
13791478
"metadata": {},
1380-
"outputs": [],
1479+
"outputs": [
1480+
{
1481+
"name": "stdout",
1482+
"output_type": "stream",
1483+
"text": [
1484+
"Factorial is only defined for integers.\n",
1485+
"None\n"
1486+
]
1487+
}
1488+
],
13811489
"source": [
13821490
"print(factorial('fred'))"
13831491
]
13841492
},
13851493
{
13861494
"cell_type": "code",
1387-
"execution_count": null,
1495+
"execution_count": 14,
13881496
"metadata": {},
1389-
"outputs": [],
1497+
"outputs": [
1498+
{
1499+
"name": "stdout",
1500+
"output_type": "stream",
1501+
"text": [
1502+
"Factorial is not defined for negative integers.\n",
1503+
"None\n"
1504+
]
1505+
}
1506+
],
13901507
"source": [
13911508
"print(factorial(-2))"
13921509
]
@@ -1478,7 +1595,7 @@
14781595
},
14791596
{
14801597
"cell_type": "code",
1481-
"execution_count": null,
1598+
"execution_count": 15,
14821599
"metadata": {},
14831600
"outputs": [],
14841601
"source": [
@@ -1506,9 +1623,36 @@
15061623
},
15071624
{
15081625
"cell_type": "code",
1509-
"execution_count": null,
1626+
"execution_count": 16,
15101627
"metadata": {},
1511-
"outputs": [],
1628+
"outputs": [
1629+
{
1630+
"name": "stdout",
1631+
"output_type": "stream",
1632+
"text": [
1633+
" factorial 4\n",
1634+
" factorial 3\n",
1635+
" factorial 2\n",
1636+
" factorial 1\n",
1637+
" factorial 0\n",
1638+
" returning 1\n",
1639+
" returning 1\n",
1640+
" returning 2\n",
1641+
" returning 6\n",
1642+
" returning 24\n"
1643+
]
1644+
},
1645+
{
1646+
"data": {
1647+
"text/plain": [
1648+
"24"
1649+
]
1650+
},
1651+
"execution_count": 16,
1652+
"metadata": {},
1653+
"output_type": "execute_result"
1654+
}
1655+
],
15121656
"source": [
15131657
"factorial(4)"
15141658
]

0 commit comments

Comments
 (0)
0