├── Untitled.txt ├── unicode.txt ├── pandas_relations.png ├── LICENSE ├── Class 08 Python Set.ipynb ├── Class 00 Introduction to the Course.ipynb ├── Class 07 Python Dictionary.ipynb ├── Class 06 Python Tuples.ipynb ├── Class 11 Python Files.ipynb ├── Class 09 Python Conditions and Loops.ipynb ├── Class 02 Python Basics.ipynb ├── Class 10 Python Comprehensions.ipynb ├── Class 01 Introduction to Python.ipynb ├── Class 05 Python Lists.ipynb ├── Class 03 Python Numbers.ipynb └── Class 04 Python Strings.ipynb /Untitled.txt: -------------------------------------------------------------------------------- 1 | 阿三大 -------------------------------------------------------------------------------- /unicode.txt: -------------------------------------------------------------------------------- 1 | Hello world 2 | Bonjour 3 | Hola 4 | Hiya -------------------------------------------------------------------------------- /pandas_relations.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/acepor/python_tutorial/HEAD/pandas_relations.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 acepor 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Class 08 Python Set.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 8 Python Set\n", 8 | "\n", 9 | "\n", 10 | "## 8.1 Set Types\n", 11 | "\n", 12 | "Characters of a set object:\n", 13 | " \n", 14 | " 1. unordered\n", 15 | " 2. hashable\n", 16 | " 3. unique\n", 17 | "\n", 18 | "> However, since sets are unordered, you do not index into or slice them, and there are no keys used to access a value.\n", 19 | "\n", 20 | "Two types of set: \n", 21 | "\n", 22 | " 1. mutable (set), cannot be used as key of a dicionary.\n", 23 | " 2. immutable (frozenset), can be used as key of a dicionary.\n", 24 | "\n", 25 | "## 8.2 Set Type Operators\n", 26 | "\n", 27 | "> Two sets are equal if and only if every member of each set is a member of the other." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 14, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "set([1, 2, 3, 4, 5, 6, 7, 9])\n", 40 | "set([1, 2, 3, 4, 5, 6, 7, 9])\n", 41 | "frozenset([1, 2, 3, 4, 5, 6, 7, 9])\n", 42 | "frozenset([1, 2, 3, 4, 5, 6, 7, 9])\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "# union\n", 48 | "\n", 49 | "s = {1, 2, 5, 6, 9}\n", 50 | "t = frozenset((1, 2, 7, 4, 3))\n", 51 | "\n", 52 | "print(s|t)\n", 53 | "print(s.union(t))\n", 54 | "\n", 55 | "print(t|s)\n", 56 | "print(t.union(s))" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 15, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "set([1, 2])\n", 69 | "set([1, 2])\n", 70 | "frozenset([1, 2])\n", 71 | "frozenset([1, 2])\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "# intersection\n", 77 | "\n", 78 | "s = {1, 2, 5, 6, 9}\n", 79 | "t = frozenset((1, 2, 7, 4, 3))\n", 80 | "\n", 81 | "print(s&t)\n", 82 | "print(s.intersection(t))\n", 83 | "\n", 84 | "print(t&s)\n", 85 | "print(t.intersection(s))" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 16, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "set([9, 5, 6])\n", 98 | "set([9, 5, 6])\n", 99 | "frozenset([3, 4, 7])\n", 100 | "frozenset([3, 4, 7])\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "# difference\n", 106 | "\n", 107 | "s = {1, 2, 5, 6, 9}\n", 108 | "t = frozenset((1, 2, 7, 4, 3))\n", 109 | "\n", 110 | "print(s-t)\n", 111 | "print(s.difference(t))\n", 112 | "\n", 113 | "print(t-s)\n", 114 | "print(t.difference(s))" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 17, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "set([3, 4, 5, 6, 7, 9])\n", 127 | "set([3, 4, 5, 6, 7, 9])\n", 128 | "frozenset([3, 4, 5, 6, 7, 9])\n", 129 | "frozenset([3, 4, 5, 6, 7, 9])\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "# symmetric difference\n", 135 | "\n", 136 | "s = {1, 2, 5, 6, 9}\n", 137 | "t = frozenset((1, 2, 7, 4, 3))\n", 138 | "\n", 139 | "print(s^t)\n", 140 | "print(s.symmetric_difference(t))\n", 141 | "\n", 142 | "print(t^s)\n", 143 | "print(t.symmetric_difference(s))" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "## 8.3 Set Type Built-in Methods" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 20, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "name": "stdout", 160 | "output_type": "stream", 161 | "text": [ 162 | "False\n", 163 | "False\n", 164 | "set([1, 2, 5, 6, 9])\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "print(s.issubset(t))\n", 170 | "print(s.issuperset(t))\n", 171 | "print(s.copy())" 172 | ] 173 | } 174 | ], 175 | "metadata": { 176 | "kernelspec": { 177 | "display_name": "Python 2", 178 | "language": "python", 179 | "name": "python2" 180 | }, 181 | "language_info": { 182 | "codemirror_mode": { 183 | "name": "ipython", 184 | "version": 2 185 | }, 186 | "file_extension": ".py", 187 | "mimetype": "text/x-python", 188 | "name": "python", 189 | "nbconvert_exporter": "python", 190 | "pygments_lexer": "ipython2", 191 | "version": "2.7.15" 192 | } 193 | }, 194 | "nbformat": 4, 195 | "nbformat_minor": 2 196 | } 197 | -------------------------------------------------------------------------------- /Class 00 Introduction to the Course.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Introduction to the Course\n", 8 | "\n", 9 | "## Who I am?\n", 10 | "\n", 11 | "I have been using Python for 4 years.\n", 12 | "\n", 13 | "I mainly use Python for natuaral languagse processing (NLP) and data analysis.\n", 14 | "\n", 15 | "Some Python toolkits I am familiar with:\n", 16 | " \n", 17 | " 1. pandas\n", 18 | " \n", 19 | " 2. scikit‑learn\n", 20 | " \n", 21 | " 3. spaCy\n", 22 | "\n", 23 | "## What is this course?\n", 24 | "\n", 25 | "This course aims to help non-programmer to learn Python from scrach.\n", 26 | "\n", 27 | "This cource will cover the fundamentals of Python data structures:\n", 28 | "\n", 29 | "0. Introduction to the course\n", 30 | "\n", 31 | "1. Introduction to Python\n", 32 | "\n", 33 | "2. Python basics\n", 34 | "\n", 35 | "3. Python objects\n", 36 | "\n", 37 | "4. Numbers\n", 38 | "\n", 39 | "5. String and string format\n", 40 | "\n", 41 | "6. Lists\n", 42 | "\n", 43 | "7. Tuples\n", 44 | "\n", 45 | "8. Sets and Dictionaries\n", 46 | "\n", 47 | "9. Conditions and loops\n", 48 | "\n", 49 | "10. Files\n", 50 | "\n", 51 | "\n", 52 | "## What will you get from this course?\n", 53 | "\n", 54 | "1. How to write Python functions\n", 55 | "\n", 56 | "2. How to use the above Python data structures\n", 57 | "\n", 58 | "3. How to write simple Python scripts\n", 59 | "\n", 60 | "4. How to think in a Python way\n" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "## A Python example" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 1, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "[147600, 162500, 177800, 193500, 209600, 226100, 243000, 260300]" 79 | ] 80 | }, 81 | "execution_count": 1, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "b = [1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900]\n", 88 | "c = [123, 125, 127, 129, 131, 133, 135, 137]\n", 89 | "\n", 90 | "def get_product(list1, list2):\n", 91 | " result = []\n", 92 | " for i in range(len(list1)):\n", 93 | " result.append(list1[i] * list2[i])\n", 94 | " return result\n", 95 | "\n", 96 | "get_product(b, c)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 4, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "[147600, 162500, 177800, 193500, 209600, 226100, 243000, 260300]" 108 | ] 109 | }, 110 | "execution_count": 4, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "b = [1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900]\n", 117 | "c = [123, 125, 127, 129, 131, 133, 135, 137]\n", 118 | "\n", 119 | "\n", 120 | "d = [1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 200]\n", 121 | "e = [123, 125, 127, 129, 131, 133, 135, 137, 12]\n", 122 | "\n", 123 | "def get_product2(list1, list2):\n", 124 | " return [(list1[i] * list2[i]) for i in range(len(list1))] # list comprehension\n", 125 | "\n", 126 | "get_product2(b, c)\n", 127 | "get_product2(d, e)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": {}, 133 | "source": [ 134 | "## Python Function" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | " result = [(list1[i] * list2[i]) for i in range(len(list1))] # !! Don't write in this way" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 20, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "# In function\n", 153 | "\n", 154 | "def get_product(list1, list2): # definition\n", 155 | " \"\"\"\n", 156 | " this function calculates products from two lists\n", 157 | " :param list1: a list of int\n", 158 | " :param list2: a list of int\n", 159 | " :return: a list of int\n", 160 | " \"\"\"\n", 161 | " \n", 162 | " result = []\n", 163 | " for i in range(len(list1)): \n", 164 | " # use range as a counter\n", 165 | " result.append(list1[i] * list2[i])\n", 166 | " \n", 167 | " return result # return" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "## How we learn together?\n", 175 | "\n", 176 | "1. Read the relevant chapter before each class,\n", 177 | "\n", 178 | "2. Finish the following exercises after reading (as much as possible)\n", 179 | "\n", 180 | "3. Prepare questions to the class\n", 181 | "\n", 182 | "4. We will go through questions and exercises\n", 183 | "\n", 184 | "5. Audio recordings and exercise solution will be provided\n", 185 | "\n", 186 | "\n", 187 | "## Which book will be used in this course?\n", 188 | "\n", 189 | "Original version:\n", 190 | "\n", 191 | "[Core Python Applications Programming](https://book.douban.com/subject/7069379/)\n", 192 | "\n", 193 | "[Amazon link](https://www.amazon.cn/mn/detailApp/ref=asc_df_01326782092989178/?asin=0132678209)\n", 194 | "\n", 195 | "Chinese version:\n", 196 | "\n", 197 | "[Python核心编程(第二版)](https://book.douban.com/subject/3112503/)\n", 198 | "\n", 199 | "[Amazon link](https://www.amazon.cn/mn/detailApp/ref=asc_df_B001BKVXOA2989618/?asin=B001BKVXOA&t)\n", 200 | "\n", 201 | "\n", 202 | "## Todo for next class\n", 203 | "\n", 204 | "1. Please install Python 2.7 on your computer https://www.python.org/downloads/\n", 205 | "\n", 206 | "2. Please review this tutorial\n", 207 | "\n", 208 | "3. Please read the chapter for next class (Ch 2 in CPAP2)\n", 209 | "\n", 210 | "4. Please finish the exercises for next class\n", 211 | "\n", 212 | "__Optinoal:__ \n", 213 | "\n", 214 | "1. Please install Jupyter Notebook on your computer https://jupyter.readthedocs.io/en/latest/install.html\n", 215 | "\n", 216 | "2. Optional: Please read Ch 1 of CPAP2" 217 | ] 218 | } 219 | ], 220 | "metadata": { 221 | "kernelspec": { 222 | "display_name": "Python 2", 223 | "language": "python", 224 | "name": "python2" 225 | }, 226 | "language_info": { 227 | "codemirror_mode": { 228 | "name": "ipython", 229 | "version": 2 230 | }, 231 | "file_extension": ".py", 232 | "mimetype": "text/x-python", 233 | "name": "python", 234 | "nbconvert_exporter": "python", 235 | "pygments_lexer": "ipython2", 236 | "version": "2.7.15" 237 | } 238 | }, 239 | "nbformat": 4, 240 | "nbformat_minor": 2 241 | } 242 | -------------------------------------------------------------------------------- /Class 07 Python Dictionary.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 7 Python Dictionary\n", 8 | "\n", 9 | "## 7.1 Mapping Type: Dictrionary\n", 10 | "\n", 11 | "### Mapping\n", 12 | "\n", 13 | "> Dictionaries are the sole mapping type in Python.\n", 14 | "\n", 15 | "> Mapping objects have a one-to-many correspondence between hashable values (keys) and the objects they represent (values).\n", 16 | "\n", 17 | "### Mutability \n", 18 | "\n", 19 | "> A dictionary object itself is mutable and is yet another container type that can store any number of Python objects, including other container types.\n", 20 | "\n", 21 | "However, the keys of a dictionary should be immutable, that's why lists and other dictionaries are not allowed to be keys.\n", 22 | "\n", 23 | "### Hash tables\n", 24 | "\n", 25 | "> Sequence types use numeric keys only (numbered sequentially as indexed offsets from the beginning of the sequence). Mapping types may use most other object types as keys; strings are the most common.\n", 26 | "\n", 27 | "> Hash tables generally provide good performance because lookups occur fairly quickly once you have a key.\n", 28 | "\n", 29 | "## 7.2 Mapping Type Operators\n", 30 | "\n", 31 | "> Dictionaries will work with not support operations such as concatenation and repetition. Why?\n", 32 | "\n", 33 | "\n", 34 | "## 7.3 Mapping Type Built-in and Factory Functions\n", 35 | "\n", 36 | "\n", 37 | "### 7.3.1 Dictionary comparison algorithm\n", 38 | "\n", 39 | "> Comparisons of dictionaries are based on an algorithm that starts with sizes first, then keys, and finally values.\n", 40 | "\n", 41 | "> It is important to note here that keys that are the same will map to the same locations in the hash table. This keeps key-checking consistent.\n", 42 | "\n", 43 | "> Once the first key with nonmatching values is found, those values are compared directly.\n", 44 | "\n", 45 | "### 7.3.2 Mapping Type Related Functions\n", 46 | "\n", 47 | "> Because creating a new dictionary from an existing one using dict() is measurably slower than using copy(), we recommend using the latter.\n", 48 | "\n", 49 | "\n", 50 | "## 7.4 Mapping Type Built-in Methods\n", 51 | "\n", 52 | "The keys(), items(), and values() methods in Python 2 return lists.\n", 53 | "\n", 54 | "Always use `dict.iterkeys()`, `dict.itervalues()` and `dict.iteritems()` to extract data from a dictionary.\n", 55 | "\n", 56 | "`dict.update()` is similar to `list.append()`.\n", 57 | "\n", 58 | "## 7.5 Dictionary Keys\n", 59 | "\n", 60 | "> Multiple values per the same key are not allowed.\n", 61 | "\n", 62 | "> Rather than producing an error, Python does not check for key collisions because that would involve taking up memory for each key-value pair assigned.\n", 63 | "\n", 64 | "> The hash function used by the interpreter to calculate where to store your data is based on the value of your key.\n", 65 | "\n", 66 | "> Tuples are valid keys only if they only contain immutable arguments like numbers and strings." 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "#!/usr/bin/env python\n", 76 | "\n", 77 | "db = {}\n", 78 | "\n", 79 | "def newuser():\n", 80 | " prompt = 'login desired: '\n", 81 | " while True:\n", 82 | " name = raw_input(prompt) \n", 83 | " if db.has_key(name):\n", 84 | " prompt = 'name taken, try another: '\n", 85 | " continue\n", 86 | " else:\n", 87 | " break\n", 88 | " pwd = raw_input('passwd: ')\n", 89 | " db[name] = pwd\n", 90 | "\n", 91 | "def olduser():\n", 92 | " name = raw_input('login: ') \n", 93 | " pwd = raw_input('passwd: ') \n", 94 | " passwd = db.get(name) \n", 95 | " if passwd == pwd:\n", 96 | " print 'welcome back', name \n", 97 | " else:\n", 98 | " print 'login incorrect'\n", 99 | "\n", 100 | "def showmenu():\n", 101 | " prompt = \"\"\"\n", 102 | " (N)ew User Login\n", 103 | " (E)xisting User Login\n", 104 | " (Q)uit\n", 105 | " Enter choice: \"\"\"\n", 106 | " \n", 107 | " done = False\n", 108 | " while not done:\n", 109 | " chosen = False \n", 110 | " while not chosen:\n", 111 | " try:\n", 112 | " choice = raw_input(prompt).strip()[0].lower()\n", 113 | " except (EOFError, KeyboardInterrupt): \n", 114 | " choice = 'q' \n", 115 | " print '\\nYou picked: [%s]' % choice \n", 116 | " \n", 117 | " if choice not in 'neq':\n", 118 | " print 'invalid option, try again' \n", 119 | " else:\n", 120 | " chosen = True\n", 121 | " \n", 122 | " if choice == 'q': done = True\n", 123 | " if choice == 'n': newuser()\n", 124 | " if choice == 'e': olduser()\n", 125 | "\n", 126 | "if __name__ == '__main__':\n", 127 | " showmenu()" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 17, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "defaultdict(, {'a': [3], 'c': [3], 'b': [3]})\n", 140 | "defaultdict(, {'a': set(['3']), 'c': set(['3']), 'b': set(['3'])})\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "from collections import defaultdict\n", 146 | "\n", 147 | "def add_list_dict(ll):\n", 148 | " dl = defaultdict(list)\n", 149 | " for key in ll:\n", 150 | " dl[key].append(3)\n", 151 | " return dl\n", 152 | "\n", 153 | "print(add_list_dict(['a', 'b', 'c']))\n", 154 | "\n", 155 | "\n", 156 | "def add_list_set(ll):\n", 157 | " ds = defaultdict(set)\n", 158 | " for key in ll:\n", 159 | " ds[key].update('3')\n", 160 | " return ds\n", 161 | "\n", 162 | "print(add_list_set(['a', 'b', 'c']))" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 1, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "{'a': 4, 'c': 2, 'b': 3}\n", 175 | "('d1: ', OrderedDict([('a', 4), ('b', 3), ('c', 2)]))\n", 176 | "('d2: ', OrderedDict([('c', 2), ('b', 3), ('a', 4)]))\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "from collections import OrderedDict\n", 182 | "\n", 183 | "dd = {'c': 2, 'b': 3, 'a': 4}\n", 184 | "print(dd)\n", 185 | "\n", 186 | "d1 = OrderedDict(sorted(dd.items()))\n", 187 | "print('d1: ', d1)\n", 188 | "\n", 189 | "from operator import itemgetter\n", 190 | "d2 = OrderedDict(sorted(dd.items(), key=itemgetter(1)))\n", 191 | "print('d2: ', d2)" 192 | ] 193 | } 194 | ], 195 | "metadata": { 196 | "kernelspec": { 197 | "display_name": "Python 2", 198 | "language": "python", 199 | "name": "python2" 200 | }, 201 | "language_info": { 202 | "codemirror_mode": { 203 | "name": "ipython", 204 | "version": 2 205 | }, 206 | "file_extension": ".py", 207 | "mimetype": "text/x-python", 208 | "name": "python", 209 | "nbconvert_exporter": "python", 210 | "pygments_lexer": "ipython2", 211 | "version": "2.7.15" 212 | } 213 | }, 214 | "nbformat": 4, 215 | "nbformat_minor": 2 216 | } 217 | -------------------------------------------------------------------------------- /Class 06 Python Tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 6 Python Tuples\n", 8 | "\n", 9 | "\n", 10 | "## 6.1 Features of Tuples\n", 11 | "\n", 12 | "1. Tuples are immutable (same as numbers and strings).\n", 13 | "2. Tuples can be dictionary keys.\n", 14 | "3. Tuples can contain other Python data structures (container type).\n", 15 | "4. A single-element tuple must contain a tailing comma." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 21, 21 | "metadata": { 22 | "scrolled": true 23 | }, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "\n", 30 | "\n", 31 | "\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "tt1 = (1,)\n", 37 | "# Create an empty tuple\n", 38 | "print(type(tt1))\n", 39 | "\n", 40 | "tt2 = (1)\n", 41 | "print(type(tt2))\n", 42 | "\n", 43 | "ll1 = [1]\n", 44 | "print(type(ll1))" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "## 6.2 Special Features of Tuples\n", 52 | "\n", 53 | "1. Immutability: \n", 54 | "\n", 55 | "> A data type that is immutable simply means that once an object is defined, its value cannot be updated, unless, of course, a completely new object is allocated.\n", 56 | "\n", 57 | "2. But tuples are flexible:\n", 58 | "\n", 59 | "> Although tuple objects themselves are immutable, this fact does not preclude tuples from containing mutable objects that can be changed." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 10, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "(['xyz', 123], 23, -103.4)\n", 72 | "123\n", 73 | "(['xyz', ['abc', 'def']], 23, -103.4)\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "t = (['xyz', 123], 23, -103.4)\n", 79 | "\n", 80 | "print(t)\n", 81 | "\n", 82 | "print(t[0][1])\n", 83 | "\n", 84 | "t[0][1] = ['abc', 'def']\n", 85 | "\n", 86 | "print(t)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "3. Any function returning multiple objects (also no enclosing symbols) is a tuple." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 23, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "\n", 106 | "3\n", 107 | "2\n", 108 | "(3, 2)\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "def calculate(a, b):\n", 114 | " return a+b, a*b\n", 115 | "\n", 116 | "result = calculate(1, 2)\n", 117 | "print(type(result))\n", 118 | "\n", 119 | "sum_result, product_result = calculate(1, 2)\n", 120 | "\n", 121 | "print(sum_result, product_result)\n", 122 | "\n", 123 | "_, product_result = calculate(1, 2)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "4. Explicit grouping of parentheses for expressions or tuple creation is always recommended to avoid unpleasant side effects" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 25, 136 | "metadata": { 137 | "scrolled": true 138 | }, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "(4, True, 5)\n", 145 | "False\n", 146 | "set([0, 9, 2])\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "print(4, 2 < 3, 5)\n", 152 | "\n", 153 | "print((4, 2) < (3, 5))" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "5. Immutable objects have values that cannot be changed. That means that they will always hash to the same value." 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "## 6.3 Copying Python Objects and Shallow and Deep Copies\n", 168 | "\n", 169 | "object assignments = object references\n", 170 | "\n", 171 | "> This means that when you create an object, then assign that object to another variable, Python copies only a reference to the object.\n", 172 | "\n", 173 | "> A shallow copy of an object is defined to be a newly created object of the same type as the original object whose contents are references to the elements in the original object.\n", 174 | "\n", 175 | " (1) taking a complete slice [:], \n", 176 | " (2) using a factory function, e.g., list(), dict(), etc., \n", 177 | " (3) using the copy() function of the copy module.\n", 178 | " \n", 179 | "> When shallow copies are made, the string is explicitly copied and a new (string) object created while the list only has its reference copied, not its members.\n", 180 | "\n", 181 | "[Example](http://pythontutor.com/visualize.html#mode=display&togetherjs=6CNIwEC6L7)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 20, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "asda\n", 194 | "['asd']\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "from copy import deepcopy\n", 200 | "\n", 201 | "a = 'asda'\n", 202 | "b = a\n", 203 | "print(b)\n", 204 | "\n", 205 | "l1 = ['asd']\n", 206 | "l2 = l1\n", 207 | "l3 = deepcopy(l1)\n", 208 | "print(l3)" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "## 6.3 Summary to Sequences\n", 216 | "\n", 217 | "### 6.3.1 Features of Python sequences\n", 218 | "\n", 219 | "1. Three data types: `str`, `list` and `tuples`.\n", 220 | "2. Immuntable data type: `str` and `tuple` VS. muttable data type: `list`.\n", 221 | "3. Singular data type: `str` VS. container data type: `list` and `tuple`.\n", 222 | "\n", 223 | "### 6.3.2 Commmon operations in sequences:\n", 224 | "\n", 225 | "1. Slicing: sequence[`begin_index:end_index`]\n", 226 | "2. `in`: item `in` sequence / item `not in` sequence\n", 227 | "3. Indexing: sequence[`index`]\n", 228 | "4. Concatenation: sequence1 + sequence2\n", 229 | "5. Repetition: Squence * `int`\n", 230 | "\n", 231 | "### 6.3.3 Important operations in `str`\n", 232 | "\n", 233 | "1. `str.split`\n", 234 | "2. `str.encode`\n", 235 | "3. `str.decode`\n", 236 | "4. `str.strip`\n", 237 | "5. `str.translate`\n", 238 | "\n", 239 | "\n", 240 | "### 6.3.4 Important operations in `list`\n", 241 | "\n", 242 | "1. `list.append`\n", 243 | "2. `list.extend`" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [] 252 | } 253 | ], 254 | "metadata": { 255 | "kernelspec": { 256 | "display_name": "Python 2", 257 | "language": "python", 258 | "name": "python2" 259 | }, 260 | "language_info": { 261 | "codemirror_mode": { 262 | "name": "ipython", 263 | "version": 2 264 | }, 265 | "file_extension": ".py", 266 | "mimetype": "text/x-python", 267 | "name": "python", 268 | "nbconvert_exporter": "python", 269 | "pygments_lexer": "ipython2", 270 | "version": "2.7.15" 271 | } 272 | }, 273 | "nbformat": 4, 274 | "nbformat_minor": 2 275 | } 276 | -------------------------------------------------------------------------------- /Class 11 Python Files.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 11 Python Files\n", 8 | "\n", 9 | "## 9.1 File Objects\n", 10 | "\n", 11 | "> File objects can be used to access not only normal disk files, but also any other type of “file” that uses that abstraction.\n", 12 | "\n", 13 | "> Remember, files are simply a contiguous sequence of bytes.\n", 14 | "\n", 15 | "## 9.2 File Built-in Functions `open()` and `file()`\n", 16 | "\n", 17 | "1. Basic format\n", 18 | "\n", 19 | "`file_object = open(file_name, access_mode='r', buffering=-1)`\n", 20 | "\n", 21 | "access_mode: `r`: read, `w`: write, `a`: append, `b`: byte, `U`: universal\n", 22 | "\n", 23 | "if the file exists, `w` will remove contents inside, and write new content instead; `a` will ignore contents inside, and put new content behind.\n", 24 | "\n", 25 | "> When you use the 'U' flag to open a file, all line separators (or terminators) will be returned by Python via any file input method, i.e., read*(), as a NEWLINE character ( \\n ) regardless of what the line-endings are.\n", 26 | "\n", 27 | "> Note that UNS only applies to reading text files." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 15, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "f_name = '/Users/acepor/Work/online_course/notebook/unicode.txt'\n", 45 | "data = open(f_name, 'r')\n", 46 | "print(data)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "## 9.3 File Built-in Methods\n", 54 | "\n", 55 | "> The read() method is used to read bytes directly into a string, reading at most the number of bytes indicated.\n", 56 | "\n", 57 | "> The readlines() method does not return a string like the other two input methods. Instead, it reads all (remaining) lines and returns them as a list of strings.\n", 58 | "\n", 59 | "> Line termination characters are not inserted between each line, so if desired, they must be added to the end of each line before writelines() is called.\n", 60 | "\n", 61 | "> When reading lines in from a file using file input methods like read() or readlines(), Python does not remove the line termination characters.\n", 62 | "\n", 63 | "> ! It is possible to lose output data that is buffered if you do not explicitly close a file." 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 34, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "read: \n", 76 | "Hello world\n", 77 | "Bonjour\n", 78 | "Hola\n", 79 | "\n", 80 | "readline: \n", 81 | "Hello world\n", 82 | "\n", 83 | "readlines: \n", 84 | "['Hello world\\n', 'Bonjour\\n', 'Hola\\n']\n", 85 | "\n", 86 | "file loop:\n", 87 | "Hello world\n", 88 | "\n", 89 | "Bonjour\n", 90 | "\n", 91 | "Hola\n", 92 | "\n", 93 | "\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "data = open(f_name, 'r')\n", 99 | "print('read: ')\n", 100 | "print(data.read())\n", 101 | "data = open(f_name, 'r')\n", 102 | "print('readline: ')\n", 103 | "print(data.readline())\n", 104 | "data = open(f_name, 'r')\n", 105 | "print('readlines: ')\n", 106 | "print(data.readlines())\n", 107 | "print\n", 108 | "print('file loop:')\n", 109 | "data = open(f_name, 'r')\n", 110 | "for line in data:\n", 111 | " print(line)\n", 112 | "\n", 113 | "print\n", 114 | "with(open(f_name, 'a')) as f:\n", 115 | " f.write('Hiya')" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "## 9.4 Pandas\n", 123 | "\n", 124 | "According to the [newest Pandas doc](http://pandas.pydata.org/pandas-docs/stable/io.html), Pandas supports reading and supporting these commonly-used file format: CSV, JSON, HTML, Local clipboard, MS Excel, HDF5 Format, Feather Format, Msgpack, Stata, SAS, Python Pickle Format, SQL, and Google Big Query. If we visualize these data formats, we can have a clearer idea:\n", 125 | "\n", 126 | "![pandoc file conversion map](http://acepor.github.io/images/pandas_relations.png)\n", 127 | "\n", 128 | "A comprehensive introduction of Pandas IO tools can be found [here](http://pandas.pydata.org/pandas-docs/stable/io.html). However, in this post, we will briefly introduce using Pandas to read / write some common file format.\n", 129 | "\n", 130 | "### CSV\n", 131 | "\n", 132 | "CSV (comma-separated-value) format is one of the most common formats in data processing. It is easy for both human and machine to read.\n", 133 | "\n", 134 | "\n", 135 | "`data = pd.read_csv(in_file, quote=0, sep=',', engine='c')`\n", 136 | "\n", 137 | "\n", 138 | "`quote` is to tell which quotation convention the data uses.\n", 139 | "\n", 140 | "If the `sep` set as `None` and `engine` as 'python', this function will automatically sniff the delimiter.\n", 141 | "\n", 142 | "`c` engine is much faster (at least 50%) than `python` engine, but `python` engine supports more features\n", 143 | "\n", 144 | "`data.to_csv(out_file, header=True, index=False)`\n", 145 | "\n", 146 | "If we want to keep header and index, we can set `header` and `index` as `True`, and vice versa.\n", 147 | "\n", 148 | "### TSV\n", 149 | "\n", 150 | "TSV (tab-separated-value) format is also very common, and Pandas can process it in a similar way as CSV.\n", 151 | "\n", 152 | "`data = pd.read_table(in_file, quote=0, sep='\\t', engine='c')`\n", 153 | "\n", 154 | "### JSON\n", 155 | "\n", 156 | "JSON has gain more popularity recently. It has more controls on data, but it is not very human-friendly. Because it has a number of orients, it is quite easy to get confused. Therefore, when we use Pandas to read a JSON file, we have to specify the orient. It could be `split`, `records`, `index`, `columns` or `values`. Moreover, it the file is line-based, we can set `lines` as `True`.\n", 157 | "\n", 158 | "`data = pd.read_json(in_file, orient='records', lines=False)`\n", 159 | "\n", 160 | "`data.to_json(out_file, orient='records', lines=False)`\n", 161 | "\n", 162 | "### MySQL\n", 163 | "\n", 164 | "MySQL is one of the most popular database, and `pandas` can easily read the data from it with the help of another Python library `sqlalchemy`.\n", 165 | "\n", 166 | "First, we use sqlalchemy to make a MySQL connection.\n", 167 | "\n", 168 | "`from sqlalchemy import create_engine`\n", 169 | "\n", 170 | "`def connect_db(host):\n", 171 | " return create_engine(host)`\n", 172 | "\n", 173 | "Then, we give a SQL query to pandas, and query from the created connection. Just that simple, we can easiily get the queried result to a `pandas` Dataframe.\n", 174 | "\n", 175 | "`def mysql_df(sql, con):\n", 176 | " df = pd.read_sql_query(sql=sql, con=con)\n", 177 | " return df`\n", 178 | "\n", 179 | "## Advantages\n", 180 | "\n", 181 | "Using Pandas as a unified IO tool has two main advantages:\n", 182 | "\n", 183 | " 1. Pandas IO tools provide a significant performance increase when reading or writing data.\n", 184 | " 2. Pandas has very detailed document, so the learning curse is reduced." 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [] 193 | } 194 | ], 195 | "metadata": { 196 | "kernelspec": { 197 | "display_name": "Python 2", 198 | "language": "python", 199 | "name": "python2" 200 | }, 201 | "language_info": { 202 | "codemirror_mode": { 203 | "name": "ipython", 204 | "version": 2 205 | }, 206 | "file_extension": ".py", 207 | "mimetype": "text/x-python", 208 | "name": "python", 209 | "nbconvert_exporter": "python", 210 | "pygments_lexer": "ipython2", 211 | "version": "2.7.15" 212 | } 213 | }, 214 | "nbformat": 4, 215 | "nbformat_minor": 2 216 | } 217 | -------------------------------------------------------------------------------- /Class 09 Python Conditions and Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 9 Python Conditions and Loops\n", 8 | "\n", 9 | "## 9.1 `if` Statement\n", 10 | "\n", 11 | "1. Grammar \n", 12 | "\n", 13 | "`if expression:\n", 14 | " expr_true_suite`\n", 15 | " \n", 16 | "2. The `True` condition can be omitted:" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 21, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "11\n", 29 | "11\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "x = 9\n", 35 | "\n", 36 | "if (x > 3) is True:\n", 37 | " y = x + 2\n", 38 | " print(y)\n", 39 | " \n", 40 | "if x > 3:\n", 41 | " y = x + 2\n", 42 | " print(y)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "3. The Boolean operators and, or, and not can be used to provide multiple conditional expressions or perform negation of expressions in the same if statement." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 22, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "3\n", 62 | "3\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "x = 1\n", 68 | "\n", 69 | "if (x > 3) is not True:\n", 70 | " y = x + 2\n", 71 | " print(y)\n", 72 | " \n", 73 | "if not x > 3:\n", 74 | " y = x + 2\n", 75 | " print(y)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "## 9.2 `else` Statement\n", 83 | "\n", 84 | "1. Grammar\n", 85 | "\n", 86 | "`if expression:\n", 87 | " expr_true_suite\n", 88 | " else:\n", 89 | " expr_false_suite`\n", 90 | "\n", 91 | "2. The else statement identifies a block of code to be executed if the conditional expression of the if statement resolves to a false Boolean value.\n", 92 | "\n", 93 | "\n", 94 | "## 9.3 `elif` Statement\n", 95 | "\n", 96 | "1. Grammar\n", 97 | "\n", 98 | "`if expression1:\n", 99 | " expr1_true_suite \n", 100 | " elif expression2:\n", 101 | " expr2_true_suite\n", 102 | " :\n", 103 | " elif expressionN:\n", 104 | " exprN_true_suite\n", 105 | " else:\n", 106 | " none_of_the_above_suite`\n", 107 | "\n", 108 | "2. There can be an arbitrary number of elif statements following an if.\n", 109 | "\n", 110 | "3. The last statement within an `elif` suite can also start with `elif`.\n", 111 | "\n", 112 | "4. We can often replace `elif` structure with a python data type, e.g.: a dictionary." 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 4, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "1\n", 125 | "1\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "x = 1\n", 131 | " \n", 132 | "if x > 3:\n", 133 | " y = x + 2\n", 134 | "else:\n", 135 | " y = x\n", 136 | "\n", 137 | "print(y)\n", 138 | " \n", 139 | "y = x + 2 if x > 3 else x\n", 140 | "\n", 141 | "print(y)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 1, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "create item\n", 154 | "create item\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "cmd = 'create'\n", 160 | "\n", 161 | "if cmd == 'create':\n", 162 | " action = \"create item\"\n", 163 | "elif cmd == 'delete':\n", 164 | " action = 'delete item'\n", 165 | "elif cmd == 'update':\n", 166 | " action = 'update item'\n", 167 | "\n", 168 | "############\n", 169 | "\n", 170 | "cmd_dict = {'create': 'create item',\n", 171 | " 'delete': 'delete item',\n", 172 | " 'update': 'update item'}\n", 173 | "\n", 174 | "command = 'create'\n", 175 | "\n", 176 | "if command in cmd_dict.keys():\n", 177 | " print(cmd_dict[command])\n", 178 | "else:\n", 179 | " print('invalid choice... try again!')\n", 180 | " \n", 181 | "print(cmd_dict[command]) if command in cmd_dict.keys() else 'invalid choice... try again!'" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "## 9.4 Conditional Expressions\n", 189 | "\n", 190 | "1. Grammar\n", 191 | "\n", 192 | "`do something if True else do somethine else`\n", 193 | "\n", 194 | "`print(cmd_dict[command]) if command in cmd_dict.keys() else 'invalid choice... try again!'`\n", 195 | "\n", 196 | "## 9.5 `while` Statement\n", 197 | "\n", 198 | "1. Grammar\n", 199 | "\n", 200 | "`while expression:\n", 201 | " suite_to_repeat`\n", 202 | " \n", 203 | "2. Counting loop:\n", 204 | "\n", 205 | "`count = 0 \n", 206 | " while (count < 9):\n", 207 | " print 'the index is:', count \n", 208 | " count += 1`\n", 209 | " \n", 210 | "__Using `for` loop to replace the counting loop:__\n", 211 | "\n", 212 | "`for count in range(0,9):\n", 213 | " print('the index is:', count)\n", 214 | "`\n", 215 | " \n", 216 | "3. Infinite loop:\n", 217 | "\n", 218 | "`while True:\n", 219 | " handle, indata = wait_for_client_connect() \n", 220 | " outdata = process_request(indata) \n", 221 | " ack_result_to_client(handle, outdata)`\n", 222 | "\n", 223 | "\n", 224 | "## 9.6 `for` Statement\n", 225 | "\n", 226 | "1. Grammar\n", 227 | "\n", 228 | "`for iter_var in iterable:\n", 229 | " suite_to_repeat`\n", 230 | " \n", 231 | " \n", 232 | "## 9.7 `range` Statement\n", 233 | "\n", 234 | "1. Grammar\n", 235 | "\n", 236 | "`range(start, end, step=2)\n", 237 | " range(end)\n", 238 | " range(start, end)\n", 239 | "`\n", 240 | "\n", 241 | "2. In Python 2, we prefer using `xrange()` than `range()` as the previous one is an iterator. Python 3 replaces `xrange()` with `range()`, so we can safely use `range()` in Python 3.\n", 242 | "\n", 243 | "\n", 244 | "## 9.8 `break` Statement\n", 245 | "\n", 246 | "1. The `break` statement in Python terminates the current loop and resumes execution at the next statement." 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 6, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "name": "stdout", 256 | "output_type": "stream", 257 | "text": [ 258 | "5 is the largest factor of 10\n" 259 | ] 260 | } 261 | ], 262 | "source": [ 263 | "num = 10\n", 264 | "count = num / 2 \n", 265 | "while count > 0:\n", 266 | " if num % count == 0:\n", 267 | " print count, 'is the largest factor of', num \n", 268 | " break \n", 269 | " count -= 1" 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": {}, 275 | "source": [ 276 | "## 9.9 `continue` Statement\n", 277 | "\n", 278 | "1. Rather than beginning the next iteration of the loop when a continue statement is encountered, a continue statement terminates or discards the remaining statements in the current loop iteration and goes back to the top." 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": null, 284 | "metadata": {}, 285 | "outputs": [], 286 | "source": [ 287 | "valid = False \n", 288 | "count = 3 \n", 289 | "while count > 0:\n", 290 | " input = raw_input(\"enter password\") # check for valid passwd \n", 291 | " for eachPasswd in passwdList:\n", 292 | " if input == eachPasswd: \n", 293 | " valid = True \n", 294 | " break \n", 295 | " if not valid:\n", 296 | " print \"invalid input\"\n", 297 | " count -= 1\n", 298 | " continue\n", 299 | " else:\n", 300 | " break" 301 | ] 302 | } 303 | ], 304 | "metadata": { 305 | "kernelspec": { 306 | "display_name": "Python 2", 307 | "language": "python", 308 | "name": "python2" 309 | }, 310 | "language_info": { 311 | "codemirror_mode": { 312 | "name": "ipython", 313 | "version": 2 314 | }, 315 | "file_extension": ".py", 316 | "mimetype": "text/x-python", 317 | "name": "python", 318 | "nbconvert_exporter": "python", 319 | "pygments_lexer": "ipython2", 320 | "version": "2.7.15" 321 | } 322 | }, 323 | "nbformat": 4, 324 | "nbformat_minor": 2 325 | } 326 | -------------------------------------------------------------------------------- /Class 02 Python Basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 2 Python Basics\n", 8 | "\n", 9 | "## 2.1 Statements and Syntax\n", 10 | "\n", 11 | "### 2.1.1 Continuation\n", 12 | "\n", 13 | "> A single statement can take up more than one line when enclosing operators are used, i.e., parentheses, square brackets, or braces, and when NEWLINEs are contained in strings enclosed in triple quotes.\n", 14 | "\n", 15 | "The length of one line should be no more than 80 characters.\n", 16 | "\n", 17 | "### 2.1.2 Spaces VS. tabs\n", 18 | "\n", 19 | "Use 4 spaces instead of a tab to be the indentation.\n", 20 | "\n", 21 | "> Also, because tabs vary in the number of spaces depending on your system, we recommend not using tabs if there is any hint of cross-platform development.\n", 22 | "\n", 23 | "### 2.1.3 Multiple statements\n", 24 | "\n", 25 | "Do not put multiple statements in a single line.\n", 26 | "\n", 27 | "\n", 28 | "## 2.2 Variable Assignment\n", 29 | "\n", 30 | "### 2.2.1 Augmented Assignment\n", 31 | "\n", 32 | "> += <<= -= >>= \\*= &= /= ^= %= |= **=\n", 33 | "\n", 34 | "### 2.2.2 “Multuple” Assignment\n", 35 | "\n", 36 | "> Obviously, Python performs evaluation before making assignments.\n" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 24, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "('x=', 1, 'y=', 2, 'z=', 'a string')\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "x, y, z = 1, 2, 'a string'\n", 54 | "print('x=', x, 'y=', y, 'z=', z)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 25, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "('x=', 'a string', 'y=', 1, 'z=', 2)\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "x, y, z = z, x, y\n", 72 | "print('x=', x, 'y=', y, 'z=', z)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "## 2.3 Identifiers\n", 80 | "\n", 81 | "### 2.3.1 Valid Python Identifiers\n", 82 | "\n", 83 | "> • First character must be a letter or underscore ( _ )\n", 84 | "\n", 85 | "> • Any additional characters can be alphanumeric or underscore\n", 86 | "\n", 87 | "> • Case-sensitive\n", 88 | "\n", 89 | "> • No identifiers can begin with a number, and no symbols other than the underscore are ever allowed.\n", 90 | "\n", 91 | "\n", 92 | "### 2.3.2 Special Underscore Identifiers\n", 93 | "\n", 94 | "Avoid beginning variable names with the underscore.\n", 95 | "\n", 96 | "> • \\_xxx Do not import with 'from module import *' \n", 97 | "\n", 98 | "> • \\__xxx\\__ System-defined name \n", 99 | "\n", 100 | "> • \\__xxx Request private name mangling in classes\n", 101 | "\n", 102 | "\n", 103 | "## 2.4 Basic Style Guidelines\n", 104 | "\n", 105 | "Follow [PEP8](https://www.python.org/dev/peps/pep-0008/), [pep8.org](http://pep8.org/)\n", 106 | "\n", 107 | "\n", 108 | "### 2.4.1 Module Structure and Layout\n", 109 | "\n", 110 | "__It is important to bear in mind that most modules are created solely to be imported rather than to execute as scripts.__\n", 111 | "\n", 112 | "(1) startup line (Unix) \n", 113 | "\n", 114 | "(2) module documentation \n", 115 | "\n", 116 | "(3) module imports \n", 117 | "\n", 118 | "(4) variable declarations \n", 119 | "\n", 120 | "(5) class declarations\n", 121 | "\n", 122 | "(6) function declarations \n", 123 | "\n", 124 | "(7) \"main\" body\n", 125 | "\n", 126 | "### 2.4.2 import ... VS from ... import ...\n", 127 | "\n", 128 | "import ... : import the entire library\n", 129 | "\n", 130 | "import ... as ... : import a libray as abbreviation\n", 131 | "\n", 132 | "from ... import ... import the specific class / function from a library" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 18, 138 | "metadata": { 139 | "collapsed": true 140 | }, 141 | "outputs": [], 142 | "source": [ 143 | "import datetime\n", 144 | "\n", 145 | "import datetime as dt\n", 146 | "\n", 147 | "from collections import defaultdict" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "## 2.5 Memory Management\n", 155 | "\n", 156 | "### 2.5.1 Dynamic Typing\n", 157 | "\n", 158 | "> In Python, the type and memory space for an object are determined and allocated at runtime.\n", 159 | "\n", 160 | "### 2.5.2 Garbage Collection\n", 161 | "\n", 162 | "> The garbage collector is a separate piece of code that looks for objects with reference counts of zero.\n", 163 | "\n", 164 | "## 2.6 The first Python Program" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 19, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "Enter file name: as\n", 177 | "\n", 178 | "Enter lines ('.' by itself to quit).\n", 179 | "\n", 180 | "> .\n", 181 | "DONE!\n" 182 | ] 183 | } 184 | ], 185 | "source": [ 186 | "#!/usr/bin/env python\n", 187 | "\n", 188 | "'makeTextFile.py -- create text file' \n", 189 | "\n", 190 | "import os \n", 191 | "\n", 192 | "# get filename \n", 193 | "\n", 194 | "def evaluate_file():\n", 195 | " while True:\n", 196 | "\n", 197 | " fname = raw_input('Enter file name: ')\n", 198 | "# if os.path.exists(fname):\n", 199 | "# print(\"ERROR: '%s' already exists\" % fname)\n", 200 | "\n", 201 | " try:\n", 202 | " os.path.exists(fname):\n", 203 | " except IOError, e:\n", 204 | " print 'file existed'\n", 205 | " \n", 206 | " \n", 207 | " else:\n", 208 | " break\n", 209 | " return fname\n", 210 | " \n", 211 | "# get file content (text) lines\n", 212 | "\n", 213 | "def get_contetnt():\n", 214 | " all = []\n", 215 | " print(\"\\nEnter lines ('.' by itself to quit).\\n\")\n", 216 | "\n", 217 | " # loop until user terminates input \n", 218 | "\n", 219 | " while True:\n", 220 | " entry = raw_input('> ')\n", 221 | " if entry == '.':\n", 222 | " break\n", 223 | " else:\n", 224 | " all.append(entry)\n", 225 | " return all\n", 226 | "\n", 227 | "# write lines to file with proper line-ending\n", 228 | "\n", 229 | "def write_file(fname, content):\n", 230 | " fobj = open(fname, 'w')\n", 231 | " fobj.write('\\n'.join(content))\n", 232 | " fobj.close()\n", 233 | " print('DONE!')\n", 234 | " \n", 235 | " \n", 236 | "if __name__ == '__main__':\n", 237 | " evaluate_file()\n", 238 | " get_content()\n", 239 | " write_file()" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 21, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "Enter filename: as\n", 252 | "\n" 253 | ] 254 | } 255 | ], 256 | "source": [ 257 | "#!/usr/bin/env python\n", 258 | "\n", 259 | "'readTextFile.py -- read and display text file' \n", 260 | "\n", 261 | "# get filename\n", 262 | "\n", 263 | "fname = raw_input('Enter filename: ')\n", 264 | "\n", 265 | "print\n", 266 | "\n", 267 | "# attempt to open file for reading\n", 268 | "\n", 269 | "try:\n", 270 | " fobj = open(fname, 'r')\n", 271 | "except IOError, e:\n", 272 | " print \"*** file open error:\", e\n", 273 | "else:\n", 274 | " # display contents to the screen\n", 275 | " for eachLine in fobj:\n", 276 | " print eachLine\n", 277 | " fobj.close()" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": { 284 | "collapsed": true 285 | }, 286 | "outputs": [], 287 | "source": [] 288 | } 289 | ], 290 | "metadata": { 291 | "kernelspec": { 292 | "display_name": "Python 2", 293 | "language": "python", 294 | "name": "python2" 295 | }, 296 | "language_info": { 297 | "codemirror_mode": { 298 | "name": "ipython", 299 | "version": 2 300 | }, 301 | "file_extension": ".py", 302 | "mimetype": "text/x-python", 303 | "name": "python", 304 | "nbconvert_exporter": "python", 305 | "pygments_lexer": "ipython2", 306 | "version": "2.7.15" 307 | } 308 | }, 309 | "nbformat": 4, 310 | "nbformat_minor": 2 311 | } 312 | -------------------------------------------------------------------------------- /Class 10 Python Comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 10 Pytyhon Comprehensions\n", 8 | "\n", 9 | "## 10.1 Iterators and `iter()` Function\n", 10 | "\n", 11 | "1. an iterator is any item that has a `next()` method.\n", 12 | "\n", 13 | "> Iterators come in handy when you are iterating over something that is not a sequence but exhibits behavior that makes it seem like a sequence, for example, keys of a dictionary, lines of a file, etc.\n", 14 | "\n", 15 | "2. If you want to iterate over the same objects again (or simultaneously), you have to create another iterator object.\n", 16 | "\n", 17 | "3. Iterators can work with most Python data types: list, str, dict, tuple, set, etc\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 3, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "ename": "NameError", 27 | "evalue": "name 'do_something_to' is not defined", 28 | "output_type": "error", 29 | "traceback": [ 30 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 31 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 32 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mseq\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mdo_something_to\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;31m#############################\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 33 | "\u001b[0;31mNameError\u001b[0m: name 'do_something_to' is not defined" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "seq = [5, 3, 2, 8]\n", 39 | "\n", 40 | "for i in seq:\n", 41 | " do_something_to(i)\n", 42 | " \n", 43 | "############################# \n", 44 | " \n", 45 | "fetch = iter(seq)\n", 46 | "while True:\n", 47 | " try:\n", 48 | " i = fetch.next() \n", 49 | " except StopIteration:\n", 50 | " break \n", 51 | " do_something_to(i)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "## 10.2 list comprehensions\n", 59 | "\n", 60 | "1. Basic format\n", 61 | "\n", 62 | "`[expr for iter_var in iterable]`\n", 63 | "\n", 64 | "Which equals to:\n", 65 | "\n", 66 | "`result_list = []\n", 67 | "for iter_vae in iterable:\n", 68 | " result = expr\n", 69 | " result_list.append(result)`\n", 70 | " \n", 71 | "2. Extended synatax with `if`\n", 72 | "\n", 73 | "`[expr for iter_var in iterable if cond_expr]`\n", 74 | "\n", 75 | "`[expr for iter_var in iterable if not cond_expr]`\n", 76 | "\n", 77 | "\n", 78 | "\n", 79 | "Which equals to:\n", 80 | "\n", 81 | "`for iter_vae in iterable:\n", 82 | " if cond_expr:\n", 83 | " expr`\n", 84 | " \n", 85 | "3. Two iterables:\n", 86 | "\n", 87 | "`[expr for iter_var1 in iterable1 for iter_var2 in iterable2]`\n", 88 | "\n", 89 | "Which equals to:\n", 90 | "\n", 91 | "`for iter_vae1 in iterable1:\n", 92 | " for iter_vae2 in iterable2:\n", 93 | " expr`" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 1, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "[6, 12, 18, 9, 27]\n", 106 | "[4, 3, 9]\n", 107 | "[4, 10, 12, 14, 16, 8, 20, 24, 28, 32, 12, 30, 36, 42, 48, 6, 15, 18, 21, 24, 18, 45, 54, 63, 72]\n", 108 | "[4, 20, 36, 21, 72]\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "ll = [2, 4, 6, 3, 9]\n", 114 | "kk = [2, 5, 6, 7, 8]\n", 115 | "\n", 116 | "print([i * 3 for i in ll])\n", 117 | "\n", 118 | "print([i for i in ll if i not in kk])\n", 119 | "\n", 120 | "print([i * j for i in ll for j in kk])\n", 121 | "\n", 122 | "print([i * j for i, j in zip(ll, kk)])" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "## 10.3 Dictionary comprehensions\n", 130 | "\n", 131 | "1. Basic format\n", 132 | "\n", 133 | "`{expr for (key, value) in dic.items()}`\n", 134 | "\n", 135 | "`{expr for (key, value) in dic.iteritems()}`\n", 136 | "\n", 137 | "\n", 138 | "Which equals to:\n", 139 | "\n", 140 | "`for key, value in dic.items():\n", 141 | " expr`\n", 142 | " \n", 143 | "2. Extended synatax with `if`\n", 144 | "\n", 145 | "`{expr for (key, value) in dic.items() if cond_expr}`\n", 146 | "\n", 147 | "Which equals to:\n", 148 | "\n", 149 | "`for key, value in dic.items():\n", 150 | " if cond_expr:\n", 151 | " expr`\n", 152 | " \n", 153 | "3. Two iterables:\n", 154 | "\n", 155 | "`{expr for (key1, value1) in dic1.items() for (key2, value2) in dic2.items()}`\n", 156 | "\n", 157 | "Which equals to:\n", 158 | "\n", 159 | "`for key1, value1 in dic1.items():\n", 160 | " for key2, value2 in dic2.items()\n", 161 | " expr`" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 2, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "{'a': 12, 'h': 36, 'q': 54, 'g': 24, 'p': 18}\n", 174 | "{'q': 1, 'p': 0}\n", 175 | "{'a': 4, 'h': 12, 'q': 18, 'g': 8, 'p': 6}\n" 176 | ] 177 | } 178 | ], 179 | "source": [ 180 | "dd = {'a': 2, 'g': 4, 'h': 6, 'p': 3, 'q': 9}\n", 181 | "ff = {'f': 2, 's': 5, 't': 6, 'p': 7, 'q': 8}\n", 182 | "\n", 183 | "print({k: v*6 for (k, v) in dd.iteritems()})\n", 184 | "\n", 185 | "print({k: v//5 for (k, v) in dd.iteritems() if k in ff.iterkeys()})\n", 186 | "\n", 187 | "print({k1: v1 * v2 for (k1, v1) in dd.iteritems() for (k2, v2) in ff.iteritems() })" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "## 10.4 Set comprehensions\n", 195 | "\n", 196 | "They are similar to list comprehensions\n", 197 | "\n", 198 | "`{expr for item in set1}`\n", 199 | "\n", 200 | "## 10.5 Generator and Lazy computing\n", 201 | "\n", 202 | "1. Lazy computing: the computing is only carried out in excution, not in defining.\n", 203 | "\n", 204 | "> A generator is a specialized function that allows you to return a value and “pause” the execution of that code and resume it at a later time.\n", 205 | "\n", 206 | "> Generator expressions are much more memory efficient by performing “lazy evaluation.”\n", 207 | "\n", 208 | "2. Basic format:\n", 209 | "\n", 210 | "list comprehension: `[expr for iter_var in iterable]`\n", 211 | "\n", 212 | "\n", 213 | "generator comprehension: `(expr for iter_var in iterable if cond_expr)`\n", 214 | "\n", 215 | "3. Genarator can be only computed once." 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 8, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "[2, 4, 6, 3, 9]\n", 228 | " at 0x108de02d0>\n", 229 | "[4, 3, 9]\n", 230 | " at 0x108de0690>\n", 231 | "[4, 10, 12, 14, 16, 8, 20, 24, 28, 32, 12, 30, 36, 42, 48, 6, 15, 18, 21, 24, 18, 45, 54, 63, 72]\n", 232 | " at 0x108de0690>\n", 233 | "[4, 20, 36, 21, 72]\n", 234 | " at 0x108de02d0>\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "ll = [2, 4, 6, 3, 9]\n", 240 | "kk = [2, 5, 6, 7, 8]\n", 241 | "\n", 242 | "print([i for i in ll])\n", 243 | "print(i for i in ll)\n", 244 | "\n", 245 | "print([i for i in ll if i not in kk])\n", 246 | "print(i for i in ll if i not in kk)\n", 247 | "\n", 248 | "print([i * j for i in ll for j in kk])\n", 249 | "print(i * j for i in ll for j in kk)\n", 250 | "\n", 251 | "print([i * j for i, j in zip(ll, kk)])\n", 252 | "print(i * j for i, j in zip(ll, kk))" 253 | ] 254 | } 255 | ], 256 | "metadata": { 257 | "kernelspec": { 258 | "display_name": "Python 2", 259 | "language": "python", 260 | "name": "python2" 261 | }, 262 | "language_info": { 263 | "codemirror_mode": { 264 | "name": "ipython", 265 | "version": 2 266 | }, 267 | "file_extension": ".py", 268 | "mimetype": "text/x-python", 269 | "name": "python", 270 | "nbconvert_exporter": "python", 271 | "pygments_lexer": "ipython2", 272 | "version": "2.7.15" 273 | } 274 | }, 275 | "nbformat": 4, 276 | "nbformat_minor": 2 277 | } 278 | -------------------------------------------------------------------------------- /Class 01 Introduction to Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Introduction to Python\n", 10 | "\n", 11 | "## 2.1 Program Output, the print statment and 'hello world!'\n", 12 | "\n", 13 | "### 2.1.1 Difference between `print` and expression" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 3, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "Hello World!\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | ">>> myString = 'Hello World!'\n", 31 | "\n", 32 | ">>> print(myString)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 24, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "'Hello World!'" 44 | ] 45 | }, 46 | "execution_count": 24, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | ">>> myString" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "### 2.1.2 Usage of `_`\n", 60 | "\n", 61 | "The underscore (\\_) also has special meaning in the interactive interpreter: the last evaluated expression." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 25, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "'Hello World!'" 73 | ] 74 | }, 75 | "execution_count": 25, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | ">>> _" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## 2.2 Program Input and the raw_input() Built-in Function\n", 89 | "\n", 90 | "### 2.2.1 Get function info by using `help`." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 26, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "Help on method raw_input in module ipykernel.kernelbase:\n", 103 | "\n", 104 | "raw_input(self, prompt='') method of ipykernel.ipkernel.IPythonKernel instance\n", 105 | " Forward raw_input to frontends\n", 106 | " \n", 107 | " Raises\n", 108 | " ------\n", 109 | " StdinNotImplentedError if active frontend doesn't support stdin.\n", 110 | "\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | ">>> help(raw_input) " 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "### 2.2.2 Do not be addicted to `print`\n", 123 | "\n", 124 | "1. remove all `print` in production code;\n", 125 | "2. use `info` instead.\n", 126 | "3. separate functions into two categories: \n", 127 | " 1. those that do things (i.e. , interact with the user or set variables)\n", 128 | " 2. those that calculate things (usually returning results)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": { 135 | "collapsed": true 136 | }, 137 | "outputs": [], 138 | "source": [ 139 | "def cal(list1):\n", 140 | " result = []\n", 141 | " for i in list1:\n", 142 | " for j in i:\n", 143 | " y = j/0.9712\n", 144 | " result.append(str(y **3))\n", 145 | " \n", 146 | " return result\n", 147 | "\n", 148 | "\n", 149 | "#########\n", 150 | " \n", 151 | "\n", 152 | "def simple_calc(j):\n", 153 | " result = []\n", 154 | " y = j/0.9712\n", 155 | " result.append(str(y **3)))\n", 156 | " return result\n", 157 | "\n", 158 | "def comlex_calc(j):\n", 159 | " final = []\n", 160 | " for i in list1:\n", 161 | " for j in i:\n", 162 | " final.append(simple_calc(j))" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "## 2.4 Operators\n", 170 | "\n", 171 | "### 2.4.1 Difference of `/` and `//`\n", 172 | "\n", 173 | "`/`: division\n", 174 | "\n", 175 | "`//`: floor division\n", 176 | "\n", 177 | "### 2.4.1 Difference of `=` and `==`\n", 178 | "\n", 179 | "`=`: assignment\n", 180 | "\n", 181 | "`==`: is equal to\n", 182 | "\n", 183 | "### 2.4.1 Difference of `*` and `**`\n", 184 | "\n", 185 | "`*`: product\n", 186 | "\n", 187 | "`**`: power\n", 188 | "\n", 189 | "\n", 190 | "## 2.5 Variables and Assignment\n", 191 | "\n", 192 | "### 2.5.1 Python is dynamically typed\n", 193 | "\n", 194 | "1. Python is flexible to use.\n", 195 | "2. Python is slow for interpretor to interprete (compare to C).\n", 196 | "\n", 197 | "\n", 198 | "## 2.6 Numbers\n", 199 | "\n", 200 | "### 2.6.1 Python starts with `0`\n", 201 | "\n", 202 | "The first element in any Python object starts with 0, not 1.\n", 203 | "\n", 204 | "\n", 205 | "## 2.7 Strings\n", 206 | "\n", 207 | "### Difference between `6` and `'6'`\n", 208 | "\n", 209 | "6: int\n", 210 | "\n", 211 | "'6': str\n", 212 | "\n", 213 | "\"6\": str\n", 214 | "\n", 215 | "## 2.9 Dictionaries\n", 216 | "\n", 217 | "### 2.9.1 Dictionaries are special\n", 218 | "\n", 219 | "We cannot use slicing to get a element in dictionaries." 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 30, 225 | "metadata": {}, 226 | "outputs": [ 227 | { 228 | "data": { 229 | "text/plain": [ 230 | "[1, 2, 3, 7, 8]" 231 | ] 232 | }, 233 | "execution_count": 30, 234 | "metadata": {}, 235 | "output_type": "execute_result" 236 | } 237 | ], 238 | "source": [ 239 | "[1, 2, 3, 7, 8][0:]" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 32, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "1" 251 | ] 252 | }, 253 | "execution_count": 32, 254 | "metadata": {}, 255 | "output_type": "execute_result" 256 | } 257 | ], 258 | "source": [ 259 | "{'a': 1, 'b': 2, 'c': 3, 'd': 7, 'e':8}['a']" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 34, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "data": { 269 | "text/plain": [ 270 | "'c'" 271 | ] 272 | }, 273 | "execution_count": 34, 274 | "metadata": {}, 275 | "output_type": "execute_result" 276 | } 277 | ], 278 | "source": [ 279 | "{'a': 1, 'b': 2, 'c': 3, 'd': 7, 'e':8}.keys()[1] # dictionaries do't keep order" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "## 2.11, 2.12 `if` loop VS. `while` loop\n", 287 | "\n", 288 | "\n", 289 | "1. If the expression is non-zero or True, the if_suite is executed; then execution continues on the first statement after.\n", 290 | "\n", 291 | "`if expression: \n", 292 | " if_suite`\n", 293 | "\n", 294 | "\n", 295 | "2. The statement while_suite is executed continuously in a loop until the expression becomes zero or false; execution then continues on the first succeeding statement.\n", 296 | "\n", 297 | "`while expression:\n", 298 | " while_suite`\n", 299 | " \n", 300 | "3. Avoid using `while` loop whenever possible.\n", 301 | "\n", 302 | "## 2.14 Comprehension\n", 303 | "\n", 304 | "\n", 305 | "### 2.14.1 List comprehension" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 37, 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "l1 = [2, 4, 7]\n", 315 | "result = []\n", 316 | "for i in l1:\n", 317 | " result.append(i * 2)\n", 318 | "\n", 319 | "\n", 320 | "l1 = [i*2 for i in [2, 4, 7]]" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "### 2.14.2 Dicionary comprehension" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": 38, 333 | "metadata": {}, 334 | "outputs": [], 335 | "source": [ 336 | "d1 = {str(i): i*2 for i in [2, 4, 7]}" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "### 2.14.3 Set comprehension" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 36, 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "s1 = {i*2 for i in [2, 4, 7, 7]}" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": {}, 358 | "source": [ 359 | "### 2.14.4 Generator comprehension" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 39, 365 | "metadata": {}, 366 | "outputs": [], 367 | "source": [ 368 | "g1 = (i*2 for i in [2, 4, 7])" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "metadata": { 375 | "collapsed": true 376 | }, 377 | "outputs": [], 378 | "source": [] 379 | } 380 | ], 381 | "metadata": { 382 | "kernelspec": { 383 | "display_name": "Python 2", 384 | "language": "python", 385 | "name": "python2" 386 | }, 387 | "language_info": { 388 | "codemirror_mode": { 389 | "name": "ipython", 390 | "version": 2 391 | }, 392 | "file_extension": ".py", 393 | "mimetype": "text/x-python", 394 | "name": "python", 395 | "nbconvert_exporter": "python", 396 | "pygments_lexer": "ipython2", 397 | "version": "2.7.15" 398 | } 399 | }, 400 | "nbformat": 4, 401 | "nbformat_minor": 2 402 | } 403 | -------------------------------------------------------------------------------- /Class 05 Python Lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 5 Python Lists\n", 8 | "\n", 9 | "Lists can store other Python objects with different hierarchy, which make the list a nested list.\n", 10 | "\n", 11 | "Design wisely when you use a nested list, as it may bring serious performance inefficiency.\n", 12 | "\n", 13 | "> Lists are flexible container objects that hold an arbitrary number of Python objects.\n", 14 | "\n", 15 | "> The objects that you can place in a list can include standard types and objects as well as user-defined ones.\n", 16 | "\n", 17 | "## 5.1 Operators\n", 18 | "\n", 19 | "### 5.1.1 Accessing items in lists\n", 20 | "\n", 21 | "Similar to slicing as in `str`.\n", 22 | "\n", 23 | "Be careful of the hierarchy when accessing an item in a nested list." 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "'x'" 35 | ] 36 | }, 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "mixup_list = [4.0, [1, 'x'], 'beef', -1.9+6j]\n", 44 | "mixup_list[1][1]" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "### 5.1.2. Concatenation\n", 52 | "\n", 53 | "> In other words, you can concatenate only objects of the same type.\n", 54 | "\n", 55 | "> Using `extend()` is advantageous over concatenation because it actually appends the elements of the new list to the original, rather than creating a new list from scratch like `+` does.\n", 56 | "\n", 57 | "### 5.1.3 Other operators" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 6, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "42\n", 70 | "2\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "ll = [12, 42, 2, 9]\n", 76 | "print(max(ll))\n", 77 | "print(min(ll))" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 18, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "[42, 12]\n", 90 | "[2, 9, 12]\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "from heapq import nlargest, nsmallest\n", 96 | "print(nlargest(2, ll))\n", 97 | "print(nsmallest(3, ll))" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "## 5.2 List Type Built-in Methods" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 50, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "[12, 42, 2, 9, 89]\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "ll = [12, 42, 2, 9]\n", 122 | "ll.append(89)\n", 123 | "print(ll)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 51, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "[14, 44, 4, 11, 91]\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "lll = []\n", 141 | "for i in ll:\n", 142 | " lll.append(i+2)\n", 143 | "print(lll)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 52, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "1" 155 | ] 156 | }, 157 | "execution_count": 52, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "ll.count(12)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 53, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "[12, 42, 2, 9, 89, 14, 44, 4, 11, 91]\n" 176 | ] 177 | } 178 | ], 179 | "source": [ 180 | "ll.extend(lll)\n", 181 | "print(ll)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 54, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "[12, 42, 2, 'a', 9, 89, 14, 44, 4, 11, 91]\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "ll.insert(3, 'a')\n", 199 | "print(ll)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 55, 205 | "metadata": {}, 206 | "outputs": [ 207 | { 208 | "name": "stdout", 209 | "output_type": "stream", 210 | "text": [ 211 | "[91, 11, 4, 44, 14, 89, 9, 'a', 2, 42, 12]\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "ll.reverse()\n", 217 | "print(ll)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 56, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "[2, 4, 9, 11, 12, 14, 42, 44, 89, 91, 'a']\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "ll.sort()\n", 235 | "print(ll)" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 57, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "9" 247 | ] 248 | }, 249 | "execution_count": 57, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "ll.pop(2)" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 58, 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [ 264 | "ll.remove('a')" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": {}, 270 | "source": [ 271 | "## 5.3 Special Features of Lists" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [ 279 | { 280 | "name": "stdout", 281 | "output_type": "stream", 282 | "text": [ 283 | " \n", 284 | " p(U)sh \n", 285 | " p(O)p\n", 286 | " (V)iew\n", 287 | " (Q)uit\n", 288 | " Enter choice:\n", 289 | " u\n", 290 | "\n", 291 | "You picked: [u]\n", 292 | "Enter new string: asd\n", 293 | " \n", 294 | " p(U)sh \n", 295 | " p(O)p\n", 296 | " (V)iew\n", 297 | " (Q)uit\n", 298 | " Enter choice:\n", 299 | " o\n", 300 | "\n", 301 | "You picked: [o]\n", 302 | "Removed [ stack.pop() ]\n", 303 | " \n", 304 | " p(U)sh \n", 305 | " p(O)p\n", 306 | " (V)iew\n", 307 | " (Q)uit\n", 308 | " Enter choice:\n", 309 | " s\n", 310 | "\n", 311 | "You picked: [s]\n", 312 | "Invalid option, try again\n", 313 | " \n", 314 | " p(U)sh \n", 315 | " p(O)p\n", 316 | " (V)iew\n", 317 | " (Q)uit\n", 318 | " Enter choice:\n", 319 | " v\n", 320 | "\n", 321 | "You picked: [v]\n", 322 | "['asd']\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "#!/usr/bin/env python\n", 328 | "\"\"\"Stack\"\"\"\n", 329 | "\n", 330 | "stack = []\n", 331 | "\n", 332 | "def pushit():\n", 333 | " stack.append(raw_input('Enter new string: ').strip())\n", 334 | "\n", 335 | "def popit():\n", 336 | " if len(stack) == 0:\n", 337 | " print 'Cannot pop from an empty stack!' \n", 338 | " else:\n", 339 | " print 'Removed [', 'stack.pop()', ']'\n", 340 | "\n", 341 | "def viewstack():\n", 342 | " print stack\n", 343 | "\n", 344 | "# calls str() internally\n", 345 | "\n", 346 | "CMDs = {'u': pushit, 'o': popit, 'v': viewstack}\n", 347 | "\n", 348 | "def showmenu():\n", 349 | " pr = \"\"\" \n", 350 | " p(U)sh \n", 351 | " p(O)p\n", 352 | " (V)iew\n", 353 | " (Q)uit\n", 354 | " Enter choice:\n", 355 | " \"\"\"\n", 356 | "\n", 357 | " while True:\n", 358 | " while True:\n", 359 | " try:\n", 360 | " choice = raw_input(pr).strip()[0].lower() \n", 361 | " except (EOFError,KeyboardInterrupt,IndexError):\n", 362 | " choice = 'q'\n", 363 | " print '\\nYou picked: [%s]' % choice \n", 364 | " if choice not in 'uovq':\n", 365 | " print 'Invalid option, try again'\n", 366 | " else:\n", 367 | " break\n", 368 | " \n", 369 | " if choice == 'q':\n", 370 | " break\n", 371 | " CMDs[choice]()\n", 372 | "\n", 373 | "if __name__ == '__main__':\n", 374 | " showmenu()" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": null, 380 | "metadata": {}, 381 | "outputs": [], 382 | "source": [ 383 | "#!/usr/bin/env python\n", 384 | "\n", 385 | "queue = []\n", 386 | "\n", 387 | "def enQ():\n", 388 | " queue.append(raw_input('Enter new string: ').strip())\n", 389 | "\n", 390 | "def deQ():\n", 391 | " if len(queue) == 0:\n", 392 | " print 'Cannot pop from an empty queue!'\n", 393 | " else:\n", 394 | " print 'Removed [', ‘queue.pop(0)‘, ']'\n", 395 | "\n", 396 | "def viewQ():\n", 397 | " print queue\n", 398 | "\n", 399 | "# calls str() internally\n", 400 | "\n", 401 | "CMDs = {'e': enQ, 'd': deQ, 'v': viewQ}\n", 402 | "\n", 403 | "def showmenu():\n", 404 | " pr = \"\"\"\n", 405 | " (E)nqueue\n", 406 | " (D)equeue\n", 407 | " (V)iew\n", 408 | " (Q)uit\n", 409 | " Enter choice:\n", 410 | " \"\"\"\n", 411 | " \n", 412 | " while True:\n", 413 | " while True:\n", 414 | " try:\n", 415 | " choice = raw_input(pr).strip()[0].lower()\n", 416 | " except (EOFError,KeyboardInterrupt,IndexError):\n", 417 | " choice = 'q'\n", 418 | " print '\\nYou picked: [%s]' % choice\n", 419 | " if choice not in 'devq':\n", 420 | " print 'Invalid option, try again'\n", 421 | " else:\n", 422 | " break\n", 423 | "\n", 424 | " if choice == 'q':\n", 425 | " break\n", 426 | " CMDs[choice]()\n", 427 | "\n", 428 | "if __name__ == '__main__':\n", 429 | " showmenu()" 430 | ] 431 | } 432 | ], 433 | "metadata": { 434 | "kernelspec": { 435 | "display_name": "Python 2", 436 | "language": "python", 437 | "name": "python2" 438 | }, 439 | "language_info": { 440 | "codemirror_mode": { 441 | "name": "ipython", 442 | "version": 2 443 | }, 444 | "file_extension": ".py", 445 | "mimetype": "text/x-python", 446 | "name": "python", 447 | "nbconvert_exporter": "python", 448 | "pygments_lexer": "ipython2", 449 | "version": "2.7.15" 450 | } 451 | }, 452 | "nbformat": 4, 453 | "nbformat_minor": 2 454 | } 455 | -------------------------------------------------------------------------------- /Class 03 Python Numbers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 03 Python Numbers\n", 8 | "\n", 9 | "Refer to Chapter 5 in CPP\n", 10 | "\n", 11 | "## 3.1 Introduction to Numbers\n", 12 | "\n", 13 | "\n", 14 | "> In Python, variables act like pointers that point to boxes. For immutable types, you do not change the contents of the box, you just point your pointer at a new box.\n", 15 | "\n" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "ename": "TypeError", 25 | "evalue": "cannot concatenate 'str' and 'int' objects", 26 | "output_type": "error", 27 | "traceback": [ 28 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 29 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 30 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'2'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 31 | "\u001b[0;31mTypeError\u001b[0m: cannot concatenate 'str' and 'int' objects" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "'2' + 2" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 6, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "CPU times: user 8 µs, sys: 1e+03 ns, total: 9 µs\n", 49 | "Wall time: 16.9 µs\n" 50 | ] 51 | }, 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "4.2" 56 | ] 57 | }, 58 | "execution_count": 6, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "2.2 + 2" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "### 3.2.2 Numeric Type (Arithmetic) Operators\n", 72 | "\n", 73 | "__Classic division VS True division VS floor division__" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 46, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "0" 85 | ] 86 | }, 87 | "execution_count": 46, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "1/2 # classic division" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 47, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "0" 105 | ] 106 | }, 107 | "execution_count": 47, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "1//2 # floor division" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 48, 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "0.0" 125 | ] 126 | }, 127 | "execution_count": 48, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "1.0//2.0 # floor divition" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 49, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "data": { 143 | "text/plain": [ 144 | "0.5" 145 | ] 146 | }, 147 | "execution_count": 49, 148 | "metadata": {}, 149 | "output_type": "execute_result" 150 | } 151 | ], 152 | "source": [ 153 | "1.0/2.0 # classic divition" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 8, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "0.5" 165 | ] 166 | }, 167 | "execution_count": 8, 168 | "metadata": {}, 169 | "output_type": "execute_result" 170 | } 171 | ], 172 | "source": [ 173 | "float(1)/2 # numeric coercion" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "__Exponetiation__" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 51, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "-9" 192 | ] 193 | }, 194 | "execution_count": 51, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "-3 ** 2 # exponentiation is computed before negation " 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 52, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "9" 212 | ] 213 | }, 214 | "execution_count": 52, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "(-3) ** 2 #with brackets, negation is computed first" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "__Modulo__" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 53, 233 | "metadata": {}, 234 | "outputs": [ 235 | { 236 | "data": { 237 | "text/plain": [ 238 | "1" 239 | ] 240 | }, 241 | "execution_count": 53, 242 | "metadata": {}, 243 | "output_type": "execute_result" 244 | } 245 | ], 246 | "source": [ 247 | "5 % 2" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 10, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "1" 259 | ] 260 | }, 261 | "execution_count": 10, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "divmod(5, 2)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "## 3.3 Built-in and Factory Functions" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 55, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "5.0" 286 | ] 287 | }, 288 | "execution_count": 55, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "from math import floor, ceil\n", 295 | "floor(5.5)" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 56, 301 | "metadata": {}, 302 | "outputs": [ 303 | { 304 | "data": { 305 | "text/plain": [ 306 | "-6.0" 307 | ] 308 | }, 309 | "execution_count": 56, 310 | "metadata": {}, 311 | "output_type": "execute_result" 312 | } 313 | ], 314 | "source": [ 315 | "floor(-5.5)" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 57, 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "data": { 325 | "text/plain": [ 326 | "6.0" 327 | ] 328 | }, 329 | "execution_count": 57, 330 | "metadata": {}, 331 | "output_type": "execute_result" 332 | } 333 | ], 334 | "source": [ 335 | "round(5.5)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 58, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "data": { 345 | "text/plain": [ 346 | "-6.0" 347 | ] 348 | }, 349 | "execution_count": 58, 350 | "metadata": {}, 351 | "output_type": "execute_result" 352 | } 353 | ], 354 | "source": [ 355 | "round(-5.5)" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 59, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "data": { 365 | "text/plain": [ 366 | "6.0" 367 | ] 368 | }, 369 | "execution_count": 59, 370 | "metadata": {}, 371 | "output_type": "execute_result" 372 | } 373 | ], 374 | "source": [ 375 | "ceil(5.5)" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 60, 381 | "metadata": {}, 382 | "outputs": [ 383 | { 384 | "data": { 385 | "text/plain": [ 386 | "-5.0" 387 | ] 388 | }, 389 | "execution_count": 60, 390 | "metadata": {}, 391 | "output_type": "execute_result" 392 | } 393 | ], 394 | "source": [ 395 | "ceil(-5.5)" 396 | ] 397 | }, 398 | { 399 | "cell_type": "markdown", 400 | "metadata": {}, 401 | "source": [ 402 | "## 3.4 Other Numeric Types\n", 403 | "\n", 404 | "### 3.4.1 Boolean 'numbers'" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 11, 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "True" 416 | ] 417 | }, 418 | "execution_count": 11, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "bool(0) # 0" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 62, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "data": { 434 | "text/plain": [ 435 | "False" 436 | ] 437 | }, 438 | "execution_count": 62, 439 | "metadata": {}, 440 | "output_type": "execute_result" 441 | } 442 | ], 443 | "source": [ 444 | "bool(None) # None" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 63, 450 | "metadata": {}, 451 | "outputs": [ 452 | { 453 | "data": { 454 | "text/plain": [ 455 | "False" 456 | ] 457 | }, 458 | "execution_count": 63, 459 | "metadata": {}, 460 | "output_type": "execute_result" 461 | } 462 | ], 463 | "source": [ 464 | "bool() # no value" 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": 64, 470 | "metadata": {}, 471 | "outputs": [ 472 | { 473 | "data": { 474 | "text/plain": [ 475 | "False" 476 | ] 477 | }, 478 | "execution_count": 64, 479 | "metadata": {}, 480 | "output_type": "execute_result" 481 | } 482 | ], 483 | "source": [ 484 | "bool('') # empty str" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 65, 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "data": { 494 | "text/plain": [ 495 | "True" 496 | ] 497 | }, 498 | "execution_count": 65, 499 | "metadata": {}, 500 | "output_type": "execute_result" 501 | } 502 | ], 503 | "source": [ 504 | "bool(' ') # str ' '" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 66, 510 | "metadata": {}, 511 | "outputs": [ 512 | { 513 | "data": { 514 | "text/plain": [ 515 | "True" 516 | ] 517 | }, 518 | "execution_count": 66, 519 | "metadata": {}, 520 | "output_type": "execute_result" 521 | } 522 | ], 523 | "source": [ 524 | "bool(1) # int 1" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 67, 530 | "metadata": {}, 531 | "outputs": [ 532 | { 533 | "data": { 534 | "text/plain": [ 535 | "True" 536 | ] 537 | }, 538 | "execution_count": 67, 539 | "metadata": {}, 540 | "output_type": "execute_result" 541 | } 542 | ], 543 | "source": [ 544 | "bool('1') # str '1'" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": 68, 550 | "metadata": {}, 551 | "outputs": [ 552 | { 553 | "data": { 554 | "text/plain": [ 555 | "False" 556 | ] 557 | }, 558 | "execution_count": 68, 559 | "metadata": {}, 560 | "output_type": "execute_result" 561 | } 562 | ], 563 | "source": [ 564 | "bool([]) # empty list" 565 | ] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "execution_count": 69, 570 | "metadata": {}, 571 | "outputs": [ 572 | { 573 | "data": { 574 | "text/plain": [ 575 | "False" 576 | ] 577 | }, 578 | "execution_count": 69, 579 | "metadata": {}, 580 | "output_type": "execute_result" 581 | } 582 | ], 583 | "source": [ 584 | "bool(()) # empty set" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 70, 590 | "metadata": {}, 591 | "outputs": [ 592 | { 593 | "data": { 594 | "text/plain": [ 595 | "False" 596 | ] 597 | }, 598 | "execution_count": 70, 599 | "metadata": {}, 600 | "output_type": "execute_result" 601 | } 602 | ], 603 | "source": [ 604 | "bool({}) # empty dict" 605 | ] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "execution_count": 1, 610 | "metadata": {}, 611 | "outputs": [ 612 | { 613 | "name": "stdout", 614 | "output_type": "stream", 615 | "text": [ 616 | "(a)\n" 617 | ] 618 | } 619 | ], 620 | "source": [ 621 | "input0 = '(a)'\n", 622 | "if input0:\n", 623 | " print(input0)" 624 | ] 625 | } 626 | ], 627 | "metadata": { 628 | "kernelspec": { 629 | "display_name": "Python 2", 630 | "language": "python", 631 | "name": "python2" 632 | }, 633 | "language_info": { 634 | "codemirror_mode": { 635 | "name": "ipython", 636 | "version": 2 637 | }, 638 | "file_extension": ".py", 639 | "mimetype": "text/x-python", 640 | "name": "python", 641 | "nbconvert_exporter": "python", 642 | "pygments_lexer": "ipython2", 643 | "version": "2.7.15" 644 | } 645 | }, 646 | "nbformat": 4, 647 | "nbformat_minor": 2 648 | } 649 | -------------------------------------------------------------------------------- /Class 04 Python Strings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Class 4 Python Strings\n", 8 | "\n", 9 | "## 4.1 Python Sequences\n", 10 | "\n", 11 | "> Strings, lists and tuples are sequences of various Python objects.\n", 12 | "\n", 13 | "> Sequence types all share the same access model: ordered set with sequentially indexed offsets to get to each element.\n", 14 | "\n", 15 | "> The numbering scheme used starts from zero (0) and ends with one less than the length of the sequence—the reason for this is because we began at 0.\n", 16 | "\n", 17 | "### 4.1.1 Membership (`in`, `not in`)\n", 18 | "\n", 19 | "> The `in` and `not in` operators are Boolean in nature; they return True if the membership is confirmed and False otherwise.\n", 20 | "\n", 21 | "### 4.1.2 Concatenation (`+`)\n", 22 | "\n", 23 | "Concatetation in sequences is different from sum in numbers.\n", 24 | "\n", 25 | "Concatenation is not as efficient as `join`, especially when there are a number of items to be merged." 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 12, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "peak memory: 38.86 MiB, increment: 0.49 MiB\n", 38 | "peak memory: 38.86 MiB, increment: 0.00 MiB\n", 39 | "peak memory: 38.86 MiB, increment: 0.00 MiB\n", 40 | "peak memory: 38.87 MiB, increment: 0.00 MiB\n", 41 | "1 loop, best of 3: 253 ms per loop\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "%load_ext memory_profiler\n", 47 | "%timeit %memit 'a' + 'b' + 'c' + 'd' + 'e'" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 13, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "peak memory: 38.87 MiB, increment: 0.00 MiB\n", 60 | "peak memory: 38.87 MiB, increment: 0.00 MiB\n", 61 | "peak memory: 38.87 MiB, increment: 0.00 MiB\n", 62 | "peak memory: 38.87 MiB, increment: 0.00 MiB\n", 63 | "1 loop, best of 3: 248 ms per loop\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "%timeit %memit ''.join(('a', 'b', 'c', 'd', 'e'))" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "### 4.1.3 Slices\n", 76 | "\n", 77 | "__Individual slicing__\n", 78 | "\n", 79 | "> Sequences are structured data types whose elements are placed sequentially in an ordered manner.\n", 80 | "\n", 81 | "`sequences[index]`\n", 82 | "\n", 83 | "Index can be either positive or negative.\n", 84 | "\n", 85 | "1. Positive index starts from left (0) to right (`len(sequence)-1`);\n", 86 | "2. Negative index starts from right (-1) ro left (`-len(sequence)`).\n", 87 | " \n", 88 | "> Accessing a group of elements is similar to accessing just a single item. Starting and ending indexes may be given, separated by a colon ( : ).\n", 89 | "\n", 90 | "\n", 91 | "__Group slicing__\n", 92 | "\n", 93 | "`sequence[starting_index:ending_index]`\n", 94 | "\n", 95 | "> Both starting_index and ending_index are optional, and if not provided, or if None is used as an index, the slice will go from the beginning of the sequence or until the end of the sequence, respectively.\n", 96 | "\n", 97 | "`sequence == sequence[:]`\n", 98 | "\n", 99 | "`sequence[0:ending_index] or sequence[:ending_index]`\n", 100 | "\n", 101 | "`sequence[starting_index:len(sequence)-1] or sequence[:len(sequence)-1]`\n", 102 | "\n", 103 | "reversed sequnce: `sequence[::-1]`\n", 104 | "\n", 105 | "> The starting and ending indices can exceed the length of the string.\n", 106 | "\n", 107 | "> Using None as an index has the same effect as a missing index." 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "### 4.1.4 Built-in Functions\n", 115 | "\n", 116 | "> If you pass a list to list(), a (shallow) copy of the list’s objects will be made and inserted into the new list.\n", 117 | "\n", 118 | "> A shallow copy is where only __references__ are copied…no new objects are made!\n", 119 | "\n", 120 | "> Note that `len()`, `reversed()`, and `sum()` can only accept sequences while the rest can take iterables." 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 17, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "(0, 'a')\n", 133 | "(1, 'b')\n", 134 | "(2, 'c')\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "for order, item in enumerate(['a', 'b', 'c']): print(order, item)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 18, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "(1, 'a')\n", 152 | "(2, 'b')\n", 153 | "(3, 'c')\n" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "for order, item in enumerate(['a', 'b', 'c'], start=1): print(order, item)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 19, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "['a', 'b', 'c']" 170 | ] 171 | }, 172 | "execution_count": 19, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "sorted(['a', 'c', 'b'])" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 21, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "['c', 'b', 'a']" 190 | ] 191 | }, 192 | "execution_count": 21, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "sorted(['a', 'c', 'b'], reverse=True)" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 22, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "" 210 | ] 211 | }, 212 | "execution_count": 22, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "reversed(['a', 'c', 'b'])" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 23, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "['b', 'c', 'a']" 230 | ] 231 | }, 232 | "execution_count": 23, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "list(reversed(['a', 'c', 'b']))" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 24, 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "data": { 248 | "text/plain": [ 249 | "['c', 'b', 'a']" 250 | ] 251 | }, 252 | "execution_count": 24, 253 | "metadata": {}, 254 | "output_type": "execute_result" 255 | } 256 | ], 257 | "source": [ 258 | "list(reversed(sorted(['a', 'c', 'b'])))" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 25, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "[('a', '1'), ('c', '2'), ('b', '0')]" 270 | ] 271 | }, 272 | "execution_count": 25, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "zip(['a', 'c', 'b'], ['1', '2', '0'])" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "## 4.2 Strings\n", 286 | "\n", 287 | "> Python treats single quotes the same as double quotes.\n", 288 | "\n", 289 | "> Strings are a literal or scalar type, meaning they are treated by the interpreter as a singular value and are not containers that hold other Python objects. \n", 290 | "\n", 291 | "> Strings are immutable, meaning that changing an element of a string requires creating a new string." 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 26, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "name": "stdout", 301 | "output_type": "stream", 302 | "text": [ 303 | "Welcome to the Identifier Checker v1.0\n", 304 | "Testees must be at least 2 chars long.\n", 305 | "Identifier to test? as\n", 306 | "okay as an identifier\n" 307 | ] 308 | } 309 | ], 310 | "source": [ 311 | "#!/usr/bin/env python\n", 312 | "\n", 313 | "import string\n", 314 | "\n", 315 | "alphas = string.ascii_letters + '_'\n", 316 | "nums = string.digits \n", 317 | "alphnum = alphas + nums\n", 318 | "\n", 319 | "print 'Welcome to the Identifier Checker v1.0'\n", 320 | "print 'Testees must be at least 2 chars long.'\n", 321 | "\n", 322 | "myInput = raw_input('Identifier to test? ')\n", 323 | "\n", 324 | "if len(myInput) > 1:\n", 325 | " if myInput[0] not in alphas:\n", 326 | " print '''invalid: first symbol must be 16 alphabetic'''\n", 327 | " else:\n", 328 | " for otherChar in myInput[1:]:\n", 329 | " if otherChar not in alphnum: # ! not efficient\n", 330 | " print '''invalid: remaining 22 symbols must be alphanumeric'''\n", 331 | " break\n", 332 | " else:\n", 333 | " print \"okay as an identifier\"" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "### 4.2.1 Concatenation\n", 341 | "\n", 342 | "> When concatenating regular and Unicode strings, regular strings are converted to Unicode first before the operation occurs.\n", 343 | "\n", 344 | "`'Hello' + u' ' + 'World' + u'!'`\n", 345 | "\n", 346 | "### 4.2.2 Format string\n", 347 | "\n", 348 | "`format_string % (arguments_to_convert)`" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 51, 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "data": { 358 | "text/plain": [ 359 | "'MM/DD/YY = 02/15/67'" 360 | ] 361 | }, 362 | "execution_count": 51, 363 | "metadata": {}, 364 | "output_type": "execute_result" 365 | } 366 | ], 367 | "source": [ 368 | "\"MM/DD/YY = %02d/%02d/%d\" % (2, 15, 67) # d for int" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 52, 374 | "metadata": {}, 375 | "outputs": [ 376 | { 377 | "data": { 378 | "text/plain": [ 379 | "'Your host is: earth'" 380 | ] 381 | }, 382 | "execution_count": 52, 383 | "metadata": {}, 384 | "output_type": "execute_result" 385 | } 386 | ], 387 | "source": [ 388 | "'Your host is: %s' % 'earth' # s for str" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 53, 394 | "metadata": {}, 395 | "outputs": [ 396 | { 397 | "data": { 398 | "text/plain": [ 399 | "'1234.567890'" 400 | ] 401 | }, 402 | "execution_count": 53, 403 | "metadata": {}, 404 | "output_type": "execute_result" 405 | } 406 | ], 407 | "source": [ 408 | "'%f' % 1234.567890 # f for floating" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": 54, 414 | "metadata": {}, 415 | "outputs": [ 416 | { 417 | "data": { 418 | "text/plain": [ 419 | "'1234.57'" 420 | ] 421 | }, 422 | "execution_count": 54, 423 | "metadata": {}, 424 | "output_type": "execute_result" 425 | } 426 | ], 427 | "source": [ 428 | "'%.2f' % 1234.567890 # .2f for floating with 2 decimals" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "metadata": {}, 434 | "source": [ 435 | "## 4.3 String Built-in Methods\n", 436 | "\n", 437 | "`string.decode(encoding='UTF-8', errors='strict')`\n", 438 | "\n", 439 | "`string.encode(encoding='UTF-8', errors='strict')`\n", 440 | "\n", 441 | "`string.join(seq)`\n", 442 | "\n", 443 | "`string.split(str=\"\", num=string.count(str))`\n", 444 | "\n", 445 | "`string.translate(str, del=\"\")`\n", 446 | "\n", 447 | "## 4.4 Special Features of Strings\n", 448 | "\n", 449 | "`\\t` # tab\n", 450 | "\n", 451 | "`\\n` # new line\n", 452 | "\n", 453 | "`\\\"` # double quote\n", 454 | "\n", 455 | "`\\'` # single quote\n", 456 | "\n", 457 | "`\\\\` # backslash\n", 458 | "\n", 459 | "\n", 460 | "## 4.5 Unicode\n", 461 | "\n", 462 | "\n", 463 | "### 4.5.1 ASCII VS Unicode VS UTF-8\n", 464 | "\n", 465 | "> ASCII was simple. Every English character was stored in the computer as a seven bit number between 32 and 126.\n", 466 | "\n", 467 | "> Unicode can currently represent over 90,000 characters.\n", 468 | "\n", 469 | "> UTF-8 may use one or two bytes to represent a letter, three (mainly) for CJK/East Asian characters, and four for some rare, special use, or historic characters.\n", 470 | "\n", 471 | "### 4.5.2 Using unicode in real life\n", 472 | "\n", 473 | "__Always convert str to unicde in Python.__\n", 474 | "\n", 475 | "__UTF-8 in, unicode() processes, and UTF-8 out.__\n", 476 | "\n", 477 | "> • Always prefix your string literals with u.\n", 478 | "\n", 479 | "> • Never use str()… always use unicode() instead.\n", 480 | "\n", 481 | "> • Never use the outdated string module—it blows up when you pass it any non-ASCII characters.\n", 482 | "\n", 483 | "> • Only call the encode() method right before you write your text to a file, database, or the network, and only call the decode() method when you are reading it back in." 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 67, 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "name": "stdout", 493 | "output_type": "stream", 494 | "text": [ 495 | "Hello world\n" 496 | ] 497 | } 498 | ], 499 | "source": [ 500 | "#!/usr/bin/env python\n", 501 | "''' An example of reading and writing Unicode strings: \n", 502 | "Writes a Unicode string to a file in utf-8 and reads it back in.'''\n", 503 | "\n", 504 | "CODEC = 'utf-8' \n", 505 | "FILE = 'unicode.txt'\n", 506 | "\n", 507 | "hello_out = u\"Hello world\\n\"\n", 508 | "bytes_out = hello_out.encode(CODEC)\n", 509 | "f = open(FILE, \"w\")\n", 510 | "f.write(bytes_out)\n", 511 | "f.close()\n", 512 | "f = open(FILE, \"r\")\n", 513 | "bytes_in = f.read()\n", 514 | "f.close()\n", 515 | "hello_in = bytes_in.decode(CODEC)\n", 516 | "print hello_in" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 68, 522 | "metadata": {}, 523 | "outputs": [], 524 | "source": [ 525 | "def to_uni(string):\n", 526 | " return string.decode('utf-8', 'ignore') if isinstance(string, str) else string\n", 527 | "\n", 528 | "\n", 529 | "def to_str(uni_str):\n", 530 | " \"\"\"\n", 531 | " convert both unicode and int to str\n", 532 | " \"\"\"\n", 533 | " return uni_str.encode('utf-8', 'ignore') if isinstance(uni_str, (unicode, int)) else uni_str" 534 | ] 535 | } 536 | ], 537 | "metadata": { 538 | "kernelspec": { 539 | "display_name": "Python 2", 540 | "language": "python", 541 | "name": "python2" 542 | }, 543 | "language_info": { 544 | "codemirror_mode": { 545 | "name": "ipython", 546 | "version": 2 547 | }, 548 | "file_extension": ".py", 549 | "mimetype": "text/x-python", 550 | "name": "python", 551 | "nbconvert_exporter": "python", 552 | "pygments_lexer": "ipython2", 553 | "version": "2.7.15" 554 | } 555 | }, 556 | "nbformat": 4, 557 | "nbformat_minor": 2 558 | } 559 | --------------------------------------------------------------------------------