|
10 | 10 | "name": "python2" |
11 | 11 | }, |
12 | 12 | "name": "", |
13 | | - "signature": "sha256:21fa40ef7368043bdaf558035d71a9c7d07f9c147ef8083eadd7fe8a891b7ca2" |
| 13 | + "signature": "sha256:b363fd42a98873952aba75206e163d01b139ef9e32ca1075b0d581f04bbf654a" |
14 | 14 | }, |
15 | 15 | "nbformat": 3, |
16 | 16 | "nbformat_minor": 0, |
|
340 | 340 | "language": "python", |
341 | 341 | "metadata": {}, |
342 | 342 | "outputs": [], |
343 | | - "prompt_number": 23 |
| 343 | + "prompt_number": 30 |
344 | 344 | }, |
345 | 345 | { |
346 | 346 | "cell_type": "code", |
|
363 | 363 | ] |
364 | 364 | } |
365 | 365 | ], |
366 | | - "prompt_number": 24 |
| 366 | + "prompt_number": 31 |
| 367 | + }, |
| 368 | + { |
| 369 | + "cell_type": "heading", |
| 370 | + "level": 2, |
| 371 | + "metadata": {}, |
| 372 | + "source": [ |
| 373 | + "Exercise: Try to break `happy_birthday()`" |
| 374 | + ] |
| 375 | + }, |
| 376 | + { |
| 377 | + "cell_type": "markdown", |
| 378 | + "metadata": {}, |
| 379 | + "source": [ |
| 380 | + "1) Try calling `happy_birthday()` with other values, such as:\n", |
| 381 | + "* A really, really, really long name\n", |
| 382 | + "* Something ridiculous that makes no sense grammatically\n", |
| 383 | + "* Numbers\n", |
| 384 | + "* Other Python types, if you know about any other ones\n", |
| 385 | + "\n", |
| 386 | + "This is how malicious \"black hat\" hackers think when they try to break into desktop and web applications.\n", |
| 387 | + "\n", |
| 388 | + "2) Try adding a bad list item to `names`. Experiment with the position of the bad item." |
| 389 | + ] |
| 390 | + }, |
| 391 | + { |
| 392 | + "cell_type": "heading", |
| 393 | + "level": 2, |
| 394 | + "metadata": {}, |
| 395 | + "source": [ |
| 396 | + "Example: Better String Formatting" |
| 397 | + ] |
| 398 | + }, |
| 399 | + { |
| 400 | + "cell_type": "markdown", |
| 401 | + "metadata": {}, |
| 402 | + "source": [ |
| 403 | + "Rather than using string concatenation, let's use a string formatting operator that doesn't care what type of value it receives." |
| 404 | + ] |
| 405 | + }, |
| 406 | + { |
| 407 | + "cell_type": "code", |
| 408 | + "collapsed": false, |
| 409 | + "input": [ |
| 410 | + "def happy_birthday(name):\n", |
| 411 | + " print(\"Happy Birthday, dear {0}!\".format(name))" |
| 412 | + ], |
| 413 | + "language": "python", |
| 414 | + "metadata": {}, |
| 415 | + "outputs": [], |
| 416 | + "prompt_number": 39 |
| 417 | + }, |
| 418 | + { |
| 419 | + "cell_type": "markdown", |
| 420 | + "metadata": {}, |
| 421 | + "source": [ |
| 422 | + "Now, the function can print happy birthday greetings for any value of `name`, even for people with bizarre names like:\n", |
| 423 | + "* 100\n", |
| 424 | + "* 3.1415927\n", |
| 425 | + "* \u01ac\u0335\u032c\u030a (The Artist Formerly Known As Prince)" |
| 426 | + ] |
| 427 | + }, |
| 428 | + { |
| 429 | + "cell_type": "code", |
| 430 | + "collapsed": false, |
| 431 | + "input": [ |
| 432 | + "happy_birthday(100)\n" |
| 433 | + ], |
| 434 | + "language": "python", |
| 435 | + "metadata": {}, |
| 436 | + "outputs": [ |
| 437 | + { |
| 438 | + "output_type": "stream", |
| 439 | + "stream": "stdout", |
| 440 | + "text": [ |
| 441 | + "Happy Birthday, dear 100!\n" |
| 442 | + ] |
| 443 | + } |
| 444 | + ], |
| 445 | + "prompt_number": 42 |
| 446 | + }, |
| 447 | + { |
| 448 | + "cell_type": "code", |
| 449 | + "collapsed": false, |
| 450 | + "input": [ |
| 451 | + "happy_birthday(3.1415927)" |
| 452 | + ], |
| 453 | + "language": "python", |
| 454 | + "metadata": {}, |
| 455 | + "outputs": [ |
| 456 | + { |
| 457 | + "output_type": "stream", |
| 458 | + "stream": "stdout", |
| 459 | + "text": [ |
| 460 | + "Happy Birthday, dear 3.1415927!\n" |
| 461 | + ] |
| 462 | + } |
| 463 | + ], |
| 464 | + "prompt_number": 43 |
| 465 | + }, |
| 466 | + { |
| 467 | + "cell_type": "code", |
| 468 | + "collapsed": false, |
| 469 | + "input": [ |
| 470 | + "happy_birthday(\"\u01ac\u0335\u032c\u030a\")" |
| 471 | + ], |
| 472 | + "language": "python", |
| 473 | + "metadata": {}, |
| 474 | + "outputs": [ |
| 475 | + { |
| 476 | + "output_type": "stream", |
| 477 | + "stream": "stdout", |
| 478 | + "text": [ |
| 479 | + "Happy Birthday, dear \u01ac\u0335\u032c\u030a!\n" |
| 480 | + ] |
| 481 | + } |
| 482 | + ], |
| 483 | + "prompt_number": 41 |
| 484 | + }, |
| 485 | + { |
| 486 | + "cell_type": "markdown", |
| 487 | + "metadata": {}, |
| 488 | + "source": [ |
| 489 | + "It's better to use `format()` just in case `name` isn't guaranteed to be a string." |
| 490 | + ] |
| 491 | + }, |
| 492 | + { |
| 493 | + "cell_type": "heading", |
| 494 | + "level": 2, |
| 495 | + "metadata": {}, |
| 496 | + "source": [ |
| 497 | + "Advanced Exercise: Why not use `str()` like this?" |
| 498 | + ] |
| 499 | + }, |
| 500 | + { |
| 501 | + "cell_type": "code", |
| 502 | + "collapsed": false, |
| 503 | + "input": [ |
| 504 | + "def happy_birthday(name):\n", |
| 505 | + " print(\"Happy Birthday, dear \" + str(name) + \"!\")" |
| 506 | + ], |
| 507 | + "language": "python", |
| 508 | + "metadata": {}, |
| 509 | + "outputs": [], |
| 510 | + "prompt_number": 47 |
| 511 | + }, |
| 512 | + { |
| 513 | + "cell_type": "markdown", |
| 514 | + "metadata": {}, |
| 515 | + "source": [ |
| 516 | + "Answer: You can, but keep in mind that this only works on Python 3:" |
| 517 | + ] |
| 518 | + }, |
| 519 | + { |
| 520 | + "cell_type": "code", |
| 521 | + "collapsed": false, |
| 522 | + "input": [ |
| 523 | + "str(u'\\xff')" |
| 524 | + ], |
| 525 | + "language": "python", |
| 526 | + "metadata": {}, |
| 527 | + "outputs": [ |
| 528 | + { |
| 529 | + "metadata": {}, |
| 530 | + "output_type": "pyout", |
| 531 | + "prompt_number": 56, |
| 532 | + "text": [ |
| 533 | + "'\u00ff'" |
| 534 | + ] |
| 535 | + } |
| 536 | + ], |
| 537 | + "prompt_number": 56 |
| 538 | + }, |
| 539 | + { |
| 540 | + "cell_type": "markdown", |
| 541 | + "metadata": {}, |
| 542 | + "source": [ |
| 543 | + "Now you have some advanced knowledge about how Python 3 handles Unicode better that Python 2!" |
| 544 | + ] |
367 | 545 | }, |
368 | 546 | { |
369 | 547 | "cell_type": "heading", |
|
475 | 653 | "level": 2, |
476 | 654 | "metadata": {}, |
477 | 655 | "source": [ |
478 | | - "What are functions used for?" |
| 656 | + "Summary: What are functions used for?" |
479 | 657 | ] |
480 | 658 | }, |
481 | 659 | { |
|
0 commit comments