├── .deepsource.toml ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── LICENSE.md ├── Python_cheatsheet.pdf ├── README.md ├── binder ├── postBuild ├── requirements.txt └── test_notebooks.py ├── data ├── baseball.csv ├── brokerage-names.tsv ├── phonetest.txt ├── restaurant-names.txt └── restaurants.txt └── notes ├── A-Introduction_to_iPython_Notebooks.ipynb ├── B-Numeric_Expressions.ipynb ├── C1-Variables.ipynb ├── C2-Variables_Exercises.ipynb ├── C3-DataTypes.ipynb ├── D1-Strings_Basic_Operations.ipynb ├── D2-Strings_Indexing_and_Slicing.ipynb ├── D3-String_Comparisons.ipynb ├── D4-Special_Characters_Splitting_Strings.ipynb ├── D5-String_Formatting.ipynb ├── E-Boolean_Variables_and_If_Then_Else_Statements.ipynb ├── F-Lists.ipynb ├── G-Sets.ipynb ├── H-Dictionaries.ipynb ├── I-Nested_Structures.ipynb ├── J1-Iteration-While_loops.ipynb ├── J2-Iteration-For_loops.ipynb ├── J3-Iteration-Common_loop_patterns.ipynb ├── J4-List_Comprehensions.ipynb ├── J5-List_comprehension_extra_practice.ipynb ├── K-Functions_shortened.ipynb ├── K1-Functions-Builtin_and_Library_Functions.ipynb ├── K2-Functions-User_Defined_Functions.ipynb ├── K3-Functions-User_Defined_Functions-Advanced.ipynb ├── L-Files.ipynb ├── L2-Folders.ipynb ├── M-In_Class_Exercise.ipynb ├── W-Standalone_Programs.ipynb ├── Y-Practice_Exercises.ipynb ├── Y2-Exam_Review.ipynb └── Z-Libraries_and_Matplotlib_Example.ipynb /.deepsource.toml: -------------------------------------------------------------------------------- 1 | version = 1 2 | 3 | [[analyzers]] 4 | name = "python" 5 | enabled = true 6 | 7 | [analyzers.meta] 8 | runtime_version = "3.x.x" 9 | 10 | [[analyzers]] 11 | name = "test-coverage" 12 | enabled = true 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.ipynb filter=nbstripout 2 | 3 | *.ipynb diff=ipynb 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | # This is a basic workflow to help you get started with Actions 2 | 3 | name: Check that all notebooks work 4 | 5 | # Controls when the action will run. Triggers the workflow on push or pull request 6 | # events but only for the master branch 7 | on: 8 | push: 9 | branches: [ master ] 10 | pull_request: 11 | branches: [ master ] 12 | 13 | # A workflow run is made up of one or more jobs that can run sequentially or in parallel 14 | jobs: 15 | # This workflow contains a single job called "build" 16 | build: 17 | # The type of runner that the job will run on 18 | runs-on: ubuntu-latest 19 | strategy: 20 | matrix: 21 | python-version: [3.8] 22 | 23 | # Steps represent a sequence of tasks that will be executed as part of the job 24 | steps: 25 | # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it 26 | - uses: actions/checkout@v2 27 | 28 | - name: Set up Python ${{ matrix.python-version }} 29 | uses: actions/setup-python@v1 30 | with: 31 | python-version: ${{ matrix.python-version }} 32 | 33 | - name: Install dependencies and Flake8/Flake8-nb for linting 34 | run: | 35 | python3 -m pip install --upgrade pip 36 | pip3 install flake8 flake8-nb 37 | pip3 install -r binder/requirements.txt 38 | 39 | - name: Lint with flake8 40 | run: | 41 | # stop the build if there are Python syntax errors or undefined names 42 | flake8-nb notes/*.ipynb --count --select=E9,F63,F7,F82 --show-source --statistics 43 | # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide 44 | flake8-nb notes/*.ipynb --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics 45 | 46 | - name: Run all the notebooks using nbconvert 47 | run: | 48 | python3 binder/test_notebooks.py notes 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | -------------------------------------------------------------------------------- /Python_cheatsheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ipeirotis/introduction-to-python/928b1b5a5526005d5c212563afc57502bb2e4fe3/Python_cheatsheet.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [](https://colab.research.google.com/github/ipeirotis/introduction-to-python/blob/master/) 2 | [](https://mybinder.org/v2/gh/ipeirotis/introduction-to-python/master) 3 | [](https://github.com/ipeirotis/introduction-to-python/actions?query=branch%3Amaster) 4 | 5 | # Introduction to Python for Data Science 6 | 7 | This is a set of notes used for teaching Python to students that have never used Python, or programmed in any language. In a usual semester, it takes approximately 4 weeks (meeting twice a week for an hour) to go through the material, for a freshmen undergraduate class. 8 | 9 | ## Notes 10 | 11 | * The notes are in the form of iPython notebooks and are stored under the `/notes` folder. 12 | * You can [open the notes in Google Colab](https://colab.research.google.com/github/ipeirotis/introduction-to-python/blob/master/). With Google Colab, you can save your work in your Google Drive. 13 | * If you do not want to use Google Colab, you can [launch the notes in Binder](https://mybinder.org/v2/gh/ipeirotis/introduction-to-python/master), which is a temporary Jupyter server launched on-demand. Note that the Binder server will shutdown after a period of idleness. If you want to save your work, and you should save the notes locally to your computer. 14 | 15 | 16 | ## Videos 17 | 18 | * [Videos for the class](https://www.youtube.com/playlist?list=PLqAPn_b_yx0TBDqe5-AMSed6sYzMj9qkN) 19 | 20 | ## Recommended Books 21 | 22 | * [Python for Everybody: Exploring Data In Python 3](https://www.py4e.com/book): This is a textbook for students that are learning Python as their first programming language, with the objective of using programming to handle and analyze data. 23 | * [Automate the Boring Stuff using Python](https://automatetheboringstuff.com): A task-driven textbook that teaches Python by focusing on how to automate various tasks, using programming. 24 | 25 | 26 | ## Additional Books for Learning Python 27 | 28 | * [How To Think Like a Computer Scientist](https://runestone.academy/ns/books/published/thinkcspy/index.html): An interactive guide to programming and Python. The book "Python for Everybody" (listed above) is partially based on this book. 29 | * [Learn Python the Hard Way](https://learnpythonthehardway.org/python3/): An introduction to programming and Python. It targets complete beginners. It uses a drill-based approach for teaching, which can be tedious at times. Nevertheless, it is considered one of the standard textbooks for learning Python. 30 | 31 | ## Online Classes 32 | 33 | * [AI Python for Beginners](https://www.deeplearning.ai/short-courses/ai-python-for-beginners/) 34 | * The following Coursera courses [Getting Started with Python](https://www.coursera.org/learn/python), [Python Data Structures](https://www.coursera.org/learn/python-data), [Using Python to Access Web Data](https://www.coursera.org/learn/python-network-data), [Using Databases with Python](https://www.coursera.org/learn/python-databases), [Capstone: Retrieving, Processing, and Visualizing Data with Python](https://www.coursera.org/learn/python-capstone) are well-alinged with the objectives of our class. 35 | * [Code Academy, Python class](https://www.codecademy.com/learn/python): This is a useful interactive tutorial for beginners, who are trying to understand programming in general, and Python in particular 36 | * [Google’s Python class](https://developers.google.com/edu/python/) 37 | * [DataCamp, Intro to Python for Data Science](https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-1-python-basics?ex=1) 38 | * [DataQuest, Python Basics](https://www.dataquest.io/mission/1/python-basics) 39 | 40 | ## Additional Pointers 41 | 42 | * [Official Python 3 Tutorial](https://docs.python.org/3/tutorial/index.html) 43 | * [Python Tutor](http://www.pythontutor.com/) 44 | * [Useful iPython Notebooks](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks): A wide variety of useful tutorials in iPython Notebooks for a wide variety of topics 45 | * [Python for Econometrics](https://www.kevinsheppard.com/Python_for_Econometrics) 46 | * [Quantitative Economics](https://python.quantecon.org/intro.html): An introduction to scientific computing using Python, by Thomas J. Sargent and John Stachurski 47 | * [Pytudes](https://github.com/norvig/pytudes) by Peter Norvig. A set of problems, in a wide variety of fields, solved with Python. Clear and structured problem descriptions, and _beautiful_ code for solving them. You will learn something everytime you read one of the provided notebooks. 48 | 49 | ## Python Exercises 50 | 51 | * http://www.pyschools.com/ [highly recommended] 52 | * http://www.singpath.com/#/paths 53 | * http://learnpython.org/ 54 | * http://www.practicepython.org/ 55 | * http://www.codeabbey.com/index/task_list 56 | * http://codingbat.com/python 57 | * http://usingpython.com/python-programming-challenges/ 58 | * http://www.openbookproject.net/pybiblio/practice/elkner/ 59 | * http://www.openbookproject.net/pybiblio/practice/wilson/ 60 | * https://github.com/donnemartin/interactive-coding-challenges 61 | 62 | ## Credits 63 | 64 | * I ~~have stolen~~ relied heavily on the "Python for Everybody" and the "How To Think Like a Computer Scientist" books to develop the structure and the material for the notes. 65 | * The initial version of the notebooks came from Josh Attenberg, from his course "Practical Data Science" that was taught at NYU/Stern. 66 | * Katherine Hoffmann contributed to the development of the current notebooks. 67 | 68 | ## License 69 | 70 | * Outside NYU, the material is licensed under the Creative Commons Attribution-ShareAlike 4.0 license. If you are working within NYU, note that any usage of the material is strictly prohibited. 71 | -------------------------------------------------------------------------------- /binder/postBuild: -------------------------------------------------------------------------------- 1 | jupyter contrib nbextension install --user 2 | 3 | # Allow collapsible headers 4 | jupyter nbextension enable collapsible_headings/main 5 | 6 | # Add a code formatter - requires yapf package 7 | jupyter nbextension enable code_prettify/code_prettify 8 | 9 | # Adds the "Solution" hiding option 10 | jupyter nbextension enable exercise2/main 11 | 12 | # Spellchecking 13 | jupyter nbextension enable spellchecker/main 14 | 15 | # Install nbstripout on git and clean the notebooks 16 | nbstripout --install --attributes .gitattributes 17 | nbstripout notes/*.ipynb 18 | -------------------------------------------------------------------------------- /binder/requirements.txt: -------------------------------------------------------------------------------- 1 | # For enabling the Solution extension 2 | jupyter_contrib_nbextensions 3 | 4 | # We use these packages for the in-class exercise 5 | jellyfish 6 | ngram 7 | 8 | # Used in notebook Z 9 | numpy 10 | matplotlib 11 | 12 | # Used for code-prettify extension 13 | yapf 14 | black 15 | 16 | # To remove the output from the notebooks 17 | nbstripout 18 | mistune>=2.0.3 # not directly required, pinned by Snyk to avoid a vulnerability 19 | -------------------------------------------------------------------------------- /binder/test_notebooks.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import sys 4 | import subprocess 5 | import nbformat 6 | 7 | from os import listdir 8 | from os.path import isfile, join 9 | 10 | 11 | def test_notebooks(path): 12 | 13 | notebooks = [ 14 | f for f in listdir(path) if isfile(join(path, f)) and f.endswith(".ipynb") 15 | ] 16 | 17 | for notebook in sorted(notebooks): 18 | print(notebook) 19 | args = [ 20 | "jupyter", 21 | "nbconvert", 22 | "--to", 23 | "notebook", 24 | "--execute", 25 | "--ExecutePreprocessor.timeout=600", 26 | join(path, notebook), 27 | ] 28 | exit_code = subprocess.check_call(args) 29 | if exit_code != 0: 30 | raise AssertionError 31 | 32 | 33 | def main(): 34 | directory = sys.argv[1] 35 | test_notebooks(directory) 36 | 37 | 38 | if __name__ == "__main__": 39 | main() 40 | -------------------------------------------------------------------------------- /data/baseball.csv: -------------------------------------------------------------------------------- 1 | id,player,year,stint,team,lg,g,ab,r,h,X2b,X3b,hr,rbi,sb,cs,bb,so,ibb,hbp,sh,sf,gidp 2 | 88641,womacto01,2006,2,CHN,NL,19,50,6,14,1,0,1,2.0,1.0,1.0,4,4.0,0.0,0.0,3.0,0.0,0.0 3 | 88643,schilcu01,2006,1,BOS,AL,31,2,0,1,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 4 | 88645,myersmi01,2006,1,NYA,AL,62,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 5 | 88649,helliri01,2006,1,MIL,NL,20,3,0,0,0,0,0,0.0,0.0,0.0,0,2.0,0.0,0.0,0.0,0.0,0.0 6 | 88650,johnsra05,2006,1,NYA,AL,33,6,0,1,0,0,0,0.0,0.0,0.0,0,4.0,0.0,0.0,0.0,0.0,0.0 7 | 88652,finlest01,2006,1,SFN,NL,139,426,66,105,21,12,6,40.0,7.0,0.0,46,55.0,2.0,2.0,3.0,4.0,6.0 8 | 88653,gonzalu01,2006,1,ARI,NL,153,586,93,159,52,2,15,73.0,0.0,1.0,69,58.0,10.0,7.0,0.0,6.0,14.0 9 | 88662,seleaa01,2006,1,LAN,NL,28,26,2,5,1,0,0,0.0,0.0,0.0,1,7.0,0.0,0.0,6.0,0.0,1.0 10 | 89177,francju01,2007,2,ATL,NL,15,40,1,10,3,0,0,8.0,0.0,0.0,4,10.0,1.0,0.0,0.0,1.0,1.0 11 | 89178,francju01,2007,1,NYN,NL,40,50,7,10,0,0,1,8.0,2.0,1.0,10,13.0,0.0,0.0,0.0,1.0,1.0 12 | 89330,zaungr01,2007,1,TOR,AL,110,331,43,80,24,1,10,52.0,0.0,0.0,51,55.0,8.0,2.0,1.0,6.0,9.0 13 | 89333,witasja01,2007,1,TBA,AL,3,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 14 | 89334,williwo02,2007,1,HOU,NL,33,59,3,6,0,0,1,2.0,0.0,0.0,0,25.0,0.0,0.0,5.0,0.0,1.0 15 | 89335,wickmbo01,2007,2,ARI,NL,8,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 16 | 89336,wickmbo01,2007,1,ATL,NL,47,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 17 | 89337,whitero02,2007,1,MIN,AL,38,109,8,19,4,0,4,20.0,0.0,0.0,6,19.0,0.0,3.0,0.0,1.0,2.0 18 | 89338,whiteri01,2007,1,HOU,NL,20,1,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 19 | 89339,wellsda01,2007,2,LAN,NL,7,15,2,4,1,0,0,1.0,0.0,0.0,0,6.0,0.0,0.0,0.0,0.0,0.0 20 | 89340,wellsda01,2007,1,SDN,NL,22,38,1,4,0,0,0,0.0,0.0,0.0,0,12.0,0.0,0.0,4.0,0.0,0.0 21 | 89341,weathda01,2007,1,CIN,NL,67,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 22 | 89343,walketo04,2007,1,OAK,AL,18,48,5,13,1,0,0,4.0,0.0,0.0,2,4.0,0.0,0.0,0.0,2.0,2.0 23 | 89345,wakefti01,2007,1,BOS,AL,1,2,0,0,0,0,0,0.0,0.0,0.0,0,2.0,0.0,0.0,0.0,0.0,0.0 24 | 89347,vizquom01,2007,1,SFN,NL,145,513,54,126,18,3,4,51.0,14.0,6.0,44,48.0,6.0,1.0,14.0,3.0,14.0 25 | 89348,villoro01,2007,1,NYA,AL,6,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 26 | 89352,valenjo03,2007,1,NYN,NL,51,166,18,40,11,1,3,18.0,2.0,1.0,15,28.0,4.0,0.0,1.0,1.0,5.0 27 | 89354,trachst01,2007,2,CHN,NL,4,7,0,1,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 28 | 89355,trachst01,2007,1,BAL,AL,3,5,0,0,0,0,0,0.0,0.0,0.0,0,3.0,0.0,0.0,0.0,0.0,0.0 29 | 89359,timlimi01,2007,1,BOS,AL,4,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 30 | 89360,thomeji01,2007,1,CHA,AL,130,432,79,119,19,0,35,96.0,0.0,1.0,95,134.0,11.0,6.0,0.0,3.0,10.0 31 | 89361,thomafr04,2007,1,TOR,AL,155,531,63,147,30,0,26,95.0,0.0,0.0,81,94.0,3.0,7.0,0.0,5.0,14.0 32 | 89363,tavarju01,2007,1,BOS,AL,2,4,0,1,0,0,0,0.0,0.0,0.0,1,3.0,0.0,0.0,0.0,0.0,0.0 33 | 89365,sweenma01,2007,2,LAN,NL,30,33,2,9,1,0,0,3.0,0.0,0.0,1,11.0,0.0,0.0,0.0,0.0,0.0 34 | 89366,sweenma01,2007,1,SFN,NL,76,90,18,23,8,0,2,10.0,2.0,0.0,13,18.0,0.0,3.0,1.0,0.0,0.0 35 | 89367,suppaje01,2007,1,MIL,NL,33,61,4,8,0,0,0,2.0,0.0,0.0,3,16.0,0.0,0.0,11.0,0.0,2.0 36 | 89368,stinnke01,2007,1,SLN,NL,26,82,7,13,3,0,1,5.0,0.0,0.0,5,22.0,2.0,0.0,0.0,0.0,2.0 37 | 89370,stantmi02,2007,1,CIN,NL,67,2,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 38 | 89371,stairma01,2007,1,TOR,AL,125,357,58,103,28,1,21,64.0,2.0,1.0,44,66.0,5.0,2.0,0.0,2.0,7.0 39 | 89372,sprinru01,2007,1,SLN,NL,72,1,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 40 | 89374,sosasa01,2007,1,TEX,AL,114,412,53,104,24,1,21,92.0,0.0,0.0,34,112.0,3.0,3.0,0.0,5.0,11.0 41 | 89375,smoltjo01,2007,1,ATL,NL,30,54,1,5,1,0,0,2.0,0.0,0.0,1,19.0,0.0,0.0,13.0,0.0,0.0 42 | 89378,sheffga01,2007,1,DET,AL,133,494,107,131,20,1,25,75.0,22.0,5.0,84,71.0,2.0,9.0,0.0,6.0,10.0 43 | 89381,seleaa01,2007,1,NYN,NL,31,4,0,0,0,0,0,0.0,0.0,0.0,1,1.0,0.0,0.0,1.0,0.0,0.0 44 | 89382,seaneru01,2007,1,LAN,NL,68,1,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 45 | 89383,schmija01,2007,1,LAN,NL,6,7,1,1,0,0,1,1.0,0.0,0.0,0,4.0,0.0,0.0,1.0,0.0,0.0 46 | 89384,schilcu01,2007,1,BOS,AL,1,2,0,1,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 47 | 89385,sandere02,2007,1,KCA,AL,24,73,12,23,7,0,2,11.0,0.0,1.0,11,15.0,0.0,1.0,0.0,0.0,2.0 48 | 89388,rogerke01,2007,1,DET,AL,1,2,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 49 | 89389,rodriiv01,2007,1,DET,AL,129,502,50,141,31,3,11,63.0,2.0,2.0,9,96.0,1.0,1.0,1.0,2.0,16.0 50 | 89396,ramirma02,2007,1,BOS,AL,133,483,84,143,33,1,20,88.0,0.0,0.0,71,92.0,13.0,7.0,0.0,8.0,21.0 51 | 89398,piazzmi01,2007,1,OAK,AL,83,309,33,85,17,1,8,44.0,0.0,0.0,18,61.0,0.0,0.0,0.0,2.0,9.0 52 | 89400,perezne01,2007,1,DET,AL,33,64,5,11,3,0,1,6.0,0.0,0.0,4,8.0,0.0,0.0,3.0,0.0,2.0 53 | 89402,parkch01,2007,1,NYN,NL,1,1,0,0,0,0,0,0.0,0.0,0.0,0,1.0,0.0,0.0,0.0,0.0,0.0 54 | 89406,oliveda02,2007,1,LAA,AL,5,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 55 | 89410,myersmi01,2007,1,NYA,AL,6,1,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 56 | 89411,mussimi01,2007,1,NYA,AL,2,2,0,0,0,0,0,0.0,0.0,0.0,1,0.0,0.0,0.0,0.0,0.0,0.0 57 | 89412,moyerja01,2007,1,PHI,NL,33,73,4,9,2,0,0,2.0,0.0,0.0,2,26.0,0.0,0.0,8.0,0.0,1.0 58 | 89420,mesajo01,2007,1,PHI,NL,38,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 59 | 89421,martipe02,2007,1,NYN,NL,5,9,1,1,1,0,0,0.0,0.0,0.0,0,6.0,0.0,0.0,2.0,0.0,0.0 60 | 89425,maddugr01,2007,1,SDN,NL,33,62,2,9,2,0,0,0.0,1.0,0.0,1,19.0,0.0,0.0,9.0,0.0,2.0 61 | 89426,mabryjo01,2007,1,COL,NL,28,34,4,4,1,0,1,5.0,0.0,0.0,5,10.0,0.0,0.0,0.0,0.0,1.0 62 | 89429,loftoke01,2007,2,CLE,AL,52,173,24,49,9,3,0,15.0,2.0,3.0,17,23.0,0.0,0.0,4.0,2.0,1.0 63 | 89430,loftoke01,2007,1,TEX,AL,84,317,62,96,16,3,7,23.0,21.0,4.0,39,28.0,1.0,2.0,2.0,3.0,5.0 64 | 89431,loaizes01,2007,1,LAN,NL,5,7,0,1,0,0,0,2.0,0.0,0.0,0,2.0,0.0,0.0,2.0,0.0,1.0 65 | 89438,kleskry01,2007,1,SFN,NL,116,362,51,94,27,3,6,44.0,5.0,1.0,46,68.0,2.0,1.0,1.0,1.0,14.0 66 | 89439,kentje01,2007,1,LAN,NL,136,494,78,149,36,1,20,79.0,1.0,3.0,57,61.0,4.0,5.0,0.0,6.0,17.0 67 | 89442,jonesto02,2007,1,DET,AL,5,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 68 | 89445,johnsra05,2007,1,ARI,NL,10,15,0,1,0,0,0,0.0,0.0,0.0,1,7.0,0.0,0.0,2.0,0.0,0.0 69 | 89450,hoffmtr01,2007,1,SDN,NL,60,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 70 | 89451,hernaro01,2007,2,LAN,NL,22,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 71 | 89452,hernaro01,2007,1,CLE,AL,2,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 72 | 89460,guarded01,2007,1,CIN,NL,15,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 73 | 89462,griffke02,2007,1,CIN,NL,144,528,78,146,24,1,30,93.0,6.0,1.0,85,99.0,14.0,1.0,0.0,9.0,14.0 74 | 89463,greensh01,2007,1,NYN,NL,130,446,62,130,30,1,10,46.0,11.0,1.0,37,62.0,4.0,5.0,1.0,1.0,14.0 75 | 89464,graffto01,2007,1,MIL,NL,86,231,34,55,8,0,9,30.0,0.0,1.0,24,44.0,6.0,3.0,0.0,2.0,7.0 76 | 89465,gordoto01,2007,1,PHI,NL,44,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 77 | 89466,gonzalu01,2007,1,LAN,NL,139,464,70,129,23,2,15,68.0,6.0,2.0,56,56.0,4.0,4.0,0.0,2.0,11.0 78 | 89467,gomezch02,2007,2,CLE,AL,19,53,4,15,2,0,0,5.0,0.0,0.0,0,6.0,0.0,0.0,1.0,1.0,1.0 79 | 89468,gomezch02,2007,1,BAL,AL,73,169,17,51,10,1,1,16.0,1.0,2.0,10,20.0,1.0,0.0,5.0,1.0,5.0 80 | 89469,glavito02,2007,1,NYN,NL,33,56,3,12,1,0,0,4.0,0.0,0.0,6,5.0,0.0,0.0,12.0,1.0,0.0 81 | 89473,floydcl01,2007,1,CHN,NL,108,282,40,80,10,1,9,45.0,0.0,0.0,35,47.0,5.0,5.0,0.0,0.0,6.0 82 | 89474,finlest01,2007,1,COL,NL,43,94,9,17,3,0,1,2.0,0.0,0.0,8,4.0,1.0,0.0,0.0,0.0,2.0 83 | 89480,embreal01,2007,1,OAK,AL,4,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 84 | 89481,edmonji01,2007,1,SLN,NL,117,365,39,92,15,2,12,53.0,0.0,2.0,41,75.0,2.0,0.0,2.0,3.0,9.0 85 | 89482,easleda01,2007,1,NYN,NL,76,193,24,54,6,0,10,26.0,0.0,1.0,19,35.0,1.0,5.0,0.0,1.0,2.0 86 | 89489,delgaca01,2007,1,NYN,NL,139,538,71,139,30,0,24,87.0,4.0,0.0,52,118.0,8.0,11.0,0.0,6.0,12.0 87 | 89493,cormirh01,2007,1,CIN,NL,6,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 88 | 89494,coninje01,2007,2,NYN,NL,21,41,2,8,2,0,0,5.0,0.0,0.0,7,8.0,2.0,0.0,1.0,1.0,1.0 89 | 89495,coninje01,2007,1,CIN,NL,80,215,23,57,11,1,6,32.0,4.0,0.0,20,28.0,0.0,0.0,1.0,6.0,4.0 90 | 89497,clemero02,2007,1,NYA,AL,2,2,0,1,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 91 | 89498,claytro01,2007,2,BOS,AL,8,6,1,0,0,0,0,0.0,0.0,0.0,0,3.0,0.0,0.0,0.0,0.0,2.0 92 | 89499,claytro01,2007,1,TOR,AL,69,189,23,48,14,0,1,12.0,2.0,1.0,14,50.0,0.0,1.0,3.0,3.0,8.0 93 | 89501,cirilje01,2007,2,ARI,NL,28,40,6,8,4,0,0,6.0,0.0,0.0,4,6.0,0.0,0.0,0.0,0.0,1.0 94 | 89502,cirilje01,2007,1,MIN,AL,50,153,18,40,9,2,2,21.0,2.0,0.0,15,13.0,0.0,1.0,3.0,2.0,9.0 95 | 89521,bondsba01,2007,1,SFN,NL,126,340,75,94,14,0,28,66.0,5.0,0.0,132,54.0,43.0,3.0,0.0,2.0,13.0 96 | 89523,biggicr01,2007,1,HOU,NL,141,517,68,130,31,3,10,50.0,4.0,3.0,23,112.0,0.0,3.0,7.0,5.0,5.0 97 | 89525,benitar01,2007,2,FLO,NL,34,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 98 | 89526,benitar01,2007,1,SFN,NL,19,0,0,0,0,0,0,0.0,0.0,0.0,0,0.0,0.0,0.0,0.0,0.0,0.0 99 | 89530,ausmubr01,2007,1,HOU,NL,117,349,38,82,16,3,3,25.0,6.0,1.0,37,74.0,3.0,6.0,4.0,1.0,11.0 100 | 89533,aloumo01,2007,1,NYN,NL,87,328,51,112,19,1,13,49.0,3.0,0.0,27,30.0,5.0,2.0,0.0,3.0,13.0 101 | 89534,alomasa02,2007,1,NYN,NL,8,22,1,3,1,0,0,0.0,0.0,0.0,0,3.0,0.0,0.0,0.0,0.0,0.0 -------------------------------------------------------------------------------- /data/phonetest.txt: -------------------------------------------------------------------------------- 1 | 679-397-5255 2 | 2126660921 3 | 212-998-0902 4 | 888-888-2222 5 | 800-555-1211 6 | 800 555 1212 7 | 800.555.1213 8 | (800) 555-1214 9 | 1-800-555-1215 10 | 1(800)555-1216 11 | 800-555-1212-1234 12 | 800-555-1212x1234 13 | 800-555-1212 ext. 1234 14 | work 1-(800) 555.1212 #1234 -------------------------------------------------------------------------------- /notes/A-Introduction_to_iPython_Notebooks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python Primer: The Basics\n", 8 | "\n", 9 | "This is a notebook file. It is an interactive document that you can edit on the fly, and you can use it to write and execute programs. You can learn more about the interface by reading the [online documentation](http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Notebook%20Basics.html), although it should be pretty simple to understand what is going on without any documentation. \n", 10 | "\n", 11 | "You can also click \"Help : User Interface tour\" from the menu, which will explain the basics of the notebook." 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "## Our first Python program\n", 19 | "\n", 20 | "Traditionally, every time that we start learning a new language, we start with a program that prints \"Hello World\" in the output." 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "print(\"Hello World\")" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "To run the \"Hello World\" program above, just select the code cell, and then click the \"Run\" button from the toolbar. Alternatively, you can also press Ctrl + Enter, if you prefer to use keyboard shortcuts. You can discover more keyboard shortcuts by selecting \"Help : Keyboard Shortcuts\" from the menu." 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "### Exercise\n", 44 | "\n", 45 | "Print your own message:" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "# your code here" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "## Comments\n", 62 | "\n", 63 | "You will have noticed in the cell above the line \n", 64 | "`# your code here` \n", 65 | "\n", 66 | "This is a _comment in the code_\n", 67 | "\n", 68 | "Comments are notes in your source code that aren't exectued when your code is run. These are useful for reminding yourself what your code does, and for notifying others to your intentions. " 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "# A comment\n", 78 | "#\n", 79 | "# We use comments to write down things that we want to rememember\n", 80 | "# or instructions for other users that will read/use our code\n", 81 | "\n", 82 | "# Anything after the # is ignored by python.\n", 83 | "\n", 84 | "print(\"I could have written code like this.\") # the comment after is ignored" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "# You can also use a comment to \"disable\" or comment out a piece of code:\n", 94 | "# print(\"This won't run.\")\n", 95 | "\n", 96 | "print(\"This will run.\")" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "### Exercise\n", 104 | "\n", 105 | "Fix the code below, so that it runs:" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "# print(\"Hi Panos!''" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "## Multiline comments \n", 122 | "\n", 123 | "Python has single line and multiline comments. These multiline comments are also called _docstrings_ because they are often used to write documentation for the code. " 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "# This is a single line comment\n", 133 | "# This a second single line comment\n", 134 | "\n", 135 | "print(\"trying out some comments\")\n", 136 | "\"\"\"This code is used to print \n", 137 | "a message of your choice. Notice that \n", 138 | "this message that is included in the triple double\n", 139 | "quotes will not execute.\"\"\"\n", 140 | "\n", 141 | "print(\"python ftw\")" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "## Notebooks: Markdown for text\n", 149 | "\n", 150 | "In notebooks, you can simply double click on a piece of text and then edit it. To restore it back, from edit mode, press \"Run\". Markdown is a very simple language for formatting text, and you can read further instructions by going to \"Help : Markdown\" from the menu, or checking the [online examples](http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Working%20With%20Markdown%20Cells.html)\n", 151 | "\n", 152 | "Below, we will see a few examples." 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "# Big Header \n", 160 | "## Smaller Header\n", 161 | "### A little smaller header\n", 162 | "#### Getting smaller and smaller\n", 163 | "##### Very very small header\n", 164 | "###### I do not even know if this is a header anymore" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "You can write plain text, **bold text**, and _italics_. \n", 172 | "You can also ~~strike~~ things that were incorrect.\n" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "If you want to write code, you can use the backtick characters: `print(\"Hello World\")`\n", 180 | "\n", 181 | "Or you can use triple backticks, and can get color coding by specifying the language that you use.\n", 182 | "```python\n", 183 | "print(\"Hello World\")\n", 184 | "```\n", 185 | "\n", 186 | "```html\n", 187 | "\n", 188 | "
Hello World!\n", 189 | "\n", 190 | "```" 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "You can also create bulleted lists:\n", 198 | "* Learn Python\n", 199 | "* ...\n", 200 | "* Millions!" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "Or perhaps you want to write ordered lists:\n", 208 | "1. Learn Python\n", 209 | "2. Learn SQL\n", 210 | "3. ....\n", 211 | "4. Millions!" 212 | ] 213 | } 214 | ], 215 | "metadata": { 216 | "kernelspec": { 217 | "display_name": "Python 3", 218 | "language": "python", 219 | "name": "python3" 220 | }, 221 | "language_info": { 222 | "codemirror_mode": { 223 | "name": "ipython", 224 | "version": 3 225 | }, 226 | "file_extension": ".py", 227 | "mimetype": "text/x-python", 228 | "name": "python", 229 | "nbconvert_exporter": "python", 230 | "pygments_lexer": "ipython3", 231 | "version": "3.8.2" 232 | } 233 | }, 234 | "nbformat": 4, 235 | "nbformat_minor": 1 236 | } 237 | -------------------------------------------------------------------------------- /notes/B-Numeric_Expressions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Expressions, Data Types, and Variables\n", 8 | "\n", 9 | "\n" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": { 15 | "heading_collapsed": true 16 | }, 17 | "source": [ 18 | "## Math Expressions for Numeric Types\n", 19 | "\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": { 25 | "hidden": true 26 | }, 27 | "source": [ 28 | "We will start first by dealing with numbers and see how we can do various math and logical operations with Python. Here are a few operators that we will be using:\n", 29 | "\n", 30 | "* `+` addition, add two numbers\n", 31 | "* `-` subtraction and negation\n", 32 | "* `*` multiplication, multiply two numbers\n", 33 | "* `/` divide first with the outcome being decimal outcome\n", 34 | "* `//` division with the outcome being integer (return only the integer part of the outcome)\n", 35 | "* `%` modulo/division remainder, what is the remainder when the first number is divided by the second?\n", 36 | "* `**` power, raise the first number into a power given by the second number" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": { 43 | "hidden": true 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "print(\"addition:\")\n", 48 | "print(1 + 1)\n", 49 | "print(1.5 + 1)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": { 56 | "hidden": true 57 | }, 58 | "outputs": [], 59 | "source": [ 60 | "print(\"subtraction:\")\n", 61 | "print(1 - 1)\n", 62 | "print(1 - 1.0)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": { 69 | "hidden": true 70 | }, 71 | "outputs": [], 72 | "source": [ 73 | "print(\"negation:\")\n", 74 | "print(-5)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": { 81 | "hidden": true 82 | }, 83 | "outputs": [], 84 | "source": [ 85 | "print(\"multiplication:\")\n", 86 | "print(3 * 3) # integer\n", 87 | "print(3 * 3.0) # floating point" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": { 94 | "hidden": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "print(\"division:\")\n", 99 | "print(4 / 2)\n", 100 | "print(5 / 2)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": { 107 | "hidden": true 108 | }, 109 | "outputs": [], 110 | "source": [ 111 | "print(\"integer division:\")\n", 112 | "print(4 // 2)\n", 113 | "print(5 // 2)" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": { 120 | "hidden": true 121 | }, 122 | "outputs": [], 123 | "source": [ 124 | "print(\"modulo:\") # modulo is a fancy term for remainder of a division\n", 125 | "print(4 % 2)\n", 126 | "print(5 % 2)\n", 127 | "print(5.5 % 2)\n", 128 | "print(75 % 4)\n", 129 | "print(10 % 3)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": { 136 | "hidden": true 137 | }, 138 | "outputs": [], 139 | "source": [ 140 | "print(\"power:\")\n", 141 | "print(10 ** 3)\n", 142 | "print(2 ** 5)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": { 148 | "heading_collapsed": true 149 | }, 150 | "source": [ 151 | "## Exercise 1" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": { 157 | "hidden": true 158 | }, 159 | "source": [ 160 | "Assume that you go to a restaurant, and you order $50 worth of food. Then you need to add the NY Sales Tax (8.875%) and add a tip (say, 20%). Write down the calculation that will print the total cost of the food." 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": { 167 | "hidden": true 168 | }, 169 | "outputs": [], 170 | "source": [ 171 | "# tip calculated on the pre-tax amount" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": { 178 | "hidden": true 179 | }, 180 | "outputs": [], 181 | "source": [ 182 | "# tip calculated on the after-tax amount" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": { 188 | "hidden": true, 189 | "solution2": "hidden", 190 | "solution2_first": true 191 | }, 192 | "source": [ 193 | "### Solution" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": { 200 | "hidden": true, 201 | "solution2": "hidden" 202 | }, 203 | "outputs": [], 204 | "source": [ 205 | "print(\"The total cost of the meal, with tip on the pre-tax amount is:\")\n", 206 | "print(50 + 50 * 8.875 / 100 + 50 * 20 / 100)" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": { 213 | "hidden": true, 214 | "solution2": "hidden" 215 | }, 216 | "outputs": [], 217 | "source": [ 218 | "print(\"The total cost of the meal, with tip on the post-tax amount is:\")\n", 219 | "print(50 + 50 * 8.875 / 100 + (50 + 50 * 8.875 / 100) * 20 / 100)" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": { 225 | "heading_collapsed": true 226 | }, 227 | "source": [ 228 | "## Exercise 2" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": { 234 | "hidden": true 235 | }, 236 | "source": [ 237 | "\n", 238 | "You have a stock that closed at `$550` on Monday, and then closed at `$560` on Tuesday. Calculate its daily return: the daily return is defined as the difference in the closing prices, divided by the closing price the day before." 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": null, 244 | "metadata": { 245 | "hidden": true 246 | }, 247 | "outputs": [], 248 | "source": [ 249 | "# your code here" 250 | ] 251 | }, 252 | { 253 | "cell_type": "markdown", 254 | "metadata": { 255 | "hidden": true, 256 | "solution2": "hidden", 257 | "solution2_first": true 258 | }, 259 | "source": [ 260 | "### Solution" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": { 267 | "hidden": true, 268 | "solution2": "hidden" 269 | }, 270 | "outputs": [], 271 | "source": [ 272 | "print(\"The daily return is:\")\n", 273 | "print((560 - 550) / 550)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "metadata": { 280 | "hidden": true, 281 | "solution2": "hidden" 282 | }, 283 | "outputs": [], 284 | "source": [ 285 | "# If we want to show percentages,\n", 286 | "# We can modify our code and\n", 287 | "# add a multiplication with 100 in front:\n", 288 | "print(\"The daily return is:\")\n", 289 | "print(100 * (560 - 550) / 550)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": { 295 | "heading_collapsed": true 296 | }, 297 | "source": [ 298 | "## Exercise 3\n" 299 | ] 300 | }, 301 | { 302 | "cell_type": "markdown", 303 | "metadata": { 304 | "hidden": true 305 | }, 306 | "source": [ 307 | "\n", 308 | "Assume that someone's height is 5 ft and 9 inches. Conver that to centimeters. Remember that one foot is 30.48 centimeters, and one inch is 2.54 centimeters." 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": { 315 | "hidden": true 316 | }, 317 | "outputs": [], 318 | "source": [ 319 | "# your code here" 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "metadata": { 325 | "hidden": true, 326 | "solution2": "hidden", 327 | "solution2_first": true 328 | }, 329 | "source": [ 330 | "### Solution" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": null, 336 | "metadata": { 337 | "hidden": true, 338 | "solution2": "hidden" 339 | }, 340 | "outputs": [], 341 | "source": [ 342 | "print(\"The height in centimeters is:\")\n", 343 | "print(5 * 30.48 + 9 * 2.54)" 344 | ] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": { 349 | "heading_collapsed": true 350 | }, 351 | "source": [ 352 | "## Exercise 4\n" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": { 358 | "hidden": true 359 | }, 360 | "source": [ 361 | "\n", 362 | "Write a Python program to compute the future value of a deposit with a a principal amount of \\$10,000, rate of interest 3%, and after 5 years. Remember that the value is: $ Principal * (1+interest)^{years}$ and you will need to use the power operator (**) for this calculation." 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": null, 368 | "metadata": { 369 | "hidden": true 370 | }, 371 | "outputs": [], 372 | "source": [ 373 | "# your code here" 374 | ] 375 | }, 376 | { 377 | "cell_type": "markdown", 378 | "metadata": { 379 | "heading_collapsed": true, 380 | "hidden": true, 381 | "solution2": "hidden", 382 | "solution2_first": true 383 | }, 384 | "source": [ 385 | "### Solution" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": null, 391 | "metadata": { 392 | "hidden": true, 393 | "solution2": "hidden" 394 | }, 395 | "outputs": [], 396 | "source": [ 397 | "print(\"The value of the deposit will be:\")\n", 398 | "print(10000 * (1 + 3 / 100) ** 5)" 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "metadata": { 404 | "heading_collapsed": true 405 | }, 406 | "source": [ 407 | "## Exercise 5" 408 | ] 409 | }, 410 | { 411 | "cell_type": "markdown", 412 | "metadata": { 413 | "hidden": true 414 | }, 415 | "source": [ 416 | "Assume that someone's height is 180 centimeters. Convert that height into feet and inches. Remember that one foot is 30.48 centimeters, and one inch is 2.54 centimeters. You will need to use the modulo operator (%) for this conversion. Also use the `int(...)` function to get the integer part of a division. Optionally, you can also use the `round(...)` function to get the rounded integer number from a decimal." 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": null, 422 | "metadata": { 423 | "hidden": true 424 | }, 425 | "outputs": [], 426 | "source": [ 427 | "# your code here" 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "metadata": { 433 | "hidden": true, 434 | "solution2": "hidden", 435 | "solution2_first": true 436 | }, 437 | "source": [ 438 | "### Solution" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "execution_count": null, 444 | "metadata": { 445 | "hidden": true, 446 | "solution2": "hidden" 447 | }, 448 | "outputs": [], 449 | "source": [ 450 | "print(\"180 cm is\")\n", 451 | "print(180 // 30.48)\n", 452 | "print(\"feet and \")\n", 453 | "print(180 % 30.48 / 2.54)\n", 454 | "print(\"inches\")" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": null, 460 | "metadata": { 461 | "hidden": true, 462 | "solution2": "hidden" 463 | }, 464 | "outputs": [], 465 | "source": [ 466 | "# let’s see how we can print the message in one line\n", 467 | "print(\"180 cm is\", 180 // 30.48, \"feet and \", 180 % 30.48 / 2.54, \"inches\")" 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": null, 473 | "metadata": { 474 | "hidden": true, 475 | "solution2": "hidden" 476 | }, 477 | "outputs": [], 478 | "source": [ 479 | "# Let's use int() function to make the output look a bit nicer.\n", 480 | "print(\"180 cm is\", int(180 / 30.48), \"feet and\", int(180 % 30.48 / 2.54), \"inches\")" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": null, 486 | "metadata": { 487 | "hidden": true, 488 | "solution2": "hidden" 489 | }, 490 | "outputs": [], 491 | "source": [ 492 | "# Let's use the functions int() and round()\n", 493 | "print(\"180 cm is\", int(180 / 30.48), \"feet and\", round(180 % 30.48 / 2.54, 1), \"inches\")" 494 | ] 495 | } 496 | ], 497 | "metadata": { 498 | "kernelspec": { 499 | "display_name": "Python 3", 500 | "language": "python", 501 | "name": "python3" 502 | }, 503 | "language_info": { 504 | "codemirror_mode": { 505 | "name": "ipython", 506 | "version": 3 507 | }, 508 | "file_extension": ".py", 509 | "mimetype": "text/x-python", 510 | "name": "python", 511 | "nbconvert_exporter": "python", 512 | "pygments_lexer": "ipython3", 513 | "version": "3.8.2" 514 | } 515 | }, 516 | "nbformat": 4, 517 | "nbformat_minor": 1 518 | } 519 | -------------------------------------------------------------------------------- /notes/C2-Variables_Exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercises for Variables" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": { 13 | "heading_collapsed": true 14 | }, 15 | "source": [ 16 | "## Exercise 1" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "hidden": true 23 | }, 24 | "source": [ 25 | "Repeat the exercise from earlier on, about the daily return of a stock, but use variables instead of the raw numbers. You can initialize the variables to use \\\\$550 for the closing price of the stock on Monday, and then use \\\\$560 as the closing price on Tuesday. Try afterwards to change these numbers and see the results. (Hint: notice what would happen if you use the name `return` for the variable.) " 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": { 32 | "hidden": true 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "# your code here" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "hidden": true, 43 | "solution2": "hidden", 44 | "solution2_first": true 45 | }, 46 | "source": [ 47 | "### Solution" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": { 54 | "hidden": true, 55 | "solution2": "hidden" 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "price_mon = 550\n", 60 | "price_tue = 560\n", 61 | "daily_return = 100 * (price_tue - price_mon) / price_mon\n", 62 | "print(\"The daily return is\")\n", 63 | "print(daily_return)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": { 70 | "hidden": true, 71 | "solution2": "hidden" 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "# If you uncomment the code below, notice that we cannot call the variable \"return\"\n", 76 | "# as \"return\" is a reserved keyword in Python\n", 77 | "\n", 78 | "# price_mon = 550\n", 79 | "# price_tue = 560\n", 80 | "# return = 100*(price_tue-price_mon)/price_mon\n", 81 | "# print(\"The daily return is\")\n", 82 | "# print(return)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": { 88 | "heading_collapsed": true 89 | }, 90 | "source": [ 91 | "## Exercise 2\n", 92 | "\n" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": { 98 | "hidden": true 99 | }, 100 | "source": [ 101 | "Write a Python program to convert centimeters to feet and inches. Remember that one foot is 30.48 centimeters, and one inch is 2.54 centimeters." 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": { 108 | "hidden": true 109 | }, 110 | "outputs": [], 111 | "source": [ 112 | "# your code here" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": { 118 | "hidden": true, 119 | "solution2": "hidden", 120 | "solution2_first": true 121 | }, 122 | "source": [ 123 | "### Solution" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": { 130 | "hidden": true, 131 | "solution2": "hidden" 132 | }, 133 | "outputs": [], 134 | "source": [ 135 | "# So, let's take a look at our code from last time:\n", 136 | "print(\"180 cm is\", int(180 // 30.48), \"feet and\", round(180 % 30.48 / 2.54, 1), \"inches\")" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": { 143 | "hidden": true, 144 | "solution2": "hidden" 145 | }, 146 | "outputs": [], 147 | "source": [ 148 | "# We will now start introducing variables\n", 149 | "centimeters = 180\n", 150 | "feet = int(centimeters / 30.48)\n", 151 | "inches = int(centimeters % 30.48 / 2.54)\n", 152 | "print(centimeters, \"cm is\", feet, \"ft and\", inches, \"inches\")" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": null, 158 | "metadata": { 159 | "hidden": true, 160 | "solution2": "hidden" 161 | }, 162 | "outputs": [], 163 | "source": [ 164 | "# Now let's introduce more variables to eliminate the \"magic numbers\" 30.48 and 2.54\n", 165 | "cm_per_foot = 30.48\n", 166 | "cm_per_in = 2.54\n", 167 | "# And now let's modify our code from above\n", 168 | "centimeters = 180\n", 169 | "feet = int(centimeters / cm_per_foot)\n", 170 | "inches = int(centimeters % cm_per_foot / cm_per_in)\n", 171 | "print(centimeters, \"cm is\", feet, \"ft and\", inches, \"inches\")\n", 172 | "\n", 173 | "# And if we want to use round instead of int:\n", 174 | "inches = round(centimeters % cm_per_foot / cm_per_in, 1)\n", 175 | "print(centimeters, \"cm is\", feet, \"ft and\", inches, \"inches\")" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": { 181 | "heading_collapsed": true 182 | }, 183 | "source": [ 184 | "## Exercise 3" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": { 190 | "hidden": true 191 | }, 192 | "source": [ 193 | "We want to compute the \"[wind chill index](https://en.wikipedia.org/wiki/Wind_chill)\" as described at Wikipedia\n", 194 | "\n", 195 | "$T_\\mathrm{wc}=35.74+0.6215 T_\\mathrm{a}-35.75 v^{0.16}+0.4275 T_\\mathrm{a} v^{0.16}$\n", 196 | "\n", 197 | "where $T_\\mathrm{wc}$ is the wind chill index, based on the Fahrenheit scale; $T_\\mathrm{a}$ is the air temperature in degrees Fahrenheit, and $v$ is the wind speed in miles per hour. (Note: Windchill temperature is defined only for temperatures at or below 50 °F and wind speeds above 3.0 miles per hour.)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": { 204 | "hidden": true 205 | }, 206 | "outputs": [], 207 | "source": [ 208 | "# your code here" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": { 214 | "hidden": true, 215 | "solution2": "hidden", 216 | "solution2_first": true 217 | }, 218 | "source": [ 219 | "### Solution" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "metadata": { 226 | "hidden": true, 227 | "solution2": "hidden" 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "v = 5 # miles per hour\n", 232 | "Ta = 32 # air temperature in F\n", 233 | "Twc = 35.74 + 0.6215 * Ta - 35.75 * v ** 0.16 + 0.4275 * Ta * v ** 0.16\n", 234 | "print(\"The windchil index (feels-like) is:\", int(Twc))" 235 | ] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": { 240 | "heading_collapsed": true 241 | }, 242 | "source": [ 243 | "## Exercise 4" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": { 249 | "hidden": true 250 | }, 251 | "source": [ 252 | "You have a loan with a given principal amount $p$, to be repaid over a period of $n$ years. At the end of each year, you get charged an interest of $r$ for the total amount that you owe. Each year you pay an installment equal to $1/n$-th of the initial principal, plus interest on the *remaining* principal. \n", 253 | "\n", 254 | "Assume that the principal is \\\\$10K, interest rate of 3%, and repayment is over 10 years.\n", 255 | "\n", 256 | "Write code that computes how much you owe at the end of year 1. Then write code for calculating your payment at the end of year 2.\n", 257 | "\n" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": { 264 | "hidden": true 265 | }, 266 | "outputs": [], 267 | "source": [ 268 | "# your code here" 269 | ] 270 | }, 271 | { 272 | "cell_type": "markdown", 273 | "metadata": { 274 | "hidden": true, 275 | "solution2": "hidden", 276 | "solution2_first": true 277 | }, 278 | "source": [ 279 | "### Solution" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": null, 285 | "metadata": { 286 | "hidden": true, 287 | "solution2": "hidden" 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "principal = 10000\n", 292 | "rate = 0.03\n", 293 | "years = 10" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": { 300 | "hidden": true, 301 | "solution2": "hidden" 302 | }, 303 | "outputs": [], 304 | "source": [ 305 | "interest = rate * principal\n", 306 | "payment = principal / years + interest\n", 307 | "print(\"Payment at the end of year 1\")\n", 308 | "print(payment)" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": { 315 | "hidden": true, 316 | "solution2": "hidden" 317 | }, 318 | "outputs": [], 319 | "source": [ 320 | "remaining_principal = principal - principal / years\n", 321 | "interest = rate * remaining_principal\n", 322 | "payment = principal / years + interest\n", 323 | "print(\"payment at the end of year 2\")\n", 324 | "print(payment)" 325 | ] 326 | } 327 | ], 328 | "metadata": { 329 | "kernelspec": { 330 | "display_name": "Python 3", 331 | "language": "python", 332 | "name": "python3" 333 | }, 334 | "language_info": { 335 | "codemirror_mode": { 336 | "name": "ipython", 337 | "version": 3 338 | }, 339 | "file_extension": ".py", 340 | "mimetype": "text/x-python", 341 | "name": "python", 342 | "nbconvert_exporter": "python", 343 | "pygments_lexer": "ipython3", 344 | "version": "3.8.2" 345 | } 346 | }, 347 | "nbformat": 4, 348 | "nbformat_minor": 1 349 | } 350 | -------------------------------------------------------------------------------- /notes/C3-DataTypes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Data Types" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Notice in the examples below, the different operations result in different types of outcomes." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "print(1 + 1) # this is an integer" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "print(1.0 + 1) # this is an integer added to a decimal/floating point" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "print(\"1\" + \"1\") # this a string/text" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "print(100 * 2)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "print(100 * \"2\")" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "We will learn more about the different types of variables later, but for now, remember that you can use the `type` command to find out the type of an expression:" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "type(1)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "type(3.14)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "type(\"Hello\")" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "type(\"1\")" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "type(\"3.14\")" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "type(1 + 1)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "type(\"1\" + \"1\")" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [ 138 | "type(10 * \"2\")" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "**Lesson**: Notice the different data types above: `str` (string), `int` (integer), `float` (decimal numbers). One thing hat you will notice is that the outcome of the various operators (`+`, `*`, etc) can be different based on the data types involved." 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "#### Exercise \n", 153 | "\n", 154 | "What will the following program print out? Figure it our _before_ running the program." 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "x = 41\n", 164 | "x = x + 1\n", 165 | "print(x)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "x = \"41\"\n", 175 | "x = x + \"1\"\n", 176 | "print(x)" 177 | ] 178 | } 179 | ], 180 | "metadata": { 181 | "kernelspec": { 182 | "display_name": "Python 3", 183 | "language": "python", 184 | "name": "python3" 185 | }, 186 | "language_info": { 187 | "codemirror_mode": { 188 | "name": "ipython", 189 | "version": 3 190 | }, 191 | "file_extension": ".py", 192 | "mimetype": "text/x-python", 193 | "name": "python", 194 | "nbconvert_exporter": "python", 195 | "pygments_lexer": "ipython3", 196 | "version": "3.8.2" 197 | } 198 | }, 199 | "nbformat": 4, 200 | "nbformat_minor": 1 201 | } 202 | -------------------------------------------------------------------------------- /notes/D1-Strings_Basic_Operations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Primitive Data Types\n", 8 | "--------------------\n", 9 | "\n", 10 | "These are the basic data types that constitute all of the more complex data structures in python. The basic data types are the following:\n", 11 | "\n", 12 | "* Strings (for text)\n", 13 | "* Numeric types (integers and decimals)\n", 14 | "* Booleans\n" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "### String variables\n", 22 | "\n", 23 | "String variables are used to store textual data, characters and sequences of characters. Can be specified by surrounding some text with single `'` or double `\"` quotes. " 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "str_1 = \"Hello World!\"\n", 33 | "print(str_1)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "str_1 = \"Hello World!\"\n", 43 | "print(str_1)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "#### Multiline strings\n", 51 | "\n", 52 | "If we have a piece of text with multiple lines, then triple quotes (either `'''` or `\"\"\"`) can be used to surround the text." 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "str_2 = \"\"\"\n", 62 | "(CNN)AirAsia Flight QZ8501 climbed rapidly before it crashed,\n", 63 | "a top Indonesian official said Tuesday, according to The Jakarta Post.\n", 64 | "\n", 65 | "Then the plane stalled, Transportation Minister Ignasius Jonan said\n", 66 | "at a parliamentary hearing, according to the AFP and Reuters news agencies.\n", 67 | "\n", 68 | "\"The plane, during the last minutes, went up faster than normal\n", 69 | "speed ... after then, it stalled. That is according to the data\n", 70 | "from the radar,\" Jonan said, according to the news agencies.\n", 71 | "\"\"\"\n", 72 | "print(str_2)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "str_3 = \"\"\"\n", 82 | "If we want to have multiple lines in the string\n", 83 | "then we can use triple quotes: This is a multiline\n", 84 | "string!\n", 85 | "\"\"\"\n", 86 | "print(str_3)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "### Operations on Strings \n", 94 | "\n", 95 | "We have seen some common operations on numeric variables. Now, let’s discuss a few **common** operations on string variables. The complete list of string operations is [available here](http://docs.python.org/3/library/string.html)." 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "#### Concatenation\n", 103 | "\n", 104 | "The `+` operator concatenates two strings:" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "# note that + concatenates strings\n", 114 | "str1 = \"hello\"\n", 115 | "str2 = \"world\"\n", 116 | "message = str1 + \" \" + str2 + \"!\"\n", 117 | "print(message)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "Notice the difference of the `+` operation when operating on text vs. numeric variables:" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": null, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "x = 41\n", 134 | "x = x + 1\n", 135 | "print(x)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "x = \"41\"\n", 145 | "x = x + \"1\"\n", 146 | "print(x)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "##### Exercise\n", 154 | "\n", 155 | "You are given a variable `name = 'Panos'` and variable `phone = '212-998-0803'`. Use the values of these variables to create and print the message `Call Panos at 212-998-0803`." 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "name = \"Panos\"\n", 165 | "phone = \"212-998-0803\"\n", 166 | "# your code here (2 lines)\n", 167 | "message = \"Call \" + name + \" at \" + phone\n", 168 | "print(message)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "#### String length\n", 176 | "\n", 177 | "`len(str)`: length of a string, number of characters" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [ 186 | "word = \"Python is the word. And on and on and on....\"\n", 187 | "print(len(word))" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "print(word)\n", 197 | "print(\"The length of the text above is \", len(word), \"characters\")" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "##### Exercise\n", 205 | "\n", 206 | "You are given a brief news article, stored in the variable `news`. Compute the length of the article, in characters." 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": {}, 213 | "outputs": [], 214 | "source": [ 215 | "news = \"\"\"\n", 216 | "Technology stocks are suffering one of their worst beatings in years,\n", 217 | "as investors reassess a sector that has been considered the growth\n", 218 | "engine of the global economy but now faces the prospect of greater\n", 219 | "regulatory scrutiny. The tech-heavy Nasdaq Composite Index\n", 220 | "fell 2.9% Tuesday. That selloff carried over to the broader market,\n", 221 | "where the S&P 500 index slumped 1.7%. The Dow Jones Industrial\n", 222 | "Average fell 1.4%, giving back some of Monday’s 2.8% rebound.\n", 223 | "\"\"\"" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "print(len(news))" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "print(news)\n", 242 | "print(\"The length of the text above is\", len(news), \"characters\")" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "#### Capitalization\n", 250 | "\n", 251 | "Now let's see how we can change the capitalization of the text. This will also be our first, informal, introduction to a new type of function. \n", 252 | "\n", 253 | "* `str.upper()`: returns an uppercase version of a string\n", 254 | "* `str.lower()`: returns a lowercase version of a string" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": null, 260 | "metadata": {}, 261 | "outputs": [], 262 | "source": [ 263 | "print(news.lower())" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "print(news.upper())" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": null, 278 | "metadata": {}, 279 | "outputs": [], 280 | "source": [ 281 | "# Notice that the original 'news' variable did not change\n", 282 | "print(news)" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [ 291 | "# If we want to keep the changed-case version,\n", 292 | "# we need to store it in a variable\n", 293 | "l_news = news.lower()\n", 294 | "print(l_news)" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "##### Small deviation\n", 302 | "\n", 303 | "The `.lower()` is a function that applies only to string variables. Notice the different way that we apply the function, compared to, say, the `len(news)` above. In this case, we have a string variable (`news`), then we have a dot (`.`), and then we put the name of the function: (`lower()`). Notice that we have the two parentheses at the end, which indicate that the function does not have any arguments, except for the `news` variable. Notice that if we omit the parentheses, we will get a strange result:" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "metadata": {}, 310 | "outputs": [], 311 | "source": [ 312 | "news.lower" 313 | ] 314 | } 315 | ], 316 | "metadata": { 317 | "kernelspec": { 318 | "display_name": "Python 3", 319 | "language": "python", 320 | "name": "python3" 321 | }, 322 | "language_info": { 323 | "codemirror_mode": { 324 | "name": "ipython", 325 | "version": 3 326 | }, 327 | "file_extension": ".py", 328 | "mimetype": "text/x-python", 329 | "name": "python", 330 | "nbconvert_exporter": "python", 331 | "pygments_lexer": "ipython3", 332 | "version": "3.8.2" 333 | } 334 | }, 335 | "nbformat": 4, 336 | "nbformat_minor": 1 337 | } 338 | -------------------------------------------------------------------------------- /notes/D2-Strings_Indexing_and_Slicing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Indexing and Slicing: Accessing parts of the string\n", 8 | "\n", 9 | "**Note: The concepts of indexing and slicing are general. We will encounter them in many other contexts, beyond strings.**\n", 10 | "\n", 11 | "#### Indexing\n", 12 | "\n", 13 | "Strings can be indexed (subscripted), with the first character having index 0." 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": null, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "word = \"Python\"" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "word[0] # character in position 0" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "word[1]" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "word[5] # character in position 5" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "#### Negative indexing\n", 57 | "\n", 58 | "Indices may also be negative numbers, to start counting from the right:" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [ 67 | "word[-1] # last character" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "word[-2] # second-last character" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "word[-6]" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "##### Illustration of indexing and negative indexing" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "| |P|y|t|h|o|n|\n", 100 | "|-----|-|-|-|-|-|-|\n", 101 | "|Index|0|1|2|3|4|5|\n", 102 | "|Negative Index|-6|-5|-4|-3|-2|-1|" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "#### Slicing\n", 110 | "\n", 111 | "In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain a substring:" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "word[0:2] # characters from position 0 (included) to 2 (excluded)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "word[2:5] # characters from position 2 (included) to 5 (excluded)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [ 138 | "word[2:] # characters from position 2 (included) to the end" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [ 147 | "word[:3] # characters from beginning (position 0) to position 3 (excluded)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "word[-3:] # last three characters" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "word[-3:-1] # penultimate two characters" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "word[1:-1]" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "##### Additional example" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": null, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "univ = \"New York University\"" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "univ[:3] # first three characters" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [ 208 | "univ[4:8] # start at the fifth character and end at the eighth" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "univ[-10:] # last ten characters of the string" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "univ[1:-3]" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "#### Exercise\n", 234 | "\n", 235 | "* Assign the string 'Dealing with Data' to a Python variable. \n", 236 | "* Print the word 'Dealing' by using the indexing/slicing approach.\n", 237 | "* Print the word 'Data' by using the negative indexing/slicing approach." 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "metadata": {}, 251 | "outputs": [], 252 | "source": [] 253 | } 254 | ], 255 | "metadata": { 256 | "kernelspec": { 257 | "display_name": "Python 3", 258 | "language": "python", 259 | "name": "python3" 260 | }, 261 | "language_info": { 262 | "codemirror_mode": { 263 | "name": "ipython", 264 | "version": 3 265 | }, 266 | "file_extension": ".py", 267 | "mimetype": "text/x-python", 268 | "name": "python", 269 | "nbconvert_exporter": "python", 270 | "pygments_lexer": "ipython3", 271 | "version": "3.8.2" 272 | } 273 | }, 274 | "nbformat": 4, 275 | "nbformat_minor": 1 276 | } 277 | -------------------------------------------------------------------------------- /notes/D4-Special_Characters_Splitting_Strings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Special characters\n", 8 | "\n", 9 | "When we use strings, you will notice that we often want to use some \"_special characters_\". These special characters consist of the backslash character (`\\`) followed by another character.\n", 10 | "\n", 11 | "**Tab character** `\\t`: For example, if we want to create an output of multiple columns, with each columns being separated with a tab from each other, we can use the tab character, which is represented as `\\t`. " 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": null, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "# Example: List the first name, last name, and email of a person, in columns\n", 21 | "# Separate the columns using a tab character\n", 22 | "str_4a =\"First Name\\tLast Name\\tEmail\"\n", 23 | "str_4b =\"Panagiotis\\tIpeirotis\\tpanos@nyu.edu\"\n", 24 | "str_4c =\"Kristaps\\tPorzingis\\tkporzee@nba.com\"\n", 25 | "print(str_4a)\n", 26 | "print(str_4b)\n", 27 | "print(str_4c)\n" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "**New line character** `\\n`: This is a special character that we use to represent a new line." 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "# Notice that we end the strings below with the \\n character, \n", 44 | "# which is the \"new line\" special character\n", 45 | "str_5 = \"Hello World!\\nHello World Twice!\"\n", 46 | "print(str_5)\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "**Backslash character** `\\\\`: In general, backslash (`\\`) is used to introduce special characters. If we want to type the backslash character itself, we do it by typing backslash twice in a row: `\\\\`." 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "print(\"I want to print backslash: \\\\\")" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "**Quotes**: " 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "str_4 = 'This is a string within single quotes that can contain \"double quotes\" as part of the string'\n", 79 | "print(str_4)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "str_5 = 'If we want to have \\'single quotes\\' in single quoted string we should escape them'\n", 89 | "print(str_5)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "str_6 = \"Similarly, if we want to have \\\"double quotes\\\" in double quoted string we should escape them\\n\"\n", 99 | "print(str_6)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "### Splitting Strings: `split` and `join`\n", 107 | "\n", 108 | "Since we talked about special characters, let’s talk now about splitting strings, using the `split()` function.\n", 109 | "\n", 110 | "\n", 111 | "+ `longstring.split(separator)`: split the first string (longstring) at every occurrence of the second string (separator) Outputs a list (see below).\n", 112 | "+ `connector.join(list)`: join is the \"reverse\" of split, and joins all the elements of the list, using the `connector` string in front." 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "print(\"practical data science\".split(\" \"))" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "print(\"billgates@microsoft.com\".split(\"@\"))" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "Or let's take the example from above:" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "str_a =\"First Name\\tLast Name\\tEmail\"\n", 147 | "str_b =\"Panagiotis\\tIpeirotis\\tpanos@nyu.edu\"\n", 148 | "str_c =\"Kristaps\\tPorzingis\\tkporzee@nba.com\"\n", 149 | "print(str_a.split('\\t'))\n", 150 | "print(str_b.split('\\t'))\n", 151 | "print(str_c.split('\\t'))" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "Notice that when we split a string and the delimeter character\n", 159 | "does not appear, then we get back the string itself, but converted\n", 160 | "into a list with a single element. (We will talk about lists in a \n", 161 | " couple of weeks)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": null, 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "print(\"hello\".split(\" \"))" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | " Join is the \"reverse\" of split, and joins all\n", 178 | " the elements of the list, using the \"string\" in front.\n", 179 | " The command below joins the elements of the `line` variable using the \n", 180 | " characters `###` as the connecting element." 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "line = ['Panagiotis', 'Ipeirotis', 'panos@nyu.edu']\n", 190 | "\"###\".join(line)" 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "#### Exercise\n", 198 | "\n", 199 | "Consider the string `billgates@microsoft.com`. Write code that finds the username of the email address and the domain of the email address, using the `split()` command." 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [ 208 | "email = \"billgates@microsoft.com\"\n", 209 | "result = email.split(\"@\")\n", 210 | "print(result)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [ 219 | "# We can also access the individual elements of a list using indexing\n", 220 | "print(\"Username:\", result[0])\n", 221 | "print(\"Domain:\", result[1])" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [ 230 | "email = \"billgates@microsoft.com\"\n", 231 | "result = email.split(\"@\")\n", 232 | "print(result)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "# And if we want to access the individual elements of a list we can use the indexing notation, as in the case of strings:\n", 242 | "print(\"Username:\", result[0])\n", 243 | "print(\"Domain:\", result[1])" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [] 252 | } 253 | ], 254 | "metadata": { 255 | "kernelspec": { 256 | "display_name": "Python 3", 257 | "language": "python", 258 | "name": "python3" 259 | }, 260 | "language_info": { 261 | "codemirror_mode": { 262 | "name": "ipython", 263 | "version": 3 264 | }, 265 | "file_extension": ".py", 266 | "mimetype": "text/x-python", 267 | "name": "python", 268 | "nbconvert_exporter": "python", 269 | "pygments_lexer": "ipython3", 270 | "version": "3.5.2" 271 | } 272 | }, 273 | "nbformat": 4, 274 | "nbformat_minor": 1 275 | } 276 | -------------------------------------------------------------------------------- /notes/D5-String_Formatting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# String Formatting" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Often one wants to embed other information into strings, sometimes with special formatting constraints. In python, one may insert special formatting characters into strings that convey what type of data should be inserted and where, and how the \"stringified\" form should be formatted. For instance:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "print('Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='115.81W'))" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "Or we can have the parameter values being variables:" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "lat = '37.24N'\n", 40 | "lon = '115.81W'\n", 41 | "print('Coordinates: {latitude}, {longitude}'.format(latitude=lat, longitude=lon))" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "We can of course re-order and use multiple times the placeholders:" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "lat = '37.24N'\n", 58 | "lon = '115.81W'\n", 59 | "print('Latitude: {latitude}, Longitude: {longitude} ==> [{latitude}, {longitude}]'\n", 60 | " .format(latitude=lat, longitude=lon))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "#### More formatting options\n", 68 | "\n", 69 | "Below we will see a few more options, mainly for formatting numbers. (For a more detailed treatment on string formatting options, [see here](https://docs.python.org/3.5/library/string.html#format-string-syntax).) We can achieve the formatting by adding after the number/name the character `:` follows by a set of formatting options." 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "```\n", 77 | "field ::= \"{\" field_name [\":\" format_spec] \"}\"\n", 78 | "format_spec ::= [[fill character]align][sign][width][,][.precision][type]\n", 79 | "align ::= \"<\" | \">\" | \"=\" | \"^\"\n", 80 | "sign ::= \"+\" | \"-\" | \" \"\n", 81 | "width ::= number of digits in total (if width has a 0 in front, we add 0's for zero-padding)\n", 82 | "precision ::= number of decimal points\n", 83 | "```" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "Some common `type`s: \n", 91 | "\n", 92 | "* `d` integer\n", 93 | "* `f` floating point\n", 94 | "* `%` percent\n", 95 | "* `e` exponential format\n", 96 | "* `c` character\n", 97 | "* `s` string \n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "##### Formatting decimal numbers" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "# Without the use of placeholders. Notice the spaces before and after the result.\n", 114 | "print(\"Result: |\",100/23,\"|\")" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "# Using a placeholder. Notice that the spaces around the number disappear.\n", 124 | "print(\"Result: |{num}|\".format(num=100/23))" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": null, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "# Now we specify the type of \"num\" and we say it will be a floating point\n", 134 | "# Notice that we got a smaller number of decimal digits.\n", 135 | "print(\"Result: |{num:f}|\".format(num=100/23))" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "# Now let's specify the length that we want to reserve for the string\n", 145 | "# The number above took a total of 8 characters (including the decimal)\n", 146 | "# Now let's specify that we have 10 characters available.\n", 147 | "# Notice the spaces in front of the number in the result\n", 148 | "print(\"Result: |{num:10f}|\".format(num=100/23))" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "# Keep six digits for the whole number, out of which 3 for the decimals\n", 158 | "print(\"Result: |{num:6.3f}|\".format(num=100/23))" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "# Compare with having 8 digits, 3 for decimals\n", 168 | "print(\"Result: |{num:8.3f}|\".format(num=100/23))" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "# Or having 8 digits, 5 for decimals\n", 178 | "print(\"Result: |{num:8.5f}|\".format(num=100/23))" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [ 187 | "# Keep seven digits for the whole number, out of which 2 for the decimals\n", 188 | "print(\"Result: |{num:7.2f}|\".format(num=100/23))\n", 189 | "print(\"Result: |{num:7.2f}|\".format(num=1000/23))\n", 190 | "print(\"Result: |{num:7.2f}|\".format(num=10000/23))\n", 191 | "print(\"Result: |{num:7.2f}|\".format(num=100000/23))\n", 192 | "print(\"Result: |{num:7.2f}|\".format(num=1000000/23))" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "##### Extra options: Comma-separated thousands, zero padding" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [ 208 | "# Sixteen digits total and four decimal digits, with comma-separated thousands\n", 209 | "print(\"Result: |{num:16,.4f}|\".format(num=1000000/7))" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": null, 215 | "metadata": {}, 216 | "outputs": [], 217 | "source": [ 218 | "# Same example without the comma-separator\n", 219 | "print(\"Result: |{num:16.4f}|\".format(num=1000000/7))" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "# Keep six digits for the whole number, out of which 3 for the decimals, with zero padding in front\n", 229 | "print(\"Result: |{num:07.3f}|\".format(num=100/23))" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": null, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "# Floating point with three seven digits\n", 239 | "print(\"Result: |{num:.7f}|\".format(num=100/23))" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "##### Percentages, alignment" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": null, 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [ 255 | "correct = 19\n", 256 | "total = 22\n", 257 | "# Expressing a percentage: We use % instead of f\n", 258 | "# We ask for 7 characters total, and two decimal numbers.\n", 259 | "# The 7 characters include the % sign, and the decimal point\n", 260 | "# so we have three characters allocated for the integer part\n", 261 | "# Our number has only two digits in the integer, so we get an \n", 262 | "# empty space in front\n", 263 | "print('Correct answers: |{result:7.2%}|'.format(result=correct /total))" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "# alignment\n", 273 | "# < means left alignment\n", 274 | "# 30 means 30 characters allocated for the message\n", 275 | "# s means string (it is the default, and often omitted)\n", 276 | "print('|{message:<30s}|'.format(message='left aligned message'))" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": null, 282 | "metadata": {}, 283 | "outputs": [], 284 | "source": [ 285 | "# ^ means center alignment\n", 286 | "print('|{message:^30s}|'.format(message='centered message'))" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": null, 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "# > means right alignment\n", 296 | "print('|{message:>30s}|'.format(message='right aligned message'))" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": null, 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [ 305 | "# fill\n", 306 | "print('|{message:#<80s}|'.format(message='left aligned with # chars as fill'))\n", 307 | "print('|{message:#>80s}|'.format(message='right aligned with # chars as fill'))\n", 308 | "print('|{message:#^80s}|'.format(message='centered with # chars as fill'))" 309 | ] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": {}, 314 | "source": [ 315 | "## Exercise\n" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "\n", 323 | "We have a list of people and scores to display. \n", 324 | "\n", 325 | "Write code that:\n", 326 | "* Align the names to the left, and the scores to the right\n", 327 | "* Allocate 10 characters for the name, and 5 characters for the score (5 for the integer, one for the decimal point, and 1 for decimal number)\n", 328 | "\n", 329 | "We want the outcome to look like this:\n", 330 | "\n", 331 | "```\n", 332 | "Name Score\n", 333 | "---- -----\n", 334 | "Beth 10.0\n", 335 | "Frederick 8.5\n", 336 | "Panos 7.1\n", 337 | "``` \n" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": null, 343 | "metadata": {}, 344 | "outputs": [], 345 | "source": [ 346 | "name1 = \"Beth\"\n", 347 | "name2 = \"Frederick\"\n", 348 | "name3 = \"Panos\"\n", 349 | "score1 = 10.0\n", 350 | "score2 = 8.51324\n", 351 | "score3 = 7.12321\n", 352 | "template_header = \"{name:10s}\\t{score:>7s}\"\n", 353 | "template_row = \"{name:10s}\\t{score:7.1f}\"\n", 354 | "\n", 355 | "print(template_header.format(name=\"NAME\", score=\"SCORE\"))\n", 356 | "print(template_header.format(name=\"----\", score=\"-----\"))\n", 357 | "print(template_row.format(name=name1, score=score1))\n", 358 | "print(template_row.format(name=name2, score=score2))\n", 359 | "print(template_row.format(name=name3, score=score3))" 360 | ] 361 | }, 362 | { 363 | "cell_type": "markdown", 364 | "metadata": { 365 | "solution2": "hidden", 366 | "solution2_first": true 367 | }, 368 | "source": [ 369 | "### Solution" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": null, 375 | "metadata": { 376 | "solution2": "hidden" 377 | }, 378 | "outputs": [], 379 | "source": [ 380 | "# Solution; do not use this before trying your own solution\n", 381 | "\n", 382 | "name1 = \"Beth\"\n", 383 | "name2 = \"Frederick\"\n", 384 | "name3 = \"Panos\"\n", 385 | "score1 = 10.0\n", 386 | "score2 = 8.51324\n", 387 | "score3 = 7.12321\n", 388 | "# Different formatting for headers and the data rows\n", 389 | "# since we cannot apply floating point formatting to the \n", 390 | "# strings in the header\n", 391 | "template_header = \"{name:10s}\\t{score:>7s}\"\n", 392 | "template_row = \"{name:10s}\\t{score:7.1f}\"\n", 393 | "# Print the header lines with the header template\n", 394 | "print(template_header.format(name=\"NAME\", score=\"SCORE\"))\n", 395 | "print(template_header.format(name=\"----\", score=\"-----\"))\n", 396 | "# Print the data lines with the data template\n", 397 | "print(template_row.format(name=name1, score=score1))\n", 398 | "print(template_row.format(name=name2, score=score2))\n", 399 | "print(template_row.format(name=name3, score=score3))" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": null, 405 | "metadata": { 406 | "solution2": "hidden" 407 | }, 408 | "outputs": [], 409 | "source": [ 410 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name1, score=score1))\n", 411 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name2, score=score2))\n", 412 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name3, score=score3))" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": null, 418 | "metadata": { 419 | "solution2": "hidden" 420 | }, 421 | "outputs": [], 422 | "source": [ 423 | "# Notice that we cannot use the :7.1f formatting \n", 424 | "# for the \"score\" variable, when \"score\" is a string\n", 425 | "# print(\"{name:10s}\\t{score:7.1f}\".format(name=\"NAME\", score=\"SCORE\"))\n", 426 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name1, score=score1))\n", 427 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name2, score=score2))\n", 428 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name3, score=score3))" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": null, 434 | "metadata": { 435 | "solution2": "hidden" 436 | }, 437 | "outputs": [], 438 | "source": [ 439 | "print(\"{name:10s}\\t{score:>7s}\".format(name=\"NAME\", score=\"SCORE\"))\n", 440 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name1, score=score1))\n", 441 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name2, score=score2))\n", 442 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name3, score=score3))" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": null, 448 | "metadata": { 449 | "solution2": "hidden" 450 | }, 451 | "outputs": [], 452 | "source": [ 453 | "print(\"{name:10s}\\t{score:>7s}\".format(name=\"NAME\", score=\"SCORE\"))\n", 454 | "print(\"{name:10s}\\t{score:>7s}\".format(name=\"----\", score=\"-----\"))\n", 455 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name1, score=score1))\n", 456 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name2, score=score2))\n", 457 | "print(\"{name:10s}\\t{score:7.1f}\".format(name=name3, score=score3))" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": null, 463 | "metadata": { 464 | "solution2": "hidden" 465 | }, 466 | "outputs": [], 467 | "source": [ 468 | "template_header = \"{name:10s}\\t{score:>7s}\"\n", 469 | "template_row = \"{name:10s}\\t{score:7.1f}\"" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": null, 475 | "metadata": { 476 | "solution2": "hidden" 477 | }, 478 | "outputs": [], 479 | "source": [ 480 | "print(template_header.format(name=\"NAME\", score=\"SCORE\"))\n", 481 | "print(template_header.format(name=\"----\", score=\"-----\"))\n", 482 | "print(template_row.format(name=name1, score=score1))\n", 483 | "print(template_row.format(name=name2, score=score2))\n", 484 | "print(template_row.format(name=name3, score=score3))" 485 | ] 486 | } 487 | ], 488 | "metadata": { 489 | "kernelspec": { 490 | "display_name": "Python 3", 491 | "language": "python", 492 | "name": "python3" 493 | }, 494 | "language_info": { 495 | "codemirror_mode": { 496 | "name": "ipython", 497 | "version": 3 498 | }, 499 | "file_extension": ".py", 500 | "mimetype": "text/x-python", 501 | "name": "python", 502 | "nbconvert_exporter": "python", 503 | "pygments_lexer": "ipython3", 504 | "version": "3.6.6" 505 | } 506 | }, 507 | "nbformat": 4, 508 | "nbformat_minor": 1 509 | } 510 | -------------------------------------------------------------------------------- /notes/H-Dictionaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Dictionaries" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Dictionaries are data structures containing key-value pairs. \n", 15 | "\n", 16 | "Dictionaries have a set of unique keys and are used to retrieve the value information associated with these keys. \n", 17 | "\n", 18 | "For instance, a dictionary might be used to store:\n", 19 | "* for each user (the key), that user's location (the value), or \n", 20 | "* for each product id (the key), the description associated with that product (the value). \n", 21 | "\n", 22 | "Dictionaries are very common and are frequently used and encountered in practice. " 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": { 28 | "heading_collapsed": true 29 | }, 30 | "source": [ 31 | "## Creating Dictionaries" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "hidden": true 38 | }, 39 | "source": [ 40 | "Dictionaries are specified by curly braces, `{ }`, containing zero or more comma-separated key-value pairs. In each key-value pair the keys and values are separated by a colon, `:`." 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "metadata": { 47 | "hidden": true 48 | }, 49 | "outputs": [], 50 | "source": [ 51 | "# Dictionary with four key value pairs\n", 52 | "a_dict = {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}\n", 53 | "\n", 54 | "# The a, b, c, d are keys\n", 55 | "# The 1, 2, 3, 4 are values\n", 56 | "print(a_dict)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": { 63 | "hidden": true 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "# A key cannot be repeated\n", 68 | "# See what happens when we repeat the key \"c\"\n", 69 | "a_dict = {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4, \"c\": 4}\n", 70 | "print(a_dict)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": { 76 | "hidden": true 77 | }, 78 | "source": [ 79 | "Here is a more realistic dictionary. It contains three keys, \"Panos\", \"Maria\", and \"John\". Each of these keys has a value associated with it, which in the case below corresponds to a phone number." 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": { 86 | "hidden": true 87 | }, 88 | "outputs": [], 89 | "source": [ 90 | "phones = {\n", 91 | " \"Panos\": \"212-998-0803\",\n", 92 | " \"Maria\": \"656-233-5555\",\n", 93 | " \"John\": \"693-232-5776\",\n", 94 | "}\n", 95 | "\n", 96 | "print(phones)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": { 102 | "hidden": true 103 | }, 104 | "source": [ 105 | "And here is another dictionary, with three keys, \"ip\", \"logitude\", and \"latitude\", which capture an IP address and its geolocation." 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": { 112 | "hidden": true 113 | }, 114 | "outputs": [], 115 | "source": [ 116 | "geoip = {\"longitude\": -73.9885, \"latitude\": 40.7317, \"ip\": \"216.165.95.68\"}\n", 117 | "\n", 118 | "print(geoip)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": { 124 | "heading_collapsed": true 125 | }, 126 | "source": [ 127 | "## Accessing Dictionary Elements" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": { 133 | "hidden": true 134 | }, 135 | "source": [ 136 | "To access elements in the dictionary we use the key in brackets, or the `get()` command, as follows:" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": { 143 | "hidden": true 144 | }, 145 | "outputs": [], 146 | "source": [ 147 | "print(geoip[\"ip\"])" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": { 154 | "hidden": true 155 | }, 156 | "outputs": [], 157 | "source": [ 158 | "# or, alternatively\n", 159 | "print(geoip.get(\"ip\"))" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": { 166 | "hidden": true 167 | }, 168 | "outputs": [], 169 | "source": [ 170 | "print(phones[\"Panos\"])" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": { 177 | "hidden": true 178 | }, 179 | "outputs": [], 180 | "source": [ 181 | "# or, alternatively\n", 182 | "print(phones.get(\"Panos\"))" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": { 188 | "hidden": true 189 | }, 190 | "source": [ 191 | "### Adding new entries, updating existing ones, deleting entries\n", 192 | "\n", 193 | "We can add an entry in the dictionary by assigning a value to a particular key. If the key already exists, the value assigned to that key gets updatd." 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": { 200 | "hidden": true 201 | }, 202 | "outputs": [], 203 | "source": [ 204 | "# Add a new key, \"isp\", with value \"New York University\"\n", 205 | "geoip[\"isp\"] = \"New York University\"\n", 206 | "print(geoip)" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": { 213 | "hidden": true 214 | }, 215 | "outputs": [], 216 | "source": [ 217 | "# Update the valye for \"John\"\n", 218 | "phones[\"John\"] = \"415-794-3423\"\n", 219 | "# Add a new key, \"Elena\", and the corresponding value\n", 220 | "phones[\"Elena\"] = \"212-998-0803\"\n", 221 | "print(phones)" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": { 227 | "hidden": true 228 | }, 229 | "source": [ 230 | "If we want to remove a key `x` from the dictionary, the command `dict.pop(x)` removes the key `x` and its associated value from the dictionary" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": { 237 | "hidden": true 238 | }, 239 | "outputs": [], 240 | "source": [ 241 | "# Remove John from the phones dictionary\n", 242 | "phones.pop(\"John\")\n", 243 | "print(phones)" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": { 249 | "heading_collapsed": true 250 | }, 251 | "source": [ 252 | "## Checking if a key appears in a dictionary\n", 253 | "\n" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": { 259 | "hidden": true 260 | }, 261 | "source": [ 262 | "Like the set, the easiest way to check if a particular **key** is in a dictionary is through the `in` keyword:" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": { 269 | "hidden": true 270 | }, 271 | "outputs": [], 272 | "source": [ 273 | "\"Panos\" in phones" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "metadata": { 280 | "hidden": true 281 | }, 282 | "outputs": [], 283 | "source": [ 284 | "\"Jose\" in phones" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": { 290 | "hidden": true 291 | }, 292 | "source": [ 293 | "Notice that the `in` will not work if we try to find a value in the dictionary." 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": { 300 | "hidden": true 301 | }, 302 | "outputs": [], 303 | "source": [ 304 | "# The in does *not* work for values\n", 305 | "\"212-998-0803\" in phones" 306 | ] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "metadata": { 311 | "heading_collapsed": true 312 | }, 313 | "source": [ 314 | "## Accessing keys and values" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": { 320 | "hidden": true 321 | }, 322 | "source": [ 323 | "Some common operations on dictionaries:\n", 324 | "\n", 325 | "+ `dict.keys()`: returns a list containing the keys of a dictionary\n", 326 | "+ `dict.values()`: returns a list containing the values in a dictionary" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": null, 332 | "metadata": { 333 | "hidden": true 334 | }, 335 | "outputs": [], 336 | "source": [ 337 | "phones = {\n", 338 | " \"Panos\": \"212-998-0803\",\n", 339 | " \"Maria\": \"656-233-5555\",\n", 340 | " \"John\": \"693-232-5776\",\n", 341 | " \"Jake\": \"415-794-3423\",\n", 342 | "}" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": { 349 | "hidden": true 350 | }, 351 | "outputs": [], 352 | "source": [ 353 | "phones.keys()" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": null, 359 | "metadata": { 360 | "hidden": true 361 | }, 362 | "outputs": [], 363 | "source": [ 364 | "sorted(phones.keys())" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": null, 370 | "metadata": { 371 | "hidden": true 372 | }, 373 | "outputs": [], 374 | "source": [ 375 | "phones.values()" 376 | ] 377 | }, 378 | { 379 | "cell_type": "markdown", 380 | "metadata": { 381 | "heading_collapsed": true 382 | }, 383 | "source": [ 384 | "## Exercise\n", 385 | "\n" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": { 391 | "hidden": true 392 | }, 393 | "source": [ 394 | "* Find the common keys in `a_dict` and `b_dict`\n", 395 | "* Find the common values in `a_dict` and `b_dict` \n" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": null, 401 | "metadata": { 402 | "hidden": true 403 | }, 404 | "outputs": [], 405 | "source": [ 406 | "a_dict = {\"a\": 5, \"b\": 5, \"c\": 3, \"c\": 4}\n", 407 | "b_dict = {\"c\": 5, \"d\": 6}\n", 408 | "\n", 409 | "# your code here" 410 | ] 411 | }, 412 | { 413 | "cell_type": "markdown", 414 | "metadata": { 415 | "hidden": true, 416 | "solution2": "hidden", 417 | "solution2_first": true 418 | }, 419 | "source": [ 420 | "### Solution" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": null, 426 | "metadata": { 427 | "hidden": true, 428 | "solution2": "hidden" 429 | }, 430 | "outputs": [], 431 | "source": [ 432 | "# Lets find the common keys first\n", 433 | "\n", 434 | "# Extract the keys from each dictionary\n", 435 | "keys_a = a_dict.keys()\n", 436 | "keys_b = b_dict.keys()" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": null, 442 | "metadata": { 443 | "hidden": true, 444 | "solution2": "hidden" 445 | }, 446 | "outputs": [], 447 | "source": [ 448 | "# Then compute the intersection\n", 449 | "# Keys are guaranteed to be unique, so the dict_keys\n", 450 | "# behaves like a set, and supports set operations\n", 451 | "common_keys = keys_a & keys_b\n", 452 | "print(\"Common keys\", common_keys)" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": null, 458 | "metadata": { 459 | "hidden": true, 460 | "solution2": "hidden" 461 | }, 462 | "outputs": [], 463 | "source": [ 464 | "# Now let's repeat the process for values\n", 465 | "values_a = a_dict.values()\n", 466 | "values_b = b_dict.values()" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": null, 472 | "metadata": { 473 | "hidden": true, 474 | "solution2": "hidden" 475 | }, 476 | "outputs": [], 477 | "source": [ 478 | "# However, trying to compute the intersection of values\n", 479 | "# will not work if we try to apply it naively.\n", 480 | "# The values_a and values_b are not like sets, as\n", 481 | "# they can contain duplicate values\n", 482 | "\n", 483 | "# Uncomment the code below to try. You will get\n", 484 | "# \"TypeError: unsupported operand type(s) for &: 'dict_values' and 'dict_values'\"\n", 485 | "#\n", 486 | "# values_a & values_b" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": null, 492 | "metadata": { 493 | "hidden": true, 494 | "solution2": "hidden" 495 | }, 496 | "outputs": [], 497 | "source": [ 498 | "# Instead, we have to convert the values_a and values_b\n", 499 | "# variables into sets first, and then compute the intersection\n", 500 | "common_values = set(values_a) & set(values_b)\n", 501 | "\n", 502 | "print(\"Common values\", common_values)" 503 | ] 504 | } 505 | ], 506 | "metadata": { 507 | "kernelspec": { 508 | "display_name": "Python 3", 509 | "language": "python", 510 | "name": "python3" 511 | }, 512 | "language_info": { 513 | "codemirror_mode": { 514 | "name": "ipython", 515 | "version": 3 516 | }, 517 | "file_extension": ".py", 518 | "mimetype": "text/x-python", 519 | "name": "python", 520 | "nbconvert_exporter": "python", 521 | "pygments_lexer": "ipython3", 522 | "version": "3.8.2" 523 | } 524 | }, 525 | "nbformat": 4, 526 | "nbformat_minor": 1 527 | } 528 | -------------------------------------------------------------------------------- /notes/J1-Iteration-While_loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Iterations: While statement\n", 8 | "\n", 9 | "We've spent some time going into detail about some of the data types and structures available in python. It's now time to talk about how to navigate through some of this data, and use data to make decisions. \n", 10 | "\n", 11 | "Traversing over data and making decisions based upon data are a common aspect of every programming language. To understand how to do this, we will study the concept of _iteration_. Iterations allow us to execute the same task multiple times, and is a key programming concept.\n" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": null, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "# This is just to use the time.sleep function\n", 21 | "# We will use the time.sleep function to slow\n", 22 | "# down the execution of loops, only for\n", 23 | "# instructional purposes. In reality, we add\n", 24 | "# such delays only in special cases.\n", 25 | "import time" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "## Introduction\n", 33 | "\n" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "The `while` statement provides a general way for creating iterative code execution. \n", 41 | "\n", 42 | "As in the case of the `if` statement, the `while` statement uses a boolean expression that controls the flow of execution. \n", 43 | "\n", 44 | "The difference is that, when we use the `if` statement, the nested code in executed only once; when we use the `while` statement, the nested code keeps executing, as long as the boolean expression is `True`.\n", 45 | "\n", 46 | "For example, the following example counts until `i` becomes larger than 5" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "i = 0\n", 56 | "while i <= 5: # check if i<=5. If yes, execute the nested block of code\n", 57 | " print(i) # print the current value of i\n", 58 | " time.sleep(1.0) # Wait for 1 sec\n", 59 | " i = i + 1 # increase i\n", 60 | " # Now go to the beginning of the loop" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "## Formal description of a while statement" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "\n", 75 | "\n", 76 | "More formally, here is the flow of execution for a `while` statement:\n", 77 | "\n", 78 | "\n", 79 | "\n", 80 | "1. Evaluate the condition, yielding `False` or `True`.\n", 81 | "2. If the condition is `False`, exit the `while` statement and continue execution at the next statement that is after the body of the while.\n", 82 | "3. If the condition is `True`, execute each of the statements in the body and then go back to step 1.\n", 83 | "\n", 84 | "The body consists of all of the statements below the header with the same indentation.\n", 85 | "\n", 86 | "\n", 87 | "\n", 88 | "This type of flow is called a **loop** because the third step loops back around to the top. Notice that if the condition is `False` the first time through the loop, the statements inside the loop are never executed." 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "## Example: Tea Cooling" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "Or consider the following example: The tea starts at 115 degrees Fahrenheit. You want it at 110 degrees. A chip of ice turns out to lower the temperature one degree every second. You test the temperature each time, and also print out the temperature before reducing the temperature. In Python you could write and run the code below:" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "temperature = 115\n", 112 | "while temperature > 110: # Execute the loop code if temperature > 110\n", 113 | " print(temperature)\n", 114 | " time.sleep(1.0) # Wait for 1 sec\n", 115 | " temperature = temperature - 1 # Decrease the value of temperature by one\n", 116 | " # Now go back to the beginning of the loop\n", 117 | "\n", 118 | "print(\"The tea is cool enough.\")" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "## Finite and Infinite Loops" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "Notice that the body of the loop should change the value of one or more variables so that eventually the condition becomes `False` and the loop terminates. If the condition never becomes False, the loop will repeat forever. Such type of a loop is called an **infinite loop**. " 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "And here is a simplified simulator that starts with an amount of money in the bank, and then examines the effect of withdrawing a specific amount of money per year, until you run out of money. Notice that the loop will keep running for ever, if you never withdraw more money than what you have. (We will deal with this issue next.)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "money_in_bank = 1000\n", 149 | "interest = 6\n", 150 | "year = 2017\n", 151 | "widthdrawal_per_period = 200\n", 152 | "\n", 153 | "while money_in_bank > 0:\n", 154 | " print(f\"At the beginning of {year} you have ${money_in_bank:.2f} in the bank.\")\n", 155 | " money_in_bank = money_in_bank - widthdrawal_per_period\n", 156 | " money_in_bank = money_in_bank * (1 + interest / 100)\n", 157 | " year = year + 1\n", 158 | " print(f\"At the end of {year} you have ${money_in_bank:.2f} in the bank.\")\n", 159 | " print(\"-----------------\")\n", 160 | "\n", 161 | "print(\"You have no money left!\")" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "## Exercise" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "\n", 176 | "\n", 177 | "The following code contains an infinite loop. \n", 178 | "\n", 179 | "```python \n", 180 | "n = 10\n", 181 | "answer = 1\n", 182 | "while n > 0:\n", 183 | " answer = answer + n\n", 184 | " n = n + 1\n", 185 | "print(answer)\n", 186 | "```\n", 187 | "\n", 188 | "Which is the best explanation for why the loop does not terminate?\n", 189 | "\n", 190 | "1. `n` starts at 10 and is incremented by 1 each time through the loop, so it will always be positive\n", 191 | "2. `answer` starts at 1 and is incremented by `n` each time, so it will always be positive\n", 192 | "3. You cannot compare `n` to 0 in `while` loop. You must compare it to another variable.\n", 193 | "4. In the `while` loop body, we must set `n` to `False`, and this code does not do that." 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "## Warning\n", 201 | "\n", 202 | "_You may have seen that Python’s `while` is very close to the English “while”. There is an important difference, though: In English “while X holds, do Y”, we usually assume that immediately after X becomes false, we stop with Y. In Python there is not an immediate stop: Even if the condition becomes false in the middle of the loop body, the rest of the loop body will still be executed, and the loop will stop only before the next iteration._\n", 203 | "\n", 204 | "\n" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "## `Break` and `Continue`\n", 212 | "\n", 213 | "\n", 214 | "\n" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "Let's discuss now two commands, `break` and `continue`, that allow us to control better the execution of code within a loop.\n", 222 | "\n", 223 | "These two statements are used to modify iteration of loops. `break` is used to *exit immediately* the *inner most _loop_* in which it appears. In contrast, `continue` stops the code executing within the loop and goes on to the *next iteration of the same loop*.\n" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "### Bank Account Example\n", 231 | "\n" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "For example, consider our example with the bank account. The loop will keep running for ever, if you never withdraw more money than what you have. \n", 239 | "\n", 240 | "To avoid this infinite loop, we can add an extra check in the code, checking if the year is above a certain limit, and stop execution of the loop at that point." 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": null, 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [ 249 | "money_in_bank = 1000\n", 250 | "interest = 6\n", 251 | "year = 2017\n", 252 | "\n", 253 | "# Try also the values 56.6 and 56.7 and see its behavior\n", 254 | "widthdrawal_per_period = 50\n", 255 | "\n", 256 | "while money_in_bank > 0:\n", 257 | " print(f\"At the beginning of {year} you have ${money_in_bank:.2f} in the bank\")\n", 258 | " money_in_bank = money_in_bank - widthdrawal_per_period\n", 259 | " money_in_bank = money_in_bank * (1 + interest / 100)\n", 260 | " year = year + 1\n", 261 | " if year > 2117:\n", 262 | " print(\"I am pretty sure you will not be alive by then\")\n", 263 | " break\n", 264 | " print(f\"At the end of {year} you have ${money_in_bank:.2f} in the bank\")\n", 265 | " print(\"-----------------\")\n", 266 | "\n", 267 | "print(\"You have no money left (or you are dead)!\")" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "### Tea Cooling Example\n", 275 | "\n", 276 | "Here is another example, this time with the `continue` command. In the example below, we modify our \"tea cooling\" example to print the temperature only when temperature is exactly divisible by 5. If not, we use the `continue` command to skip executing the rest of the loop." 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": null, 282 | "metadata": {}, 283 | "outputs": [], 284 | "source": [ 285 | "import time\n", 286 | "\n", 287 | "temperature = 133\n", 288 | "while temperature > 110: # first while loop code\n", 289 | " temperature = temperature - 1\n", 290 | " if temperature % 5 != 0: # If the temperature is not divisible by 5\n", 291 | " continue # We keep running the loop, but will not print\n", 292 | " # the temperature and have a delay\n", 293 | " time.sleep(0.5)\n", 294 | " print(temperature)\n", 295 | "\n", 296 | "print(\"The tea is cool enough.\")" 297 | ] 298 | }, 299 | { 300 | "cell_type": "markdown", 301 | "metadata": {}, 302 | "source": [ 303 | "## Exercise" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": {}, 309 | "source": [ 310 | "Write a program that receives user for three pieces of information as variables: \n", 311 | "* a starting balance, \n", 312 | "* a target balance, \n", 313 | "* and an interest rate (entered as 0.05 for 5%, for example)\n", 314 | "\n", 315 | "The program then outputs the number of years required for the starting balance to have grown larger than the target balance. While this can be computed directly mathematically, we want for this exercise to use a while loop to figure out the answer. The answer should just be a line stating something like: \"_To grow an initial investment of \\\\$1000 to \\\\$2000 at 5.0% will require `XX` years_\".\n", 316 | "\n", 317 | "Hint: You will need a variable to store the number of years that passed. You will also need a variable to hold the current balance in the account, as it grows over the years." 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [ 326 | "starting = 1000\n", 327 | "target = 2000\n", 328 | "interest = 0.05\n", 329 | "\n", 330 | "# your code here" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": { 336 | "solution2": "hidden", 337 | "solution2_first": true 338 | }, 339 | "source": [ 340 | "### Solution" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": null, 346 | "metadata": { 347 | "solution2": "hidden" 348 | }, 349 | "outputs": [], 350 | "source": [ 351 | "starting = 1000\n", 352 | "target = 2000\n", 353 | "interest = 0.045\n", 354 | "\n", 355 | "# We introduce a variable to store the current amount of money we have\n", 356 | "current = starting\n", 357 | "# We also introduce a variable year to count the number of years that passed\n", 358 | "year = 0\n", 359 | "\n", 360 | "# We will keep increasing the current balance, year after year, until\n", 361 | "# the value of current surpases the target\n", 362 | "while current < target:\n", 363 | " # We increase the value of current, by adding interest\n", 364 | " current = current * (1 + interest)\n", 365 | " # Alternatively, you can write the above as\n", 366 | " # current += current * interest\n", 367 | " # current = current + current * interest\n", 368 | " # current *= 1+interest\n", 369 | "\n", 370 | " # We increase the year value, as a year has passed\n", 371 | " year = year + 1\n", 372 | "\n", 373 | " # We also print a message with the current balance\n", 374 | " # as this can be useful for debugging\n", 375 | " print(f\"After {year} years, you have ${current:.2f}\")\n", 376 | "\n", 377 | "# We are out of the loop, let's print how long it took:\n", 378 | "print(\n", 379 | " f\"To grow an initial investment of ${starting} to ${target} at {100*interest:.1f}% will require {year} years\"\n", 380 | ")" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": {}, 387 | "outputs": [], 388 | "source": [] 389 | } 390 | ], 391 | "metadata": { 392 | "kernelspec": { 393 | "display_name": "Python 3", 394 | "language": "python", 395 | "name": "python3" 396 | }, 397 | "language_info": { 398 | "codemirror_mode": { 399 | "name": "ipython", 400 | "version": 3 401 | }, 402 | "file_extension": ".py", 403 | "mimetype": "text/x-python", 404 | "name": "python", 405 | "nbconvert_exporter": "python", 406 | "pygments_lexer": "ipython3", 407 | "version": "3.8.2" 408 | } 409 | }, 410 | "nbformat": 4, 411 | "nbformat_minor": 1 412 | } 413 | -------------------------------------------------------------------------------- /notes/J3-Iteration-Common_loop_patterns.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Common Loop Patterns\n", 8 | "\n", 9 | "When we use loops, there are often a set of common patterns that we use. For example, we may go through all the elements of a list and find the items with the highest value. Or do something with every element of the list, and store the result in another.\n", 10 | "\n", 11 | "Below, we will discuss a few of these common patterns. Most of these patterns have the following structure:\n", 12 | "\n", 13 | "* Initialize one or more variables before the loop starts\n", 14 | "* Perform some computation on each item in the loop body, possibly changing the variables in the body of the loop\n", 15 | "* Look at the resulting variables when the loop completes" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "## Summing Elements" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "Let's say now that we want to sum all the elements in a list. \n", 30 | "\n", 31 | "Here is an example list:" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "numbers = [40, 27, 50, 15, 32, 16, 31, 38, 45, 33]" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "We could of course use the `sum()` function," 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "sum(numbers)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | " but let's write this as a loop, to understand the pattern.\n", 64 | "\n", 65 | "We need to keep the sum in a variable, so let's introduce a variable, say `result` (don't call it `sum`, as that is the name of the function.)\n", 66 | "\n", 67 | "Initially we set the result to 0. Then every time we encounter a new element, we increase the value of the result by the corresponding amount. " 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "result = 0\n", 77 | "\n", 78 | "for num in numbers:\n", 79 | " print(\"Processing number:\", num)\n", 80 | " result += num\n", 81 | " print(\"The current sum is\", result)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## Finding the Maximum" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "Let's see an example now, where we have a list of positive numbers and we want to find the largest one in the list.\n", 96 | "\n", 97 | "Here is an example list:" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "numbers = [40, 27, 50, 15, 32, 16, 31, 38, 45, 33]" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "Of course, in most cases, to find the maximum element we can just write" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "max(numbers)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "but we want now to see how we can find the maximum using a `for` loop.\n", 130 | "\n", 131 | "The first thing that we do is to create a variable (say, `maximum`) that contains the largest value we have seen so far.\n", 132 | "\n", 133 | "Initially, we can set it to -1, which is by definition smaller than any of the numbers that we will deal with (as we will only deal with positive numbers).\n", 134 | "\n", 135 | "Then, we go through all the numbers, using a for loop. If we find a number that is greater than the current `maximum` we change the value of maximum to be the new largest number that we encountered." 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "numbers = [40, 27, 50, 15, 32, 16, 31, 38, 45, 33]\n", 145 | "\n", 146 | "maximum = -1\n", 147 | "for num in numbers: \n", 148 | " print(\"Processing number:\", num)\n", 149 | " if num > maximum:\n", 150 | " maximum = num\n", 151 | " print(\"We found a new maximum:\", maximum)\n", 152 | " print(\"The current maximum is:\", maximum)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "Now, notice that our code relies on the assumption that all elements are going to be positive. So, setting the maximum initially to -1 works.\n", 160 | "\n", 161 | "However, what happens if the assumption does not hold? Let's say that we want to find the smallest number in a list. \n", 162 | "\n", 163 | "Let's see how we can do that." 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "## Finding the Minimum" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "Let's change now the problem, and try to find the minimum. We will take our prior code and change `maximum` to `minimum` and then change the comparison from ` if num > maximum:` to `if num