├── .gitignore ├── Exercises.ipynb ├── Lecture.ipynb └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | 53 | # Translations 54 | *.mo 55 | *.pot 56 | 57 | # Django stuff: 58 | *.log 59 | local_settings.py 60 | db.sqlite3 61 | db.sqlite3-journal 62 | 63 | # Flask stuff: 64 | instance/ 65 | .webassets-cache 66 | 67 | # Scrapy stuff: 68 | .scrapy 69 | 70 | # Sphinx documentation 71 | docs/_build/ 72 | 73 | # PyBuilder 74 | target/ 75 | 76 | # Jupyter Notebook 77 | .ipynb_checkpoints 78 | 79 | # IPython 80 | profile_default/ 81 | ipython_config.py 82 | 83 | # pyenv 84 | .python-version 85 | 86 | # pipenv 87 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 88 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 89 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 90 | # install all needed dependencies. 91 | #Pipfile.lock 92 | 93 | # celery beat schedule file 94 | celerybeat-schedule 95 | 96 | # SageMath parsed files 97 | *.sage.py 98 | 99 | # Environments 100 | .env 101 | .venv 102 | env/ 103 | venv/ 104 | ENV/ 105 | env.bak/ 106 | venv.bak/ 107 | 108 | # Spyder project settings 109 | .spyderproject 110 | .spyproject 111 | 112 | # Rope project settings 113 | .ropeproject 114 | 115 | # mkdocs documentation 116 | /site 117 | 118 | # mypy 119 | .mypy_cache/ 120 | .dmypy.json 121 | dmypy.json 122 | 123 | # Pyre type checker 124 | .pyre/ 125 | -------------------------------------------------------------------------------- /Exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", 8 | "
\n", 9 | "\n", 10 | "# Python in under 10 minutes - Exercises\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 18 | "\n", 19 | "### Exercise 1\n", 20 | "\n", 21 | "Convert the following `str` into `int` type.\n", 22 | "\n", 23 | "`revenue_as_string = \"500000\"`" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 12, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "revenue_as_string = \"500000\"\n", 33 | "\n", 34 | "# your code goes here\n", 35 | "\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 13, 41 | "metadata": { 42 | "cell_type": "solution" 43 | }, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "500000" 49 | ] 50 | }, 51 | "execution_count": 13, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "revenue_as_integer = int(revenue_as_string)\n", 58 | "\n", 59 | "revenue_as_integer" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 67 | "\n", 68 | "### Exercise 2\n", 69 | "\n", 70 | "Make a function using the following formula:\n", 71 | "\n", 72 | "$$ f(x,y) = 2 x + y $$" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "# your code goes here\n" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": { 87 | "cell_type": "hint" 88 | }, 89 | "source": [ 90 | "The function will have two parameters $x$ and $y$." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 23, 96 | "metadata": { 97 | "cell_type": "solution" 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "def func(x, y):\n", 102 | " return 2*x + y" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 24, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "25" 114 | ] 115 | }, 116 | "execution_count": 24, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "func(10, 5)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 34, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "4" 134 | ] 135 | }, 136 | "execution_count": 34, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "func(-3, 10)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 150 | "\n", 151 | "### Exercise 3\n", 152 | "\n", 153 | "Make a function that receives a string as parameter and return the last letter on it." 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [ 162 | "# your code goes here\n", 163 | "\n" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 21, 169 | "metadata": { 170 | "cell_type": "solution" 171 | }, 172 | "outputs": [], 173 | "source": [ 174 | "def last_letter(string):\n", 175 | " return string[-1]" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 22, 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "'n'" 187 | ] 188 | }, 189 | "execution_count": 22, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | "last_letter('Python')" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 32, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "'R'" 207 | ] 208 | }, 209 | "execution_count": 32, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "last_letter('RMOTR')" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 223 | "\n", 224 | "### Exercise 4\n", 225 | "\n", 226 | "Make a function that receives a list as parameter and return how many elements it has; if it doesn't have any element return an error." 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": null, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "# your code goes here\n" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 29, 241 | "metadata": { 242 | "cell_type": "solution" 243 | }, 244 | "outputs": [], 245 | "source": [ 246 | "def list_size(my_list):\n", 247 | " size = len(my_list)\n", 248 | " \n", 249 | " if (size > 0):\n", 250 | " return 'List has {} elements'.format(size)\n", 251 | " else:\n", 252 | " return 'Error, no elements'" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 30, 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "data": { 262 | "text/plain": [ 263 | "'List has 3 elements'" 264 | ] 265 | }, 266 | "execution_count": 30, 267 | "metadata": {}, 268 | "output_type": "execute_result" 269 | } 270 | ], 271 | "source": [ 272 | "list_size([1, 2, 3])" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 31, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "data": { 282 | "text/plain": [ 283 | "'Error, no elements'" 284 | ] 285 | }, 286 | "execution_count": 31, 287 | "metadata": {}, 288 | "output_type": "execute_result" 289 | } 290 | ], 291 | "source": [ 292 | "list_size([])" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": {}, 298 | "source": [ 299 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 300 | ] 301 | } 302 | ], 303 | "metadata": { 304 | "kernelspec": { 305 | "display_name": "Python 3", 306 | "language": "python", 307 | "name": "python3" 308 | }, 309 | "language_info": { 310 | "codemirror_mode": { 311 | "name": "ipython", 312 | "version": 3 313 | }, 314 | "file_extension": ".py", 315 | "mimetype": "text/x-python", 316 | "name": "python", 317 | "nbconvert_exporter": "python", 318 | "pygments_lexer": "ipython3", 319 | "version": "3.6.8" 320 | } 321 | }, 322 | "nbformat": 4, 323 | "nbformat_minor": 2 324 | } 325 | -------------------------------------------------------------------------------- /Lecture.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", 8 | "
\n", 9 | "\n", 10 | "\n", 12 | "\n", 13 | "# Python in under 10 minutes\n", 14 | "\n", 15 | "Ok, so this will techincally take us more than 10 minutes, but you get the idea: this is a very high level overview of the Python programming language. Specially for those of you that already know some other programming language (R, Javascript, Ruby, etc) and want to do the switch to Python.\n", 16 | "\n", 17 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "### The high level\n", 25 | "\n", 26 | "[Python](https://en.wikipedia.org/wiki/Python_%28programming_language%29) is a interpreted, high level programming language created by Dutch programmer [Guido van Rossum](https://en.wikipedia.org/wiki/Guido_van_Rossum) and released in 1991.\n", 27 | "\n", 28 | "As I always say, Python *is old*. It gained a lot of traction around 2006, with its popularity being driven its beauty and simplicity to do Web Development (its main Web Development Framework, [Django](https://www.djangoproject.com/), was released in 2006). From there, it took off as one of the most popular scripting languages.\n", 29 | "\n", 30 | "Python is multi-paradigm: you can write code using Object Oriented, Functional and/or Imperative programming. Python is interpreted and uses a dynamic type system, although considered _strongly typed_.\n", 31 | "\n", 32 | "It has an extensive builtin standard library with features that ranges from time management, to http servers, concurrency and async programming.\n", 33 | "\n", 34 | "Python is Open Source and is managed by a non-profit organization: the [Python Software Foundation](https://en.wikipedia.org/wiki/Python_Software_Foundation).\n", 35 | "\n", 36 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 37 | "\n", 38 | "## Python 3 vs Python 2\n", 39 | "\n", 40 | "You might have heard that there are two main versions of Python around: _Python 2_ and _Python 3_. Well, I'll keep it simple for you:\n", 41 | "\n", 42 | "### 👍 Python 3: YES!\n", 43 | "### 🙅 Python 2: NO!\n", 44 | "\n", 45 | "Python 2 [will be deprecated in 2020](https://pythonclock.org/). So **stick to Python 3**, it's the present and the future of the language (at least until we start planning Python 4 😅, just kidding).\n", 46 | "\n", 47 | "## A quick note about different \"implementations\"\n", 48 | "\n", 49 | "Finally, all what I'm saying here technically applies to \"CPython\", Python's main _implementation_. Don't worry if this doesn't make sense. It's just for the curious ones. If you're interested, we've written an entire post about it: [A quick guide about Python implementations](https://blog.rmotr.com/a-quick-guide-about-python-implementations-aa224109f321).\n", 50 | "\n", 51 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 52 | "\n", 53 | "# Syntax Overview\n", 54 | "\n", 55 | "The following paragraphs will be dedicated to Python's syntax and technical details. There are more to Python than just syntax, as its community, events, email lists, etc. But after all, this is just a technical introduction.\n", 56 | "\n", 57 | "### Indentation based\n", 58 | "\n", 59 | "This might feel weird at first, but in Python we do NOT use curly braces to denote blocks of code. We use _\"indentation\"_ instead. This is similar to Ruby. For example, this is a very simple `add_numbers` function in javascript:\n", 60 | "\n", 61 | "```javascript\n", 62 | "function add_numbers(x, y){\n", 63 | " return x + y\n", 64 | "}\n", 65 | "```\n", 66 | "\n", 67 | "In Python, we'd write it in this way:" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "def add_numbers(x, y):\n", 77 | " return x + y" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "An `if-else` block in Javascript:\n", 85 | "\n", 86 | "```javascript\n", 87 | "let language = \"Python\"\n", 88 | "\n", 89 | "if (language === \"Python\"){\n", 90 | " console.log(\"Let the fun begin\");\n", 91 | "} else {\n", 92 | " console.log(\"You sure?\");\n", 93 | "}\n", 94 | "```\n", 95 | "\n", 96 | "In Python:" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "# try changing \"Python\" to something else.\n", 106 | "# Don't remove the quotes.\n", 107 | "language = \"Python\"\n", 108 | "\n", 109 | "if language == \"Python\":\n", 110 | " print(\"Let the fun begin!\")\n", 111 | "else:\n", 112 | " print(\"You sure?\")" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 120 | "\n", 121 | "### Comments\n", 122 | "\n", 123 | "You've seen comments in the previous block of code: they're prefixed with a pound/hashtag sign:" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "# this is a comment" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "# it doesn't produce any output" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "# can be above...\n", 151 | "print(\"Hello World\") # next to...\n", 152 | "# or below your code" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 160 | "\n", 161 | "### Variables\n", 162 | "\n", 163 | "We've defined a variable `language` in one of our previous examples. In Python, you can set a variable at any time, in any block of code, by just assigning a valid name to any value you want:" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "name = \"Mary\"\n", 173 | "print(name)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "age = 30\n", 183 | "print(age)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "Variables, once set, will be preserved:" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "print(name, \"is\", age, \"years old\")" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 207 | "\n", 208 | "### Data Types\n", 209 | "\n", 210 | "Python supports the most common data types, the usual suspects we could say:\n", 211 | "\n", 212 | "#### Integers, type `int`:\n", 213 | "\n", 214 | "Integers have unlimited magnitude." 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "# an int\n", 224 | "age = 30" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "age" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": null, 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [ 242 | "type(age)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "#### Floats, type `float`:\n", 250 | "\n", 251 | "The standard floating point number" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": {}, 258 | "outputs": [], 259 | "source": [ 260 | "# a float\n", 261 | "price = 2.50" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": null, 267 | "metadata": {}, 268 | "outputs": [], 269 | "source": [ 270 | "price" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "type(price)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "Remember that floats sometimes exhibit \"extraneous\" behavior:" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": null, 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "0.1 * 3" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "If you need decimal fixed point precision, you can use the [`decimal`](https://docs.python.org/3/library/decimal.html#module-decimal) module:" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "from decimal import Decimal" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "Decimal('0.1') * 3" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "#### Strings, type `str`\n", 328 | "\n", 329 | "Strings are used to store text. Technically, they're _\"immutable sequences of Unicode code points\"_. Which means that Python supports Unicode:" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": null, 335 | "metadata": {}, 336 | "outputs": [], 337 | "source": [ 338 | "# Create them with double quotes:\n", 339 | "print(\"Hello unicode 👋\")" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": null, 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [ 348 | "# single quotes:\n", 349 | "print('Omelette Du Fromage 🧀')" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": {}, 356 | "outputs": [], 357 | "source": [ 358 | "type('Hello World')" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "metadata": {}, 365 | "outputs": [], 366 | "source": [ 367 | "len(\"Hello\")" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": {}, 373 | "source": [ 374 | "You can use double or single quotes, it's the same. We also have \"multi-line\" strings, that are created with a pair of 3 quotes (simple or double, either works):" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": null, 380 | "metadata": {}, 381 | "outputs": [], 382 | "source": [ 383 | "joke = \"\"\"\n", 384 | "Me: What’s the best thing about Switzerland?\n", 385 | "Friend: I don't know. What?\n", 386 | "Me: I don’t know, but the flag is a big plus.\n", 387 | "F: 😒\n", 388 | "\"\"\"\n", 389 | "\n", 390 | "print(joke)" 391 | ] 392 | }, 393 | { 394 | "cell_type": "markdown", 395 | "metadata": {}, 396 | "source": [ 397 | "#### Booleans, type `bool`\n", 398 | "\n", 399 | "Python booleans are as simple as they get: `True` and `False`, **capitalized**." 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": null, 405 | "metadata": {}, 406 | "outputs": [], 407 | "source": [ 408 | "True" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": null, 414 | "metadata": {}, 415 | "outputs": [], 416 | "source": [ 417 | "type(False)" 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "metadata": {}, 423 | "source": [ 424 | "#### None, type `NoneType`\n", 425 | "\n", 426 | "As other languages have `null`, in Python we have `None`, which pretty much represents the absence of value:" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": null, 432 | "metadata": {}, 433 | "outputs": [], 434 | "source": [ 435 | "x = None" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": null, 441 | "metadata": {}, 442 | "outputs": [], 443 | "source": [ 444 | "x" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": null, 450 | "metadata": {}, 451 | "outputs": [], 452 | "source": [ 453 | "print(x)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": null, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [ 462 | "type(None)" 463 | ] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 470 | "\n", 471 | "### `int`, `float`, `str` and `bool` objects and functions\n", 472 | "\n", 473 | "You'll often see some of these _\"keywords/names\"_ used both as functions and as individual objects. When used as functions, their usage is to transform/cast objects into its corresponding type. Example:" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": null, 479 | "metadata": {}, 480 | "outputs": [], 481 | "source": [ 482 | "age_as_string = \"28\"" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": null, 488 | "metadata": {}, 489 | "outputs": [], 490 | "source": [ 491 | "type(age_as_string)" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": null, 497 | "metadata": {}, 498 | "outputs": [], 499 | "source": [ 500 | "int(age_as_string)" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": null, 506 | "metadata": {}, 507 | "outputs": [], 508 | "source": [ 509 | "age = int(age_as_string)" 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "execution_count": null, 515 | "metadata": {}, 516 | "outputs": [], 517 | "source": [ 518 | "type(age)" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "Their use as objects is mainly associated with their type:" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": null, 531 | "metadata": {}, 532 | "outputs": [], 533 | "source": [ 534 | "type(13) == int" 535 | ] 536 | }, 537 | { 538 | "cell_type": "markdown", 539 | "metadata": {}, 540 | "source": [ 541 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 542 | "\n", 543 | "### Functions\n", 544 | "\n", 545 | "We've seen a couple of functions defined already, but let's dig a little bit deeper. Functions in Python are very intuitive. Let's start with an example of a function without parameters:" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": null, 551 | "metadata": {}, 552 | "outputs": [], 553 | "source": [ 554 | "def hello():\n", 555 | " return \"Hello World\"" 556 | ] 557 | }, 558 | { 559 | "cell_type": "markdown", 560 | "metadata": {}, 561 | "source": [ 562 | "The `def` keyword indicate the _definition_ of a function, followed by a name and a list of arguments (which this function doesn't receive). The `return` statement is used to break the flow of the function and return a value back to the caller:" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": {}, 569 | "outputs": [], 570 | "source": [ 571 | "result = hello()" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": null, 577 | "metadata": {}, 578 | "outputs": [], 579 | "source": [ 580 | "result" 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "metadata": {}, 586 | "source": [ 587 | "If a function doesn't explicitly include a `return` statement, Python will return `None` by default:" 588 | ] 589 | }, 590 | { 591 | "cell_type": "code", 592 | "execution_count": null, 593 | "metadata": {}, 594 | "outputs": [], 595 | "source": [ 596 | "def empty():\n", 597 | " x = 3" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": null, 603 | "metadata": {}, 604 | "outputs": [], 605 | "source": [ 606 | "result = empty()" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": null, 612 | "metadata": {}, 613 | "outputs": [], 614 | "source": [ 615 | "print(result)" 616 | ] 617 | }, 618 | { 619 | "cell_type": "markdown", 620 | "metadata": {}, 621 | "source": [ 622 | "#### Receiving parameters\n", 623 | "\n", 624 | "There's a lot that can be done with Python parameters; including default and named parameters, and even variable/dynamic ones. But for now, we'll just focus on the basics. Function parameters are listed at the function definition, and they're part of the function's local scope:" 625 | ] 626 | }, 627 | { 628 | "cell_type": "code", 629 | "execution_count": null, 630 | "metadata": {}, 631 | "outputs": [], 632 | "source": [ 633 | "def add(x, y):\n", 634 | " return x + y" 635 | ] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "execution_count": null, 640 | "metadata": {}, 641 | "outputs": [], 642 | "source": [ 643 | "add(2, 3)" 644 | ] 645 | }, 646 | { 647 | "cell_type": "markdown", 648 | "metadata": {}, 649 | "source": [ 650 | "We can also define functions that accept variable number of arguments, using the star args `*`:" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": null, 656 | "metadata": {}, 657 | "outputs": [], 658 | "source": [ 659 | "def add(*args):\n", 660 | " return sum(args)" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": null, 666 | "metadata": {}, 667 | "outputs": [], 668 | "source": [ 669 | "add(1, 1, 1)" 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "execution_count": null, 675 | "metadata": {}, 676 | "outputs": [], 677 | "source": [ 678 | "add(1)" 679 | ] 680 | }, 681 | { 682 | "cell_type": "markdown", 683 | "metadata": {}, 684 | "source": [ 685 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 686 | "\n", 687 | "### Operators\n", 688 | "\n", 689 | "Both arithmetic and boolean operators are available, for example:\n", 690 | "\n", 691 | "#### Arithmetic operators" 692 | ] 693 | }, 694 | { 695 | "cell_type": "code", 696 | "execution_count": null, 697 | "metadata": {}, 698 | "outputs": [], 699 | "source": [ 700 | "3 + 3" 701 | ] 702 | }, 703 | { 704 | "cell_type": "code", 705 | "execution_count": null, 706 | "metadata": {}, 707 | "outputs": [], 708 | "source": [ 709 | "11 % 7" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": null, 715 | "metadata": {}, 716 | "outputs": [], 717 | "source": [ 718 | "2 ** 4" 719 | ] 720 | }, 721 | { 722 | "cell_type": "markdown", 723 | "metadata": {}, 724 | "source": [ 725 | "Precedence can be consulted on [the official docs](https://docs.python.org/3/reference/expressions.html#operator-precedence). But, for the most part, the precedence is similar to the usual in arithmetic:" 726 | ] 727 | }, 728 | { 729 | "cell_type": "code", 730 | "execution_count": null, 731 | "metadata": {}, 732 | "outputs": [], 733 | "source": [ 734 | "3 + 4 * 5" 735 | ] 736 | }, 737 | { 738 | "cell_type": "code", 739 | "execution_count": null, 740 | "metadata": {}, 741 | "outputs": [], 742 | "source": [ 743 | "3 + 4 * 2**3" 744 | ] 745 | }, 746 | { 747 | "cell_type": "markdown", 748 | "metadata": {}, 749 | "source": [ 750 | "#### Boolean operators\n", 751 | "\n", 752 | "Regular comparison operators are available:" 753 | ] 754 | }, 755 | { 756 | "cell_type": "code", 757 | "execution_count": null, 758 | "metadata": {}, 759 | "outputs": [], 760 | "source": [ 761 | "7 > 3" 762 | ] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": null, 767 | "metadata": {}, 768 | "outputs": [], 769 | "source": [ 770 | "8 >= 8" 771 | ] 772 | }, 773 | { 774 | "cell_type": "markdown", 775 | "metadata": {}, 776 | "source": [ 777 | "We said that Python is strongly typed, so comparison between different types will fail if these types are not compatible:" 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "execution_count": null, 783 | "metadata": {}, 784 | "outputs": [], 785 | "source": [ 786 | "8 > \"abc\"" 787 | ] 788 | }, 789 | { 790 | "cell_type": "markdown", 791 | "metadata": {}, 792 | "source": [ 793 | "Python also has other common boolean operators like `and`, `or`, `not`, etc. They are short circuited, as most modern programming languages:" 794 | ] 795 | }, 796 | { 797 | "cell_type": "code", 798 | "execution_count": null, 799 | "metadata": {}, 800 | "outputs": [], 801 | "source": [ 802 | "True and True" 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": null, 808 | "metadata": {}, 809 | "outputs": [], 810 | "source": [ 811 | "not False" 812 | ] 813 | }, 814 | { 815 | "cell_type": "code", 816 | "execution_count": null, 817 | "metadata": {}, 818 | "outputs": [], 819 | "source": [ 820 | "False or True" 821 | ] 822 | }, 823 | { 824 | "cell_type": "markdown", 825 | "metadata": {}, 826 | "source": [ 827 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 828 | "\n", 829 | "### Control Flow\n", 830 | "\n", 831 | "Python supports the most common control flow blocks. Keep in mind they're defined with indentation.\n", 832 | "\n", 833 | "#### If/else/elif statements" 834 | ] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "execution_count": null, 839 | "metadata": {}, 840 | "outputs": [], 841 | "source": [ 842 | "days_subscribed = 28" 843 | ] 844 | }, 845 | { 846 | "cell_type": "code", 847 | "execution_count": null, 848 | "metadata": {}, 849 | "outputs": [], 850 | "source": [ 851 | "if days_subscribed >= 30:\n", 852 | " print(\"Loyal customer\")\n", 853 | "elif days_subscribed >= 15:\n", 854 | " print(\"Halfway there\")\n", 855 | "elif days_subscribed >= 1:\n", 856 | " print(\"Building confidence\")\n", 857 | "else:\n", 858 | " print(\"Too early\")" 859 | ] 860 | }, 861 | { 862 | "cell_type": "markdown", 863 | "metadata": {}, 864 | "source": [ 865 | "#### For loops\n", 866 | "\n", 867 | "For loops in Python are different than other languages, specially those C/Java-inspired languages. In Python, `for` loops are designed to iterate over collections (we'll see collections later). But keep that in mind." 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": null, 873 | "metadata": {}, 874 | "outputs": [], 875 | "source": [ 876 | "names = ['Monica', 'Ross', 'Chandler', 'Joey', 'Rachel']" 877 | ] 878 | }, 879 | { 880 | "cell_type": "code", 881 | "execution_count": null, 882 | "metadata": {}, 883 | "outputs": [], 884 | "source": [ 885 | "for name in names:\n", 886 | " print(name)" 887 | ] 888 | }, 889 | { 890 | "cell_type": "markdown", 891 | "metadata": {}, 892 | "source": [ 893 | "#### While loops\n", 894 | "\n", 895 | "While loops are seldom used in Python. For loops are the preferred choice 99% of the time. Still, they're available and are useful for some situations:" 896 | ] 897 | }, 898 | { 899 | "cell_type": "code", 900 | "execution_count": null, 901 | "metadata": {}, 902 | "outputs": [], 903 | "source": [ 904 | "count = 0" 905 | ] 906 | }, 907 | { 908 | "cell_type": "code", 909 | "execution_count": null, 910 | "metadata": {}, 911 | "outputs": [], 912 | "source": [ 913 | "while count < 3:\n", 914 | " print(\"Counting...\")\n", 915 | " count += 1" 916 | ] 917 | }, 918 | { 919 | "cell_type": "markdown", 920 | "metadata": {}, 921 | "source": [ 922 | "There's another block to mention, `try/except`, but it's in the **_Exceptions_** section." 923 | ] 924 | }, 925 | { 926 | "cell_type": "markdown", 927 | "metadata": {}, 928 | "source": [ 929 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 930 | "\n", 931 | "### Collections\n", 932 | "\n", 933 | "Python has multiple versatile collection types, each with different features and capabilities. These are the most common collections we'll explore:\n", 934 | "\n", 935 | "* Lists\n", 936 | "* Tuples\n", 937 | "* Dictionaries\n", 938 | "* Sets\n", 939 | "\n", 940 | "Even though they all have different capabilities, there is one common property to all of them, and it's that Python collections are heterogeneous, that is, you can mix multiple types. That **doesn't mean we should** mix types, usually it's better to have a consistent collection. But it's still possible.\n", 941 | "\n", 942 | "#### Lists\n", 943 | "\n", 944 | "Lists are mutable, ordered sequences. We could argue, the most common collection type." 945 | ] 946 | }, 947 | { 948 | "cell_type": "code", 949 | "execution_count": null, 950 | "metadata": {}, 951 | "outputs": [], 952 | "source": [ 953 | "l = [3, 'Hello World', True]" 954 | ] 955 | }, 956 | { 957 | "cell_type": "code", 958 | "execution_count": null, 959 | "metadata": {}, 960 | "outputs": [], 961 | "source": [ 962 | "len(l)" 963 | ] 964 | }, 965 | { 966 | "cell_type": "markdown", 967 | "metadata": {}, 968 | "source": [ 969 | "List elements are accessed using sequential indices (starting from `0`):" 970 | ] 971 | }, 972 | { 973 | "cell_type": "code", 974 | "execution_count": null, 975 | "metadata": {}, 976 | "outputs": [], 977 | "source": [ 978 | "l[0]" 979 | ] 980 | }, 981 | { 982 | "cell_type": "code", 983 | "execution_count": null, 984 | "metadata": {}, 985 | "outputs": [], 986 | "source": [ 987 | "l[1]" 988 | ] 989 | }, 990 | { 991 | "cell_type": "markdown", 992 | "metadata": {}, 993 | "source": [ 994 | "Negative indices are also supported:" 995 | ] 996 | }, 997 | { 998 | "cell_type": "code", 999 | "execution_count": null, 1000 | "metadata": {}, 1001 | "outputs": [], 1002 | "source": [ 1003 | "l[-1]" 1004 | ] 1005 | }, 1006 | { 1007 | "cell_type": "code", 1008 | "execution_count": null, 1009 | "metadata": {}, 1010 | "outputs": [], 1011 | "source": [ 1012 | "l[-2]" 1013 | ] 1014 | }, 1015 | { 1016 | "cell_type": "markdown", 1017 | "metadata": {}, 1018 | "source": [ 1019 | "Lists have many useful methods to add/remove elements:" 1020 | ] 1021 | }, 1022 | { 1023 | "cell_type": "code", 1024 | "execution_count": null, 1025 | "metadata": {}, 1026 | "outputs": [], 1027 | "source": [ 1028 | "l.append('Python 🐍')" 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "code", 1033 | "execution_count": null, 1034 | "metadata": {}, 1035 | "outputs": [], 1036 | "source": [ 1037 | "l" 1038 | ] 1039 | }, 1040 | { 1041 | "cell_type": "code", 1042 | "execution_count": null, 1043 | "metadata": {}, 1044 | "outputs": [], 1045 | "source": [ 1046 | "'Python 🐍' in l" 1047 | ] 1048 | }, 1049 | { 1050 | "cell_type": "code", 1051 | "execution_count": null, 1052 | "metadata": {}, 1053 | "outputs": [], 1054 | "source": [ 1055 | "'Ruby ♦️' in l" 1056 | ] 1057 | }, 1058 | { 1059 | "cell_type": "markdown", 1060 | "metadata": {}, 1061 | "source": [ 1062 | "#### Tuples\n", 1063 | "\n", 1064 | "Tuples are very similar to lists, but with a huge difference: **they're immutable**. That means, once a tuple is created, it can't be further modified:" 1065 | ] 1066 | }, 1067 | { 1068 | "cell_type": "code", 1069 | "execution_count": null, 1070 | "metadata": {}, 1071 | "outputs": [], 1072 | "source": [ 1073 | "t = (3, 'Hello World', True)" 1074 | ] 1075 | }, 1076 | { 1077 | "cell_type": "markdown", 1078 | "metadata": {}, 1079 | "source": [ 1080 | "Indexing them works in the same way:" 1081 | ] 1082 | }, 1083 | { 1084 | "cell_type": "code", 1085 | "execution_count": null, 1086 | "metadata": {}, 1087 | "outputs": [], 1088 | "source": [ 1089 | "t[0]" 1090 | ] 1091 | }, 1092 | { 1093 | "cell_type": "code", 1094 | "execution_count": null, 1095 | "metadata": {}, 1096 | "outputs": [], 1097 | "source": [ 1098 | "t[-1]" 1099 | ] 1100 | }, 1101 | { 1102 | "cell_type": "code", 1103 | "execution_count": null, 1104 | "metadata": {}, 1105 | "outputs": [], 1106 | "source": [ 1107 | "'Hello World' in t" 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "markdown", 1112 | "metadata": {}, 1113 | "source": [ 1114 | "But there's no way of modifying them." 1115 | ] 1116 | }, 1117 | { 1118 | "cell_type": "markdown", 1119 | "metadata": {}, 1120 | "source": [ 1121 | "#### Dictionaries\n", 1122 | "\n", 1123 | "Dictionaries are map-like collections that store values under a user-defined key. The key must be an immutable object; we usually employ strings for keys. Dictionaries are mutable, and more importantly, **unordered**." 1124 | ] 1125 | }, 1126 | { 1127 | "cell_type": "code", 1128 | "execution_count": null, 1129 | "metadata": {}, 1130 | "outputs": [], 1131 | "source": [ 1132 | "user = {\n", 1133 | " \"name\": \"Mary Smith\",\n", 1134 | " \"email\": \"mary@example.com\",\n", 1135 | " \"age\": 30,\n", 1136 | " \"subscribed\": True\n", 1137 | "}" 1138 | ] 1139 | }, 1140 | { 1141 | "cell_type": "code", 1142 | "execution_count": null, 1143 | "metadata": {}, 1144 | "outputs": [], 1145 | "source": [ 1146 | "user" 1147 | ] 1148 | }, 1149 | { 1150 | "cell_type": "markdown", 1151 | "metadata": {}, 1152 | "source": [ 1153 | "Access is by key, also using square brackets:" 1154 | ] 1155 | }, 1156 | { 1157 | "cell_type": "code", 1158 | "execution_count": null, 1159 | "metadata": {}, 1160 | "outputs": [], 1161 | "source": [ 1162 | "user['email']" 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "code", 1167 | "execution_count": null, 1168 | "metadata": {}, 1169 | "outputs": [], 1170 | "source": [ 1171 | "'age' in user" 1172 | ] 1173 | }, 1174 | { 1175 | "cell_type": "code", 1176 | "execution_count": null, 1177 | "metadata": {}, 1178 | "outputs": [], 1179 | "source": [ 1180 | "'last_name' in user" 1181 | ] 1182 | }, 1183 | { 1184 | "cell_type": "markdown", 1185 | "metadata": {}, 1186 | "source": [ 1187 | "#### Sets\n", 1188 | "\n", 1189 | "Sets are unordered collection which the unique characteristic that they only contain unique elements:" 1190 | ] 1191 | }, 1192 | { 1193 | "cell_type": "code", 1194 | "execution_count": null, 1195 | "metadata": {}, 1196 | "outputs": [], 1197 | "source": [ 1198 | "s = {3, 1, 3, 7, 9, 1, 3, 1}" 1199 | ] 1200 | }, 1201 | { 1202 | "cell_type": "code", 1203 | "execution_count": null, 1204 | "metadata": {}, 1205 | "outputs": [], 1206 | "source": [ 1207 | "s" 1208 | ] 1209 | }, 1210 | { 1211 | "cell_type": "markdown", 1212 | "metadata": {}, 1213 | "source": [ 1214 | "Adding elements is done with the `add` method:" 1215 | ] 1216 | }, 1217 | { 1218 | "cell_type": "code", 1219 | "execution_count": null, 1220 | "metadata": {}, 1221 | "outputs": [], 1222 | "source": [ 1223 | "s.add(10)" 1224 | ] 1225 | }, 1226 | { 1227 | "cell_type": "markdown", 1228 | "metadata": {}, 1229 | "source": [ 1230 | "Removing elements can be done with `pop()`:" 1231 | ] 1232 | }, 1233 | { 1234 | "cell_type": "code", 1235 | "execution_count": null, 1236 | "metadata": {}, 1237 | "outputs": [], 1238 | "source": [ 1239 | "s.pop()" 1240 | ] 1241 | }, 1242 | { 1243 | "cell_type": "markdown", 1244 | "metadata": {}, 1245 | "source": [ 1246 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1247 | "\n", 1248 | "### Iterating collections\n", 1249 | "\n", 1250 | "As mentioned in the control flow section, Python's `for` loop is specially designed to iterate over collections:" 1251 | ] 1252 | }, 1253 | { 1254 | "cell_type": "code", 1255 | "execution_count": null, 1256 | "metadata": {}, 1257 | "outputs": [], 1258 | "source": [ 1259 | "l = [3, 'Hello World', True]" 1260 | ] 1261 | }, 1262 | { 1263 | "cell_type": "code", 1264 | "execution_count": null, 1265 | "metadata": {}, 1266 | "outputs": [], 1267 | "source": [ 1268 | "for elem in l:\n", 1269 | " print(elem)" 1270 | ] 1271 | }, 1272 | { 1273 | "cell_type": "code", 1274 | "execution_count": null, 1275 | "metadata": {}, 1276 | "outputs": [], 1277 | "source": [ 1278 | "for key in user:\n", 1279 | " print(key.title(), '=>', user[key])" 1280 | ] 1281 | }, 1282 | { 1283 | "cell_type": "markdown", 1284 | "metadata": {}, 1285 | "source": [ 1286 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1287 | "\n", 1288 | "### Modules\n", 1289 | "\n", 1290 | "One of the best features of Python as a language, is its rich builtin library. To use external modules, you must first import them:" 1291 | ] 1292 | }, 1293 | { 1294 | "cell_type": "code", 1295 | "execution_count": null, 1296 | "metadata": {}, 1297 | "outputs": [], 1298 | "source": [ 1299 | "import random" 1300 | ] 1301 | }, 1302 | { 1303 | "cell_type": "code", 1304 | "execution_count": null, 1305 | "metadata": {}, 1306 | "outputs": [], 1307 | "source": [ 1308 | "random.randint(0, 99)" 1309 | ] 1310 | }, 1311 | { 1312 | "cell_type": "markdown", 1313 | "metadata": {}, 1314 | "source": [ 1315 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1316 | "\n", 1317 | "### Exceptions\n", 1318 | "\n", 1319 | "Exceptions are raised at runtime when an abnormal situation is produced in your program. Exceptions can also be constructed and raised by your code. Example of an exception:" 1320 | ] 1321 | }, 1322 | { 1323 | "cell_type": "code", 1324 | "execution_count": null, 1325 | "metadata": {}, 1326 | "outputs": [], 1327 | "source": [ 1328 | "age = \"30\"" 1329 | ] 1330 | }, 1331 | { 1332 | "cell_type": "code", 1333 | "execution_count": null, 1334 | "metadata": {}, 1335 | "outputs": [], 1336 | "source": [ 1337 | "if age > 21:\n", 1338 | " print(\"Allowed entrance\")" 1339 | ] 1340 | }, 1341 | { 1342 | "cell_type": "markdown", 1343 | "metadata": {}, 1344 | "source": [ 1345 | "Exceptions can be handled at runtime with a `try/except` block:" 1346 | ] 1347 | }, 1348 | { 1349 | "cell_type": "code", 1350 | "execution_count": null, 1351 | "metadata": {}, 1352 | "outputs": [], 1353 | "source": [ 1354 | "try:\n", 1355 | " if age > 21:\n", 1356 | " print(\"Allowed entrance\")\n", 1357 | "except:\n", 1358 | " print(\"Something went wrong\")" 1359 | ] 1360 | }, 1361 | { 1362 | "cell_type": "markdown", 1363 | "metadata": {}, 1364 | "source": [ 1365 | "The `except` portion can receive also be parametrized with the expected exception:" 1366 | ] 1367 | }, 1368 | { 1369 | "cell_type": "code", 1370 | "execution_count": null, 1371 | "metadata": {}, 1372 | "outputs": [], 1373 | "source": [ 1374 | "try:\n", 1375 | " if age > 21:\n", 1376 | " print(\"Allowed entrance\")\n", 1377 | "except TypeError:\n", 1378 | " print(\"Age is probably of a wrong type\")" 1379 | ] 1380 | }, 1381 | { 1382 | "cell_type": "markdown", 1383 | "metadata": {}, 1384 | "source": [ 1385 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1386 | "\n", 1387 | "## We're just getting started\n", 1388 | "\n", 1389 | "As you can imagine, there's a lot more to add about such a versatile and powerful language as Python. We're still missing many important topics like _OOP_, _File Management_, _Networking_, _Concurrency_, etc.\n", 1390 | "\n", 1391 | "If you're eager to learn more about Python, we've compiled a list of [3 recommended free Python books](https://blog.rmotr.com/the-3-python-books-you-need-to-get-started-for-free-9b72a2c6fb17) to get started." 1392 | ] 1393 | }, 1394 | { 1395 | "cell_type": "markdown", 1396 | "metadata": {}, 1397 | "source": [ 1398 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 1399 | ] 1400 | } 1401 | ], 1402 | "metadata": { 1403 | "kernelspec": { 1404 | "display_name": "Python 3", 1405 | "language": "python", 1406 | "name": "python3" 1407 | }, 1408 | "language_info": { 1409 | "codemirror_mode": { 1410 | "name": "ipython", 1411 | "version": 3 1412 | }, 1413 | "file_extension": ".py", 1414 | "mimetype": "text/x-python", 1415 | "name": "python", 1416 | "nbconvert_exporter": "python", 1417 | "pygments_lexer": "ipython3", 1418 | "version": "3.6.8" 1419 | } 1420 | }, 1421 | "nbformat": 4, 1422 | "nbformat_minor": 2 1423 | } 1424 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### rmotr.com 2 | # Data Science with Python Course 3 | 4 | This material is created for our [Data Science with Python Course](https://rmotr.com/data-science-python-course) --------------------------------------------------------------------------------