|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:d8ba69c66769cf62e5201b70ed7d717913017f6f09492848ce164b50068bd2ba" |
| 4 | + "signature": "sha256:5e808c95d2003c791e04a26d378bc816b2010a59171244a18d73487060d9e029" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
79 | 79 | "- [Creating copies of mutable objects](#copy_mutable)\n", |
80 | 80 | "- [Key differences between Python 2 and 3](#python_differences)\n", |
81 | 81 | "- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n", |
82 | | - "- [Abortive statements in `finally` blocks](#finally_blocks)" |
| 82 | + "- [Abortive statements in `finally` blocks](#finally_blocks)\n", |
| 83 | + "- [Assigning types to variables as values](#variable_types)" |
83 | 84 | ] |
84 | 85 | }, |
85 | 86 | { |
|
922 | 923 | "cell_type": "markdown", |
923 | 924 | "metadata": {}, |
924 | 925 | "source": [ |
925 | | - "Chicken or egg? In the history of Python (Python 2.2 to be specific) truth values were implemented via 1 and 0 (similar to the old C), to avoid syntax error in old (but perfectly working) code, `bool` was added as a subclass of `int` in Python 2.3.\n", |
| 926 | + "Chicken or egg? In the history of Python (Python 2.2 to be specific) truth values were implemented via 1 and 0 (similar to the old C). In order to avoid syntax errors in old (but perfectly working) Python code, `bool` was added as a subclass of `int` in Python 2.3.\n", |
926 | 927 | "\n", |
927 | 928 | "Original source: [http://www.peterbe.com/plog/bool-is-int](http://www.peterbe.com/plog/bool-is-int)" |
928 | 929 | ] |
|
933 | 934 | "input": [ |
934 | 935 | "print('isinstance(True, int):', isinstance(True, int))\n", |
935 | 936 | "print('True + True:', True + True)\n", |
936 | | - "print('3*True:', 3*True)\n", |
| 937 | + "print('3*True + True:', 3*True + True)\n", |
937 | 938 | "print('3*True - False:', 3*True - False)\n" |
938 | 939 | ], |
939 | 940 | "language": "python", |
|
945 | 946 | "text": [ |
946 | 947 | "isinstance(True, int): True\n", |
947 | 948 | "True + True: 2\n", |
948 | | - "3*True: 3\n", |
| 949 | + "3*True + True: 4\n", |
949 | 950 | "3*True - False: 3\n" |
950 | 951 | ] |
951 | 952 | } |
952 | 953 | ], |
953 | | - "prompt_number": 16 |
| 954 | + "prompt_number": 28 |
954 | 955 | }, |
955 | 956 | { |
956 | 957 | "cell_type": "markdown", |
|
979 | 980 | "cell_type": "markdown", |
980 | 981 | "metadata": {}, |
981 | 982 | "source": [ |
982 | | - "Remember the [\"consuming generators\"](consuming_generators)? This example is somewhat related, but the result might still come unexpected. \n", |
| 983 | + "Remember the section about the [\"consuming generators\"](consuming_generators)? This example is somewhat related, but the result might still come unexpected. \n", |
983 | 984 | "\n", |
984 | 985 | "(Original source: [http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html](http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html))\n", |
985 | 986 | "\n", |
986 | | - "In the first example below, where we call a `lambda` function in a list comprehension, the value `i` is dereferenced every time we call `lambda` within the scope of the list comprehension. Since the list is already constructed when we `for-loop` through the list, it is set to the last value 4." |
| 987 | + "In the first example below, we call a `lambda` function in a list comprehension, and the value `i` will be dereferenced every time we call `lambda` within the scope of the list comprehension. Since the list is already constructed when we `for-loop` through the list, it will be set to the last value 4." |
987 | 988 | ] |
988 | 989 | }, |
989 | 990 | { |
|
1009 | 1010 | ] |
1010 | 1011 | } |
1011 | 1012 | ], |
1012 | | - "prompt_number": 11 |
| 1013 | + "prompt_number": 29 |
1013 | 1014 | }, |
1014 | 1015 | { |
1015 | 1016 | "cell_type": "markdown", |
|
1041 | 1042 | ] |
1042 | 1043 | } |
1043 | 1044 | ], |
1044 | | - "prompt_number": 9 |
| 1045 | + "prompt_number": 30 |
1045 | 1046 | }, |
1046 | 1047 | { |
1047 | 1048 | "cell_type": "markdown", |
|
1106 | 1107 | ] |
1107 | 1108 | } |
1108 | 1109 | ], |
1109 | | - "prompt_number": 33 |
| 1110 | + "prompt_number": 31 |
1110 | 1111 | }, |
1111 | 1112 | { |
1112 | 1113 | "cell_type": "markdown", |
|
1484 | 1485 | "cell_type": "markdown", |
1485 | 1486 | "metadata": {}, |
1486 | 1487 | "source": [ |
1487 | | - "Not, really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n", |
1488 | | - "- use lists if you want to use list methods \n", |
| 1488 | + "\"List comprehensions are fast, but generators are faster!?\" - No, not really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n", |
| 1489 | + "- use lists if you want to use the plethora of list methods \n", |
1489 | 1490 | "- use generators when you are dealing with huge collections to avoid memory issues" |
1490 | 1491 | ] |
1491 | 1492 | }, |
|
1699 | 1700 | "cell_type": "markdown", |
1700 | 1701 | "metadata": {}, |
1701 | 1702 | "source": [ |
1702 | | - "It can be really dangerous to modify a list when iterating through - it is a very common pitfall that can cause unintended behavior! \n", |
| 1703 | + "It can be really dangerous to modify a list when iterating through it - this is a very common pitfall that can cause unintended behavior! \n", |
1703 | 1704 | "Look at the following examples, and for a fun exercise: try to figure out what is going on before you skip to the solution!" |
1704 | 1705 | ] |
1705 | 1706 | }, |
|
2865 | 2866 | "Here, the abortive `return` statement in the `finally` block simply overrules the `return` in the `try` block, since **`finally` is guaranteed to always be executed.** So, be careful using abortive statements in `finally` blocks!" |
2866 | 2867 | ] |
2867 | 2868 | }, |
| 2869 | + { |
| 2870 | + "cell_type": "markdown", |
| 2871 | + "metadata": {}, |
| 2872 | + "source": [ |
| 2873 | + "<br>\n", |
| 2874 | + "<br>\n", |
| 2875 | + "<a name=\"variable_types\">" |
| 2876 | + ] |
| 2877 | + }, |
| 2878 | + { |
| 2879 | + "cell_type": "markdown", |
| 2880 | + "metadata": {}, |
| 2881 | + "source": [ |
| 2882 | + "#Assigning types to variables as values" |
| 2883 | + ] |
| 2884 | + }, |
| 2885 | + { |
| 2886 | + "cell_type": "markdown", |
| 2887 | + "metadata": {}, |
| 2888 | + "source": [ |
| 2889 | + "I am not yet sure in which context this can be useful, but it is a nice fun fact to know that we can assign types as values to variables." |
| 2890 | + ] |
| 2891 | + }, |
| 2892 | + { |
| 2893 | + "cell_type": "code", |
| 2894 | + "collapsed": false, |
| 2895 | + "input": [ |
| 2896 | + "a_var = str\n", |
| 2897 | + "a_var(123)" |
| 2898 | + ], |
| 2899 | + "language": "python", |
| 2900 | + "metadata": {}, |
| 2901 | + "outputs": [ |
| 2902 | + { |
| 2903 | + "metadata": {}, |
| 2904 | + "output_type": "pyout", |
| 2905 | + "prompt_number": 1, |
| 2906 | + "text": [ |
| 2907 | + "'123'" |
| 2908 | + ] |
| 2909 | + } |
| 2910 | + ], |
| 2911 | + "prompt_number": 1 |
| 2912 | + }, |
| 2913 | + { |
| 2914 | + "cell_type": "code", |
| 2915 | + "collapsed": false, |
| 2916 | + "input": [ |
| 2917 | + "from random import choice\n", |
| 2918 | + "\n", |
| 2919 | + "a, b, c = float, int, str\n", |
| 2920 | + "for i in range(5):\n", |
| 2921 | + " j = choice([a,b,c])(i)\n", |
| 2922 | + " print(j, type(j))" |
| 2923 | + ], |
| 2924 | + "language": "python", |
| 2925 | + "metadata": {}, |
| 2926 | + "outputs": [ |
| 2927 | + { |
| 2928 | + "output_type": "stream", |
| 2929 | + "stream": "stdout", |
| 2930 | + "text": [ |
| 2931 | + "0 <class 'int'>\n", |
| 2932 | + "1 <class 'int'>\n", |
| 2933 | + "2.0 <class 'float'>\n", |
| 2934 | + "3 <class 'str'>\n", |
| 2935 | + "4 <class 'int'>\n" |
| 2936 | + ] |
| 2937 | + } |
| 2938 | + ], |
| 2939 | + "prompt_number": 4 |
| 2940 | + }, |
2868 | 2941 | { |
2869 | 2942 | "cell_type": "code", |
2870 | 2943 | "collapsed": false, |
|
0 commit comments