File size: 14,783 Bytes
7998276 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | {
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def mytest():\n",
" print('mytest function')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mytest function\n"
]
}
],
"source": [
"mytest()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"my name is yasaka, my age is 19\n"
]
}
],
"source": [
"def mytest(name, age):\n",
" print('my name is %s, my age is %d' % (name, age))\n",
" \n",
"mytest('yasaka', 19)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"my name is yasaka, my age is 18\n",
"my name is yasaka, my age is 28\n",
"my name is yasaka, my age is 28\n",
"my name is yasaka, my age is 28\n"
]
}
],
"source": [
"def mytest(name, age=18):\n",
" print('my name is %s, my age is %d' % (name, age))\n",
" \n",
"mytest('yasaka')\n",
"mytest('yasaka', 28)\n",
"mytest(name='yasaka', age=28)\n",
"mytest(age=28, name='yasaka')"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"my name is yasaka, my age is 19\n",
"() 0\n",
"my name is yasaka, my age is 19\n",
"('soccer', 'basketball', 'baseball') 3\n"
]
}
],
"source": [
"def mytest(name, age, *hobby):\n",
" print('my name is %s, my age is %d' % (name, age))\n",
" print(hobby, len(hobby))\n",
" \n",
"mytest('yasaka', 19)\n",
"mytest('yasaka', 19, 'soccer', 'basketball', 'baseball')"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"my name is yasaka, my age is 19\n",
"{} 0\n",
"my name is yasaka, my age is 19\n",
"{'fisrt_hobby': 'soccer', 'second_hobby': 'basketball', 'third_hobby': 'baseball'} 3\n"
]
}
],
"source": [
"def mytest(name, age, **hobby):\n",
" print('my name is %s, my age is %d' % (name, age))\n",
" print(hobby, len(hobby))\n",
" \n",
"mytest('yasaka', 19)\n",
"mytest('yasaka', 19, fisrt_hobby='soccer', second_hobby='basketball', third_hobby='baseball')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"55\n",
"此方法是来将两个对象进行加和\n"
]
}
],
"source": [
"# 此方法是来将两个对象进行加和\n",
"def my_add(a, b):\n",
" \"\"\"此方法是来将两个对象进行加和\"\"\"\n",
" return a+b\n",
"\n",
"print(my_add(50, 5))\n",
"print(my_add.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"150"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def func1(x):\n",
" y = 100\n",
" return x+y\n",
"\n",
"func1(50)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'z' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-18-e0a3c1d18b6d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mz\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc1\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m50\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m30\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-18-e0a3c1d18b6d>\u001b[0m in \u001b[0;36mfunc2\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfunc2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mz\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc1\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m50\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m30\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'z' is not defined"
]
}
],
"source": [
"def func1(x):\n",
" z = 100\n",
" return x+z\n",
"\n",
"def func2(x):\n",
" return x+z\n",
"\n",
"print(func1(50), func2(30))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"150 130\n"
]
}
],
"source": [
"y = 100\n",
"\n",
"def func1(x):\n",
" return x+y\n",
"\n",
"def func2(x):\n",
" return x+y\n",
"\n",
"print(func1(50), func2(30))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"就近原则"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"150 230\n"
]
}
],
"source": [
"y = 100\n",
"\n",
"def func1(x):\n",
" return x+y\n",
"\n",
"def func2(x):\n",
" y = 200\n",
" return x+y\n",
"\n",
"print(func1(50), func2(30))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"global关键字\n",
"全局变量的访问或者说使用是直接可以去用的,不过要注意一下就近原则,才能确定是否真的访问到了你要的全局变量\n",
"全局变量的重新赋值,我们必须在函数内部去使用global关键字才可以"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"200\n"
]
}
],
"source": [
"y = 100\n",
"\n",
"def func():\n",
" global y\n",
" y = 200\n",
"\n",
"func()\n",
"print(y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"递归"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
]
},
{
"data": {
"text/plain": [
"5050"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = list(range(1, 101))\n",
"print(data)\n",
"\n",
"def my_add(a,b):\n",
" if len(a)==1:\n",
" return a[0]+b\n",
" return my_add(a[:-1] ,a[-1]) + b\n",
"\n",
"my_add(data[:-1], data[-1])"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[123, 45.67, -620000000.0, 123, 45.67, -620000000.0]"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myseq = [123, 45.67, -6.2e8]\n",
"\n",
"def mytest(num):\n",
" return num*2\n",
"\n",
"mytest(10)\n",
"mytest(myseq)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"所谓高阶函数,就是不光可以传递变量,还可以传递函数\n",
"面向函数编程,说白了就是把函数传来传去,函数是第一要素\n",
"面向对象编程,说白了就是把对象传来传去,对象是第一要素"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"转换序列中的数值,要它们统一为一样的数值类型\n",
"转换序列中的数值,要它们统一为一样的数值类型\n",
"转换序列中的数值,要它们统一为一样的数值类型\n"
]
},
{
"data": {
"text/plain": [
"[246, 91.34, -1240000000.0]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myseq = [123, 45.67, -6.2e8]\n",
"\n",
"def convert(func, seq):\n",
" print('转换序列中的数值,要它们统一为一样的数值类型')\n",
" return [func(eachNum) for eachNum in seq]\n",
"\n",
"convert(int, myseq)\n",
"convert(float, myseq)\n",
"convert(mytest, myseq)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[246, 91.34, -1240000000.0]"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(mytest, myseq))"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[246, 91.34, -1240000000.0]"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x: x*2, myseq))"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
]
},
{
"data": {
"text/plain": [
"5050"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = list(range(1, 101))\n",
"print(data)\n",
"from functools import reduce\n",
"\n",
"def my_add(a,b):\n",
" return a+b\n",
"\n",
"reduce(my_add, data)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
]
},
{
"data": {
"text/plain": [
"5050"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = list(range(1, 101))\n",
"print(data)\n",
"from functools import reduce\n",
"reduce(lambda a,b: a+b , data)"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n"
]
}
],
"source": [
"def my_condition(x):\n",
" return x%2==0\n",
"\n",
"print(list(filter(my_condition, data)))"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n"
]
}
],
"source": [
"print(list(filter(lambda x: x%2==0, data)))"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 3, 6, -20, -70]"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted([1,3,6,-20,-70],key=abs)"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|