├── .gitignore ├── LICENSE ├── README.rst ├── part-0.ipynb ├── part-1.ipynb ├── part-2.ipynb ├── part-3.ipynb ├── part-4.ipynb ├── part-5.ipynb ├── part3 ├── ex01 │ └── birthday.py ├── ex02 │ └── birthday.py ├── ex03 │ └── birthday.py ├── ex04 │ └── birthday.py ├── ex05-exercise │ ├── answer01a.py │ ├── answer01b.py │ ├── answer01c.py │ └── answer02.py ├── ex05 │ └── birthday.py ├── ex06 │ └── hi.py ├── ex07 │ └── hi.py ├── ex08 │ └── factors.py ├── ex09 │ └── cupcakes.py └── ex10 │ ├── cupcakes.py │ └── main.py ├── slides ├── 20-things-part-a.key ├── 20-things-part-a.pdf ├── 20-things-part-b.key ├── 20-things-part-b.pdf ├── conclusion.key ├── conclusion.pdf ├── part-3.key └── part-3.pdf └── watermark.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | eggs/ 15 | lib/ 16 | lib64/ 17 | parts/ 18 | sdist/ 19 | var/ 20 | *.egg-info/ 21 | .installed.cfg 22 | *.egg 23 | 24 | # PyInstaller 25 | # Usually these files are written by a python script from a template 26 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 27 | *.manifest 28 | *.spec 29 | 30 | # Installer logs 31 | pip-log.txt 32 | pip-delete-this-directory.txt 33 | 34 | # Unit test / coverage reports 35 | htmlcov/ 36 | .tox/ 37 | .coverage 38 | .cache 39 | nosetests.xml 40 | coverage.xml 41 | 42 | # Translations 43 | *.mo 44 | *.pot 45 | 46 | # Django stuff: 47 | *.log 48 | 49 | # Sphinx documentation 50 | docs/_build/ 51 | 52 | # PyBuilder 53 | target/ 54 | 55 | # IPython Notebook 56 | .ipynb_checkpoints/ 57 | 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Attribution 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More_considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution 4.0 International Public License 58 | 59 | By exercising the Licensed Rights (defined below), You accept and agree 60 | to be bound by the terms and conditions of this Creative Commons 61 | Attribution 4.0 International Public License ("Public License"). To the 62 | extent this Public License may be interpreted as a contract, You are 63 | granted the Licensed Rights in consideration of Your acceptance of 64 | these terms and conditions, and the Licensor grants You such rights in 65 | consideration of benefits the Licensor receives from making the 66 | Licensed Material available under these terms and conditions. 67 | 68 | 69 | Section 1 -- Definitions. 70 | 71 | a. Adapted Material means material subject to Copyright and Similar 72 | Rights that is derived from or based upon the Licensed Material 73 | and in which the Licensed Material is translated, altered, 74 | arranged, transformed, or otherwise modified in a manner requiring 75 | permission under the Copyright and Similar Rights held by the 76 | Licensor. For purposes of this Public License, where the Licensed 77 | Material is a musical work, performance, or sound recording, 78 | Adapted Material is always produced where the Licensed Material is 79 | synched in timed relation with a moving image. 80 | 81 | b. Adapter's License means the license You apply to Your Copyright 82 | and Similar Rights in Your contributions to Adapted Material in 83 | accordance with the terms and conditions of this Public License. 84 | 85 | c. Copyright and Similar Rights means copyright and/or similar rights 86 | closely related to copyright including, without limitation, 87 | performance, broadcast, sound recording, and Sui Generis Database 88 | Rights, without regard to how the rights are labeled or 89 | categorized. For purposes of this Public License, the rights 90 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 91 | Rights. 92 | 93 | d. Effective Technological Measures means those measures that, in the 94 | absence of proper authority, may not be circumvented under laws 95 | fulfilling obligations under Article 11 of the WIPO Copyright 96 | Treaty adopted on December 20, 1996, and/or similar international 97 | agreements. 98 | 99 | e. Exceptions and Limitations means fair use, fair dealing, and/or 100 | any other exception or limitation to Copyright and Similar Rights 101 | that applies to Your use of the Licensed Material. 102 | 103 | f. Licensed Material means the artistic or literary work, database, 104 | or other material to which the Licensor applied this Public 105 | License. 106 | 107 | g. Licensed Rights means the rights granted to You subject to the 108 | terms and conditions of this Public License, which are limited to 109 | all Copyright and Similar Rights that apply to Your use of the 110 | Licensed Material and that the Licensor has authority to license. 111 | 112 | h. Licensor means the individual(s) or entity(ies) granting rights 113 | under this Public License. 114 | 115 | i. Share means to provide material to the public by any means or 116 | process that requires permission under the Licensed Rights, such 117 | as reproduction, public display, public performance, distribution, 118 | dissemination, communication, or importation, and to make material 119 | available to the public including in ways that members of the 120 | public may access the material from a place and at a time 121 | individually chosen by them. 122 | 123 | j. Sui Generis Database Rights means rights other than copyright 124 | resulting from Directive 96/9/EC of the European Parliament and of 125 | the Council of 11 March 1996 on the legal protection of databases, 126 | as amended and/or succeeded, as well as other essentially 127 | equivalent rights anywhere in the world. 128 | 129 | k. You means the individual or entity exercising the Licensed Rights 130 | under this Public License. Your has a corresponding meaning. 131 | 132 | 133 | Section 2 -- Scope. 134 | 135 | a. License grant. 136 | 137 | 1. Subject to the terms and conditions of this Public License, 138 | the Licensor hereby grants You a worldwide, royalty-free, 139 | non-sublicensable, non-exclusive, irrevocable license to 140 | exercise the Licensed Rights in the Licensed Material to: 141 | 142 | a. reproduce and Share the Licensed Material, in whole or 143 | in part; and 144 | 145 | b. produce, reproduce, and Share Adapted Material. 146 | 147 | 2. Exceptions and Limitations. For the avoidance of doubt, where 148 | Exceptions and Limitations apply to Your use, this Public 149 | License does not apply, and You do not need to comply with 150 | its terms and conditions. 151 | 152 | 3. Term. The term of this Public License is specified in Section 153 | 6(a). 154 | 155 | 4. Media and formats; technical modifications allowed. The 156 | Licensor authorizes You to exercise the Licensed Rights in 157 | all media and formats whether now known or hereafter created, 158 | and to make technical modifications necessary to do so. The 159 | Licensor waives and/or agrees not to assert any right or 160 | authority to forbid You from making technical modifications 161 | necessary to exercise the Licensed Rights, including 162 | technical modifications necessary to circumvent Effective 163 | Technological Measures. For purposes of this Public License, 164 | simply making modifications authorized by this Section 2(a) 165 | (4) never produces Adapted Material. 166 | 167 | 5. Downstream recipients. 168 | 169 | a. Offer from the Licensor -- Licensed Material. Every 170 | recipient of the Licensed Material automatically 171 | receives an offer from the Licensor to exercise the 172 | Licensed Rights under the terms and conditions of this 173 | Public License. 174 | 175 | b. No downstream restrictions. You may not offer or impose 176 | any additional or different terms or conditions on, or 177 | apply any Effective Technological Measures to, the 178 | Licensed Material if doing so restricts exercise of the 179 | Licensed Rights by any recipient of the Licensed 180 | Material. 181 | 182 | 6. No endorsement. Nothing in this Public License constitutes or 183 | may be construed as permission to assert or imply that You 184 | are, or that Your use of the Licensed Material is, connected 185 | with, or sponsored, endorsed, or granted official status by, 186 | the Licensor or others designated to receive attribution as 187 | provided in Section 3(a)(1)(A)(i). 188 | 189 | b. Other rights. 190 | 191 | 1. Moral rights, such as the right of integrity, are not 192 | licensed under this Public License, nor are publicity, 193 | privacy, and/or other similar personality rights; however, to 194 | the extent possible, the Licensor waives and/or agrees not to 195 | assert any such rights held by the Licensor to the limited 196 | extent necessary to allow You to exercise the Licensed 197 | Rights, but not otherwise. 198 | 199 | 2. Patent and trademark rights are not licensed under this 200 | Public License. 201 | 202 | 3. To the extent possible, the Licensor waives any right to 203 | collect royalties from You for the exercise of the Licensed 204 | Rights, whether directly or through a collecting society 205 | under any voluntary or waivable statutory or compulsory 206 | licensing scheme. In all other cases the Licensor expressly 207 | reserves any right to collect such royalties. 208 | 209 | 210 | Section 3 -- License Conditions. 211 | 212 | Your exercise of the Licensed Rights is expressly made subject to the 213 | following conditions. 214 | 215 | a. Attribution. 216 | 217 | 1. If You Share the Licensed Material (including in modified 218 | form), You must: 219 | 220 | a. retain the following if it is supplied by the Licensor 221 | with the Licensed Material: 222 | 223 | i. identification of the creator(s) of the Licensed 224 | Material and any others designated to receive 225 | attribution, in any reasonable manner requested by 226 | the Licensor (including by pseudonym if 227 | designated); 228 | 229 | ii. a copyright notice; 230 | 231 | iii. a notice that refers to this Public License; 232 | 233 | iv. a notice that refers to the disclaimer of 234 | warranties; 235 | 236 | v. a URI or hyperlink to the Licensed Material to the 237 | extent reasonably practicable; 238 | 239 | b. indicate if You modified the Licensed Material and 240 | retain an indication of any previous modifications; and 241 | 242 | c. indicate the Licensed Material is licensed under this 243 | Public License, and include the text of, or the URI or 244 | hyperlink to, this Public License. 245 | 246 | 2. You may satisfy the conditions in Section 3(a)(1) in any 247 | reasonable manner based on the medium, means, and context in 248 | which You Share the Licensed Material. For example, it may be 249 | reasonable to satisfy the conditions by providing a URI or 250 | hyperlink to a resource that includes the required 251 | information. 252 | 253 | 3. If requested by the Licensor, You must remove any of the 254 | information required by Section 3(a)(1)(A) to the extent 255 | reasonably practicable. 256 | 257 | 4. If You Share Adapted Material You produce, the Adapter's 258 | License You apply must not prevent recipients of the Adapted 259 | Material from complying with this Public License. 260 | 261 | 262 | Section 4 -- Sui Generis Database Rights. 263 | 264 | Where the Licensed Rights include Sui Generis Database Rights that 265 | apply to Your use of the Licensed Material: 266 | 267 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 268 | to extract, reuse, reproduce, and Share all or a substantial 269 | portion of the contents of the database; 270 | 271 | b. if You include all or a substantial portion of the database 272 | contents in a database in which You have Sui Generis Database 273 | Rights, then the database in which You have Sui Generis Database 274 | Rights (but not its individual contents) is Adapted Material; and 275 | 276 | c. You must comply with the conditions in Section 3(a) if You Share 277 | all or a substantial portion of the contents of the database. 278 | 279 | For the avoidance of doubt, this Section 4 supplements and does not 280 | replace Your obligations under this Public License where the Licensed 281 | Rights include other Copyright and Similar Rights. 282 | 283 | 284 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 285 | 286 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 287 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 288 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 289 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 290 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 291 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 292 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 293 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 294 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 295 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 296 | 297 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 298 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 299 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 300 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 301 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 302 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 303 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 304 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 305 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 306 | 307 | c. The disclaimer of warranties and limitation of liability provided 308 | above shall be interpreted in a manner that, to the extent 309 | possible, most closely approximates an absolute disclaimer and 310 | waiver of all liability. 311 | 312 | 313 | Section 6 -- Term and Termination. 314 | 315 | a. This Public License applies for the term of the Copyright and 316 | Similar Rights licensed here. However, if You fail to comply with 317 | this Public License, then Your rights under this Public License 318 | terminate automatically. 319 | 320 | b. Where Your right to use the Licensed Material has terminated under 321 | Section 6(a), it reinstates: 322 | 323 | 1. automatically as of the date the violation is cured, provided 324 | it is cured within 30 days of Your discovery of the 325 | violation; or 326 | 327 | 2. upon express reinstatement by the Licensor. 328 | 329 | For the avoidance of doubt, this Section 6(b) does not affect any 330 | right the Licensor may have to seek remedies for Your violations 331 | of this Public License. 332 | 333 | c. For the avoidance of doubt, the Licensor may also offer the 334 | Licensed Material under separate terms or conditions or stop 335 | distributing the Licensed Material at any time; however, doing so 336 | will not terminate this Public License. 337 | 338 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 339 | License. 340 | 341 | 342 | Section 7 -- Other Terms and Conditions. 343 | 344 | a. The Licensor shall not be bound by any additional or different 345 | terms or conditions communicated by You unless expressly agreed. 346 | 347 | b. Any arrangements, understandings, or agreements regarding the 348 | Licensed Material not stated herein are separate from and 349 | independent of the terms and conditions of this Public License. 350 | 351 | 352 | Section 8 -- Interpretation. 353 | 354 | a. For the avoidance of doubt, this Public License does not, and 355 | shall not be interpreted to, reduce, limit, restrict, or impose 356 | conditions on any use of the Licensed Material that could lawfully 357 | be made without permission under this Public License. 358 | 359 | b. To the extent possible, if any provision of this Public License is 360 | deemed unenforceable, it shall be automatically reformed to the 361 | minimum extent necessary to make it enforceable. If the provision 362 | cannot be reformed, it shall be severed from this Public License 363 | without affecting the enforceability of the remaining terms and 364 | conditions. 365 | 366 | c. No term or condition of this Public License will be waived and no 367 | failure to comply consented to unless expressly agreed to by the 368 | Licensor. 369 | 370 | d. Nothing in this Public License constitutes or may be interpreted 371 | as a limitation upon, or waiver of, any privileges and immunities 372 | that apply to the Licensor or You, including from the legal 373 | processes of any jurisdiction or authority. 374 | 375 | 376 | ======================================================================= 377 | 378 | Creative Commons is not a party to its public licenses. 379 | Notwithstanding, Creative Commons may elect to apply one of its public 380 | licenses to material it publishes and in those instances will be 381 | considered the "Licensor." Except for the limited purpose of indicating 382 | that material is shared under a Creative Commons public license or as 383 | otherwise permitted by the Creative Commons policies published at 384 | creativecommons.org/policies, Creative Commons does not authorize the 385 | use of the trademark "Creative Commons" or any other trademark or logo 386 | of Creative Commons without its prior written consent including, 387 | without limitation, in connection with any unauthorized modifications 388 | to any of its public licenses or any other arrangements, 389 | understandings, or agreements concerning use of licensed material. For 390 | the avoidance of doubt, this paragraph does not form part of the public 391 | licenses. 392 | 393 | Creative Commons may be contacted at creativecommons.org. 394 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Intro to Python Workshop, San Diego, 2014 2 | ========================================== 3 | 4 | Brought to you by San Diego Python, San Diego PyLadies, Inland Empire Python, 5 | Inland Empire PyLadies. 6 | 7 | This repo is adapted from the geekgirl repo, which was for the San Diego PyLadies 8 | workshop at the Geek Girl Tech Conference on Saturday June 21, 2014. 9 | 10 | Instructions For Students 11 | ============================= 12 | 13 | Before coming to the workshop, do the pre-workshop setup: `part-0.ipynb`_ 14 | 15 | Workshop Agenda and Files 16 | ------------------------- 17 | 18 | Students can follow along with the slides and speaker notes. This is helpful 19 | in case anyone falls behind and misses something. 20 | 21 | 9:30-12:30 Morning 22 | ~~~~~~~~~~~~~~~~~~~ 23 | 24 | Welcome and Intros 25 | 26 | * Finish setup if you haven't yet: `part-0.ipynb`_ 27 | 28 | 20 Cool Things You Can Do With Python, Part A 29 | 30 | * Slides: `20-things-part-a.pdf`_ 31 | 32 | Part 1: Numbers, strings, variables, booleans, "if statements" 33 | 34 | * REPL demo examples: `part-1.ipynb`_ 35 | 36 | Part 2: Lists and loops 37 | 38 | * REPL demo examples: `part-2.ipynb`_ 39 | 40 | 12:30-1:15pm Lunch break 41 | ~~~~~~~~~~~~~~~~~~~~~~~~~ 42 | 43 | Eat and meet other students. 44 | 45 | 1:15-4pm Afternoon 46 | ~~~~~~~~~~~~~~~~~~~ 47 | 48 | 20 Cool Things You Can Do With Python, Part B 49 | 50 | * Slides: `20-things-part-b.pdf`_ 51 | 52 | Functions and modules 53 | 54 | * Slides: `part-3.pdf`_ 55 | * Code examples: `part3/`_ 56 | 57 | List comprehensions, dictionaries, and tuples 58 | 59 | * REPL demo examples: `part-4.ipynb`_ 60 | 61 | Conclusion: stdlib, PyPI, next steps 62 | 63 | * Slides: `conclusion.pdf`_ 64 | 65 | * `Survey`_ 66 | 67 | Instructions For Instructors 68 | ============================= 69 | 70 | Giving the Talk 71 | --------------- 72 | 73 | Before giving the talk, IPython Notebook must be installed: 74 | 75 | .. code-block:: bash 76 | 77 | $ pip install 'ipython[notebook]' 78 | 79 | How to run an IPython Notebook: 80 | 81 | .. code-block:: bash 82 | 83 | $ ipython notebook part-1.ipynb 84 | 85 | You can view each of the files online using `nbviewer`_. 86 | 87 | At the workshop we gave the talk like this: 88 | 89 | * We opened a Python shell on the projector monitor and an IPython notebook on the laptop monitor 90 | * One person read the notebook and typed into the Python shell 91 | * Another person stood in front of the audience and explained each step 92 | * Questions were answered ad-hoc, occasionally by Googling answers or typing at the terminal 93 | 94 | 95 | Copying 96 | ------- 97 | 98 | You can give this talk too! 99 | 100 | This work is licensed under a `Creative Commons Attribution 4.0 International License`_. See LICENSE_ file for more details. 101 | 102 | 103 | .. _nbviewer: http://nbviewer.ipython.org/ 104 | .. _part-0.ipynb: http://nbviewer.ipython.org/github/PythonWorkshop/intro-to-python/blob/master/part-0.ipynb 105 | .. _20-things-part-a.pdf: https://github.com/PythonWorkshop/intro-to-python/tree/master/slides/20-things-part-a.pdf 106 | .. _part-1.ipynb: http://nbviewer.ipython.org/github/PythonWorkshop/intro-to-python/blob/master/part-1.ipynb 107 | .. _part-2.ipynb: http://nbviewer.ipython.org/github/PythonWorkshop/intro-to-python/blob/master/part-2.ipynb 108 | .. _20-things-part-b.pdf: https://github.com/PythonWorkshop/intro-to-python/tree/master/slides/20-things-part-b.pdf 109 | .. _part-3.pdf: https://github.com/PythonWorkshop/intro-to-python/tree/master/slides/part-3.pdf 110 | .. _`part3/`: https://github.com/PythonWorkshop/intro-to-python/tree/master/part3 111 | .. _part-4.ipynb: http://nbviewer.ipython.org/github/PythonWorkshop/intro-to-python/blob/master/part-4.ipynb 112 | .. _conclusion.pdf: https://github.com/PythonWorkshop/intro-to-python/tree/master/slides/conclusion.pdf 113 | .. _Survey: https://www.surveymonkey.com/s/8KX9GGQ 114 | .. _Creative Commons Attribution 4.0 International License: http://creativecommons.org/licenses/by/4.0/ 115 | .. _LICENSE: LICENSE 116 | -------------------------------------------------------------------------------- /part-0.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "kernelspec": { 4 | "codemirror_mode": { 5 | "name": "ipython", 6 | "version": 2 7 | }, 8 | "display_name": "IPython (Python 2)", 9 | "language": "python", 10 | "name": "python2" 11 | }, 12 | "name": "", 13 | "signature": "sha256:bcf9b99fa7aeba5ee12ed312039e072c00cf46f8cd218cd9ce2559061b52e6d6" 14 | }, 15 | "nbformat": 3, 16 | "nbformat_minor": 0, 17 | "worksheets": [ 18 | { 19 | "cells": [ 20 | { 21 | "cell_type": "heading", 22 | "level": 1, 23 | "metadata": {}, 24 | "source": [ 25 | "Setup" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "**Please complete the instructions on this page before you arrive at the Intro to Python workshop.**\n", 33 | "\n", 34 | "Stuck or need help? [Ask for help by describing your error here](https://github.com/pythonsd/intro-to-python/issues/new), in as much detail as possible. Or ask for help through [SD Python](http://www.meetup.com/pythonsd/), [SD Pyladies](http://www.meetup.com/sd-pyladies/), or [IE Pyladies](http://www.meetup.com/iepyladies/)." 35 | ] 36 | }, 37 | { 38 | "cell_type": "heading", 39 | "level": 2, 40 | "metadata": {}, 41 | "source": [ 42 | "Windows" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Please go to this website page https://www.python.org/downloads/ to download the Python installer:\n", 50 | "- click on the `Download Python 3.4.1` button (a yellow button on left side of screen),\n", 51 | "- this will start the download of the appropriate Windows MSI installer \n", 52 | "- accept the default download filename: `python-3.4.1.msi`(win32bits) or `python-3.4.1.amd.msi`(win64bits) \n", 53 | "- save the MSI Installer file to your pc,\n", 54 | "- double click the downloaded file to open it and run the installer \n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "heading", 59 | "level": 2, 60 | "metadata": {}, 61 | "source": [ 62 | "Linux" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "It is very likely that you already have Python installed out of the box. To check if you have it installed (and which version it is), open a console and type the following command:\n", 70 | "\n", 71 | "```\n", 72 | "$ python3 --version\n", 73 | "Python 3.4.1\n", 74 | "```\n", 75 | "\n", 76 | "If you don't have Python installed or if you have a version older then 3.4 , you can install version 3.4 as follows:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "heading", 81 | "level": 4, 82 | "metadata": {}, 83 | "source": [ 84 | "Ubuntu" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "Type this command into your console:\n", 92 | "\n", 93 | "```\n", 94 | "sudo apt-get install python3.4\n", 95 | "```" 96 | ] 97 | }, 98 | { 99 | "cell_type": "heading", 100 | "level": 4, 101 | "metadata": {}, 102 | "source": [ 103 | "Fedora" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "Use this command in your console:\n", 111 | "\n", 112 | "```\n", 113 | "sudo yum install python3.4\n", 114 | "```" 115 | ] 116 | }, 117 | { 118 | "cell_type": "heading", 119 | "level": 2, 120 | "metadata": {}, 121 | "source": [ 122 | "Mac OS X" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "Please go to this website page https://www.python.org/downloads/ to download the Python installer:\n", 130 | "- click on the `Download Python 3.4.1` button (a yellow button on left side of screen),\n", 131 | "- download the Mac OS X 64-bit/32-bit installer dmg file `python-3.4.1-macosx10.6.dmg` and save it to your mac,\n", 132 | "- double click the downloaded file to open it and run the installer (Tip: If you forget or can't find where you saved the downloaded file, you may use the magnifying glass in the upper right corner of the screen to search for the file by name.)\n", 133 | "\n", 134 | "\n", 135 | "** Verify the installation **\n", 136 | "\n", 137 | "Verify the installation was successful by opening the Terminal application and running the python3 command by typing python3 --version after the `$` command prompt:\n", 138 | "```\n", 139 | "$ python3 --version\n", 140 | "Python 3.4.1\n", 141 | "```" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "**Are you using Mac OS X 10.5 (a possibility if you have a Mac laptop built before mid-2010)?** \n", 149 | "If you are using Mac OS X 10.5, your installation process will differ slightly. (Tip: You may check your Mac OS X version by selecting the Apple icon in the top left corner of the screen and choosing \"About This Mac\".)\n", 150 | "\n", 151 | "For Mac OS X 10.5 users only:\n", 152 | "- click on this link: https://www.python.org/downloads/release/python-341/\n", 153 | "- scroll down to \"Files\" heading and double click on \"Mac OS X 32-bit i386/PPC installer\"\n", 154 | "- save the \"Mac OS X 32-bit i386/PPC installer\" dmg file `python-3.4.1-macosx10.5.dmg` to your mac,\n", 155 | "- double click the downloaded file to open it and run the installer (Tip: If you forget or can't find where you saved the downloaded file, you may use the magnifying glass in the upper right corner of the screen to search for the file by name.),\n", 156 | "- follow the steps above to verify your installation" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "*Attribution: These instructions are partially based on tutorials by [Geek Girls Carrots](http://django.carrots.pl/) and [DjangoGirls](http://tutorial.djangogirls.org/python_installation/README.html).*" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "collapsed": false, 169 | "input": [], 170 | "language": "python", 171 | "metadata": {}, 172 | "outputs": [] 173 | } 174 | ], 175 | "metadata": {} 176 | } 177 | ] 178 | } 179 | -------------------------------------------------------------------------------- /part-1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "kernelspec": { 4 | "codemirror_mode": { 5 | "name": "ipython", 6 | "version": 2 7 | }, 8 | "display_name": "IPython (Python 2)", 9 | "language": "python", 10 | "name": "python2" 11 | }, 12 | "name": "", 13 | "signature": "sha256:ee402650c9c8bdf6b8ce855b70a387d27a8deea6b89f3d23cf71c6a67169f6fb" 14 | }, 15 | "nbformat": 3, 16 | "nbformat_minor": 0, 17 | "worksheets": [ 18 | { 19 | "cells": [ 20 | { 21 | "cell_type": "heading", 22 | "level": 1, 23 | "metadata": {}, 24 | "source": [ 25 | "Introduction to Python Workshop\n", 26 | "Part 1" 27 | ] 28 | }, 29 | { 30 | "cell_type": "heading", 31 | "level": 2, 32 | "metadata": {}, 33 | "source": [ 34 | "Welcome again!" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "We want to thank the many people that have made this workshop possible.\n", 42 | "\n", 43 | "First, the generosity of our sponsors have provided facilities for the workshop, food and refreshments, and travel assistance for our guest speakers. Please give a hand to our fabulous sponsors:\n", 44 | "- [Ansir Innovation Center](http://ansirsd.com/) Twitter: @AnsirSD\n", 45 | "- [Python Software Foundation](https://www.python.org/psf/) Twitter: @ThePSF\n", 46 | "\n", 47 | "We are committed to offering a positive and productive workshop for you. We are proud to be an [OpenHatch](https://openhatch.org) (@openhatch) affiliated event. OpenHatch is a non-profit that helps people become contributors to free and open source software. OpenHatch is a friendly community and can help you find a suitable project if you are interested in contributing.\n", 48 | "\n", 49 | "While we are thanking the PSF and OpenHatch, I would like to thank Jessica McKellar, PSF Director and OpenHatch board member (@jessicamckellar), for sharing her materials from her Intro to Python workshop as well as providing encouragement and support to us.\n", 50 | "\n", 51 | "If anyone wishes to tweet their appreciation, please do so.\n", 52 | "\n" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "A programming community outreach workshop, brought to you by the generous volunteers and leadership from:\n", 60 | "\n", 61 | "- [PyLadies San Diego](www.meetup.com/sd-pyladies/)\n", 62 | "- [San Diego Python User Group](www.meetup.com/pythonsd/)\n", 63 | "- [Inland Empire Pyladies](www.meetup.com/iepyladies/)\n", 64 | "- [Inland Empire Python](www.meetup.com/iepython/)\n", 65 | "\n", 66 | "Thanks to David and Kendall of SDPUG, Juliet of PyLadies SD, and John of Inland Empire Python for their support." 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "**Introduction of who is here to teach and help you today.**\n", 74 | "- Audrey\n", 75 | "- Danny\n", 76 | "- Rise\n", 77 | "- Trey\n", 78 | "- Alain\n", 79 | "- Micah\n", 80 | "- Jim\n", 81 | "- Others that are helping on day of event\n", 82 | "- Carol\n", 83 | "\n", 84 | "Please take a moment to share 2-4 sentences about yourself.\n", 85 | "\n", 86 | "We are all volunteers. If you enjoy this workshop and decide to continue with Python programming, we encourage you to volunteer at the next Intro to Python Workshop.\n", 87 | "\n", 88 | "We are also very thankful that you have chosen to spend the better part of your Saturday sharing Python with us. It's a language that we have fun using to make all sorts of projects and a thoughtful, accepting, considerate, and fun community that we are glad to take part in.\n", 89 | "\n", 90 | "And now...let's get going with some Python development!\n", 91 | " " 92 | ] 93 | }, 94 | { 95 | "cell_type": "heading", 96 | "level": 2, 97 | "metadata": {}, 98 | "source": [ 99 | "The Game Plan" 100 | ] 101 | }, 102 | { 103 | "cell_type": "heading", 104 | "level": 2, 105 | "metadata": {}, 106 | "source": [ 107 | "Did you do your Python setup homework?" 108 | ] 109 | }, 110 | { 111 | "cell_type": "heading", 112 | "level": 3, 113 | "metadata": {}, 114 | "source": [ 115 | "Recap: Setting Up Python" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "You should have completed the Python 3.4 setup instructions on your own:\n", 123 | "http://nbviewer.ipython.org/github/pythonsd/intro-to-python/blob/master/part-0.ipynb\n", 124 | "\n", 125 | "If not, please do it now." 126 | ] 127 | }, 128 | { 129 | "cell_type": "heading", 130 | "level": 3, 131 | "metadata": {}, 132 | "source": [ 133 | "Stuck? Raise Your Hand / Place Yellow Sticky Note" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "Tried but ran into errors? Raise your hand or place a yellow sticky note on your screen to show that you need help. A volunteer will come over to help you.\n", 141 | "\n", 142 | "Don't be shy. Setup problems are common. It's particularly important for everyone who's stuck to get help right now before we move on, to ensure that you get the most out of this workshop." 143 | ] 144 | }, 145 | { 146 | "cell_type": "heading", 147 | "level": 3, 148 | "metadata": {}, 149 | "source": [ 150 | "Done With Setup?" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "Volunteers will be walking around to check everyone's setup. Have your setup checked.\n", 158 | "\n", 159 | "When a volunteer comes over, open up a command prompt and type this, to show that you are ready:\n", 160 | "\n", 161 | "```\n", 162 | "$ python3 --version\n", 163 | "Python 3.4.1\n", 164 | "```\n", 165 | "\n", 166 | "Once you've had your setup checked:\n", 167 | "* While waiting, introduce yourself to at least 3 of the people sitting near you. Tell them your name, something about yourself, and why you decided to come to today's workshop." 168 | ] 169 | }, 170 | { 171 | "cell_type": "heading", 172 | "level": 2, 173 | "metadata": {}, 174 | "source": [ 175 | "Setup Complete!" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "After completing these steps, you should:\n", 183 | "* Have Python installed\n", 184 | "* Be able to enter and exit Python\n", 185 | "\n" 186 | ] 187 | }, 188 | { 189 | "cell_type": "heading", 190 | "level": 6, 191 | "metadata": {}, 192 | "source": [ 193 | "Let's get started with the interactive lecture" 194 | ] 195 | }, 196 | { 197 | "cell_type": "heading", 198 | "level": 3, 199 | "metadata": {}, 200 | "source": [ 201 | "Python as a Calculator" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "From your command prompt, type `python3` to enter IDLE.\n", 209 | "\n", 210 | "Some regular math operations." 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "collapsed": false, 216 | "input": [ 217 | "2 + 2" 218 | ], 219 | "language": "python", 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "metadata": {}, 224 | "output_type": "pyout", 225 | "prompt_number": 1, 226 | "text": [ 227 | "4" 228 | ] 229 | } 230 | ], 231 | "prompt_number": 1 232 | }, 233 | { 234 | "cell_type": "code", 235 | "collapsed": false, 236 | "input": [ 237 | "1.4 + 2.25" 238 | ], 239 | "language": "python", 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "metadata": {}, 244 | "output_type": "pyout", 245 | "prompt_number": 2, 246 | "text": [ 247 | "3.65" 248 | ] 249 | } 250 | ], 251 | "prompt_number": 2 252 | }, 253 | { 254 | "cell_type": "code", 255 | "collapsed": false, 256 | "input": [ 257 | "4 - 2" 258 | ], 259 | "language": "python", 260 | "metadata": {}, 261 | "outputs": [ 262 | { 263 | "metadata": {}, 264 | "output_type": "pyout", 265 | "prompt_number": 3, 266 | "text": [ 267 | "2" 268 | ] 269 | } 270 | ], 271 | "prompt_number": 3 272 | }, 273 | { 274 | "cell_type": "code", 275 | "collapsed": false, 276 | "input": [ 277 | "2 * 3" 278 | ], 279 | "language": "python", 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "metadata": {}, 284 | "output_type": "pyout", 285 | "prompt_number": 4, 286 | "text": [ 287 | "6" 288 | ] 289 | } 290 | ], 291 | "prompt_number": 4 292 | }, 293 | { 294 | "cell_type": "code", 295 | "collapsed": false, 296 | "input": [ 297 | "4 / 2" 298 | ], 299 | "language": "python", 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "metadata": {}, 304 | "output_type": "pyout", 305 | "prompt_number": 5, 306 | "text": [ 307 | "2" 308 | ] 309 | } 310 | ], 311 | "prompt_number": 5 312 | }, 313 | { 314 | "cell_type": "code", 315 | "collapsed": false, 316 | "input": [ 317 | "0.5/2" 318 | ], 319 | "language": "python", 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "metadata": {}, 324 | "output_type": "pyout", 325 | "prompt_number": 6, 326 | "text": [ 327 | "0.25" 328 | ] 329 | } 330 | ], 331 | "prompt_number": 6 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "**Special type of division (floor)**" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "collapsed": false, 343 | "input": [ 344 | "3 // 2" 345 | ], 346 | "language": "python", 347 | "metadata": {}, 348 | "outputs": [] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "collapsed": false, 353 | "input": [ 354 | "15.5 // 2" 355 | ], 356 | "language": "python", 357 | "metadata": {}, 358 | "outputs": [] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "Horizontal line spacing does not matter in Python in an individual statement. In a multiline program it does, you typically indent 4 spaces." 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "collapsed": false, 370 | "input": [ 371 | "2 + 2" 372 | ], 373 | "language": "python", 374 | "metadata": {}, 375 | "outputs": [] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "collapsed": false, 380 | "input": [ 381 | "2+2" 382 | ], 383 | "language": "python", 384 | "metadata": {}, 385 | "outputs": [] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "metadata": {}, 390 | "source": [ 391 | "Parens and order of operations follow typical mathematics conventions in Python. `_` in IPython signifies the previous result." 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "collapsed": false, 397 | "input": [ 398 | "(1 + 3) * 4" 399 | ], 400 | "language": "python", 401 | "metadata": {}, 402 | "outputs": [] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "collapsed": false, 407 | "input": [ 408 | "x = 4" 409 | ], 410 | "language": "python", 411 | "metadata": {}, 412 | "outputs": [] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "collapsed": false, 417 | "input": [ 418 | "x * 3" 419 | ], 420 | "language": "python", 421 | "metadata": {}, 422 | "outputs": [] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "collapsed": false, 427 | "input": [ 428 | "_ + 8" 429 | ], 430 | "language": "python", 431 | "metadata": {}, 432 | "outputs": [] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "metadata": {}, 437 | "source": [ 438 | "**A trip to PyCon**" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "collapsed": false, 444 | "input": [ 445 | "jeans = 5" 446 | ], 447 | "language": "python", 448 | "metadata": {}, 449 | "outputs": [] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "collapsed": false, 454 | "input": [ 455 | "shoes = 2" 456 | ], 457 | "language": "python", 458 | "metadata": {}, 459 | "outputs": [] 460 | }, 461 | { 462 | "cell_type": "code", 463 | "collapsed": false, 464 | "input": [ 465 | "socks = 12" 466 | ], 467 | "language": "python", 468 | "metadata": {}, 469 | "outputs": [] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "collapsed": false, 474 | "input": [ 475 | "shirts = 1" 476 | ], 477 | "language": "python", 478 | "metadata": {}, 479 | "outputs": [] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "collapsed": false, 484 | "input": [ 485 | "items_packed = jeans + shoes + socks + shirts" 486 | ], 487 | "language": "python", 488 | "metadata": {}, 489 | "outputs": [] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "collapsed": false, 494 | "input": [ 495 | "items_packed" 496 | ], 497 | "language": "python", 498 | "metadata": {}, 499 | "outputs": [] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "collapsed": false, 504 | "input": [ 505 | "print(items_packed)" 506 | ], 507 | "language": "python", 508 | "metadata": {}, 509 | "outputs": [] 510 | }, 511 | { 512 | "cell_type": "markdown", 513 | "metadata": {}, 514 | "source": [ 515 | "**Using type() to find the datatype**\n", 516 | "\n", 517 | "\n", 518 | "Let's use a function. You'll be hearing more about functions later today. For now let's say that a function is like a Personal Assistant. You ask for a job to be done, and if you give the assistant the correct instructions, he will do the tasks asked.\n", 519 | "\n", 520 | "One handy thing that our Python Personal Assistant, aka Funky Function, can do is tell us a variable's current data type." 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "collapsed": false, 526 | "input": [ 527 | "type(shirts)" 528 | ], 529 | "language": "python", 530 | "metadata": {}, 531 | "outputs": [] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "collapsed": false, 536 | "input": [ 537 | "type(0.99)" 538 | ], 539 | "language": "python", 540 | "metadata": {}, 541 | "outputs": [] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": {}, 546 | "source": [ 547 | "Questions?" 548 | ] 549 | }, 550 | { 551 | "cell_type": "markdown", 552 | "metadata": {}, 553 | "source": [ 554 | "**String data type**" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "collapsed": false, 560 | "input": [ 561 | "\"Hello\"" 562 | ], 563 | "language": "python", 564 | "metadata": {}, 565 | "outputs": [] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "collapsed": false, 570 | "input": [ 571 | "\"Python, I'm your #1 fan!\"" 572 | ], 573 | "language": "python", 574 | "metadata": {}, 575 | "outputs": [] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "collapsed": false, 580 | "input": [ 581 | "type(\"Hello\")" 582 | ], 583 | "language": "python", 584 | "metadata": {}, 585 | "outputs": [] 586 | }, 587 | { 588 | "cell_type": "markdown", 589 | "metadata": {}, 590 | "source": [ 591 | "**Concatenating strings**" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "collapsed": false, 597 | "input": [ 598 | "name = \"Carol\"" 599 | ], 600 | "language": "python", 601 | "metadata": {}, 602 | "outputs": [] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "collapsed": false, 607 | "input": [ 608 | "2 + 2" 609 | ], 610 | "language": "python", 611 | "metadata": {}, 612 | "outputs": [] 613 | }, 614 | { 615 | "cell_type": "code", 616 | "collapsed": false, 617 | "input": [ 618 | "\"Carol\" + \"Willing\"" 619 | ], 620 | "language": "python", 621 | "metadata": {}, 622 | "outputs": [] 623 | }, 624 | { 625 | "cell_type": "code", 626 | "collapsed": false, 627 | "input": [ 628 | "\n", 629 | "\"Carol \" + \"Willing\"\n" 630 | ], 631 | "language": "python", 632 | "metadata": {}, 633 | "outputs": [] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "collapsed": false, 638 | "input": [ 639 | "\"Carol\" + \" \" + \"Willing\"" 640 | ], 641 | "language": "python", 642 | "metadata": {}, 643 | "outputs": [] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "collapsed": false, 648 | "input": [ 649 | "name = \"Carol\"" 650 | ], 651 | "language": "python", 652 | "metadata": {}, 653 | "outputs": [] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "collapsed": false, 658 | "input": [ 659 | "\"My name is \" + name" 660 | ], 661 | "language": "python", 662 | "metadata": {}, 663 | "outputs": [] 664 | }, 665 | { 666 | "cell_type": "markdown", 667 | "metadata": {}, 668 | "source": [ 669 | "*Tip - arrow up to save time typing*" 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "collapsed": false, 675 | "input": [ 676 | "\"Hello\" + 1" 677 | ], 678 | "language": "python", 679 | "metadata": {}, 680 | "outputs": [] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "collapsed": false, 685 | "input": [ 686 | "\"Hello\" + \"1\"" 687 | ], 688 | "language": "python", 689 | "metadata": {}, 690 | "outputs": [] 691 | }, 692 | { 693 | "cell_type": "code", 694 | "collapsed": false, 695 | "input": [ 696 | "type(1)" 697 | ], 698 | "language": "python", 699 | "metadata": {}, 700 | "outputs": [] 701 | }, 702 | { 703 | "cell_type": "code", 704 | "collapsed": false, 705 | "input": [ 706 | "type(\"1\")" 707 | ], 708 | "language": "python", 709 | "metadata": {}, 710 | "outputs": [] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "collapsed": false, 715 | "input": [ 716 | "\"Hello\" + str(1)" 717 | ], 718 | "language": "python", 719 | "metadata": {}, 720 | "outputs": [] 721 | }, 722 | { 723 | "cell_type": "code", 724 | "collapsed": false, 725 | "input": [ 726 | "len(\"Hello\")" 727 | ], 728 | "language": "python", 729 | "metadata": {}, 730 | "outputs": [] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "collapsed": false, 735 | "input": [ 736 | "len(name)" 737 | ], 738 | "language": "python", 739 | "metadata": {}, 740 | "outputs": [] 741 | }, 742 | { 743 | "cell_type": "code", 744 | "collapsed": false, 745 | "input": [ 746 | "\"The length of my name is \" + str(len(name))" 747 | ], 748 | "language": "python", 749 | "metadata": {}, 750 | "outputs": [] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "collapsed": false, 755 | "input": [ 756 | "\"Hello\"" 757 | ], 758 | "language": "python", 759 | "metadata": {}, 760 | "outputs": [] 761 | }, 762 | { 763 | "cell_type": "code", 764 | "collapsed": false, 765 | "input": [ 766 | "'Hello'\n" 767 | ], 768 | "language": "python", 769 | "metadata": {}, 770 | "outputs": [] 771 | }, 772 | { 773 | "cell_type": "markdown", 774 | "metadata": {}, 775 | "source": [ 776 | "**Quotes (single, double, triple)**" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "collapsed": false, 782 | "input": [ 783 | "\"Python, I'm your #1 fan!\"" 784 | ], 785 | "language": "python", 786 | "metadata": {}, 787 | "outputs": [] 788 | }, 789 | { 790 | "cell_type": "code", 791 | "collapsed": false, 792 | "input": [ 793 | "\"A\" * 40" 794 | ], 795 | "language": "python", 796 | "metadata": {}, 797 | "outputs": [] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "collapsed": false, 802 | "input": [ 803 | "h = \"Happy\"" 804 | ], 805 | "language": "python", 806 | "metadata": {}, 807 | "outputs": [] 808 | }, 809 | { 810 | "cell_type": "code", 811 | "collapsed": false, 812 | "input": [ 813 | "b = \"Birthday\"" 814 | ], 815 | "language": "python", 816 | "metadata": {}, 817 | "outputs": [] 818 | }, 819 | { 820 | "cell_type": "code", 821 | "collapsed": false, 822 | "input": [ 823 | "(h + b) * 10" 824 | ], 825 | "language": "python", 826 | "metadata": {}, 827 | "outputs": [] 828 | }, 829 | { 830 | "cell_type": "markdown", 831 | "metadata": {}, 832 | "source": [ 833 | "**Displaying versus printing in IPython**" 834 | ] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "collapsed": false, 839 | "input": [ 840 | "\"Hello\"" 841 | ], 842 | "language": "python", 843 | "metadata": {}, 844 | "outputs": [] 845 | }, 846 | { 847 | "cell_type": "code", 848 | "collapsed": false, 849 | "input": [ 850 | "print(\"Hello\")" 851 | ], 852 | "language": "python", 853 | "metadata": {}, 854 | "outputs": [] 855 | }, 856 | { 857 | "cell_type": "markdown", 858 | "metadata": {}, 859 | "source": [ 860 | "Questions? Quick review" 861 | ] 862 | }, 863 | { 864 | "cell_type": "code", 865 | "collapsed": false, 866 | "input": [ 867 | "3 ** 3" 868 | ], 869 | "language": "python", 870 | "metadata": {}, 871 | "outputs": [] 872 | }, 873 | { 874 | "cell_type": "code", 875 | "collapsed": false, 876 | "input": [ 877 | "type(1)" 878 | ], 879 | "language": "python", 880 | "metadata": {}, 881 | "outputs": [] 882 | }, 883 | { 884 | "cell_type": "code", 885 | "collapsed": false, 886 | "input": [ 887 | "type(1.0)" 888 | ], 889 | "language": "python", 890 | "metadata": {}, 891 | "outputs": [] 892 | }, 893 | { 894 | "cell_type": "code", 895 | "collapsed": false, 896 | "input": [ 897 | "type(\"1\")" 898 | ], 899 | "language": "python", 900 | "metadata": {}, 901 | "outputs": [] 902 | }, 903 | { 904 | "cell_type": "heading", 905 | "level": 3, 906 | "metadata": {}, 907 | "source": [ 908 | "Make choices" 909 | ] 910 | }, 911 | { 912 | "cell_type": "code", 913 | "collapsed": false, 914 | "input": [ 915 | "True" 916 | ], 917 | "language": "python", 918 | "metadata": {}, 919 | "outputs": [] 920 | }, 921 | { 922 | "cell_type": "code", 923 | "collapsed": false, 924 | "input": [ 925 | "False" 926 | ], 927 | "language": "python", 928 | "metadata": {}, 929 | "outputs": [] 930 | }, 931 | { 932 | "cell_type": "code", 933 | "collapsed": false, 934 | "input": [ 935 | "type(True)" 936 | ], 937 | "language": "python", 938 | "metadata": {}, 939 | "outputs": [] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "collapsed": false, 944 | "input": [ 945 | "type(False)" 946 | ], 947 | "language": "python", 948 | "metadata": {}, 949 | "outputs": [] 950 | }, 951 | { 952 | "cell_type": "code", 953 | "collapsed": false, 954 | "input": [ 955 | "0 == 0" 956 | ], 957 | "language": "python", 958 | "metadata": {}, 959 | "outputs": [] 960 | }, 961 | { 962 | "cell_type": "code", 963 | "collapsed": false, 964 | "input": [ 965 | "0 == 1" 966 | ], 967 | "language": "python", 968 | "metadata": {}, 969 | "outputs": [] 970 | }, 971 | { 972 | "cell_type": "code", 973 | "collapsed": false, 974 | "input": [ 975 | "0 != 1" 976 | ], 977 | "language": "python", 978 | "metadata": {}, 979 | "outputs": [] 980 | }, 981 | { 982 | "cell_type": "code", 983 | "collapsed": false, 984 | "input": [ 985 | "\"a\" == \"A\"" 986 | ], 987 | "language": "python", 988 | "metadata": {}, 989 | "outputs": [] 990 | }, 991 | { 992 | "cell_type": "code", 993 | "collapsed": false, 994 | "input": [ 995 | "1 > 0" 996 | ], 997 | "language": "python", 998 | "metadata": {}, 999 | "outputs": [] 1000 | }, 1001 | { 1002 | "cell_type": "code", 1003 | "collapsed": false, 1004 | "input": [ 1005 | "2 >= 3" 1006 | ], 1007 | "language": "python", 1008 | "metadata": {}, 1009 | "outputs": [] 1010 | }, 1011 | { 1012 | "cell_type": "code", 1013 | "collapsed": false, 1014 | "input": [ 1015 | "-1<0" 1016 | ], 1017 | "language": "python", 1018 | "metadata": {}, 1019 | "outputs": [] 1020 | }, 1021 | { 1022 | "cell_type": "code", 1023 | "collapsed": false, 1024 | "input": [ 1025 | ".5 <= 1" 1026 | ], 1027 | "language": "python", 1028 | "metadata": {}, 1029 | "outputs": [] 1030 | }, 1031 | { 1032 | "cell_type": "code", 1033 | "collapsed": false, 1034 | "input": [ 1035 | "\"H\" in \"Hello\"" 1036 | ], 1037 | "language": "python", 1038 | "metadata": {}, 1039 | "outputs": [] 1040 | }, 1041 | { 1042 | "cell_type": "code", 1043 | "collapsed": false, 1044 | "input": [ 1045 | "\"x\" in \"Hello\"" 1046 | ], 1047 | "language": "python", 1048 | "metadata": {}, 1049 | "outputs": [] 1050 | }, 1051 | { 1052 | "cell_type": "code", 1053 | "collapsed": false, 1054 | "input": [ 1055 | "\"a\" not in \"abcde\"" 1056 | ], 1057 | "language": "python", 1058 | "metadata": {}, 1059 | "outputs": [] 1060 | }, 1061 | { 1062 | "cell_type": "code", 1063 | "collapsed": false, 1064 | "input": [ 1065 | "type(True)" 1066 | ], 1067 | "language": "python", 1068 | "metadata": {}, 1069 | "outputs": [] 1070 | }, 1071 | { 1072 | "cell_type": "code", 1073 | "collapsed": false, 1074 | "input": [ 1075 | "type(\"True\")" 1076 | ], 1077 | "language": "python", 1078 | "metadata": {}, 1079 | "outputs": [] 1080 | }, 1081 | { 1082 | "cell_type": "code", 1083 | "collapsed": false, 1084 | "input": [ 1085 | "type(true)" 1086 | ], 1087 | "language": "python", 1088 | "metadata": {}, 1089 | "outputs": [] 1090 | }, 1091 | { 1092 | "cell_type": "code", 1093 | "collapsed": false, 1094 | "input": [ 1095 | "x = 4" 1096 | ], 1097 | "language": "python", 1098 | "metadata": {}, 1099 | "outputs": [] 1100 | }, 1101 | { 1102 | "cell_type": "code", 1103 | "collapsed": false, 1104 | "input": [ 1105 | "x == 4" 1106 | ], 1107 | "language": "python", 1108 | "metadata": {}, 1109 | "outputs": [] 1110 | }, 1111 | { 1112 | "cell_type": "code", 1113 | "collapsed": false, 1114 | "input": [ 1115 | "if 6 > 5:\n", 1116 | " print(\"Six is greater than 5\")\n", 1117 | " " 1118 | ], 1119 | "language": "python", 1120 | "metadata": {}, 1121 | "outputs": [] 1122 | }, 1123 | { 1124 | "cell_type": "code", 1125 | "collapsed": false, 1126 | "input": [ 1127 | "if 0 > 2:\n", 1128 | " print(\"Zero is greater\")" 1129 | ], 1130 | "language": "python", 1131 | "metadata": {}, 1132 | "outputs": [] 1133 | }, 1134 | { 1135 | "cell_type": "code", 1136 | "collapsed": false, 1137 | "input": [ 1138 | "if \"banana\" in \"bananarama\":\n", 1139 | " print(\"I miss the 80s\")" 1140 | ], 1141 | "language": "python", 1142 | "metadata": {}, 1143 | "outputs": [] 1144 | }, 1145 | { 1146 | "cell_type": "markdown", 1147 | "metadata": {}, 1148 | "source": [ 1149 | "One of two actions" 1150 | ] 1151 | }, 1152 | { 1153 | "cell_type": "code", 1154 | "collapsed": false, 1155 | "input": [ 1156 | "sister = 15" 1157 | ], 1158 | "language": "python", 1159 | "metadata": {}, 1160 | "outputs": [] 1161 | }, 1162 | { 1163 | "cell_type": "code", 1164 | "collapsed": false, 1165 | "input": [ 1166 | "brother = 12" 1167 | ], 1168 | "language": "python", 1169 | "metadata": {}, 1170 | "outputs": [] 1171 | }, 1172 | { 1173 | "cell_type": "code", 1174 | "collapsed": false, 1175 | "input": [ 1176 | "if sister > brother:\n", 1177 | " print(\"Sister is older\")\n", 1178 | "else:\n", 1179 | " print(\"Brother is older\")" 1180 | ], 1181 | "language": "python", 1182 | "metadata": {}, 1183 | "outputs": [] 1184 | }, 1185 | { 1186 | "cell_type": "code", 1187 | "collapsed": false, 1188 | "input": [ 1189 | "1 > 0 and 1 < 2" 1190 | ], 1191 | "language": "python", 1192 | "metadata": {}, 1193 | "outputs": [] 1194 | }, 1195 | { 1196 | "cell_type": "code", 1197 | "collapsed": false, 1198 | "input": [ 1199 | "1 < 2 and \"x\" in \"abc\"" 1200 | ], 1201 | "language": "python", 1202 | "metadata": {}, 1203 | "outputs": [] 1204 | }, 1205 | { 1206 | "cell_type": "code", 1207 | "collapsed": false, 1208 | "input": [ 1209 | "\"a\" in \"abc\" and \"b\" in \"abc\" and \"c\" in \"abc\"" 1210 | ], 1211 | "language": "python", 1212 | "metadata": {}, 1213 | "outputs": [] 1214 | }, 1215 | { 1216 | "cell_type": "code", 1217 | "collapsed": false, 1218 | "input": [ 1219 | "\"a\" in \"hello\" or \"e\" in \"hello\"" 1220 | ], 1221 | "language": "python", 1222 | "metadata": {}, 1223 | "outputs": [] 1224 | }, 1225 | { 1226 | "cell_type": "code", 1227 | "collapsed": false, 1228 | "input": [ 1229 | "temp = 32" 1230 | ], 1231 | "language": "python", 1232 | "metadata": {}, 1233 | "outputs": [] 1234 | }, 1235 | { 1236 | "cell_type": "code", 1237 | "collapsed": false, 1238 | "input": [ 1239 | "if temp > 60 and temp < 75:\n", 1240 | " print(\"Nice and cozy\")\n", 1241 | "else:\n", 1242 | " print(\"Too extreme for me\")" 1243 | ], 1244 | "language": "python", 1245 | "metadata": {}, 1246 | "outputs": [] 1247 | }, 1248 | { 1249 | "cell_type": "markdown", 1250 | "metadata": {}, 1251 | "source": [ 1252 | "One of three things" 1253 | ] 1254 | }, 1255 | { 1256 | "cell_type": "code", 1257 | "collapsed": false, 1258 | "input": [ 1259 | "sister = 15\n", 1260 | "brother = 15\n" 1261 | ], 1262 | "language": "python", 1263 | "metadata": {}, 1264 | "outputs": [] 1265 | }, 1266 | { 1267 | "cell_type": "code", 1268 | "collapsed": false, 1269 | "input": [ 1270 | "if sister > brother:\n", 1271 | " print(\"Sister is older\")\n", 1272 | "elif sister == brother:\n", 1273 | " print(\"Twinsies!!\")\n", 1274 | "else:\n", 1275 | " print(\"Brother is older\")" 1276 | ], 1277 | "language": "python", 1278 | "metadata": {}, 1279 | "outputs": [] 1280 | }, 1281 | { 1282 | "cell_type": "markdown", 1283 | "metadata": {}, 1284 | "source": [ 1285 | "**Exiting the interpreter is a good thing to know.** \n", 1286 | "To exit, type `exit()` or press CNTL-D." 1287 | ] 1288 | }, 1289 | { 1290 | "cell_type": "markdown", 1291 | "metadata": {}, 1292 | "source": [ 1293 | "Practice problems - Codeacademy practice\n", 1294 | "Strings and choices\n", 1295 | "http://bit.ly/py-practice" 1296 | ] 1297 | }, 1298 | { 1299 | "cell_type": "code", 1300 | "collapsed": false, 1301 | "input": [ 1302 | "from IPython.display import YouTubeVideo\n", 1303 | "# a tutorial about Python at PyCon 2014 in Montreal, Canada by Jessica McKellar\n", 1304 | "# Credit: William Stein.\n", 1305 | "YouTubeVideo('MirG-vJOg04')" 1306 | ], 1307 | "language": "python", 1308 | "metadata": {}, 1309 | "outputs": [] 1310 | }, 1311 | { 1312 | "cell_type": "code", 1313 | "collapsed": false, 1314 | "input": [ 1315 | "from IPython.display import IFrame\n", 1316 | "# Pull in the tutorial prep information from OpenHatch wiki\n", 1317 | "IFrame('http://bit.ly/intro-setup', width='100%', height=350)" 1318 | ], 1319 | "language": "python", 1320 | "metadata": {}, 1321 | "outputs": [] 1322 | }, 1323 | { 1324 | "cell_type": "code", 1325 | "collapsed": false, 1326 | "input": [], 1327 | "language": "python", 1328 | "metadata": {}, 1329 | "outputs": [] 1330 | } 1331 | ], 1332 | "metadata": {} 1333 | } 1334 | ] 1335 | } -------------------------------------------------------------------------------- /part-2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:88d4276acc1e0e7988cbce895805afe31524537464b3097ae3201a1b3d5d5b2d" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "heading", 13 | "level": 1, 14 | "metadata": {}, 15 | "source": [ 16 | "List Basics" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "slideshow": { 23 | "slide_type": "slide" 24 | } 25 | }, 26 | "source": [ 27 | "Let's create a list that has three items. Lists are a kind of 'collection' such that they collect or contain things (a.ka.a 'members' or 'items'. So, in the list, things, we have a collection of 3 things - two numbers and a string." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "collapsed": false, 33 | "input": [ 34 | "things = [100, 500, \"thing\"]" 35 | ], 36 | "language": "python", 37 | "metadata": { 38 | "slideshow": { 39 | "slide_type": "slide" 40 | } 41 | }, 42 | "outputs": [], 43 | "prompt_number": 8 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "To see what the 'your_list' variable contains..." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "collapsed": false, 55 | "input": [ 56 | "things" 57 | ], 58 | "language": "python", 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "metadata": {}, 63 | "output_type": "pyout", 64 | "prompt_number": 2, 65 | "text": [ 66 | "[100, 500, 'things']" 67 | ] 68 | } 69 | ], 70 | "prompt_number": 2 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "We can confirm that this is a list by using 'type'" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "collapsed": false, 82 | "input": [ 83 | "type(things)" 84 | ], 85 | "language": "python", 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "metadata": {}, 90 | "output_type": "pyout", 91 | "prompt_number": 3, 92 | "text": [ 93 | "list" 94 | ] 95 | } 96 | ], 97 | "prompt_number": 3 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "Like strings, lists have a length" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "collapsed": false, 109 | "input": [ 110 | "len(things)" 111 | ], 112 | "language": "python", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "metadata": {}, 117 | "output_type": "pyout", 118 | "prompt_number": 4, 119 | "text": [ 120 | "3" 121 | ] 122 | } 123 | ], 124 | "prompt_number": 4 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "Also like strings, list have content that can be checked. By using the 'in' operator, we can find out if a char such as the letter 'c' is contained in the list, your_list" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "collapsed": false, 136 | "input": [ 137 | "\"e\" in \"Hello\"" 138 | ], 139 | "language": "python", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "metadata": {}, 144 | "output_type": "pyout", 145 | "prompt_number": 5, 146 | "text": [ 147 | "True" 148 | ] 149 | } 150 | ], 151 | "prompt_number": 5 152 | }, 153 | { 154 | "cell_type": "code", 155 | "collapsed": false, 156 | "input": [ 157 | "\"thing\" in things" 158 | ], 159 | "language": "python", 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "metadata": {}, 164 | "output_type": "pyout", 165 | "prompt_number": 10, 166 | "text": [ 167 | "True" 168 | ] 169 | } 170 | ], 171 | "prompt_number": 10 172 | }, 173 | { 174 | "cell_type": "code", 175 | "collapsed": false, 176 | "input": [ 177 | "\"nothing\" in things" 178 | ], 179 | "language": "python", 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "metadata": {}, 184 | "output_type": "pyout", 185 | "prompt_number": 9, 186 | "text": [ 187 | "False" 188 | ] 189 | } 190 | ], 191 | "prompt_number": 9 192 | }, 193 | { 194 | "cell_type": "code", 195 | "collapsed": false, 196 | "input": [ 197 | "things = [100, 500, \"things\"]" 198 | ], 199 | "language": "python", 200 | "metadata": {}, 201 | "outputs": [], 202 | "prompt_number": 12 203 | }, 204 | { 205 | "cell_type": "heading", 206 | "level": 1, 207 | "metadata": {}, 208 | "source": [ 209 | "List Indexes" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "Lists are ordered. We can access any individual item in a list based on its position inside the list. Let's say I wanted to get the leftmost item." 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "collapsed": false, 222 | "input": [ 223 | "things[0]" 224 | ], 225 | "language": "python", 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "metadata": {}, 230 | "output_type": "pyout", 231 | "prompt_number": 13, 232 | "text": [ 233 | "100" 234 | ] 235 | } 236 | ], 237 | "prompt_number": 13 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "Notice that we start counting at zero, which is a little funny since one might think it would start with the number, 1." 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "collapsed": false, 249 | "input": [ 250 | "things[1]" 251 | ], 252 | "language": "python", 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "metadata": {}, 257 | "output_type": "pyout", 258 | "prompt_number": 22, 259 | "text": [ 260 | "500" 261 | ] 262 | } 263 | ], 264 | "prompt_number": 22 265 | }, 266 | { 267 | "cell_type": "code", 268 | "collapsed": false, 269 | "input": [ 270 | "things[2]" 271 | ], 272 | "language": "python", 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "metadata": {}, 277 | "output_type": "pyout", 278 | "prompt_number": 23, 279 | "text": [ 280 | "'things'" 281 | ] 282 | } 283 | ], 284 | "prompt_number": 23 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": {}, 289 | "source": [ 290 | "What happens if we try to access item 3?" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "collapsed": false, 296 | "input": [ 297 | "things[3]" 298 | ], 299 | "language": "python", 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "ename": "IndexError", 304 | "evalue": "list index out of range", 305 | "output_type": "pyerr", 306 | "traceback": [ 307 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 308 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mthings\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 309 | "\u001b[0;31mIndexError\u001b[0m: list index out of range" 310 | ] 311 | } 312 | ], 313 | "prompt_number": 16 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": {}, 318 | "source": [ 319 | "We get a traceback, but a pretty readable one." 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "metadata": {}, 325 | "source": [ 326 | "Fortunately, we can add items to the end of the list" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "collapsed": false, 332 | "input": [ 333 | "things.append(\"d\")" 334 | ], 335 | "language": "python", 336 | "metadata": {}, 337 | "outputs": [], 338 | "prompt_number": 24 339 | }, 340 | { 341 | "cell_type": "code", 342 | "collapsed": false, 343 | "input": [ 344 | "len(things)" 345 | ], 346 | "language": "python", 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "metadata": {}, 351 | "output_type": "pyout", 352 | "prompt_number": 25, 353 | "text": [ 354 | "5" 355 | ] 356 | } 357 | ], 358 | "prompt_number": 25 359 | }, 360 | { 361 | "cell_type": "code", 362 | "collapsed": false, 363 | "input": [ 364 | "things[3]" 365 | ], 366 | "language": "python", 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "metadata": {}, 371 | "output_type": "pyout", 372 | "prompt_number": 26, 373 | "text": [ 374 | "'d'" 375 | ] 376 | } 377 | ], 378 | "prompt_number": 26 379 | }, 380 | { 381 | "cell_type": "heading", 382 | "level": 1, 383 | "metadata": {}, 384 | "source": [ 385 | "Adding stuff to lists" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "What if we want to append a bunch of stuff? A related question is how do we even know we can append to this list? We don't rely on memorization, we use documentation. Let's type *python lists* into Google.\n", 393 | "\n", 394 | "We'll see the \"extend\" function which looks like what we need to add a bunch of items to the list at once." 395 | ] 396 | }, 397 | { 398 | "cell_type": "markdown", 399 | "metadata": {}, 400 | "source": [ 401 | "Another way of using documentation is by the built-in 'help' function. It's not pretty, and it's not as intuitive, but it works. To exit out of help, you have to press \"q\".\n", 402 | "\n", 403 | "In general, if you want to know whether it's possible to do something in Python, Google it." 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "metadata": {}, 409 | "source": [ 410 | "Here's a question: how would you create an empty list?" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "collapsed": false, 416 | "input": [ 417 | "stuff = []" 418 | ], 419 | "language": "python", 420 | "metadata": {}, 421 | "outputs": [], 422 | "prompt_number": 2 423 | }, 424 | { 425 | "cell_type": "markdown", 426 | "metadata": {}, 427 | "source": [ 428 | "Now let's check the length!" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "collapsed": false, 434 | "input": [ 435 | "len(stuff)" 436 | ], 437 | "language": "python", 438 | "metadata": {}, 439 | "outputs": [ 440 | { 441 | "metadata": {}, 442 | "output_type": "pyout", 443 | "prompt_number": 3, 444 | "text": [ 445 | "0" 446 | ] 447 | } 448 | ], 449 | "prompt_number": 3 450 | }, 451 | { 452 | "cell_type": "markdown", 453 | "metadata": {}, 454 | "source": [ 455 | "I have no stuff. :(\n", 456 | "\n", 457 | "Let's add some things to my list of stuff" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "collapsed": false, 463 | "input": [ 464 | "stuff.append(\"laptop\")" 465 | ], 466 | "language": "python", 467 | "metadata": {}, 468 | "outputs": [], 469 | "prompt_number": 4 470 | }, 471 | { 472 | "cell_type": "code", 473 | "collapsed": false, 474 | "input": [ 475 | "stuff" 476 | ], 477 | "language": "python", 478 | "metadata": {}, 479 | "outputs": [ 480 | { 481 | "metadata": {}, 482 | "output_type": "pyout", 483 | "prompt_number": 5, 484 | "text": [ 485 | "['laptop']" 486 | ] 487 | } 488 | ], 489 | "prompt_number": 5 490 | }, 491 | { 492 | "cell_type": "code", 493 | "collapsed": false, 494 | "input": [ 495 | "stuff.append(\"wedding ring\")" 496 | ], 497 | "language": "python", 498 | "metadata": {}, 499 | "outputs": [], 500 | "prompt_number": 6 501 | }, 502 | { 503 | "cell_type": "code", 504 | "collapsed": false, 505 | "input": [ 506 | "stuff" 507 | ], 508 | "language": "python", 509 | "metadata": {}, 510 | "outputs": [ 511 | { 512 | "metadata": {}, 513 | "output_type": "pyout", 514 | "prompt_number": 7, 515 | "text": [ 516 | "['laptop', 'wedding ring']" 517 | ] 518 | } 519 | ], 520 | "prompt_number": 7 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "metadata": {}, 525 | "source": [ 526 | "Okay now we want to replace laptop with Macbook Pro." 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "collapsed": false, 532 | "input": [ 533 | "stuff[0] = \"Macbook Pro\"" 534 | ], 535 | "language": "python", 536 | "metadata": {}, 537 | "outputs": [], 538 | "prompt_number": 12 539 | }, 540 | { 541 | "cell_type": "code", 542 | "collapsed": false, 543 | "input": [ 544 | "stuff" 545 | ], 546 | "language": "python", 547 | "metadata": {}, 548 | "outputs": [ 549 | { 550 | "metadata": {}, 551 | "output_type": "pyout", 552 | "prompt_number": 13, 553 | "text": [ 554 | "['Macbook Pro', 'wedding ring']" 555 | ] 556 | } 557 | ], 558 | "prompt_number": 13 559 | }, 560 | { 561 | "cell_type": "markdown", 562 | "metadata": {}, 563 | "source": [ 564 | "Now let's replace wedding ring with tungsten wedding ring" 565 | ] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "collapsed": false, 570 | "input": [ 571 | "stuff[1] = \"tungsten wedding ring\"" 572 | ], 573 | "language": "python", 574 | "metadata": {}, 575 | "outputs": [], 576 | "prompt_number": 14 577 | }, 578 | { 579 | "cell_type": "markdown", 580 | "metadata": {}, 581 | "source": [ 582 | "Here's a question: how is this different from the line above?" 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "collapsed": false, 588 | "input": [ 589 | "stuff[1] == \"tungsten wedding ring\"" 590 | ], 591 | "language": "python", 592 | "metadata": {}, 593 | "outputs": [ 594 | { 595 | "metadata": {}, 596 | "output_type": "pyout", 597 | "prompt_number": 16, 598 | "text": [ 599 | "True" 600 | ] 601 | } 602 | ], 603 | "prompt_number": 16 604 | }, 605 | { 606 | "cell_type": "markdown", 607 | "metadata": {}, 608 | "source": [ 609 | "How do we get the last item in the list?" 610 | ] 611 | }, 612 | { 613 | "cell_type": "markdown", 614 | "metadata": {}, 615 | "source": [ 616 | "We know we can get the length of the list" 617 | ] 618 | }, 619 | { 620 | "cell_type": "code", 621 | "collapsed": false, 622 | "input": [ 623 | "len(stuff)" 624 | ], 625 | "language": "python", 626 | "metadata": {}, 627 | "outputs": [ 628 | { 629 | "metadata": {}, 630 | "output_type": "pyout", 631 | "prompt_number": 39, 632 | "text": [ 633 | "3" 634 | ] 635 | } 636 | ], 637 | "prompt_number": 39 638 | }, 639 | { 640 | "cell_type": "markdown", 641 | "metadata": {}, 642 | "source": [ 643 | "To get the last item's index, let's try using the length of the list, stuff, minus one." 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "collapsed": false, 649 | "input": [ 650 | "len(stuff) - 1" 651 | ], 652 | "language": "python", 653 | "metadata": {}, 654 | "outputs": [ 655 | { 656 | "metadata": {}, 657 | "output_type": "pyout", 658 | "prompt_number": 40, 659 | "text": [ 660 | "2" 661 | ] 662 | } 663 | ], 664 | "prompt_number": 40 665 | }, 666 | { 667 | "cell_type": "code", 668 | "collapsed": false, 669 | "input": [ 670 | "stuff[len(stuff) - 1]" 671 | ], 672 | "language": "python", 673 | "metadata": {}, 674 | "outputs": [ 675 | { 676 | "metadata": {}, 677 | "output_type": "pyout", 678 | "prompt_number": 41, 679 | "text": [ 680 | "'tungsten wedding ring'" 681 | ] 682 | } 683 | ], 684 | "prompt_number": 41 685 | }, 686 | { 687 | "cell_type": "markdown", 688 | "metadata": {}, 689 | "source": [ 690 | "But there's a shortcut in Python for getting the last item from a list.\n", 691 | "\n", 692 | "You can use negative numbers to start counting from the end of the list. So -1 gives us the last item:" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "collapsed": false, 698 | "input": [ 699 | "stuff[-1]" 700 | ], 701 | "language": "python", 702 | "metadata": {}, 703 | "outputs": [ 704 | { 705 | "metadata": {}, 706 | "output_type": "pyout", 707 | "prompt_number": 42, 708 | "text": [ 709 | "'tungsten wedding ring'" 710 | ] 711 | } 712 | ], 713 | "prompt_number": 42 714 | }, 715 | { 716 | "cell_type": "markdown", 717 | "metadata": {}, 718 | "source": [ 719 | "It's just doing the math for us. There's no real magic here." 720 | ] 721 | }, 722 | { 723 | "cell_type": "markdown", 724 | "metadata": {}, 725 | "source": [ 726 | "We can do the same for strings to access a character or the last character within a string, its length, and so on." 727 | ] 728 | }, 729 | { 730 | "cell_type": "code", 731 | "collapsed": false, 732 | "input": [ 733 | "name = \"Danny\"" 734 | ], 735 | "language": "python", 736 | "metadata": {}, 737 | "outputs": [], 738 | "prompt_number": 43 739 | }, 740 | { 741 | "cell_type": "code", 742 | "collapsed": false, 743 | "input": [ 744 | "name[0]" 745 | ], 746 | "language": "python", 747 | "metadata": {}, 748 | "outputs": [ 749 | { 750 | "metadata": {}, 751 | "output_type": "pyout", 752 | "prompt_number": 44, 753 | "text": [ 754 | "'D'" 755 | ] 756 | } 757 | ], 758 | "prompt_number": 44 759 | }, 760 | { 761 | "cell_type": "code", 762 | "collapsed": false, 763 | "input": [ 764 | "name[-1]" 765 | ], 766 | "language": "python", 767 | "metadata": {}, 768 | "outputs": [ 769 | { 770 | "metadata": {}, 771 | "output_type": "pyout", 772 | "prompt_number": 45, 773 | "text": [ 774 | "'y'" 775 | ] 776 | } 777 | ], 778 | "prompt_number": 45 779 | }, 780 | { 781 | "cell_type": "markdown", 782 | "metadata": {}, 783 | "source": [ 784 | "A quick review of lists:\n", 785 | "So the way we create a list is we give it a name and in square brackets give it a comma-separated set of items." 786 | ] 787 | }, 788 | { 789 | "cell_type": "code", 790 | "collapsed": false, 791 | "input": [ 792 | "fruits = [\"apples\", \"bananas\", \"oranges\"]" 793 | ], 794 | "language": "python", 795 | "metadata": {}, 796 | "outputs": [], 797 | "prompt_number": 53 798 | }, 799 | { 800 | "cell_type": "markdown", 801 | "metadata": {}, 802 | "source": [ 803 | "We can use this square brackets syntax to get individual items" 804 | ] 805 | }, 806 | { 807 | "cell_type": "code", 808 | "collapsed": false, 809 | "input": [ 810 | "fruits[0]" 811 | ], 812 | "language": "python", 813 | "metadata": {}, 814 | "outputs": [ 815 | { 816 | "metadata": {}, 817 | "output_type": "pyout", 818 | "prompt_number": 47, 819 | "text": [ 820 | "'apples'" 821 | ] 822 | } 823 | ], 824 | "prompt_number": 47 825 | }, 826 | { 827 | "cell_type": "markdown", 828 | "metadata": {}, 829 | "source": [ 830 | "We can use negative numbers to get items from the end of a list" 831 | ] 832 | }, 833 | { 834 | "cell_type": "code", 835 | "collapsed": false, 836 | "input": [ 837 | "fruits[-1]" 838 | ], 839 | "language": "python", 840 | "metadata": {}, 841 | "outputs": [ 842 | { 843 | "metadata": {}, 844 | "output_type": "pyout", 845 | "prompt_number": 48, 846 | "text": [ 847 | "'oranges'" 848 | ] 849 | } 850 | ], 851 | "prompt_number": 48 852 | }, 853 | { 854 | "cell_type": "markdown", 855 | "metadata": {}, 856 | "source": [ 857 | "We can do asignment. We can replace items in the list by using the notation to get the item and then do an assignment." 858 | ] 859 | }, 860 | { 861 | "cell_type": "code", 862 | "collapsed": false, 863 | "input": [ 864 | "fruits[0] = \"plums\"" 865 | ], 866 | "language": "python", 867 | "metadata": {}, 868 | "outputs": [], 869 | "prompt_number": 49 870 | }, 871 | { 872 | "cell_type": "markdown", 873 | "metadata": {}, 874 | "source": [ 875 | "We can add to the end of the list with .append" 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "collapsed": false, 881 | "input": [ 882 | "fruits.append(\"cherries\")" 883 | ], 884 | "language": "python", 885 | "metadata": {}, 886 | "outputs": [], 887 | "prompt_number": 50 888 | }, 889 | { 890 | "cell_type": "markdown", 891 | "metadata": {}, 892 | "source": [ 893 | "We can check the length of a list" 894 | ] 895 | }, 896 | { 897 | "cell_type": "code", 898 | "collapsed": false, 899 | "input": [ 900 | "len(fruits)" 901 | ], 902 | "language": "python", 903 | "metadata": {}, 904 | "outputs": [ 905 | { 906 | "metadata": {}, 907 | "output_type": "pyout", 908 | "prompt_number": 51, 909 | "text": [ 910 | "4" 911 | ] 912 | } 913 | ], 914 | "prompt_number": 51 915 | }, 916 | { 917 | "cell_type": "markdown", 918 | "metadata": {}, 919 | "source": [ 920 | "We can do things like check for things contained in a list using the 'in' operator" 921 | ] 922 | }, 923 | { 924 | "cell_type": "code", 925 | "collapsed": false, 926 | "input": [ 927 | "\"apples\" in fruits" 928 | ], 929 | "language": "python", 930 | "metadata": {}, 931 | "outputs": [ 932 | { 933 | "metadata": {}, 934 | "output_type": "pyout", 935 | "prompt_number": 52, 936 | "text": [ 937 | "False" 938 | ] 939 | } 940 | ], 941 | "prompt_number": 52 942 | }, 943 | { 944 | "cell_type": "markdown", 945 | "metadata": {}, 946 | "source": [ 947 | "Question: can we grab more than one item from a list?\n", 948 | "\n", 949 | "This is the range syntax:" 950 | ] 951 | }, 952 | { 953 | "cell_type": "code", 954 | "collapsed": false, 955 | "input": [ 956 | "fruits = [\"apples\", \"bananas\", \"oranges\"]" 957 | ], 958 | "language": "python", 959 | "metadata": {}, 960 | "outputs": [], 961 | "prompt_number": 54 962 | }, 963 | { 964 | "cell_type": "heading", 965 | "level": 1, 966 | "metadata": {}, 967 | "source": [ 968 | "Slicing of lists" 969 | ] 970 | }, 971 | { 972 | "cell_type": "code", 973 | "collapsed": false, 974 | "input": [ 975 | "fruits[0:2]" 976 | ], 977 | "language": "python", 978 | "metadata": {}, 979 | "outputs": [ 980 | { 981 | "metadata": {}, 982 | "output_type": "pyout", 983 | "prompt_number": 55, 984 | "text": [ 985 | "['apples', 'bananas']" 986 | ] 987 | } 988 | ], 989 | "prompt_number": 55 990 | }, 991 | { 992 | "cell_type": "markdown", 993 | "metadata": {}, 994 | "source": [ 995 | "The left side of the colon is implicit. It starts at the beginning by default, but does not include the last item in the list." 996 | ] 997 | }, 998 | { 999 | "cell_type": "code", 1000 | "collapsed": false, 1001 | "input": [ 1002 | "fruits[:2]" 1003 | ], 1004 | "language": "python", 1005 | "metadata": {}, 1006 | "outputs": [ 1007 | { 1008 | "metadata": {}, 1009 | "output_type": "pyout", 1010 | "prompt_number": 56, 1011 | "text": [ 1012 | "['apples', 'bananas']" 1013 | ] 1014 | } 1015 | ], 1016 | "prompt_number": 56 1017 | }, 1018 | { 1019 | "cell_type": "markdown", 1020 | "metadata": {}, 1021 | "source": [ 1022 | "The right side of the colon can also be left off. It starts from and ends at the end of the list" 1023 | ] 1024 | }, 1025 | { 1026 | "cell_type": "code", 1027 | "collapsed": false, 1028 | "input": [ 1029 | "fruits[2:]" 1030 | ], 1031 | "language": "python", 1032 | "metadata": {}, 1033 | "outputs": [ 1034 | { 1035 | "metadata": {}, 1036 | "output_type": "pyout", 1037 | "prompt_number": 57, 1038 | "text": [ 1039 | "['oranges']" 1040 | ] 1041 | } 1042 | ], 1043 | "prompt_number": 57 1044 | }, 1045 | { 1046 | "cell_type": "markdown", 1047 | "metadata": {}, 1048 | "source": [ 1049 | "By extension, if you leave off both the left and right sides you can make a copy of a list" 1050 | ] 1051 | }, 1052 | { 1053 | "cell_type": "code", 1054 | "collapsed": false, 1055 | "input": [ 1056 | "fruits[:]" 1057 | ], 1058 | "language": "python", 1059 | "metadata": {}, 1060 | "outputs": [ 1061 | { 1062 | "metadata": {}, 1063 | "output_type": "pyout", 1064 | "prompt_number": 58, 1065 | "text": [ 1066 | "['apples', 'bananas', 'oranges']" 1067 | ] 1068 | } 1069 | ], 1070 | "prompt_number": 58 1071 | }, 1072 | { 1073 | "cell_type": "code", 1074 | "collapsed": false, 1075 | "input": [ 1076 | "my_favorite_fruits = fruits[:]" 1077 | ], 1078 | "language": "python", 1079 | "metadata": {}, 1080 | "outputs": [], 1081 | "prompt_number": 59 1082 | }, 1083 | { 1084 | "cell_type": "markdown", 1085 | "metadata": {}, 1086 | "source": [ 1087 | "Now we see that the original fruits list" 1088 | ] 1089 | }, 1090 | { 1091 | "cell_type": "code", 1092 | "collapsed": false, 1093 | "input": [ 1094 | "fruits" 1095 | ], 1096 | "language": "python", 1097 | "metadata": {}, 1098 | "outputs": [ 1099 | { 1100 | "metadata": {}, 1101 | "output_type": "pyout", 1102 | "prompt_number": 60, 1103 | "text": [ 1104 | "['apples', 'bananas', 'oranges']" 1105 | ] 1106 | } 1107 | ], 1108 | "prompt_number": 60 1109 | }, 1110 | { 1111 | "cell_type": "markdown", 1112 | "metadata": {}, 1113 | "source": [ 1114 | "has indeed been copied into my_favorite_fruits list" 1115 | ] 1116 | }, 1117 | { 1118 | "cell_type": "code", 1119 | "collapsed": false, 1120 | "input": [ 1121 | "my_favorite_fruits" 1122 | ], 1123 | "language": "python", 1124 | "metadata": {}, 1125 | "outputs": [ 1126 | { 1127 | "metadata": {}, 1128 | "output_type": "pyout", 1129 | "prompt_number": 61, 1130 | "text": [ 1131 | "['apples', 'bananas', 'oranges']" 1132 | ] 1133 | } 1134 | ], 1135 | "prompt_number": 61 1136 | }, 1137 | { 1138 | "cell_type": "code", 1139 | "collapsed": false, 1140 | "input": [ 1141 | "names = [\"Crystal\", \"Kendall\", \"Daniel\", \"Alain\"]" 1142 | ], 1143 | "language": "python", 1144 | "metadata": {}, 1145 | "outputs": [], 1146 | "prompt_number": 17 1147 | }, 1148 | { 1149 | "cell_type": "heading", 1150 | "level": 1, 1151 | "metadata": {}, 1152 | "source": [ 1153 | "Loops" 1154 | ] 1155 | }, 1156 | { 1157 | "cell_type": "markdown", 1158 | "metadata": {}, 1159 | "source": [ 1160 | "What if we wanted to loop over and do some action for every item in the list?" 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "code", 1165 | "collapsed": false, 1166 | "input": [ 1167 | "print(\"Hello\")\n", 1168 | "print(\"My name is Crystal\")\n", 1169 | "print(\"I live in Sunnyvale\")" 1170 | ], 1171 | "language": "python", 1172 | "metadata": {}, 1173 | "outputs": [] 1174 | }, 1175 | { 1176 | "cell_type": "code", 1177 | "collapsed": false, 1178 | "input": [ 1179 | "print(\"Hello\")\n", 1180 | "print(\"My name is Kendall\")\n", 1181 | "print(\"I live in Mountain View\")" 1182 | ], 1183 | "language": "python", 1184 | "metadata": {}, 1185 | "outputs": [] 1186 | }, 1187 | { 1188 | "cell_type": "code", 1189 | "collapsed": false, 1190 | "input": [ 1191 | "print(\"Hello\")\n", 1192 | "print(\"My name is Daniel\")\n", 1193 | "print(\"I live in Redwood City\")" 1194 | ], 1195 | "language": "python", 1196 | "metadata": {}, 1197 | "outputs": [] 1198 | }, 1199 | { 1200 | "cell_type": "markdown", 1201 | "metadata": {}, 1202 | "source": [ 1203 | "That's the hard way. The easy way is by using a 'for' loop. A simple one:" 1204 | ] 1205 | }, 1206 | { 1207 | "cell_type": "code", 1208 | "collapsed": false, 1209 | "input": [ 1210 | "for name in names:\n", 1211 | " print(name)" 1212 | ], 1213 | "language": "python", 1214 | "metadata": {}, 1215 | "outputs": [ 1216 | { 1217 | "output_type": "stream", 1218 | "stream": "stdout", 1219 | "text": [ 1220 | "Crystal\n", 1221 | "Kendall\n", 1222 | "Daniel\n", 1223 | "Alain\n" 1224 | ] 1225 | } 1226 | ], 1227 | "prompt_number": 18 1228 | }, 1229 | { 1230 | "cell_type": "markdown", 1231 | "metadata": {}, 1232 | "source": [ 1233 | "To hammer home that this is a variable name of our choosing let's use a different variable name, x." 1234 | ] 1235 | }, 1236 | { 1237 | "cell_type": "code", 1238 | "collapsed": false, 1239 | "input": [ 1240 | "for x in names:\n", 1241 | " print(x)" 1242 | ], 1243 | "language": "python", 1244 | "metadata": {}, 1245 | "outputs": [ 1246 | { 1247 | "output_type": "stream", 1248 | "stream": "stdout", 1249 | "text": [ 1250 | "Crystal\n", 1251 | "Kendall\n", 1252 | "Daniel\n", 1253 | "Alain\n" 1254 | ] 1255 | } 1256 | ], 1257 | "prompt_number": 19 1258 | }, 1259 | { 1260 | "cell_type": "markdown", 1261 | "metadata": {}, 1262 | "source": [ 1263 | "We can have arbitrarily complex statements and as many lines as we want as long as we keep it indented" 1264 | ] 1265 | }, 1266 | { 1267 | "cell_type": "code", 1268 | "collapsed": false, 1269 | "input": [ 1270 | "for name in names:\n", 1271 | " print \"Hello, {}\".format(name)" 1272 | ], 1273 | "language": "python", 1274 | "metadata": {}, 1275 | "outputs": [ 1276 | { 1277 | "output_type": "stream", 1278 | "stream": "stdout", 1279 | "text": [ 1280 | "Hello, Crystal\n", 1281 | "Hello, Kendall\n", 1282 | "Hello, Daniel\n", 1283 | "Hello, Alain\n" 1284 | ] 1285 | } 1286 | ], 1287 | "prompt_number": 23 1288 | }, 1289 | { 1290 | "cell_type": "markdown", 1291 | "metadata": {}, 1292 | "source": [ 1293 | "What if we only want to print out the names that start with a vowel?" 1294 | ] 1295 | }, 1296 | { 1297 | "cell_type": "markdown", 1298 | "metadata": {}, 1299 | "source": [ 1300 | "How do we check whether a name starts with a vowel?" 1301 | ] 1302 | }, 1303 | { 1304 | "cell_type": "markdown", 1305 | "metadata": {}, 1306 | "source": [ 1307 | "We know how to get the first character" 1308 | ] 1309 | }, 1310 | { 1311 | "cell_type": "code", 1312 | "collapsed": false, 1313 | "input": [ 1314 | "name = \"Alain\"" 1315 | ], 1316 | "language": "python", 1317 | "metadata": {}, 1318 | "outputs": [], 1319 | "prompt_number": 24 1320 | }, 1321 | { 1322 | "cell_type": "code", 1323 | "collapsed": false, 1324 | "input": [ 1325 | "name[0]" 1326 | ], 1327 | "language": "python", 1328 | "metadata": {}, 1329 | "outputs": [ 1330 | { 1331 | "metadata": {}, 1332 | "output_type": "pyout", 1333 | "prompt_number": 25, 1334 | "text": [ 1335 | "'A'" 1336 | ] 1337 | } 1338 | ], 1339 | "prompt_number": 25 1340 | }, 1341 | { 1342 | "cell_type": "markdown", 1343 | "metadata": {}, 1344 | "source": [ 1345 | "We want to check whether it equals A, E, I, O, or U. There are a lot of different ways to write this" 1346 | ] 1347 | }, 1348 | { 1349 | "cell_type": "code", 1350 | "collapsed": false, 1351 | "input": [ 1352 | "name[0] == \"A\" or name[0] == \"E\" or name[0] == \"I\" or name[0] == \"O\" or name[0] == \"U\"" 1353 | ], 1354 | "language": "python", 1355 | "metadata": {}, 1356 | "outputs": [ 1357 | { 1358 | "metadata": {}, 1359 | "output_type": "pyout", 1360 | "prompt_number": 26, 1361 | "text": [ 1362 | "True" 1363 | ] 1364 | } 1365 | ], 1366 | "prompt_number": 26 1367 | }, 1368 | { 1369 | "cell_type": "markdown", 1370 | "metadata": {}, 1371 | "source": [ 1372 | "We could also say this:" 1373 | ] 1374 | }, 1375 | { 1376 | "cell_type": "code", 1377 | "collapsed": false, 1378 | "input": [ 1379 | "name[0] in \"AEIOU\"" 1380 | ], 1381 | "language": "python", 1382 | "metadata": {}, 1383 | "outputs": [ 1384 | { 1385 | "metadata": {}, 1386 | "output_type": "pyout", 1387 | "prompt_number": 27, 1388 | "text": [ 1389 | "True" 1390 | ] 1391 | } 1392 | ], 1393 | "prompt_number": 27 1394 | }, 1395 | { 1396 | "cell_type": "markdown", 1397 | "metadata": {}, 1398 | "source": [ 1399 | "We could even say:" 1400 | ] 1401 | }, 1402 | { 1403 | "cell_type": "code", 1404 | "collapsed": false, 1405 | "input": [ 1406 | "name[0] in [\"A\", \"E\", \"I\", \"O\", \"U\"]" 1407 | ], 1408 | "language": "python", 1409 | "metadata": {}, 1410 | "outputs": [ 1411 | { 1412 | "metadata": {}, 1413 | "output_type": "pyout", 1414 | "prompt_number": 28, 1415 | "text": [ 1416 | "True" 1417 | ] 1418 | } 1419 | ], 1420 | "prompt_number": 28 1421 | }, 1422 | { 1423 | "cell_type": "markdown", 1424 | "metadata": {}, 1425 | "source": [ 1426 | "Now let's setup our for loop" 1427 | ] 1428 | }, 1429 | { 1430 | "cell_type": "code", 1431 | "collapsed": false, 1432 | "input": [ 1433 | "for name in names:\n", 1434 | " if name[0] in \"AEIOU\":\n", 1435 | " print(name + \" starts with a vowel\")" 1436 | ], 1437 | "language": "python", 1438 | "metadata": {}, 1439 | "outputs": [ 1440 | { 1441 | "output_type": "stream", 1442 | "stream": "stdout", 1443 | "text": [ 1444 | "Alain starts with a vowel\n" 1445 | ] 1446 | } 1447 | ], 1448 | "prompt_number": 29 1449 | }, 1450 | { 1451 | "cell_type": "markdown", 1452 | "metadata": {}, 1453 | "source": [ 1454 | "The amount of work we had to do here is independent of the length of the list. This list could have had a billion elements in it and this code would still work." 1455 | ] 1456 | }, 1457 | { 1458 | "cell_type": "markdown", 1459 | "metadata": {}, 1460 | "source": [ 1461 | "Instead of printing the names, how do we build up a list of only the names that start with a vowel?\n", 1462 | "\n", 1463 | "We're going to need a for loop. We're also going to need some storage; so let's use a list as storage." 1464 | ] 1465 | }, 1466 | { 1467 | "cell_type": "code", 1468 | "collapsed": false, 1469 | "input": [ 1470 | "vowel_names = []" 1471 | ], 1472 | "language": "python", 1473 | "metadata": {}, 1474 | "outputs": [], 1475 | "prompt_number": 30 1476 | }, 1477 | { 1478 | "cell_type": "code", 1479 | "collapsed": false, 1480 | "input": [ 1481 | "for name in names:\n", 1482 | " if name[0] in \"AEIOU\":\n", 1483 | " vowel_names.append(name)" 1484 | ], 1485 | "language": "python", 1486 | "metadata": {}, 1487 | "outputs": [], 1488 | "prompt_number": 31 1489 | }, 1490 | { 1491 | "cell_type": "markdown", 1492 | "metadata": {}, 1493 | "source": [ 1494 | "Nothing is printed. Let's check that vowel_names has the vowel names" 1495 | ] 1496 | }, 1497 | { 1498 | "cell_type": "code", 1499 | "collapsed": false, 1500 | "input": [ 1501 | "vowel_names" 1502 | ], 1503 | "language": "python", 1504 | "metadata": {}, 1505 | "outputs": [ 1506 | { 1507 | "metadata": {}, 1508 | "output_type": "pyout", 1509 | "prompt_number": 32, 1510 | "text": [ 1511 | "['Alain']" 1512 | ] 1513 | } 1514 | ], 1515 | "prompt_number": 32 1516 | }, 1517 | { 1518 | "cell_type": "markdown", 1519 | "metadata": {}, 1520 | "source": [ 1521 | "Let's say that I'm going to the store and I bought goods and I have their costs. What if I want to add up all the items?" 1522 | ] 1523 | }, 1524 | { 1525 | "cell_type": "code", 1526 | "collapsed": false, 1527 | "input": [ 1528 | "prices = [1.5, 2.35, 5.99, 16.49]" 1529 | ], 1530 | "language": "python", 1531 | "metadata": {}, 1532 | "outputs": [], 1533 | "prompt_number": 75 1534 | }, 1535 | { 1536 | "cell_type": "markdown", 1537 | "metadata": {}, 1538 | "source": [ 1539 | "It's sort of like we have a running total. I need to setup some storage ahead of time again." 1540 | ] 1541 | }, 1542 | { 1543 | "cell_type": "code", 1544 | "collapsed": false, 1545 | "input": [ 1546 | "total = 0" 1547 | ], 1548 | "language": "python", 1549 | "metadata": {}, 1550 | "outputs": [], 1551 | "prompt_number": 76 1552 | }, 1553 | { 1554 | "cell_type": "code", 1555 | "collapsed": false, 1556 | "input": [ 1557 | "for cost in prices:\n", 1558 | " total = total + cost # looks funny but when you think about it, it makes sense" 1559 | ], 1560 | "language": "python", 1561 | "metadata": {}, 1562 | "outputs": [], 1563 | "prompt_number": 77 1564 | }, 1565 | { 1566 | "cell_type": "code", 1567 | "collapsed": false, 1568 | "input": [ 1569 | "total" 1570 | ], 1571 | "language": "python", 1572 | "metadata": {}, 1573 | "outputs": [ 1574 | { 1575 | "metadata": {}, 1576 | "output_type": "pyout", 1577 | "prompt_number": 78, 1578 | "text": [ 1579 | "26.33" 1580 | ] 1581 | } 1582 | ], 1583 | "prompt_number": 78 1584 | }, 1585 | { 1586 | "cell_type": "markdown", 1587 | "metadata": {}, 1588 | "source": [ 1589 | "Now I'm cheating on you guys a little bit because there's a function called sum that does the same thing so we'd probably use that in an actual program" 1590 | ] 1591 | }, 1592 | { 1593 | "cell_type": "code", 1594 | "collapsed": false, 1595 | "input": [ 1596 | "sum(prices)" 1597 | ], 1598 | "language": "python", 1599 | "metadata": {}, 1600 | "outputs": [] 1601 | }, 1602 | { 1603 | "cell_type": "markdown", 1604 | "metadata": {}, 1605 | "source": [ 1606 | "In general, be comfortable with not knowing things. Looking things up is important when programming." 1607 | ] 1608 | }, 1609 | { 1610 | "cell_type": "heading", 1611 | "level": 1, 1612 | "metadata": {}, 1613 | "source": [ 1614 | "Looping over lists of lists" 1615 | ] 1616 | }, 1617 | { 1618 | "cell_type": "markdown", 1619 | "metadata": {}, 1620 | "source": [ 1621 | "Remember that earlier example?" 1622 | ] 1623 | }, 1624 | { 1625 | "cell_type": "code", 1626 | "collapsed": false, 1627 | "input": [ 1628 | "print(\"Hello\")\n", 1629 | "print(\"My name is Crystal\")\n", 1630 | "print(\"I live in Sunnyvale\")" 1631 | ], 1632 | "language": "python", 1633 | "metadata": {}, 1634 | "outputs": [ 1635 | { 1636 | "output_type": "stream", 1637 | "stream": "stdout", 1638 | "text": [ 1639 | "Hello\n", 1640 | "My name is Crystal\n", 1641 | "I live in Sunnyvale\n" 1642 | ] 1643 | } 1644 | ], 1645 | "prompt_number": 33 1646 | }, 1647 | { 1648 | "cell_type": "markdown", 1649 | "metadata": {}, 1650 | "source": [ 1651 | "What if I didn't live in Sunnyvale and Alain does? How would we I resolve that with a list and a loop? " 1652 | ] 1653 | }, 1654 | { 1655 | "cell_type": "markdown", 1656 | "metadata": {}, 1657 | "source": [ 1658 | "The answer is to use a list of lists." 1659 | ] 1660 | }, 1661 | { 1662 | "cell_type": "code", 1663 | "collapsed": false, 1664 | "input": [ 1665 | "mentors = [[\"Crystal\", \"Sunnyvale\"], [\"Kendall\", \"Mountain View\"], [\"Daniel\", \"Redwood City\"]]\n", 1666 | "print(mentors)" 1667 | ], 1668 | "language": "python", 1669 | "metadata": {}, 1670 | "outputs": [ 1671 | { 1672 | "output_type": "stream", 1673 | "stream": "stdout", 1674 | "text": [ 1675 | "[['Crystal', 'Sunnyvale'], ['Kendall', 'Mountain View'], ['Daniel', 'Redwood City']]\n" 1676 | ] 1677 | } 1678 | ], 1679 | "prompt_number": 34 1680 | }, 1681 | { 1682 | "cell_type": "markdown", 1683 | "metadata": {}, 1684 | "source": [ 1685 | "okay, let's loop through them." 1686 | ] 1687 | }, 1688 | { 1689 | "cell_type": "code", 1690 | "collapsed": false, 1691 | "input": [ 1692 | "for mentor in mentors:\n", 1693 | " print(mentor)" 1694 | ], 1695 | "language": "python", 1696 | "metadata": {}, 1697 | "outputs": [ 1698 | { 1699 | "output_type": "stream", 1700 | "stream": "stdout", 1701 | "text": [ 1702 | "['Crystal', 'Sunnyvale']\n", 1703 | "['Kendall', 'Mountain View']\n", 1704 | "['Daniel', 'Redwood City']\n" 1705 | ] 1706 | } 1707 | ], 1708 | "prompt_number": 35 1709 | }, 1710 | { 1711 | "cell_type": "markdown", 1712 | "metadata": {}, 1713 | "source": [ 1714 | "We can loop through the big list, then use indexes to grab stuff from the internal list" 1715 | ] 1716 | }, 1717 | { 1718 | "cell_type": "code", 1719 | "collapsed": false, 1720 | "input": [ 1721 | "for mentor in mentors: # Don't forget to end the statement with a :\n", 1722 | " print(\"Hello\")\n", 1723 | " print(\"My name is {}\".format(mentor[0]))\n", 1724 | " print(\"I live in {}\".format(mentor[1]))\n", 1725 | " print(\"\")" 1726 | ], 1727 | "language": "python", 1728 | "metadata": {}, 1729 | "outputs": [ 1730 | { 1731 | "output_type": "stream", 1732 | "stream": "stdout", 1733 | "text": [ 1734 | "Hello\n", 1735 | "My name is Crystal\n", 1736 | "I live in Sunnyvale\n", 1737 | "\n", 1738 | "Hello\n", 1739 | "My name is Kendall\n", 1740 | "I live in Mountain View\n", 1741 | "\n", 1742 | "Hello\n", 1743 | "My name is Daniel\n", 1744 | "I live in Redwood City\n", 1745 | "\n" 1746 | ] 1747 | } 1748 | ], 1749 | "prompt_number": 39 1750 | }, 1751 | { 1752 | "cell_type": "heading", 1753 | "level": 1, 1754 | "metadata": {}, 1755 | "source": [ 1756 | "Neato Trick" 1757 | ] 1758 | }, 1759 | { 1760 | "cell_type": "code", 1761 | "collapsed": false, 1762 | "input": [ 1763 | "for name, city in mentors: # Don't forget to end the statement with a :\n", 1764 | " print(\"Hello\")\n", 1765 | " print(\"My name is {}\".format(name))\n", 1766 | " print(\"I live in {}\".format(city))" 1767 | ], 1768 | "language": "python", 1769 | "metadata": {}, 1770 | "outputs": [ 1771 | { 1772 | "output_type": "stream", 1773 | "stream": "stdout", 1774 | "text": [ 1775 | "Hello\n", 1776 | "My name is Crystal\n", 1777 | "I live in Sunnyvale\n", 1778 | "Hello\n", 1779 | "My name is Kendall\n", 1780 | "I live in Mountain View\n", 1781 | "Hello\n", 1782 | "My name is Daniel\n", 1783 | "I live in Redwood City\n" 1784 | ] 1785 | } 1786 | ], 1787 | "prompt_number": 42 1788 | }, 1789 | { 1790 | "cell_type": "markdown", 1791 | "metadata": {}, 1792 | "source": [ 1793 | "You can also do this without the use of a loop" 1794 | ] 1795 | }, 1796 | { 1797 | "cell_type": "code", 1798 | "collapsed": false, 1799 | "input": [ 1800 | "crystal, kendall, daniel = mentors\n", 1801 | "print(crystal)" 1802 | ], 1803 | "language": "python", 1804 | "metadata": {}, 1805 | "outputs": [ 1806 | { 1807 | "output_type": "stream", 1808 | "stream": "stdout", 1809 | "text": [ 1810 | "['Crystal', 'Sunnyvale']\n" 1811 | ] 1812 | } 1813 | ], 1814 | "prompt_number": 43 1815 | }, 1816 | { 1817 | "cell_type": "heading", 1818 | "level": 1, 1819 | "metadata": {}, 1820 | "source": [ 1821 | "Sets" 1822 | ] 1823 | }, 1824 | { 1825 | "cell_type": "markdown", 1826 | "metadata": {}, 1827 | "source": [ 1828 | "A set is similar to a list, except it eats the duplicates." 1829 | ] 1830 | }, 1831 | { 1832 | "cell_type": "markdown", 1833 | "metadata": {}, 1834 | "source": [ 1835 | "Let's take a list of things where we want to remove duplicates. Why? Well, what if we want to count the number of different, distinct words used in the Gettysburg Address? Google for it (it's on wikipedia and stick it into a string." 1836 | ] 1837 | }, 1838 | { 1839 | "cell_type": "code", 1840 | "collapsed": false, 1841 | "input": [ 1842 | "gettysburg_address = \"\"\"Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.\"\"\"" 1843 | ], 1844 | "language": "python", 1845 | "metadata": {}, 1846 | "outputs": [], 1847 | "prompt_number": 98 1848 | }, 1849 | { 1850 | "cell_type": "markdown", 1851 | "metadata": {}, 1852 | "source": [ 1853 | "We'll use the split() method of strings to chop that up into words." 1854 | ] 1855 | }, 1856 | { 1857 | "cell_type": "code", 1858 | "collapsed": false, 1859 | "input": [ 1860 | "words = gettysburg_address.split(\" \")\n", 1861 | "print(words)" 1862 | ], 1863 | "language": "python", 1864 | "metadata": {}, 1865 | "outputs": [ 1866 | { 1867 | "output_type": "stream", 1868 | "stream": "stdout", 1869 | "text": [ 1870 | "['Four', 'score', 'and', 'seven', 'years', 'ago', 'our', 'fathers', 'brought', 'forth', 'on', 'this', 'continent', 'a', 'new', 'nation,', 'conceived', 'in', 'liberty,', 'and', 'dedicated', 'to', 'the', 'proposition', 'that', 'all', 'men', 'are', 'created', 'equal.']\n" 1871 | ] 1872 | } 1873 | ], 1874 | "prompt_number": 99 1875 | }, 1876 | { 1877 | "cell_type": "markdown", 1878 | "metadata": {}, 1879 | "source": [ 1880 | "Not perfect, but lets count the words it generates using the len() method" 1881 | ] 1882 | }, 1883 | { 1884 | "cell_type": "code", 1885 | "collapsed": false, 1886 | "input": [ 1887 | "print(len(words))" 1888 | ], 1889 | "language": "python", 1890 | "metadata": {}, 1891 | "outputs": [ 1892 | { 1893 | "output_type": "stream", 1894 | "stream": "stdout", 1895 | "text": [ 1896 | "30\n" 1897 | ] 1898 | } 1899 | ], 1900 | "prompt_number": 100 1901 | }, 1902 | { 1903 | "cell_type": "markdown", 1904 | "metadata": {}, 1905 | "source": [ 1906 | "If you look at the address however, you'll see that 'and' is used several times. Let's turn our list into a set.\n", 1907 | "Using.... well.... the set method." 1908 | ] 1909 | }, 1910 | { 1911 | "cell_type": "code", 1912 | "collapsed": false, 1913 | "input": [ 1914 | "wordset = set(words)\n", 1915 | "print(wordset)" 1916 | ], 1917 | "language": "python", 1918 | "metadata": {}, 1919 | "outputs": [ 1920 | { 1921 | "output_type": "stream", 1922 | "stream": "stdout", 1923 | "text": [ 1924 | "{'fathers', 'proposition', 'forth', 'equal.', 'conceived', 'men', 'nation,', 'ago', 'created', 'in', 'a', 'that', 'dedicated', 'are', 'our', 'brought', 'the', 'seven', 'and', 'score', 'all', 'this', 'Four', 'new', 'continent', 'on', 'liberty,', 'to', 'years'}\n" 1925 | ] 1926 | } 1927 | ], 1928 | "prompt_number": 102 1929 | }, 1930 | { 1931 | "cell_type": "markdown", 1932 | "metadata": {}, 1933 | "source": [ 1934 | "Whoa! Looks different! Curly braces instead of bracket.s Maybe even shorter! Let's check, shall we?" 1935 | ] 1936 | }, 1937 | { 1938 | "cell_type": "code", 1939 | "collapsed": false, 1940 | "input": [ 1941 | "print(len(wordset))" 1942 | ], 1943 | "language": "python", 1944 | "metadata": {}, 1945 | "outputs": [ 1946 | { 1947 | "output_type": "stream", 1948 | "stream": "stdout", 1949 | "text": [ 1950 | "29\n" 1951 | ] 1952 | } 1953 | ], 1954 | "prompt_number": 103 1955 | }, 1956 | { 1957 | "cell_type": "markdown", 1958 | "metadata": {}, 1959 | "source": [ 1960 | "As you can see, sets removed duplicates." 1961 | ] 1962 | }, 1963 | { 1964 | "cell_type": "heading", 1965 | "level": 1, 1966 | "metadata": {}, 1967 | "source": [ 1968 | "Extra Credit / Pop Quiz time!" 1969 | ] 1970 | }, 1971 | { 1972 | "cell_type": "markdown", 1973 | "metadata": {}, 1974 | "source": [ 1975 | "1. Use google to figure out how to add the following phrase to the Gettysburg Address **wordset**: \"Abraham Lincoln" 1976 | ] 1977 | }, 1978 | { 1979 | "cell_type": "markdown", 1980 | "metadata": {}, 1981 | "source": [ 1982 | "2. sort the list of names in reverse order.\n" 1983 | ] 1984 | }, 1985 | { 1986 | "cell_type": "markdown", 1987 | "metadata": {}, 1988 | "source": [ 1989 | "3. Lists: Items 1 through 9\n", 1990 | "http://www.codecademy.com/courses/python-beginner-en-pwmb1/0/1?curriculum_id=4f89dab3d788890003000096\n" 1991 | ] 1992 | }, 1993 | { 1994 | "cell_type": "markdown", 1995 | "metadata": {}, 1996 | "source": [ 1997 | "4. Lists: Items 1, 3, 5\n", 1998 | "http://www.codecademy.com/courses/python-beginner-en-IZ9Ra/0/2?curriculum_id=4f89dab3d788890003000096" 1999 | ] 2000 | } 2001 | ], 2002 | "metadata": {} 2003 | } 2004 | ] 2005 | } -------------------------------------------------------------------------------- /part-3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "kernelspec": { 4 | "codemirror_mode": { 5 | "name": "ipython", 6 | "version": 2 7 | }, 8 | "display_name": "IPython (Python 2)", 9 | "language": "python", 10 | "name": "python2" 11 | }, 12 | "name": "", 13 | "signature": "sha256:7cf277e28b0242bd51b679f9211a88c2100b7c34ff7fd7977b2ad160b75c9df3" 14 | }, 15 | "nbformat": 3, 16 | "nbformat_minor": 0, 17 | "worksheets": [ 18 | { 19 | "cells": [ 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "Introductions: Hi, I'm .... I'll be co-facilitating 'Programming with Python' with [our mentors names] and [our volunteers names]. If you would like to follow along and type in some of the snippets of code that we demo, our volunteers can assist you in setting up your computer. " 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "Let's begin by giving a quick recap on what we did in the earlier workshops today. In the first workshop we learned about numbers, variables, strings, booleans, and making choices with the use of if-statements. In the second workshop we added lists and looping to our skills. Along the way we used a few functions, such as the built-in functions, print() and len()." 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "In this workshop, we're going to go a little further in depth into functions and Python modules." 39 | ] 40 | }, 41 | { 42 | "cell_type": "heading", 43 | "level": 1, 44 | "metadata": {}, 45 | "source": [ 46 | "Functions" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "We have already seen a few functions (recall len() for calculating list and string lengths and sum() for summing lists).\n", 54 | "\n", 55 | "Python also lets us make our own functions." 56 | ] 57 | }, 58 | { 59 | "cell_type": "heading", 60 | "level": 2, 61 | "metadata": {}, 62 | "source": [ 63 | "Example: Greeting Script" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "Let's write our first Python script. Create a file called `birthday.py`. Put the following line into the file. " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "collapsed": false, 76 | "input": [ 77 | "print(\"Happy Birthday, dear Carol!\")" 78 | ], 79 | "language": "python", 80 | "metadata": {}, 81 | "outputs": [] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "Now you have a program that simply prints a happy birthday greeting for Carol.\n", 88 | "\n", 89 | "It's not Carol's birthday, but let's run this program anyway, just to confuse her in good fun ;)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "collapsed": false, 95 | "input": [ 96 | "%%bash\n", 97 | "python3 greeting.py" 98 | ], 99 | "language": "python", 100 | "metadata": {}, 101 | "outputs": [] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "We can make this code into a function like so. Let's take a quick look at what we have here." 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "collapsed": false, 113 | "input": [ 114 | "def happy_birthday_carol():\n", 115 | " print(\"Happy Birthday, dear Carol!\")" 116 | ], 117 | "language": "python", 118 | "metadata": {}, 119 | "outputs": [] 120 | }, 121 | { 122 | "cell_type": "heading", 123 | "level": 3, 124 | "metadata": {}, 125 | "source": [ 126 | "Examine the function in detail" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "The top part is the *header* of the function. The header contains the keyword, 'def', the name of the function, a parameter, and a colon.\n", 134 | "\n", 135 | "`def` is short for *define*. It's the Python keyword used to indicate the start of a function definition.\n", 136 | "\n", 137 | "`happy_birthday_carol` is the name of the function.\n", 138 | "\n", 139 | "This is a function with no parameters.\n", 140 | "\n", 141 | "The body of our function contains one print line. We need to indent this line, and any other lines of code in a function's body, by 4 spaces." 142 | ] 143 | }, 144 | { 145 | "cell_type": "heading", 146 | "level": 3, 147 | "metadata": {}, 148 | "source": [ 149 | "Let's call the function" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "Notice that nothing was printed out after we defined our function. To use our function we need to call it, like this:" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "collapsed": false, 162 | "input": [ 163 | "happy_birthday_carol()" 164 | ], 165 | "language": "python", 166 | "metadata": {}, 167 | "outputs": [] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "Don't forget those parentheses. \n", 174 | "\n", 175 | "Without the parentheses, Python wouldn't know that we were trying to call our function, and will print out the value of the thing called `happy_birthday_carol`." 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "collapsed": false, 181 | "input": [ 182 | "happy_birthday_carol" 183 | ], 184 | "language": "python", 185 | "metadata": {}, 186 | "outputs": [] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "Here, the Python interpreter is basically saying, \"The value of happy_birthday_carol is a function.\"" 193 | ] 194 | }, 195 | { 196 | "cell_type": "heading", 197 | "level": 3, 198 | "metadata": {}, 199 | "source": [ 200 | "Let's define and call another function" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "That function printed a nice greeting for Carol. But Rise's now feeling left out. Let's define a function for her too." 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "collapsed": false, 213 | "input": [ 214 | "def happy_birthday_rise():\n", 215 | " print(\"Happy Birthday, dear Rise!\")" 216 | ], 217 | "language": "python", 218 | "metadata": {}, 219 | "outputs": [] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "Now, let's call the function:" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "collapsed": false, 231 | "input": [ 232 | "happy_birthday_rise()" 233 | ], 234 | "language": "python", 235 | "metadata": {}, 236 | "outputs": [] 237 | }, 238 | { 239 | "cell_type": "heading", 240 | "level": 3, 241 | "metadata": {}, 242 | "source": [ 243 | "Refactor time!" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "Ok, now, let's work on reusing our code so that we reduce code duplication.\n", 251 | "\n", 252 | "Wouldn't it be cool if we could pass a name into our function so we don't need to write a different function for every name?\n", 253 | "\n", 254 | "Python allows us to do just that with function parameters. We can add a parameter by typing a variable name in between the parethesis after our function name. We can then use the parameter variable in our function.\n", 255 | "\n", 256 | "We'll create a new function called `happy_birthday`, and add a parameter, `name`." 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "collapsed": false, 262 | "input": [ 263 | "def happy_birthday(name):\n", 264 | " print(\"Happy Birthday, dear \" + name + \"!\")" 265 | ], 266 | "language": "python", 267 | "metadata": {}, 268 | "outputs": [] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "Here, we are concatenating 3 strings:\n", 275 | "* \"Happy Birthday, dear \"\n", 276 | "* whatever the value of `name` is (Note: for the purposes of this example, let's assume that we're always getting a string)\n", 277 | "* \"!\"" 278 | ] 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "metadata": {}, 283 | "source": [ 284 | "To execute or run our new function, we simply call it by its name, `happy_birthday`, and pass a value in between the parenthesis, such as the string `Trey`." 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "collapsed": false, 290 | "input": [ 291 | "happy_birthday(\"Trey\")" 292 | ], 293 | "language": "python", 294 | "metadata": {}, 295 | "outputs": [] 296 | }, 297 | { 298 | "cell_type": "heading", 299 | "level": 3, 300 | "metadata": {}, 301 | "source": [ 302 | "Calling it over and over in a loop" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "Let's get a little bolder and make a list of names that we can loop over and then pass each one into our `happy_birthday` function. What we get is the same results as the other earlier lines of code!" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "collapsed": false, 315 | "input": [ 316 | "names = [\"Carol\", \"Rise\", \"Trey\", \"Alain\"]" 317 | ], 318 | "language": "python", 319 | "metadata": {}, 320 | "outputs": [] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "collapsed": false, 325 | "input": [ 326 | "for name in names:\n", 327 | " happy_birthday(name)" 328 | ], 329 | "language": "python", 330 | "metadata": {}, 331 | "outputs": [] 332 | }, 333 | { 334 | "cell_type": "heading", 335 | "level": 2, 336 | "metadata": {}, 337 | "source": [ 338 | "Exercise: Try to break `happy_birthday()`" 339 | ] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": {}, 344 | "source": [ 345 | "1) Try calling `happy_birthday()` with other values, such as:\n", 346 | "* A really, really, really long name\n", 347 | "* Something ridiculous that makes no sense grammatically\n", 348 | "* Numbers\n", 349 | "* Other Python types, if you know about any other ones\n", 350 | "\n", 351 | "This is how malicious \"black hat\" hackers think when they try to break into desktop and web applications.\n", 352 | "\n", 353 | "2) Try adding a bad list item to `names`. Experiment with the position of the bad item." 354 | ] 355 | }, 356 | { 357 | "cell_type": "heading", 358 | "level": 2, 359 | "metadata": {}, 360 | "source": [ 361 | "Example: Better String Formatting" 362 | ] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": {}, 367 | "source": [ 368 | "Rather than using string concatenation, let's use a string formatting operator that doesn't care what type of value it receives." 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "collapsed": false, 374 | "input": [ 375 | "def happy_birthday(name):\n", 376 | " print(\"Happy Birthday, dear {0}!\".format(name))" 377 | ], 378 | "language": "python", 379 | "metadata": {}, 380 | "outputs": [] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "Now, the function can print happy birthday greetings for any value of `name`, even for people with bizarre names like:\n", 387 | "* 100\n", 388 | "* 3.1415927\n", 389 | "* \u01ac\u0335\u032c\u030a (The Artist Formerly Known As Prince)" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "collapsed": false, 395 | "input": [ 396 | "happy_birthday(100)\n" 397 | ], 398 | "language": "python", 399 | "metadata": {}, 400 | "outputs": [] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "collapsed": false, 405 | "input": [ 406 | "happy_birthday(3.1415927)" 407 | ], 408 | "language": "python", 409 | "metadata": {}, 410 | "outputs": [] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "collapsed": false, 415 | "input": [ 416 | "happy_birthday(\"\u01ac\u0335\u032c\u030a\")" 417 | ], 418 | "language": "python", 419 | "metadata": {}, 420 | "outputs": [] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": {}, 425 | "source": [ 426 | "It's better to use `format()` just in case `name` isn't guaranteed to be a string." 427 | ] 428 | }, 429 | { 430 | "cell_type": "heading", 431 | "level": 2, 432 | "metadata": {}, 433 | "source": [ 434 | "Advanced Exercise: Why not use `str()` like this?" 435 | ] 436 | }, 437 | { 438 | "cell_type": "code", 439 | "collapsed": false, 440 | "input": [ 441 | "def happy_birthday(name):\n", 442 | " print(\"Happy Birthday, dear \" + str(name) + \"!\")" 443 | ], 444 | "language": "python", 445 | "metadata": {}, 446 | "outputs": [] 447 | }, 448 | { 449 | "cell_type": "markdown", 450 | "metadata": {}, 451 | "source": [ 452 | "Answer: You can, but keep in mind that this only works on Python 3:" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "collapsed": false, 458 | "input": [ 459 | "str(u'\\xff')" 460 | ], 461 | "language": "python", 462 | "metadata": {}, 463 | "outputs": [] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "Now you have some advanced knowledge about how Python 3 handles Unicode better than Python 2!" 470 | ] 471 | }, 472 | { 473 | "cell_type": "heading", 474 | "level": 2, 475 | "metadata": {}, 476 | "source": [ 477 | "Example: Getting Fancy With Math" 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": {}, 483 | "source": [ 484 | "What about a function that does some math for us?\n", 485 | "\n", 486 | "Every year on my birthday, I like to figure out what numbers are divisible by my new age. That is, the factors of my age:" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "collapsed": false, 492 | "input": [ 493 | "def print_factors(age):\n", 494 | " for i in range(1, age + 1):\n", 495 | " if age % i == 0:\n", 496 | " print(i)" 497 | ], 498 | "language": "python", 499 | "metadata": {}, 500 | "outputs": [] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "metadata": {}, 505 | "source": [ 506 | "This year, I turned 32." 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "collapsed": false, 512 | "input": [ 513 | "print_factors(32)" 514 | ], 515 | "language": "python", 516 | "metadata": {}, 517 | "outputs": [] 518 | }, 519 | { 520 | "cell_type": "markdown", 521 | "metadata": {}, 522 | "source": [ 523 | "From the output of `print_factors()`, I now know that I'm:\n", 524 | "* Twice as old as a 16-year-old\n", 525 | "* Four times as old as an 8-year-old\n", 526 | "* Eight times as old as a 4-year-old\n", 527 | "* Sixteen times as old as a 2-year-old\n", 528 | "\n", 529 | "How is this useful? Well, the next time a 4-year-old bullies me, I'm going to say, \"Watch it, I'm exactly eight times as old as you.\"\n", 530 | "\n", 531 | "Next year, I'll be able to brag to 3-year-olds: \"Ha ha ha, I'm exactly eleven times as old as you.\"" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "collapsed": false, 537 | "input": [ 538 | "print_factors(33)" 539 | ], 540 | "language": "python", 541 | "metadata": {}, 542 | "outputs": [] 543 | 544 | }, 545 | { 546 | "cell_type": "heading", 547 | "level": 2, 548 | "metadata": {}, 549 | "source": [ 550 | "Example: Cupcake Tally" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "metadata": {}, 556 | "source": [ 557 | "So far we've explored functions that:\n", 558 | "* Take strings or other values as input\n", 559 | "* Print strings" 560 | ] 561 | }, 562 | { 563 | "cell_type": "markdown", 564 | "metadata": {}, 565 | "source": [ 566 | "Let's create a new function, `cupcake_tally()`. This function will be different from the others that I covered. Rather than just printing stuff, it'll return a value when called.\n", 567 | "\n", 568 | "A good birthday party should allocate:\n", 569 | "* 2 cupcakes per guest\n", 570 | "* 13 cupcakes for the birthday person, because if it's your birthday, you should be allowed to eat a baker's dozen of cupcakes." 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "collapsed": false, 576 | "input": [ 577 | "def cupcake_tally(guests):\n", 578 | " \"\"\" Given number of party guests, returns how many cupcakes are needed. \"\"\"\n", 579 | " return 2 * guests + 13" 580 | ], 581 | "language": "python", 582 | "metadata": {}, 583 | "outputs": [] 584 | }, 585 | { 586 | "cell_type": "markdown", 587 | "metadata": {}, 588 | "source": [ 589 | "How many cupcakes do we need for a party with 30 guests?" 590 | ] 591 | }, 592 | { 593 | "cell_type": "code", 594 | "collapsed": false, 595 | "input": [ 596 | "cupcake_tally(30)" 597 | ], 598 | "language": "python", 599 | "metadata": {}, 600 | "outputs": [] 601 | }, 602 | { 603 | "cell_type": "markdown", 604 | "metadata": {}, 605 | "source": [ 606 | "What about if I'm celebrating my birthday at home with just my husband, Daniel?" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "collapsed": false, 612 | "input": [ 613 | "cupcake_tally(guests=1)" 614 | ], 615 | "language": "python", 616 | "metadata": {}, 617 | "outputs": [] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": {}, 622 | "source": [ 623 | "Ooh, 15 cupcakes sounds about right for our little party, yeah? ;)\n", 624 | "\n", 625 | "Notice how we explicitly named the `guests` parameter in that example." 626 | ] 627 | }, 628 | { 629 | "cell_type": "heading", 630 | "level": 3, 631 | "metadata": {}, 632 | "source": [ 633 | "Printing the return value" 634 | ] 635 | }, 636 | { 637 | "cell_type": "code", 638 | "collapsed": false, 639 | "input": [ 640 | "cupcakes = cupcake_tally(10)\n", 641 | "print(\"We need to make {0} cupcakes.\".format(cupcakes))" 642 | ], 643 | "language": "python", 644 | "metadata": {}, 645 | "outputs": [] 646 | }, 647 | { 648 | "cell_type": "markdown", 649 | "metadata": {}, 650 | "source": [ 651 | "We call our function, `cupcake_tally()`, and pass in 10 as the value of `guests`. \n", 652 | "\n", 653 | "`cupcake_tally(10)` returns 33, which gets stored in the `cupcakes` variable.\n", 654 | "\n", 655 | "Lastly, we print \"We need to make 33 cupcakes.\" using Python string formatting." 656 | ] 657 | }, 658 | { 659 | "cell_type": "heading", 660 | "level": 3, 661 | "metadata": {}, 662 | "source": [ 663 | "Multiple function arguments" 664 | ] 665 | }, 666 | { 667 | "cell_type": "markdown", 668 | "metadata": {}, 669 | "source": [ 670 | "Let's talk about one last thing that functions can do and that is, they can be given more than one parameter. Let's demonstrate this by passing 2 parameters to our cupcake function.\n", 671 | "\n", 672 | "We want to be able to specify the number of cupcakes each guest will eat. Let's change our function to accept two parameters:" 673 | ] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "collapsed": false, 678 | "input": [ 679 | "def cupcake_tally(cupcakes, guests): \n", 680 | " return cupcakes * guests + 13" 681 | ], 682 | "language": "python", 683 | "metadata": {}, 684 | "outputs": [] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "collapsed": false, 689 | "input": [ 690 | "cupcake_tally(4, 15)" 691 | ], 692 | "language": "python", 693 | "metadata": {}, 694 | "outputs": [] 695 | }, 696 | { 697 | "cell_type": "markdown", 698 | "metadata": {}, 699 | "source": [ 700 | "Note that we have 2 parameters now: `cupcakes` and `guests`\n", 701 | "\n", 702 | "Our function is called and passes in two values of 4 and 15. It returns 73." 703 | ] 704 | }, 705 | { 706 | "cell_type": "heading", 707 | "level": 2, 708 | "metadata": {}, 709 | "source": [ 710 | "Summary: What are functions used for?" 711 | ] 712 | }, 713 | { 714 | "cell_type": "heading", 715 | "level": 3, 716 | "metadata": {}, 717 | "source": [ 718 | "Program decomposition, or factoring" 719 | ] 720 | }, 721 | { 722 | "cell_type": "markdown", 723 | "metadata": {}, 724 | "source": [ 725 | "To break down a problem into parts. It makes the code more readable and is easier to understand." 726 | ] 727 | }, 728 | { 729 | "cell_type": "heading", 730 | "level": 3, 731 | "metadata": {}, 732 | "source": [ 733 | "Code reuse" 734 | ] 735 | }, 736 | { 737 | "cell_type": "markdown", 738 | "metadata": {}, 739 | "source": [ 740 | "Functions can be used instead of repeating the same lines of code at different times throughout a program. This reduces duplication of code." 741 | ] 742 | }, 743 | { 744 | "cell_type": "heading", 745 | "level": 3, 746 | "metadata": {}, 747 | "source": [ 748 | "Abstraction or simplification" 749 | ] 750 | }, 751 | { 752 | "cell_type": "markdown", 753 | "metadata": {}, 754 | "source": [ 755 | "Using functions allows us to hide all of the details involved and put them into one place and simply call the function to execute the lines of code." 756 | ] 757 | }, 758 | { 759 | "cell_type": "heading", 760 | "level": 2, 761 | "metadata": {}, 762 | "source": [ 763 | "Exercise: Why are functions useful?" 764 | ] 765 | }, 766 | { 767 | "cell_type": "markdown", 768 | "metadata": {}, 769 | "source": [ 770 | "First, create a new Python script and type in the following." 771 | ] 772 | }, 773 | { 774 | "cell_type": "code", 775 | "collapsed": false, 776 | "input": [ 777 | "print(\"On the day of Sept 20, 2014, were you at Intro to Python?\")\n", 778 | "answer = input(\"Answer truthfully, yes or no --> \")\n", 779 | "if answer == \"no\":\n", 780 | " answer = input(\"Are you sure that you weren't? Tell the truth, now --> \")\n", 781 | "\n", 782 | "print(\"Were you thinking of skipping the workshop to go to Sea World?\")\n", 783 | "answer = input(\"Answer truthfully, yes or no --> \")\n", 784 | "if answer == \"no\":\n", 785 | " answer = input(\"Are you sure that you weren't? Tell the truth, now --> \")" 786 | ], 787 | "language": "python", 788 | "metadata": {}, 789 | "outputs": [] 790 | }, 791 | { 792 | "cell_type": "markdown", 793 | "metadata": {}, 794 | "source": [ 795 | "Look up when you're done, so that we know to move on to the next step." 796 | ] 797 | }, 798 | { 799 | "cell_type": "heading", 800 | "level": 4, 801 | "metadata": {}, 802 | "source": [ 803 | "Was this annoying?" 804 | ] 805 | }, 806 | { 807 | "cell_type": "markdown", 808 | "metadata": {}, 809 | "source": [ 810 | "Raise your hand if you felt like this exercise was repetitive...TODO" 811 | ] 812 | }, 813 | { 814 | "cell_type": "heading", 815 | "level": 2, 816 | "metadata": {}, 817 | "source": [ 818 | "Modules" 819 | ] 820 | }, 821 | { 822 | "cell_type": "markdown", 823 | "metadata": {}, 824 | "source": [ 825 | "Modules are used to make a programmer's life easier. A module is used to group functions, variables, and other things together in an understandable bundle.\n", 826 | "\n", 827 | "Python comes with lots of modules built-in. For example there's a random module for generating random numbers. Let's import it so we can use some of its functions:" 828 | ] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "collapsed": false, 833 | "input": [ 834 | "import random" 835 | ], 836 | "language": "python", 837 | "metadata": {}, 838 | "outputs": [], 839 | "prompt_number": 8 840 | }, 841 | { 842 | "cell_type": "markdown", 843 | "metadata": {}, 844 | "source": [ 845 | "The [random](https://docs.python.org/3.4/library/random.html) module has a [randint](https://docs.python.org/3.4/library/random.html?highlight=randint#random.randint) function which takes two parameters. randint will return a random integer in between (or including) the two parameters. For example we can generate a random number between 1 and 6." 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "collapsed": false, 851 | "input": [ 852 | "random.randint(1, 6)" 853 | ], 854 | "language": "python", 855 | "metadata": {}, 856 | "outputs": [ 857 | { 858 | "metadata": {}, 859 | "output_type": "pyout", 860 | "prompt_number": 10, 861 | "text": [ 862 | "2" 863 | ] 864 | } 865 | ], 866 | "prompt_number": 10 867 | }, 868 | { 869 | "cell_type": "markdown", 870 | "metadata": {}, 871 | "source": [ 872 | "Let's use the random integer function to simulate rolling a die in a game" 873 | ] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "collapsed": false, 878 | "input": [ 879 | "vegas_die_1 = random.randint(1, 6)\n", 880 | "vegas_die_2 = random.randint(1, 6)\n", 881 | "print(\"First die: \" + str(vegas_die_1))\n", 882 | "print(\"Second die: \" + str(vegas_die_2))\n", 883 | "print(\"You rolled a \" + str(vegas_die_1 + vegas_die_2))" 884 | ], 885 | "language": "python", 886 | "metadata": {}, 887 | "outputs": [ 888 | { 889 | "output_type": "stream", 890 | "stream": "stdout", 891 | "text": [ 892 | "First die: 3\n", 893 | "Second die: 3\n", 894 | "You rolled a 6\n" 895 | ] 896 | } 897 | ], 898 | "prompt_number": 12 899 | }, 900 | { 901 | "cell_type": "markdown", 902 | "metadata": {}, 903 | "source": [ 904 | "choice is a function in the random module that returns a random item from a given list or string" 905 | ] 906 | }, 907 | { 908 | "cell_type": "code", 909 | "collapsed": false, 910 | "input": [ 911 | "random.choice('abcdefghijklmnopqrstuvwxyz')" 912 | ], 913 | "language": "python", 914 | "metadata": {}, 915 | "outputs": [ 916 | { 917 | "metadata": {}, 918 | "output_type": "pyout", 919 | "prompt_number": 13, 920 | "text": [ 921 | "'m'" 922 | ] 923 | } 924 | ], 925 | "prompt_number": 13 926 | }, 927 | { 928 | "cell_type": "markdown", 929 | "metadata": {}, 930 | "source": [ 931 | "We could use random.choice to wish happy birthday to a random friend." 932 | ] 933 | }, 934 | { 935 | "cell_type": "markdown", 936 | "metadata": {}, 937 | "source": [ 938 | "Let's make a function random_happy_birthday that takes a list of names and wishes a random person happy birthday" 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "collapsed": false, 944 | "input": [ 945 | "def random_happy_birthday(names):\n", 946 | " name = random.choice(names)\n", 947 | " happy_birthday(name)" 948 | ], 949 | "language": "python", 950 | "metadata": {}, 951 | "outputs": [], 952 | "prompt_number": 14 953 | }, 954 | { 955 | "cell_type": "markdown", 956 | "metadata": {}, 957 | "source": [ 958 | "Now let's try it out with a list of names:" 959 | ] 960 | }, 961 | { 962 | "cell_type": "code", 963 | "collapsed": false, 964 | "input": [ 965 | "random_happy_birthday([\"Alain\", \"Carol\", \"Rise\", \"Trey\"])" 966 | ], 967 | "language": "python", 968 | "metadata": {}, 969 | "outputs": [ 970 | { 971 | "output_type": "stream", 972 | "stream": "stdout", 973 | "text": [ 974 | "Happy Birthday, dear Carol!\n" 975 | ] 976 | } 977 | ], 978 | "prompt_number": 20 979 | }, 980 | { 981 | "cell_type": "markdown", 982 | "metadata": {}, 983 | "source": [ 984 | "Modules and functions can make things really simple to do. For example we could in a few lines of code get and display a YouTube video" 985 | ] 986 | }, 987 | { 988 | "cell_type": "code", 989 | "collapsed": false, 990 | "input": [ 991 | "from IPython.display import YouTubeVideo\n", 992 | "# a tutorial about Python at PyCon 2014 in Montreal, Canada by Jessica McKellar\n", 993 | "YouTubeVideo('MirG-vJOg04')" 994 | ], 995 | "language": "python", 996 | "metadata": {}, 997 | "outputs": [ 998 | { 999 | "html": [ 1000 | "\n", 1001 | " \n", 1008 | " " 1009 | ], 1010 | "metadata": {}, 1011 | "output_type": "pyout", 1012 | "prompt_number": 11, 1013 | "text": [ 1014 | "" 1015 | ] 1016 | } 1017 | ], 1018 | "prompt_number": 11 1019 | }, 1020 | { 1021 | "cell_type": "code", 1022 | "collapsed": false, 1023 | "input": [], 1024 | "language": "python", 1025 | "metadata": {}, 1026 | "outputs": [] 1027 | }, 1028 | { 1029 | "cell_type": "code", 1030 | "collapsed": false, 1031 | "input": [], 1032 | "language": "python", 1033 | "metadata": {}, 1034 | "outputs": [] 1035 | } 1036 | ], 1037 | "metadata": {} 1038 | } 1039 | ] 1040 | } -------------------------------------------------------------------------------- /part-4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:d806710bd05c6f416dc3842515677b9246aa116881cfd0f7dd2c6b748a3a67e5" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "Let's learn more cool Python stuff." 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "## Dictionaries\n", 23 | "\n", 24 | "Unlike lists, dictionaries are indexed by keys. Dictionaries can be used to represent unordered key-value pairs. Keys are unique and are used for looking up values assigned to them.\n", 25 | "\n", 26 | "Let's make a Spanish to English translator. We'll ignore grammar and just translate word-by-word for now." 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "collapsed": false, 32 | "input": [ 33 | "words = {\n", 34 | " 'gato': \"cat\",\n", 35 | " 'casa': \"house\",\n", 36 | " 'esta': \"is\",\n", 37 | " 'en': \"in\",\n", 38 | " 'el': \"the\",\n", 39 | " 'la': \"the\",\n", 40 | "}" 41 | ], 42 | "language": "python", 43 | "metadata": {}, 44 | "outputs": [] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "collapsed": false, 49 | "input": [ 50 | "words['gato']" 51 | ], 52 | "language": "python", 53 | "metadata": {}, 54 | "outputs": [] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "Similar to lists and strings, you can check whether a dictionary contains a given key." 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "collapsed": false, 66 | "input": [ 67 | "'gato' in words" 68 | ], 69 | "language": "python", 70 | "metadata": {}, 71 | "outputs": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "We can also reassign keys in our dictionary:" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "collapsed": false, 83 | "input": [ 84 | "words['gato'] = \"dog\"\n", 85 | "words['gato']" 86 | ], 87 | "language": "python", 88 | "metadata": {}, 89 | "outputs": [] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "Let's change that back so we don't confuse anyone:" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "collapsed": false, 101 | "input": [ 102 | "words['gato'] = \"cat\"" 103 | ], 104 | "language": "python", 105 | "metadata": {}, 106 | "outputs": [] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "We can also check how many key-value pairs are in our dictionary:" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "collapsed": false, 118 | "input": [ 119 | "len(words)" 120 | ], 121 | "language": "python", 122 | "metadata": {}, 123 | "outputs": [] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "Strings have a split method which we can use to split strings with spaces into a list of words." 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "collapsed": false, 135 | "input": [ 136 | "sentence = \"el gato esta en la casa\"\n", 137 | "sentence_words = sentence.split()\n", 138 | "sentence_words" 139 | ], 140 | "language": "python", 141 | "metadata": {}, 142 | "outputs": [] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Let's translate each word and save them in the list called translated_words. Let's start with an empty list and use a for loop to populate our list:" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "collapsed": false, 154 | "input": [ 155 | "translated_words = []\n", 156 | "for spanish_word in sentence_words:\n", 157 | " translated_words.append(words[spanish_word])" 158 | ], 159 | "language": "python", 160 | "metadata": {}, 161 | "outputs": [] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "collapsed": false, 166 | "input": [ 167 | "translated_words" 168 | ], 169 | "language": "python", 170 | "metadata": {}, 171 | "outputs": [] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "We've almost made a translated sentence! Let's join our new list of words back together with spaces in between each word." 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "collapsed": false, 183 | "input": [ 184 | "translated_sentence = \" \".join(translated_words)\n", 185 | "translated_sentence" 186 | ], 187 | "language": "python", 188 | "metadata": {}, 189 | "outputs": [] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "Let's put this all together into a function:" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "collapsed": false, 201 | "input": [ 202 | "words = {\n", 203 | " 'gato': \"cat\",\n", 204 | " 'casa': \"house\",\n", 205 | " 'esta': \"is\",\n", 206 | " 'en': \"in\",\n", 207 | " 'el': \"the\",\n", 208 | " 'la': \"the\",\n", 209 | "}\n", 210 | "\n", 211 | "def translate(sentence):\n", 212 | " spanish_words = sentence.split()\n", 213 | " english_words = []\n", 214 | " for w in spanish_words:\n", 215 | " english_words.append(words[w])\n", 216 | " return \" \".join(english_words)" 217 | ], 218 | "language": "python", 219 | "metadata": {}, 220 | "outputs": [] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "collapsed": false, 225 | "input": [ 226 | "translate(\"el gato esta en la casa\")" 227 | ], 228 | "language": "python", 229 | "metadata": {}, 230 | "outputs": [] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "Let's add another word to our dictionary:" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "collapsed": false, 242 | "input": [ 243 | "words['jugo'] = \"juice\"" 244 | ], 245 | "language": "python", 246 | "metadata": {}, 247 | "outputs": [] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "collapsed": false, 252 | "input": [ 253 | "translate(\"el jugo esta en la casa\")" 254 | ], 255 | "language": "python", 256 | "metadata": {}, 257 | "outputs": [] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "That's how we say juice in Mexican Spanish but what if we're traveling to Spain? Let's remove jugo from our dictionary and add zumo:" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "collapsed": false, 269 | "input": [ 270 | "del words['jugo']\n", 271 | "words['zumo'] = \"juice\"" 272 | ], 273 | "language": "python", 274 | "metadata": {}, 275 | "outputs": [] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "metadata": {}, 280 | "source": [ 281 | "Let's try our Mexican Spanish sentence again. What should happen?" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "collapsed": false, 287 | "input": [ 288 | "translate(\"el jugo esta en la casa\")" 289 | ], 290 | "language": "python", 291 | "metadata": {}, 292 | "outputs": [] 293 | }, 294 | { 295 | "cell_type": "markdown", 296 | "metadata": {}, 297 | "source": [ 298 | "Now let's try our Spanish Spanish sentence:" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "collapsed": false, 304 | "input": [ 305 | "translate(\"el zumo esta en la casa\")" 306 | ], 307 | "language": "python", 308 | "metadata": {}, 309 | "outputs": [] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": {}, 314 | "source": [ 315 | "It's also possible to loop through the keys and values of a dictionary using the items method.\n", 316 | "Let's loop through the words dictionary and see how this works:" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "collapsed": false, 322 | "input": [ 323 | "for key, value in words.items():\n", 324 | " print(key, value)" 325 | ], 326 | "language": "python", 327 | "metadata": {}, 328 | "outputs": [] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "**Exercises:**\n", 335 | "\n", 336 | "1. Make a new py file and put that translate function in the file. Use the print function to print out some examples of using the function.\n", 337 | "\n", 338 | "2. Refactor the translate function to use a list comprehension.\n", 339 | "\n", 340 | "3. There are lots of ways to write that translate function. Just for fun, see if you can write the whole translate function in one line.\n", 341 | "\n", 342 | "**Note:** Typing out the exercises for the next section, \"Sets\", is optional. If your brain is melting, feel free to sit back, relax, and enjoy the rest of the lecture." 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "## Tuples\n", 350 | "\n", 351 | "Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n", 352 | "\n", 353 | "One real world use of tuples is to hold one particular record within a complex file. For example, a stock holding or a restaurant inventory.\n", 354 | "\n", 355 | "Tuples are comma-separated lists:" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "collapsed": false, 361 | "input": [ 362 | "food = (\"cones\", 100, 3.79)" 363 | ], 364 | "language": "python", 365 | "metadata": {}, 366 | "outputs": [] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": {}, 371 | "source": [ 372 | "This is sometimes called packing a tuple." 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "collapsed": false, 378 | "input": [ 379 | "food" 380 | ], 381 | "language": "python", 382 | "metadata": {}, 383 | "outputs": [] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "collapsed": false, 388 | "input": [ 389 | "type(food)" 390 | ], 391 | "language": "python", 392 | "metadata": {}, 393 | "outputs": [] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "The parenthesis are often optional. They're often added for clarity. This should work too:" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "collapsed": false, 405 | "input": [ 406 | "food = \"sprinkles\", 10, 4.99" 407 | ], 408 | "language": "python", 409 | "metadata": {}, 410 | "outputs": [] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "collapsed": false, 415 | "input": [ 416 | "food" 417 | ], 418 | "language": "python", 419 | "metadata": {}, 420 | "outputs": [] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": {}, 425 | "source": [ 426 | "Let's see what happens when we try to change our tuple:" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "collapsed": false, 432 | "input": [ 433 | "food[2] = 3" 434 | ], 435 | "language": "python", 436 | "metadata": {}, 437 | "outputs": [] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": {}, 442 | "source": [ 443 | "We can make an empty tuple with an empty set of parenthesis:" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "collapsed": false, 449 | "input": [ 450 | "empty = ()" 451 | ], 452 | "language": "python", 453 | "metadata": {}, 454 | "outputs": [] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "collapsed": false, 459 | "input": [ 460 | "empty" 461 | ], 462 | "language": "python", 463 | "metadata": {}, 464 | "outputs": [] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "collapsed": false, 469 | "input": [ 470 | "type(empty)" 471 | ], 472 | "language": "python", 473 | "metadata": {}, 474 | "outputs": [] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "metadata": {}, 479 | "source": [ 480 | "Any guesses how we can make a single-item tuple?" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "collapsed": false, 486 | "input": [ 487 | "food = (3)" 488 | ], 489 | "language": "python", 490 | "metadata": {}, 491 | "outputs": [] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "collapsed": false, 496 | "input": [ 497 | "food" 498 | ], 499 | "language": "python", 500 | "metadata": {}, 501 | "outputs": [] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "collapsed": false, 506 | "input": [ 507 | "type(food)" 508 | ], 509 | "language": "python", 510 | "metadata": {}, 511 | "outputs": [] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": {}, 516 | "source": [ 517 | "Python just sees this as a number with parenthesis around it. The commas are the important part. We need to add a comma after the number to make a single-item tuple:" 518 | ] 519 | }, 520 | { 521 | "cell_type": "code", 522 | "collapsed": false, 523 | "input": [ 524 | "food = (3,)" 525 | ], 526 | "language": "python", 527 | "metadata": {}, 528 | "outputs": [] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "collapsed": false, 533 | "input": [ 534 | "food" 535 | ], 536 | "language": "python", 537 | "metadata": {}, 538 | "outputs": [] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "collapsed": false, 543 | "input": [ 544 | "type(food)" 545 | ], 546 | "language": "python", 547 | "metadata": {}, 548 | "outputs": [] 549 | }, 550 | { 551 | "cell_type": "markdown", 552 | "metadata": {}, 553 | "source": [ 554 | "Sometimes, unpacking a tuple is also desired." 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "collapsed": false, 560 | "input": [ 561 | "yummies = [(\"cones\", 100, 3.79), (\"sprinkles\", 10, 4.99), (\"hot fudge\", 8, 3.29), (\"nuts\", 6, 8.99)]" 562 | ], 563 | "language": "python", 564 | "metadata": {}, 565 | "outputs": [] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "collapsed": false, 570 | "input": [ 571 | "yummies[2]" 572 | ], 573 | "language": "python", 574 | "metadata": {}, 575 | "outputs": [] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "collapsed": false, 580 | "input": [ 581 | "name, amount, price = yummies[2]" 582 | ], 583 | "language": "python", 584 | "metadata": {}, 585 | "outputs": [] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "collapsed": false, 590 | "input": [ 591 | "print(\"Our ice cream shop serves \" + name + \".\")" 592 | ], 593 | "language": "python", 594 | "metadata": {}, 595 | "outputs": [] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "collapsed": false, 600 | "input": [ 601 | "print(\"The shop's inventory value for \" + name + \" is $\" + str(amount * price) + \".\")" 602 | ], 603 | "language": "python", 604 | "metadata": {}, 605 | "outputs": [] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "collapsed": false, 610 | "input": [ 611 | "print(\"All this food talk is making me hungry for \" + yummies[3][0] + \".\")" 612 | ], 613 | "language": "python", 614 | "metadata": {}, 615 | "outputs": [] 616 | }, 617 | { 618 | "cell_type": "markdown", 619 | "metadata": {}, 620 | "source": [ 621 | "Why not always use lists instead of tuples? Tuples use less storage space than a list. So if you are reading 1000s of small records from a file, a tuple can be an efficient way to go.\n", 622 | "\n", 623 | "A practical learning tip: When learning a new language, there are many new terms and concepts. Sometimes it is helpful to see a bigger picture of how everything comes together. I'm a lover of \"Table of Contents\" since I can scan the big picture and see where something like a tuple fits in. The official Python tutorial has a very good [Table of Contents]( https://docs.python.org/3.4/tutorial/index.html), and I sometimes take 5 minutes to just click and explore something new to me.\n", 624 | "\n", 625 | "Wow! We've come a long way today. " 626 | ] 627 | }, 628 | { 629 | "cell_type": "markdown", 630 | "metadata": {}, 631 | "source": [ 632 | "## List Comprehensions\n", 633 | "\n", 634 | "Now we're going to learn about list comprehensions.\n", 635 | "\n", 636 | "A list comprehension is kind of like a reverse for-loop. It makes it easy to do operations on elements in a list and return a new list." 637 | ] 638 | }, 639 | { 640 | "cell_type": "markdown", 641 | "metadata": {}, 642 | "source": [ 643 | "Say we have a list of numbers:" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "collapsed": false, 649 | "input": [ 650 | "my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]" 651 | ], 652 | "language": "python", 653 | "metadata": {}, 654 | "outputs": [] 655 | }, 656 | { 657 | "cell_type": "markdown", 658 | "metadata": {}, 659 | "source": [ 660 | "Now let's make a new list that contains a square of every number in our list." 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "collapsed": false, 666 | "input": [ 667 | "squared_numbers = []\n", 668 | "for n in my_favorite_numbers:\n", 669 | " squared_numbers.append(n * n)" 670 | ], 671 | "language": "python", 672 | "metadata": {}, 673 | "outputs": [] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "collapsed": false, 678 | "input": [ 679 | "squared_numbers" 680 | ], 681 | "language": "python", 682 | "metadata": {}, 683 | "outputs": [] 684 | }, 685 | { 686 | "cell_type": "markdown", 687 | "metadata": {}, 688 | "source": [ 689 | "**Stop for questions?:** Does this look familiar? Any questions about this?" 690 | ] 691 | }, 692 | { 693 | "cell_type": "markdown", 694 | "metadata": {}, 695 | "source": [ 696 | "Now let's do the same thing by using a list comprehension instead:" 697 | ] 698 | }, 699 | { 700 | "cell_type": "code", 701 | "collapsed": false, 702 | "input": [ 703 | "squared_numbers = [n * n for n in my_favorite_numbers]" 704 | ], 705 | "language": "python", 706 | "metadata": {}, 707 | "outputs": [] 708 | }, 709 | { 710 | "cell_type": "code", 711 | "collapsed": false, 712 | "input": [ 713 | "squared_numbers" 714 | ], 715 | "language": "python", 716 | "metadata": {}, 717 | "outputs": [] 718 | }, 719 | { 720 | "cell_type": "markdown", 721 | "metadata": {}, 722 | "source": [ 723 | "Note how our list comprehension is written within the brackets: The list we're looping over (my_favorite_numbers) is written at the end whereas the action inside of the for-loop is written first: n * n" 724 | ] 725 | }, 726 | { 727 | "cell_type": "markdown", 728 | "metadata": {}, 729 | "source": [ 730 | "First written as a conventional 'for' loop:\n", 731 | "
\n",
732 |       "squared_numbers = []\n",
733 |       "for n in my_favorite_numbers:\n",
734 |       "    squared_numbers.append(n * n)\n",
735 |       "
" 736 | ] 737 | }, 738 | { 739 | "cell_type": "markdown", 740 | "metadata": {}, 741 | "source": [ 742 | "And then written as a list comprehension:\n", 743 | "
\n",
744 |       "squared_numbers = [n * n for n in my_favorite_numbers]\n",
745 |       "
" 746 | ] 747 | }, 748 | { 749 | "cell_type": "markdown", 750 | "metadata": {}, 751 | "source": [ 752 | "Look for the new list being created, the iteration being done over the orignal list and the transformation being done on each element. List comprehensions are just a cleaner way of building lists from other iterables." 753 | ] 754 | }, 755 | { 756 | "cell_type": "markdown", 757 | "metadata": {}, 758 | "source": [ 759 | "### Improving our work\n", 760 | "\n", 761 | "Let's revisit a problem we've already solved in Danny's lecture on lists:\n", 762 | "\n", 763 | "Pick every name from a list that begins with a vowel.\n", 764 | "\n", 765 | "We started with a list of names first:" 766 | ] 767 | }, 768 | { 769 | "cell_type": "code", 770 | "collapsed": false, 771 | "input": [ 772 | "names = [\"Danny\", \"Audrey\", \"Rise\", \"Alain\"]" 773 | ], 774 | "language": "python", 775 | "metadata": {}, 776 | "outputs": [] 777 | }, 778 | { 779 | "cell_type": "markdown", 780 | "metadata": {}, 781 | "source": [ 782 | "Then we filtered names like this:" 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "collapsed": false, 788 | "input": [ 789 | "vowel_names = []\n", 790 | "for name in names:\n", 791 | " if name[0] in \"AEIOU\":\n", 792 | " vowel_names.append(name)" 793 | ], 794 | "language": "python", 795 | "metadata": {}, 796 | "outputs": [] 797 | }, 798 | { 799 | "cell_type": "code", 800 | "collapsed": false, 801 | "input": [ 802 | "vowel_names" 803 | ], 804 | "language": "python", 805 | "metadata": {}, 806 | "outputs": [] 807 | }, 808 | { 809 | "cell_type": "markdown", 810 | "metadata": {}, 811 | "source": [ 812 | "Here's another way to grab all names starting with a vowel, using list comprehensions:" 813 | ] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "collapsed": false, 818 | "input": [ 819 | "vowel_names = [name for name in names if name[0] in \"AEIOU\"]" 820 | ], 821 | "language": "python", 822 | "metadata": {}, 823 | "outputs": [] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "collapsed": false, 828 | "input": [ 829 | "vowel_names" 830 | ], 831 | "language": "python", 832 | "metadata": {}, 833 | "outputs": [] 834 | }, 835 | 836 | { 837 | "cell_type": "code", 838 | "collapsed": false, 839 | "input": [], 840 | "language": "python", 841 | "metadata": {}, 842 | "outputs": [] 843 | } 844 | ], 845 | "metadata": {} 846 | } 847 | ] 848 | } 849 | -------------------------------------------------------------------------------- /part-5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "kernelspec": { 4 | "codemirror_mode": { 5 | "name": "ipython", 6 | "version": 2 7 | }, 8 | "display_name": "IPython (Python 2)", 9 | "language": "python", 10 | "name": "python2" 11 | }, 12 | "name": "", 13 | "signature": "sha256:d3073baa9f8a050a7ae6616386382b6e189019b47ce0a3205bb089be53c07e9c" 14 | }, 15 | "nbformat": 3, 16 | "nbformat_minor": 0, 17 | "worksheets": [ 18 | { 19 | "cells": [ 20 | { 21 | "cell_type": "heading", 22 | "level": 1, 23 | "metadata": {}, 24 | "source": [ 25 | "Using third-party libraries" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "Great programmers don't write all their code themselves. \n", 33 | "\n", 34 | "In fact, they write as little as possible, reusing whatever they can." 35 | ] 36 | }, 37 | { 38 | "cell_type": "heading", 39 | "level": 2, 40 | "metadata": {}, 41 | "source": [ 42 | "Free and Open Source Software" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "There is a lot of FOSS Python code out there. It's a treasure trove for your projects:\n", 50 | "* Free to use, usually even commercially\n", 51 | "* Open source means you can study/modify the code and contribute back\n", 52 | "\n", 53 | "Usually maintained by volunteers, so:\n", 54 | "* When you use it, you're encouraged to contribute back\n", 55 | "* Be extra-friendly and nice to people who aren't getting paid to help you\n", 56 | "* If you use free Python libraries, consider creating free Python libraries too" 57 | ] 58 | }, 59 | { 60 | "cell_type": "heading", 61 | "level": 2, 62 | "metadata": {}, 63 | "source": [ 64 | "Where to Get Free Python Libraries" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "* When you installed Python, it came with a **standard library**\n", 72 | "* The Python Package Index has 50,000 free packages for you\n", 73 | "* GitHub search\n", 74 | "* Bitbucket search" 75 | ] 76 | }, 77 | { 78 | "cell_type": "heading", 79 | "level": 2, 80 | "metadata": {}, 81 | "source": [ 82 | "Python Package Index" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "Memorize this website URL and visit it every day:\n", 90 | "https://pypi.python.org\n", 91 | "\n", 92 | "(screenshot)\n", 93 | "\n", 94 | "Sometimes referred to as \"The Cheese Shop\" because of Monty Python\n", 95 | "\n", 96 | "(photo)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "heading", 101 | "level": 2, 102 | "metadata": {}, 103 | "source": [ 104 | "Module vs. Package vs. Library" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "TODO" 112 | ] 113 | }, 114 | { 115 | "cell_type": "heading", 116 | "level": 2, 117 | "metadata": {}, 118 | "source": [ 119 | "Examples of Python Packages" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "Here, we:\n", 127 | "* Download package X from PyPI\n", 128 | "* Import and use a function from X\n", 129 | "\n", 130 | "TODO\n", 131 | "\n", 132 | "...\n", 133 | "\n", 134 | "...\n", 135 | "\n", 136 | "(10 min using a package)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "What are GitHub, Gitlab, Bitbucket, Gitorious, Sourceforge, etc.?" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "TODO" 151 | ] 152 | }, 153 | { 154 | "cell_type": "heading", 155 | "level": 3, 156 | "metadata": {}, 157 | "source": [ 158 | "Example: Package X" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "(10 min using another package)\n", 166 | "\n", 167 | "* Show its PyPI listing\n", 168 | "* Show its GitHub repo\n", 169 | "* Click through and show its files and docs" 170 | ] 171 | }, 172 | { 173 | "cell_type": "heading", 174 | "level": 3, 175 | "metadata": {}, 176 | "source": [ 177 | "What is Version Control?" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": {}, 183 | "source": [ 184 | "TODO" 185 | ] 186 | }, 187 | { 188 | "cell_type": "heading", 189 | "level": 3, 190 | "metadata": {}, 191 | "source": [ 192 | "DVCS and Collaboration" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "TODO" 200 | ] 201 | }, 202 | { 203 | "cell_type": "heading", 204 | "level": 2, 205 | "metadata": {}, 206 | "source": [ 207 | "What's in a Python Package?" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "TODO" 215 | ] 216 | }, 217 | { 218 | "cell_type": "heading", 219 | "level": 2, 220 | "metadata": {}, 221 | "source": [ 222 | "Using Third-Party Libraries in Projects" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "Keeping track of requirements\n", 230 | "\n", 231 | "Avoiding dependency conflicts" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [] 238 | } 239 | ], 240 | "metadata": {} 241 | } 242 | ] 243 | } -------------------------------------------------------------------------------- /part3/ex01/birthday.py: -------------------------------------------------------------------------------- 1 | # Script that prints greeting directly 2 | 3 | print("Happy Birthday, dear Carol!") 4 | -------------------------------------------------------------------------------- /part3/ex02/birthday.py: -------------------------------------------------------------------------------- 1 | # Script that prints greeting by calling function 2 | 3 | def happy_birthday_carol(): 4 | print("Happy Birthday, dear Carol!") 5 | 6 | happy_birthday_carol() 7 | -------------------------------------------------------------------------------- /part3/ex03/birthday.py: -------------------------------------------------------------------------------- 1 | # Script that prints 2 greetings via 2 function calls 2 | 3 | def happy_birthday_carol(): 4 | print("Happy Birthday, dear Carol!") 5 | 6 | def happy_birthday_rise(): 7 | print("Happy Birthday, dear Rise!") 8 | 9 | happy_birthday_carol() 10 | happy_birthday_rise() 11 | -------------------------------------------------------------------------------- /part3/ex04/birthday.py: -------------------------------------------------------------------------------- 1 | def happy_birthday(name): 2 | print("Happy Birthday, dear " + name + "!") 3 | 4 | happy_birthday("Trey") 5 | -------------------------------------------------------------------------------- /part3/ex05-exercise/answer01a.py: -------------------------------------------------------------------------------- 1 | # Script that wishes happy birthday to Wolfe+585, Senior 2 | # http://en.wikipedia.org/wiki/Wolfe+585,_Senior 3 | 4 | def happy_birthday(name): 5 | print("Happy Birthday, dear " + name + "!") 6 | 7 | # This guy is a real-life test case for name fields 8 | 9 | happy_birthday("Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvin John Kenneth Lloyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor William Xerxes Yancy Zeus Wolfe­schlegelstein­hausenberger­dorffvoraltern­waren­gewissenhaft­schaferswessen­schafewaren­wohlgepflege­und­sorgfaltigkeit­beschutzen­von­angreifen­durch­ihrraubgierigfeinde­welche­voraltern­zwolftausend­jahres­vorandieerscheinen­wander­ersteer­dem­enschderraumschiff­gebrauchlicht­als­sein­ursprung­von­kraftgestart­sein­lange­fahrt­hinzwischen­sternartigraum­auf­der­suchenach­diestern­welche­gehabt­bewohnbar­planeten­kreise­drehen­sich­und­wohin­der­neurasse­von­verstandigmen­schlichkeit­konnte­fortplanzen­und­sicher­freuen­anlebens­langlich­freude­und­ruhe­mit­nicht­ein­furcht­vor­angreifen­von­anderer­intelligent­geschopfs­von­hinzwischen­sternartigraum, Senior") 10 | -------------------------------------------------------------------------------- /part3/ex05-exercise/answer01b.py: -------------------------------------------------------------------------------- 1 | # Script that wishes happy birthday to the Gettysburg Address 2 | 3 | def happy_birthday(name): 4 | print("Happy Birthday, dear " + name + "!") 5 | 6 | happy_birthday("""Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. 7 | 8 | Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. 9 | 10 | But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.""") 11 | 12 | -------------------------------------------------------------------------------- /part3/ex05-exercise/answer01c.py: -------------------------------------------------------------------------------- 1 | # Script that wishes happy birthday to people who have number names 2 | 3 | def happy_birthday(name): 4 | print("Happy Birthday, dear " + name + "!") 5 | 6 | # Try one of these 7 | 8 | happy_birthday(100) 9 | happy_birthday(3.1415927) 10 | -------------------------------------------------------------------------------- /part3/ex05-exercise/answer02.py: -------------------------------------------------------------------------------- 1 | # Script that prints greeting 4 times via refactored function 2 | 3 | def happy_birthday(name): 4 | print("Happy Birthday, dear " + name + "!") 5 | 6 | names = [5, "Carol", "Rise", "Trey", "Alain"] 7 | for name in names: 8 | happy_birthday(name) 9 | -------------------------------------------------------------------------------- /part3/ex05/birthday.py: -------------------------------------------------------------------------------- 1 | # Script that prints greeting 4 times via refactored function 2 | 3 | def happy_birthday(name): 4 | print("Happy Birthday, dear " + name + "!") 5 | 6 | names = ["Carol", "Rise", "Trey", "Alain"] 7 | for name in names: 8 | happy_birthday(name) -------------------------------------------------------------------------------- /part3/ex06/hi.py: -------------------------------------------------------------------------------- 1 | def hi(first, last): 2 | print("Hi {} {}".format(first, last)) 3 | 4 | hi("Henry", 8) -------------------------------------------------------------------------------- /part3/ex07/hi.py: -------------------------------------------------------------------------------- 1 | def hi(first, last): 2 | print("Hi {} {}".format(first, last)) 3 | 4 | hi("Henry", 8) 5 | -------------------------------------------------------------------------------- /part3/ex08/factors.py: -------------------------------------------------------------------------------- 1 | def print_factors(age): 2 | for i in range(1, age + 1): 3 | if age % i == 0: 4 | print(i) 5 | 6 | print_factors(32) -------------------------------------------------------------------------------- /part3/ex09/cupcakes.py: -------------------------------------------------------------------------------- 1 | def cupcake_tally(guests): 2 | """ Given number of party guests, 3 | returns how many cupcakes are needed. """ 4 | return 2 * guests + 13 5 | 6 | cupcakes = cupcake_tally(30) + cupcake_tally(1) 7 | print("You need {} cupcakes".format(cupcakes)) -------------------------------------------------------------------------------- /part3/ex10/cupcakes.py: -------------------------------------------------------------------------------- 1 | def cupcake_tally(guests): 2 | """ Given number of party guests, 3 | returns how many cupcakes are needed. """ 4 | return 2 * guests + 13 5 | -------------------------------------------------------------------------------- /part3/ex10/main.py: -------------------------------------------------------------------------------- 1 | from cupcakes import cupcake_tally 2 | 3 | tally = cupcake_tally(30) + cupcake_tally(1) 4 | print("You need {} cupcakes".format(cupcakes)) -------------------------------------------------------------------------------- /slides/20-things-part-a.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/20-things-part-a.key -------------------------------------------------------------------------------- /slides/20-things-part-a.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/20-things-part-a.pdf -------------------------------------------------------------------------------- /slides/20-things-part-b.key: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /slides/20-things-part-b.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/20-things-part-b.pdf -------------------------------------------------------------------------------- /slides/conclusion.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/conclusion.key -------------------------------------------------------------------------------- /slides/conclusion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/conclusion.pdf -------------------------------------------------------------------------------- /slides/part-3.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/part-3.key -------------------------------------------------------------------------------- /slides/part-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/part-3.pdf -------------------------------------------------------------------------------- /watermark.txt: -------------------------------------------------------------------------------- 1 | Watermark is a project by Sebastian Rasbt (GitHub @rasbt). 2 | It's a nifty "IPython magic extension for printing date and time stamps, version numbers, and hardware information". 3 | 4 | To check if our workshop's IPython notebook is running under 3.4.1, the following 3 line snippet can be pasted into a cell: 5 | %install_ext https://raw.githubusercontent.com/rasbt/watermark/master/watermark.py 6 | %load_ext watermark 7 | %watermark 8 | --------------------------------------------------------------------------------