├── .gitignore ├── LICENSE ├── README.md ├── session_eight ├── .gitignore ├── resources │ ├── heights.csv │ ├── passcode.txt │ └── the_joke.txt ├── session_eight_blank_template.ipynb ├── session_eight_exercises.ipynb ├── session_eight_filled_template.ipynb ├── session_eight_presentation.pptx └── session_eight_solutions.ipynb ├── session_five ├── session_five_blank_template.ipynb ├── session_five_exercises.ipynb ├── session_five_filled_template.ipynb ├── session_five_presentation.pptx └── session_five_solutions.ipynb ├── session_four ├── session_four_blank_template.ipynb ├── session_four_exercises.ipynb ├── session_four_filled_template.ipynb ├── session_four_presentation.pptx └── session_four_solutions.ipynb ├── session_one ├── session_one_blank_template.ipynb ├── session_one_exercises.ipynb ├── session_one_filled_template.ipynb ├── session_one_presentation.pptx └── session_one_solutions.ipynb ├── session_seven ├── session_seven_additional_notes.ipynb ├── session_seven_blank_template.ipynb ├── session_seven_exercises.ipynb ├── session_seven_filled_template.ipynb ├── session_seven_presentation.pptx └── session_seven_solutions.ipynb ├── session_six ├── session_six_blank_template.ipynb ├── session_six_exercises.ipynb ├── session_six_filled_template.ipynb ├── session_six_presentation.pptx └── session_six_solutions.ipynb ├── session_three ├── session_three_blank_template.ipynb ├── session_three_exercises.ipynb ├── session_three_filled_template.ipynb ├── session_three_presentation.pptx └── session_three_solutions.ipynb └── session_two ├── session_two_blank_template.ipynb ├── session_two_exercises.ipynb ├── session_two_filled_template.ipynb ├── session_two_presentation.pptx └── session_two_solutions.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | */.ipynb_checkpoints 2 | ~$* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Tim Hargreaves, Sam Bradley, David McKinney, Ryan Whittaker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Beginner's Python 2 | 3 | _Session material and solutions for the Beginner's Python course at the AstraZeneca Code Club_ 4 | 5 | Spotted a mistake, dead link, or have suggestions for improvements? Report these [here](https://github.com/THargreaves/beginners-python/issues/new) 6 | 7 | Navigation: 8 | 9 | * [Introduction](#introduction) 10 | * [Using This Repository](#using-this-repository) 11 | * [Quick Access to Resources](#quick-access-to-resources) 12 | * [Session Summaries](#session-summaries) 13 | * [Getting Help](#getting-help) 14 | * [Acknowledgements](#acknowledgements) 15 | 16 | ## Introduction 17 | 18 | AstraZeneca Code Club's Beginner's Python course is designed for anyone completely new to Python or even programming in general. The course is self-contained and approaches the language in a slow and friendly manner, assuming no prior knowledge. All of our resources are free to be used by anyone—AZ employee or not. If you are an employee of AZ however, make sure you follow the Code Club group on Workplace for updates, support, and general Python fun. 19 | 20 | Have fun learning! 21 | 22 | ## Getting Started 23 | 24 | ### Using Google Colab (Recommended) 25 | 26 | To make learing Python as simple as possible, we have centred our course around the use of Google Colab, a free, interactive cloud environment for Python programming. You can get setup with Colab by visiting [this link](https://colab.research.google.com/) or searching for Google Colab (using Google Chrome, as this has the best compatibility, unsurprisingly). Sign up using a Google account and get started by going to `File > New Python 3 Notebook`. 27 | 28 | All of our exercise and solution sheets also have links to open them in Colab. Once you are finished with your work, save these to your own drive using `File > Save a Copy in Drive`. 29 | 30 | ### Using Anaconda Locally (Optional) 31 | 32 | If you wish to avoid using Google Colab or want to code without an internet connection, you can setup up Anaconda on your computer to obtain a similiar, but local, experience. If you are on an AZ computer, use the software store to download this, and for personal computers visit [this link](https://www.anaconda.com/distribution/). Once you have Anaconda installed, open the Anaconda Navigator (this is just a normal program you'll find in your start menu). Then, launch 'Jupyter Notebook' and click `New > Python 3 Notebook`. This will create a blank Python 3 notebook just like with Colab. You can still use our templates by downloading the raw file from GitHub, placing it in your documents, and navigating to it in Jupyter. 33 | 34 | ## Using This Repository 35 | 36 | Each session is contained in its own folder and includes the Powerpoint presentation given in the session, an answer template for the session exercises, a homework sheet to practice your skills, as well as corresponding solutions. All exercise and solutions notebooks will contain a link at the top of the page to open the document in Google Colab 37 | 38 | The project solution is intended to be a model solution containing full documentation and testing. It is not expected for you to add such additional components to your solution but rather you should focus on the essential parts. A full solution is only provided as indication of the direction we want to be heading in and to build up experience with looking at source code. 39 | 40 | ## Quick Access to Resources 41 | 42 | We are still working on transferring the course to this repo, improving and standardising the content in the process. Links will appear here as soon as they are ready (which hopefully won't be long). If you want to be updated when changes are made, sign up to GitHub and click the watch icon at the top of the page (whilst you're at it, stars are appreciated!). 43 | 44 | ### Presentations 45 | 46 | * Session One [Presentation](https://github.com/THargreaves/beginners-python/blob/master/session_one/session_one_presentation.pptx?raw=true) 47 | * Session Two [Presentation](https://github.com/THargreaves/beginners-python/blob/master/session_two/session_two_presentation.pptx?raw=true) 48 | * Session Three [Presentation](https://github.com/THargreaves/beginners-python/blob/master/session_three/session_three_presentation.pptx?raw=true) 49 | * Session Four [Presentation](https://github.com/THargreaves/beginners-python/blob/master/session_four/session_four_presentation.pptx?raw=true) 50 | * Session Five [Presentation](https://github.com/THargreaves/beginners-python/blob/master/session_five/session_five_presentation.pptx?raw=true) 51 | * Session Six [Presentation](https://github.com/THargreaves/beginners-python/blob/master/session_six/session_six_presentation.pptx?raw=true) 52 | * Session Seven [Presentation](https://github.com/THargreaves/beginners-python/blob/master/session_seven/session_seven_presentation.pptx?raw=true) and [Additional Notes](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_seven/session_seven_additional_notes.ipynb) 53 | * Session Eight [Presentation](https://github.com/THargreaves/beginners-python/blob/master/session_eight/session_eight_presentation.pptx?raw=true) 54 | 55 | ### Templates 56 | 57 | * Session One [Blank Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_one/session_one_blank_template.ipynb) and [Filled Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_one/session_one_filled_template.ipynb) 58 | * Session Two [Blank Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_two/session_two_blank_template.ipynb) and [Filled Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_two/session_two_filled_template.ipynb) 59 | * Session Three [Blank Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_three/session_three_blank_template.ipynb) and [Filled Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_three/session_three_filled_template.ipynb) 60 | * Session Four [Blank Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_four/session_four_blank_template.ipynb) and [Filled Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_four/session_four_filled_template.ipynb) 61 | * Session Five [Blank Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_five/session_five_blank_template.ipynb) and [Filled Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_five/session_five_filled_template.ipynb) 62 | * Session Six [Blank Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_six/session_six_blank_template.ipynb) and [Filled Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_six/session_six_filled_template.ipynb) 63 | * Session Seven [Blank Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_seven/session_seven_blank_template.ipynb) and [Filled Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_seven/session_seven_filled_template.ipynb) 64 | * Session Eight [Blank Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_eight/session_eight_blank_template.ipynb) and [Filled Template](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_eight/session_eight_filled_template.ipynb) 65 | 66 | ### Homework 67 | 68 | * Session One [Exercises](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_one/session_one_exercises.ipynb) and [Solutions](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_one/session_one_solutions.ipynb) 69 | * Session Two [Exercises](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_two/session_two_exercises.ipynb) and [Solutions](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_two/session_two_solutions.ipynb) 70 | * Session Three [Exercises](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_three/session_three_exercises.ipynb) and [Solutions](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_three/session_three_solutions.ipynb) 71 | * Session Four [Exercises](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_four/session_four_exercises.ipynb) and [Solutions](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_four/session_four_solutions.ipynb) 72 | * Session Five [Exercises](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_five/session_five_exercises.ipynb) and [Solutions](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_five/session_five_solutions.ipynb) 73 | * Session Six [Exercises](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_six/session_six_exercises.ipynb) and [Solutions](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_six/session_six_solutions.ipynb) 74 | * Session Seven [Exercises](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_seven/session_seven_exercises.ipynb) and [Solutions](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_seven/session_seven_solutions.ipynb) 75 | * Session Eight [Exercises](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_eight/session_eight_exercises.ipynb) and [Solutions](https://colab.research.google.com/github/THargreaves/beginners-python/blob/master/session_eight/session_eight_solutions.ipynb) 76 | 77 | ## Session Summaries 78 | 79 | ### Session 1 80 | 81 | * Get set up on Google Colab 82 | * Let Python 'talk' to you using the `print()` function 83 | * Use Python as a basic calculator 84 | * Give Python 'memory' using variables 85 | 86 | ### Session 2 87 | 88 | * 'Talk' back to Python using the `input()` function 89 | * Learn why variable types are important 90 | * Use lists to store multiple numbers 91 | * Compute attributes of lists such as total, length, minimum and maximum 92 | 93 | ### Session 3 94 | 95 | * Learn how to compare two difference numbers with comparison operators 96 | * Combine conditions using Boolean operators 97 | * Add logic to your code using `if`, `elif`, and `else` 98 | * Use `while` loops to repeatedly run blocks of code 99 | 100 | ### Session 4 101 | 102 | * Recap the use of lists in Python and use them to store more than just numbers 103 | * Learn how to access individual items of lists as well as extract sub-lists 104 | * Manipulate lists, adding, removing, and altering elements 105 | * Understand the difference and similarities between tuples and lists 106 | 107 | ### Session 5 108 | 109 | * Learn how to use `for` loops to run code a predetermined number of times 110 | * Loop through lists and ranges of numbers 111 | * Escape loops using `break` and `continue` 112 | * Learn how to enumerate loops 113 | 114 | ### Session 6 115 | 116 | * Create functions to modularise your code 117 | * Add function parameters 118 | * Apply recursive strategies to solve common problems 119 | * Understand the nature of variable scope 120 | 121 | ### Session 7 122 | 123 | * Learn the limitations of lists/tuples 124 | * Use dictionaries to overcome these 125 | * Manipulate dictionaries, as with lists 126 | * Loop through dictionaries in multiple ways 127 | 128 | ### Session 8 129 | 130 | * Read from a text file 131 | * Write text to a file 132 | * Learn how to raise errors 133 | * Use common built-in packages in your code 134 | 135 | ### What now? 136 | 137 | Checkout my other courses (written by me, in parallel with the AZ Code Club): 138 | 139 | * [Practical Python](https://github.com/THargreaves/practical-python) — learn more Python by applying your skills to fun, real-life problems. Prerequisite: Beginner's Python or equivalent 140 | * [Into the Tidyverse](https://github.com/THargreaves/into-the-tidyverse) — learn your way around the tidyverse, a collection of R packages desgined to make data importing, manipulation, and visualisation as simple as possible. No prerequisites. 141 | * SQL for Scientists (coming soon) 142 | 143 | ## Getting Help 144 | 145 | If you are internal to AstraZeneca your best port of call for recieving help with this course is via the Code Club workplace page. We're a friendly bunch so don't feel scared to post a question even if it seems simple—likely there is someone else with the same issue. 146 | 147 | Failing that, any of the [core contributers](#acknowledgements) will be happy to help or prompt you in the right direction. 148 | 149 | If you are external to AstraZenca, feel free to reach out to me on [LinkedIn](https://www.linkedin.com/in/tim-hargreaves/). 150 | 151 | On top of any support the AZ Code Club can offer, there are many open-source resources for learning Python. Here are some of our favourites: 152 | 153 | * [Learn Python](https://www.learnpython.org/) 154 | * [Python Cheat Sheets](https://ehmatthes.github.io/pcc/cheatsheets/README.html) 155 | * [Practice Python](https://www.practicepython.org/) 156 | * [Real Python](https://realpython.com/start-here/) 157 | * [GeeksforGeeks](https://www.geeksforgeeks.org/python-programming-language/) 158 | 159 | ## Acknowledgements 160 | 161 | The original contributers to this course are (in alphabetical order): 162 | 163 | * David McKinney 164 | * Mark Porter 165 | * Ryan Whittaker 166 | * Sam Bradley 167 | * Tim Hargreaves 168 | 169 | The current version of the course is a vast redesign of the original material, with the addition of homework, solution notebooks, and content for three additional weeks. This adaptation was made solely by [Tim Hargreaves]((https://www.linkedin.com/in/tim-hargreaves/)), who also manages this repository. 170 | 171 | -------------------------------------------------------------------------------- /session_eight/.gitignore: -------------------------------------------------------------------------------- 1 | countries.txt 2 | names.txt 3 | reversed_names.txt 4 | squares.txt 5 | 6 | -------------------------------------------------------------------------------- /session_eight/resources/heights.csv: -------------------------------------------------------------------------------- 1 | Ann, 156 2 | Bob, 183 3 | Cat, 165 4 | Dan, 171 -------------------------------------------------------------------------------- /session_eight/resources/passcode.txt: -------------------------------------------------------------------------------- 1 | never 2 | then 3 | children 4 | roll 5 | separate 6 | column 7 | slip 8 | meet 9 | song 10 | stream 11 | gonna 12 | wife 13 | brown 14 | north 15 | often 16 | front 17 | quart 18 | most 19 | brought 20 | lost 21 | give 22 | third 23 | wire 24 | and 25 | length 26 | subject 27 | ear 28 | arrange 29 | forest 30 | bat 31 | you 32 | name 33 | against 34 | order 35 | winter 36 | particular 37 | could 38 | thin 39 | food 40 | gather 41 | up 42 | final 43 | which 44 | proper 45 | notice 46 | shell 47 | there 48 | share 49 | ride 50 | test 51 | never 52 | event 53 | mother 54 | field 55 | which 56 | village 57 | sound 58 | rest 59 | sugar 60 | front 61 | gonna 62 | spell 63 | fun 64 | fill 65 | basic 66 | solve 67 | flow 68 | desert 69 | food 70 | back 71 | let 72 | favor 73 | seven 74 | eight 75 | if 76 | dear 77 | large 78 | eye 79 | begin 80 | death 81 | you 82 | power 83 | fell 84 | step 85 | doctor 86 | rose 87 | basic 88 | boy 89 | single 90 | hour 91 | down 92 | follow 93 | these 94 | determine 95 | bad 96 | cross 97 | cat 98 | sister 99 | glass 100 | story 101 | never 102 | consonant 103 | king 104 | tree 105 | saw 106 | quiet 107 | eight 108 | sand 109 | smile 110 | invent 111 | gonna 112 | million 113 | ready 114 | snow 115 | card 116 | include 117 | brown 118 | call 119 | my 120 | steam 121 | run 122 | gentle 123 | reach 124 | fact 125 | new 126 | knew 127 | unit 128 | through 129 | six 130 | me 131 | around 132 | father 133 | speed 134 | ask 135 | excite 136 | see 137 | spread 138 | gun 139 | piece 140 | clean 141 | and 142 | wild 143 | felt 144 | gun 145 | does 146 | edge 147 | teach 148 | raise 149 | done 150 | thick 151 | desert 152 | anger 153 | tree 154 | drop 155 | leave 156 | pair 157 | property 158 | help 159 | short 160 | wing 161 | you 162 | drop 163 | kill 164 | by 165 | space 166 | land 167 | path 168 | took 169 | fire 170 | fell 171 | string -------------------------------------------------------------------------------- /session_eight/resources/the_joke.txt: -------------------------------------------------------------------------------- 1 | The Funniest Joke in the World 2 | ------------------------------ 3 | Wenn ist das Nunstück git und Slotermeyer? 4 | Ja! 5 | Beiherhund das Oder die Flipperwaldt gersput! -------------------------------------------------------------------------------- /session_eight/session_eight_blank_template.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Eight Template" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "-Ceh1HnZIsVO" 29 | }, 30 | "source": [ 31 | "## Reading From a Text File" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "6AZ6H-6jKoSt" 39 | }, 40 | "source": [ 41 | "### Introduction\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "Read the contents of `the_joke.txt` and print it with correct formatting" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/", 57 | "height": 35 58 | }, 59 | "colab_type": "code", 60 | "id": "D4l_2DsZIlgB", 61 | "outputId": "e63ad5b1-2062-4d8c-9ee7-b03a25d72001" 62 | }, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": { 69 | "colab": { 70 | "base_uri": "https://localhost:8080/", 71 | "height": 35 72 | }, 73 | "colab_type": "code", 74 | "id": "9lLW387eKgNN", 75 | "outputId": "860dcf3b-f3e5-402d-f58f-4d28fb69a6b1" 76 | }, 77 | "source": [ 78 | "Read `the_joke.txt` into a list, one element for each line of the text" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/", 87 | "height": 35 88 | }, 89 | "colab_type": "code", 90 | "id": "bIr0l5kJI_VV", 91 | "outputId": "c7891804-4acf-4856-e51a-271914a8dc60" 92 | }, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "### Standard Puzzles" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "Print out every tenth line of the text file to obtain the passcode" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "## Reading Text Files Sequentially" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "### Introduction" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "Read `the_joke.txt` one line at a time, printing each out" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "### Standard Puzzles" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "Repeat the passcode exercise using sequential reading" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "Read only the title and first line from `the_joke.txt`" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": {}, 183 | "source": [ 184 | "## Writing to Text Files" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "### Introduction" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "Write to a text file using write mode" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "Write to a text file using append mode" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "### Standard Puzzles" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "Ask the user for their name and write this to a text file" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": null, 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "Ask the user for their favourite food and append this on a new line to the same text file" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "### Bonus Puzzles" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "What happens when you append to a file that doesn't exist?" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "## Raising Errors" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "### Introduction" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Create a function that accepts a positive integer and raise a `ValueError` if the argument is not positive" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "Test the `isinstance()` function" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "Test the `isnumeric()` and `isalpha()` methods" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": null, 342 | "metadata": {}, 343 | "outputs": [], 344 | "source": [] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": {}, 349 | "source": [ 350 | "Test the `in` keyword for strings" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": null, 356 | "metadata": {}, 357 | "outputs": [], 358 | "source": [] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "### Standard Puzzles" 365 | ] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "metadata": {}, 370 | "source": [ 371 | "Create the `print_sentence` function as specified in the presentation" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": null, 377 | "metadata": {}, 378 | "outputs": [], 379 | "source": [] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": {}, 384 | "source": [ 385 | "Create the `forgiving_double` function as specified in the presentation" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": null, 391 | "metadata": {}, 392 | "outputs": [], 393 | "source": [] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "## Built-in Modules" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "### Introduction" 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "metadata": {}, 412 | "source": [ 413 | "Import the `time` module to use the `time` function" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": null, 419 | "metadata": {}, 420 | "outputs": [], 421 | "source": [] 422 | }, 423 | { 424 | "cell_type": "markdown", 425 | "metadata": {}, 426 | "source": [ 427 | "Import the `randint` function from the `random` module and use it" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": null, 433 | "metadata": {}, 434 | "outputs": [], 435 | "source": [] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "Use `os.path.exists()` to test if a file exists and raise an error if not" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "metadata": {}, 448 | "outputs": [], 449 | "source": [] 450 | }, 451 | { 452 | "cell_type": "markdown", 453 | "metadata": {}, 454 | "source": [ 455 | "Import the `datetime` module as `dt` and use it to print the current date" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": null, 461 | "metadata": {}, 462 | "outputs": [], 463 | "source": [] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "Import the `normalvariate` function as `rnorm` and use it to print a random normal variable" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": null, 475 | "metadata": {}, 476 | "outputs": [], 477 | "source": [] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "metadata": {}, 482 | "source": [ 483 | "### Standard Puzzles" 484 | ] 485 | }, 486 | { 487 | "cell_type": "markdown", 488 | "metadata": {}, 489 | "source": [ 490 | "Create a guessing game" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": null, 496 | "metadata": {}, 497 | "outputs": [], 498 | "source": [] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "metadata": {}, 503 | "source": [ 504 | "Use the `combinations` function from `itertools` to list all possible pairs of the names Ann, Bob, and Cat" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": null, 510 | "metadata": {}, 511 | "outputs": [], 512 | "source": [] 513 | } 514 | ], 515 | "metadata": { 516 | "colab": { 517 | "name": "Code Club Session 1", 518 | "provenance": [] 519 | }, 520 | "kernelspec": { 521 | "display_name": "Python 3", 522 | "language": "python", 523 | "name": "python3" 524 | }, 525 | "language_info": { 526 | "codemirror_mode": { 527 | "name": "ipython", 528 | "version": 3 529 | }, 530 | "file_extension": ".py", 531 | "mimetype": "text/x-python", 532 | "name": "python", 533 | "nbconvert_exporter": "python", 534 | "pygments_lexer": "ipython3", 535 | "version": "3.7.3" 536 | } 537 | }, 538 | "nbformat": 4, 539 | "nbformat_minor": 1 540 | } 541 | -------------------------------------------------------------------------------- /session_eight/session_eight_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Eight Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "_As we are moving deeper into the world of Python, the tasks we can complete with our skills can become more and more complicated. For that reason, there are fewer exercises to complete this week but of a slightly harder standard. Don't be put off; these are meant to challenge you._" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## Reading From a Text File" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Download the text file linked [here](https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries), containing a list of of countries and upload this to Colab" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Import the file as a list" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "Create an empty dictionary `first_letter_counts` to keep track of how many times a country starts with each letter. Loop through the countries, extracting there first letter with `country_name[0]` to populate this dictionary" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "Read the file `heights.csv` from the this session's [resource folder](https://github.com/THargreaves/beginners-python/tree/master/session_eight/resources) into a list of lines. Loop through the each line and run `name, height = line.split(', ')` to extract each value. Use these to create a dictionary mapping people to their height (Note: you'll have to convert the height to an integer or float first)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "## Reading Text Files Sequentially" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "Read the first 100 names from [this](https://www.usna.edu/Users/cs/roche/courses/s15si335/proj1/files.php%3Ff=names.txt&downloadcode=yes) list of names. Append these to a list called `names`" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 10, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "## Writing to Text Files" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Take the names list above and loop through it, writing the reverse of each name to a new file `reversed_names.txt`. Remember, you can reverse a string using `my_string[::-1]`" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "Append the reverse of your name to this list" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "Create a list of square numbers called `squares`, stored as strings. Write this to `squares.txt` without using a loop by using `'\\n'.join(squares)`" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "## Raising Errors" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "Have a scan through [this list](https://www.tutorialsteacher.com/python/error-types-in-python) of the standard error types in Python. Remember, it is also possible to define your own errors, though this is out of scope for this course" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "Create a function called `contains_z()`. This should accept an argument, check that it's a string (else raise a `TypeError`) and then return whether the input contained the letter 'z'" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "Create a function `divide(a, b)` which divides two floats `a` by `b`, with `b` defaulting to one. First check that these are indeed both floats, else raise a `TypeError`. Then, check that `b` is non-zero else raise a `ValueError` with message \"cannot divide by zero\". If both these conditions are met, return `a / b`" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "## Built-in Modules" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "Use the `time` function from the `time` module to time how many seconds it takes to run the following function on your computer (hint: before running the function, store the starting time in a variable `start` and compare this to the time at the end)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 22, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [ 219 | "def long_running_func():\n", 220 | " n = 2\n", 221 | " for __ in range(10 ** 6):\n", 222 | " n = n ** 1000 % (10 * 9)" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": null, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "Search for the documentation on `random.choice`. Use this to select a random name from the list defined above" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "Import `random` from `random` as `rnd`. Use this to create a function `biased_coin_flip(p)` which simulates a coin having probablity $0 < p < 1$ of coming up heads. First check that `p` is a float and between zero and one. Then compare `rnd()` to `p`. If `rnd()` is the smaller of the two, return \"Heads\", else return \"Tails\"" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "Run the above functions a few times to verify that it is biased" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": null, 270 | "metadata": {}, 271 | "outputs": [], 272 | "source": [] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "Pat yourself on the back. You've made it to the end!" 279 | ] 280 | } 281 | ], 282 | "metadata": { 283 | "kernelspec": { 284 | "display_name": "Python 3", 285 | "language": "python", 286 | "name": "python3" 287 | }, 288 | "language_info": { 289 | "codemirror_mode": { 290 | "name": "ipython", 291 | "version": 3 292 | }, 293 | "file_extension": ".py", 294 | "mimetype": "text/x-python", 295 | "name": "python", 296 | "nbconvert_exporter": "python", 297 | "pygments_lexer": "ipython3", 298 | "version": "3.7.3" 299 | } 300 | }, 301 | "nbformat": 4, 302 | "nbformat_minor": 2 303 | } 304 | -------------------------------------------------------------------------------- /session_eight/session_eight_presentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/THargreaves/beginners-python/2fb59b3ae1518db129897019274a56b2e50d0ee5/session_eight/session_eight_presentation.pptx -------------------------------------------------------------------------------- /session_eight/session_eight_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Eight Homework Solutions" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "_As we are moving deeper into the world of Python, the tasks we can complete with our skills can become more and more complicated. For that reason, there are fewer exercises to complete this week but of a slightly harder standard. Don't be put off; these are meant to challenge you._" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## Reading From a Text File" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Download the text file linked [here](https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries), containing a list of of countries and upload this to Colab" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Import the file as a list" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 2, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "with open('countries.txt') as f:\n", 59 | " contents = f.read()\n", 60 | " countries = contents.split('\\n')" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "Create an empty dictionary `first_letter_counts` to keep track of how many times a country starts with each letter. Loop through the countries, extracting there first letter with `country_name[0]` to populate this dictionary" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 6, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "first_letter_counts = {}\n", 77 | "for country in countries:\n", 78 | " first_letter = country[0]\n", 79 | " if first_letter in first_letter_counts:\n", 80 | " first_letter_counts[first_letter] += 1\n", 81 | " else:\n", 82 | " first_letter_counts[first_letter] = 1" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "Read the file `heights.csv` from the this session's [resource folder](https://github.com/THargreaves/beginners-python/tree/master/session_eight/resources) into a list of lines. Loop through the each line and run `name, height = line.split(', ')` to extract each value. Use these to create a dictionary mapping people to their height (Note: you'll have to convert the height to an integer or float first)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 4, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "heights = {}\n", 99 | "with open('resources/heights.csv') as f:\n", 100 | " lines = f.read().split('\\n')\n", 101 | " for l in lines:\n", 102 | " name, height = l.split(', ')\n", 103 | " heights[name] = int(height)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "## Reading Text Files Sequentially" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "Read the first 100 names from [this](https://www.usna.edu/Users/cs/roche/courses/s15si335/proj1/files.php%3Ff=names.txt&downloadcode=yes) list of names. Append these to a list called `names`" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 13, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "names = []\n", 127 | "with open('names.txt') as f:\n", 128 | " for i in range(100):\n", 129 | " name = f.readline().replace('\\n', '')\n", 130 | " names.append(name)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "## Writing to Text Files" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "Take the names list above and loop through it, writing the reverse of each name to a new file `reversed_names.txt`. Remember, you can reverse a string using `my_string[::-1]`" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 14, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "with open('reversed_names.txt', 'w') as f:\n", 154 | " for name in names:\n", 155 | " f.write(name[::-1] + '\\n')" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "Append the reverse of your name to this list" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 15, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [ 171 | "with open('reversed_names.txt', 'w') as f:\n", 172 | " f.write('miT')" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "Create a list of square numbers called `squares`, stored as strings. Write this to `squares.txt` without using a loop by using `'\\n'.join(squares)`" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 17, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [ 188 | "squares = []\n", 189 | "for n in range(1, 10):\n", 190 | " squares.append(str(n ** 2))\n", 191 | "\n", 192 | "with open('squares.txt', 'w') as f:\n", 193 | " f.write('\\n'.join(squares))" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "## Raising Errors" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "Have a scan through [this list](https://www.tutorialsteacher.com/python/error-types-in-python) of the standard error types in Python. Remember, it is also possible to define your own errors, though this is out of scope for this course" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "Create a function called `contains_z()`. This should accept an argument, check that it's a string (else raise a `TypeError`) and then return whether the input contained the letter 'z'" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 21, 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "name": "stdout", 224 | "output_type": "stream", 225 | "text": [ 226 | "False\n", 227 | "True\n" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "def contains_z(text):\n", 233 | " if not isinstance(text, str):\n", 234 | " raise TypeError(\"input must be a string\")\n", 235 | " return 'z' in text or 'Z' in text\n", 236 | " # or: return 'z' in text.lower()\n", 237 | "\n", 238 | "print(contains_z('Apple'))\n", 239 | "print(contains_z('Zucchini'))" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "Create a function `divide(a, b)` which divides two floats `a` by `b`, with `b` defaulting to one. First check that these are indeed both floats, else raise a `TypeError`. Then, check that `b` is non-zero else raise a `ValueError` with message \"cannot divide by zero\". If both these conditions are met, return `a / b`" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 22, 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [ 255 | "def divide(a, b=1.0):\n", 256 | " if not isinstance(a, float) or not isinstance(b, float):\n", 257 | " raise TypeError(\"inputs must be floats\")\n", 258 | " if not b != 0:\n", 259 | " raise ValueError(\"cannot divide by zero\")\n", 260 | " return a / b" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "## Built-in Modules" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "Use the `time` function from the `time` module to time how many seconds it takes to run the following function on your computer (hint: before running the function, store the starting time in a variable `start` and compare this to the time at the end)" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 27, 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [ 283 | "def long_running_func():\n", 284 | " n = 2\n", 285 | " for __ in range(10 ** 6):\n", 286 | " n = n ** 1000 % (10 * 9)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 28, 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "name": "stdout", 296 | "output_type": "stream", 297 | "text": [ 298 | "Function ran in 7 seconds\n" 299 | ] 300 | } 301 | ], 302 | "source": [ 303 | "from time import time\n", 304 | "\n", 305 | "start = time()\n", 306 | "long_running_func()\n", 307 | "end = time()\n", 308 | "print(\"Function ran in\", \n", 309 | " round(end - start),\n", 310 | " \"seconds\")" 311 | ] 312 | }, 313 | { 314 | "cell_type": "markdown", 315 | "metadata": {}, 316 | "source": [ 317 | "Search for the documentation on `random.choice`. Use this to select a random name from the list defined above" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 29, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "name": "stdout", 327 | "output_type": "stream", 328 | "text": [ 329 | "Christine\n" 330 | ] 331 | } 332 | ], 333 | "source": [ 334 | "import random\n", 335 | "\n", 336 | "random_name = random.choice(names)\n", 337 | "print(random_name)" 338 | ] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": {}, 343 | "source": [ 344 | "Import `random` from `random` as `rnd`. Use this to create a function `biased_coin_flip(p)` which simulates a coin having probablity $0 < p < 1$ of coming up heads. First check that `p` is a float and between zero and one. Then compare `rnd()` to `p`. If `rnd()` is the smaller of the two, return \"Heads\", else return \"Tails\"" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 30, 350 | "metadata": {}, 351 | "outputs": [], 352 | "source": [ 353 | "from random import random as rnd\n", 354 | "\n", 355 | "def biased_coin_flip(p):\n", 356 | " if not isinstance(p, float):\n", 357 | " raise TypeError(\"p must be a float\")\n", 358 | " if not 0 < p < 1:\n", 359 | " raise ValueError(\"p must be between 0 and 1\")\n", 360 | " \n", 361 | " if rnd() < p:\n", 362 | " return \"Heads\"\n", 363 | " else:\n", 364 | " return \"Tails\"" 365 | ] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "metadata": {}, 370 | "source": [ 371 | "Run the above functions a few times to verify that it is biased" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": 34, 377 | "metadata": {}, 378 | "outputs": [ 379 | { 380 | "name": "stdout", 381 | "output_type": "stream", 382 | "text": [ 383 | "Heads\n", 384 | "Heads\n", 385 | "Tails\n", 386 | "Heads\n", 387 | "Heads\n", 388 | "Tails\n", 389 | "Heads\n", 390 | "Tails\n", 391 | "Heads\n", 392 | "Heads\n" 393 | ] 394 | } 395 | ], 396 | "source": [ 397 | "for i in range(10):\n", 398 | " print(biased_coin_flip(0.8))" 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "metadata": {}, 404 | "source": [ 405 | "Pat yourself on the back. You've made it to the end!" 406 | ] 407 | } 408 | ], 409 | "metadata": { 410 | "kernelspec": { 411 | "display_name": "Python 3", 412 | "language": "python", 413 | "name": "python3" 414 | }, 415 | "language_info": { 416 | "codemirror_mode": { 417 | "name": "ipython", 418 | "version": 3 419 | }, 420 | "file_extension": ".py", 421 | "mimetype": "text/x-python", 422 | "name": "python", 423 | "nbconvert_exporter": "python", 424 | "pygments_lexer": "ipython3", 425 | "version": "3.7.3" 426 | } 427 | }, 428 | "nbformat": 4, 429 | "nbformat_minor": 2 430 | } 431 | -------------------------------------------------------------------------------- /session_five/session_five_blank_template.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Five Template" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "-Ceh1HnZIsVO" 29 | }, 30 | "source": [ 31 | "## Looping Through a Range" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "6AZ6H-6jKoSt" 39 | }, 40 | "source": [ 41 | "### Introduction\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "Use a for loop to print the numbers zero to four" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/", 57 | "height": 35 58 | }, 59 | "colab_type": "code", 60 | "id": "D4l_2DsZIlgB", 61 | "outputId": "e63ad5b1-2062-4d8c-9ee7-b03a25d72001" 62 | }, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": { 69 | "colab": { 70 | "base_uri": "https://localhost:8080/", 71 | "height": 35 72 | }, 73 | "colab_type": "code", 74 | "id": "9lLW387eKgNN", 75 | "outputId": "860dcf3b-f3e5-402d-f58f-4d28fb69a6b1" 76 | }, 77 | "source": [ 78 | "Use a for loop to print the numbers two to five" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/", 87 | "height": 35 88 | }, 89 | "colab_type": "code", 90 | "id": "bIr0l5kJI_VV", 91 | "outputId": "c7891804-4acf-4856-e51a-271914a8dc60" 92 | }, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "### Standard Puzzles" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "Use a for loop to print the number -5 to 5, inclusive" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "Print the first ten square numbers" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "### Bonus Puzzles" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "Experiment with passing a third argument into `range(...)`. What does it do?" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Use this technique to count down from ten to zero" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": null, 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "## Looping Through a List" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "### Introduction" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "Create a shopping list and print out each item in a sentence" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "### Standard Puzzles" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "Calculate the product of the elements in a list (see the presentation for a similar example)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "Use a for loop to count how many positive elements a numeric list contains" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "## Early-stopping" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "### Introduction" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "Print the elements of a list until a negative number is encountered, at which point you should stop" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "Print the elements of a list, skipping any negative values" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": null, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "### Standard Puzzles" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "Create a list as specified in the presentation" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Loop through the list, printing only positive elements and halting when a negative entry is encountered" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": {}, 314 | "source": [ 315 | "### Bonus Puzzles" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "Setup up a nested for loop and attempt to break when a condition is met in the inner loop" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "## Enumeration" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "### Introduction" 344 | ] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": {}, 349 | "source": [ 350 | "Use enumeration to print out the elements of a list alongside their ordinal indices" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": null, 356 | "metadata": {}, 357 | "outputs": [], 358 | "source": [] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "### Standard Puzzles" 365 | ] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "metadata": {}, 370 | "source": [ 371 | "Define the list shown in the presentation" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": null, 377 | "metadata": {}, 378 | "outputs": [], 379 | "source": [] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": {}, 384 | "source": [ 385 | "Print whether each element is greater than, equal to, or less than its index" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": null, 391 | "metadata": {}, 392 | "outputs": [], 393 | "source": [] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "Use enumeration to print every other element of a list" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": null, 405 | "metadata": {}, 406 | "outputs": [], 407 | "source": [] 408 | } 409 | ], 410 | "metadata": { 411 | "colab": { 412 | "name": "Code Club Session 1", 413 | "provenance": [] 414 | }, 415 | "kernelspec": { 416 | "display_name": "Python 3", 417 | "language": "python", 418 | "name": "python3" 419 | }, 420 | "language_info": { 421 | "codemirror_mode": { 422 | "name": "ipython", 423 | "version": 3 424 | }, 425 | "file_extension": ".py", 426 | "mimetype": "text/x-python", 427 | "name": "python", 428 | "nbconvert_exporter": "python", 429 | "pygments_lexer": "ipython3", 430 | "version": "3.7.3" 431 | } 432 | }, 433 | "nbformat": 4, 434 | "nbformat_minor": 1 435 | } 436 | -------------------------------------------------------------------------------- /session_five/session_five_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Five Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Looping Through a Range" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Use a for loop with two arguments to print the first 10 multiples of 12" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Convert the following while loop into a for loop" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 1, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "5\n", 62 | "6\n", 63 | "7\n", 64 | "8\n", 65 | "9\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "i = 5\n", 71 | "while i < 10:\n", 72 | " print(i)\n", 73 | " i += 1" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "Loop through the first 100 integers, printing each number if and only if they are a multiple of 13 (remember use `%` for calculating a remainder—a number is divisable by $n$ whenever the remainder when dividing by $n$ is zero)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "What happens when you reverse the order of the two parameters in range so that the largest comes first?" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "How many numbers less than 1000 are multiples of one of 3 or 5? Use a for loop along with a variable `count`, initially set to zero, to find out" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": {}, 133 | "source": [ 134 | "Ask the user for an integer greater than one (remember to convert this to an integer). Print whether or not it is prime using the following (admittedly inefficient) approach:\n", 135 | "* Create a variable `prime`, initial set to `True`\n", 136 | "* Loop through the numbers 2 to one less than the user's input\n", 137 | "* For each number, check if this divides the user's input. If so, set `prime` to `False`\n", 138 | "* After the loop, print whether the number is prime or not using an if statement" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "## Looping Through a List" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "Create a list of words and loop through it. If a word is less than 6 letters, print '{word} is a short word' otherwise print '{word} is a long word', replacing '{word}' with the corresponding word" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "Loop through the following list, only printing animals that begin with the letter 'a' (remember you can access the letters of a string in the same way that you'd access the elements of a list)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 2, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "animals = ['anteater', 'bear', 'armadillo', \n", 183 | " 'cougar', 'aardvark', 'dear']" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 3, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "Create a list of numbers. Use a for list to create a new list which only contains the even numbers of the original (hint: you can use `.append()`)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "Without using `max()`, calculate the maximum number in a list. You can do this by using a variable `current_max` (initially set to the first value in the list) to keep track of the current largest value, updating whenever a larger value is found." 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "## Early-stopping" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "Loop through and print the numbers 3 to 7, skipping the number 4 using a `continue` statement" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "Loop through the following list, converting each value to an integer and appending it to a new list called `int_list`. Before converting, test if the value is numeric using `value.isnumeric()`. If this is not true, print an error message and break. Check the value of `int_list` after to ensure that is was only converted up to the point of the error." 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 3, 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [ 255 | "str_list = ['3', '6', '4', 'five', '2']" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": null, 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "Take the primality test we wrote earlier and make it more efficient by breaking whenever a divisor is found" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": {}, 282 | "source": [ 283 | "Replace the condition in the while loop with `True` and instead use a `break` statement inside of an if statement to end the loop" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 5, 289 | "metadata": {}, 290 | "outputs": [ 291 | { 292 | "name": "stdout", 293 | "output_type": "stream", 294 | "text": [ 295 | "4\n", 296 | "5\n", 297 | "6\n", 298 | "7\n", 299 | "8\n" 300 | ] 301 | } 302 | ], 303 | "source": [ 304 | "i = 4\n", 305 | "while i <= 8:\n", 306 | " print(i)\n", 307 | " i += 1" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "metadata": {}, 314 | "outputs": [], 315 | "source": [] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": {}, 320 | "source": [ 321 | "## Enumeration" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": {}, 327 | "source": [ 328 | "Create a list of positive and negative numbers. Loop through the list using enumeration and replace and negative numbers with zero. (Hint: use something similar to `numbers[index] = 0`)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": null, 334 | "metadata": {}, 335 | "outputs": [], 336 | "source": [] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": {}, 341 | "source": [ 342 | "Create a list of numbers an print out the square of every second number" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [] 351 | }, 352 | { 353 | "cell_type": "markdown", 354 | "metadata": {}, 355 | "source": [ 356 | "Enumerate allows for a second optional argument. What does this do? Read the docs:\n", 357 | "https://book.pythontips.com/en/latest/enumerate.html" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "Use this second argument to rewrite the last example in the session five presentation, without using `index + 1`" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": null, 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "metadata": {}, 382 | "source": [ 383 | "The alternating sum of the list `[a, b, c, d, ...]` is `a - b + c - d + ...`. Create a list of numbers and find its alternating sum. You will need to use a variable (say, `running_alt_sum`) to keep track of the alterating sum as you loop through the list. (Hint: `(-1) ** i` is `1` if i is even, and `-1` if i is odd—alternatively, you can use an if statement looking at whether `i` is odd)" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": null, 389 | "metadata": {}, 390 | "outputs": [], 391 | "source": [] 392 | } 393 | ], 394 | "metadata": { 395 | "kernelspec": { 396 | "display_name": "Python 3", 397 | "language": "python", 398 | "name": "python3" 399 | }, 400 | "language_info": { 401 | "codemirror_mode": { 402 | "name": "ipython", 403 | "version": 3 404 | }, 405 | "file_extension": ".py", 406 | "mimetype": "text/x-python", 407 | "name": "python", 408 | "nbconvert_exporter": "python", 409 | "pygments_lexer": "ipython3", 410 | "version": "3.7.3" 411 | } 412 | }, 413 | "nbformat": 4, 414 | "nbformat_minor": 2 415 | } 416 | -------------------------------------------------------------------------------- /session_five/session_five_presentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/THargreaves/beginners-python/2fb59b3ae1518db129897019274a56b2e50d0ee5/session_five/session_five_presentation.pptx -------------------------------------------------------------------------------- /session_four/session_four_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Four Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Non-numeric Lists" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Ask the user to input 3 days of the week (using `input()` three times) and use these to create a list of strings" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Create a list containing three lists. The first list should contain the numbers $1$ to $3$, the second should contain the English forms (e.g. \"one\") of these words. And the last should contain Boolean values which are True if the corresponding number is prime (hint: $1$ is not prime, $2$ and $3$ are)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "Lists of lists don't have to be square. Create a list containing lists of length $1$, $2$, and $3$, each filled with numbers of your choosing" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "## Extracting List Elements" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "Run the following code to generate a list of random numbers with a length randomly chosen between 50 and 100" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 3, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "from random import random, randint, seed\n", 94 | "seed(1729)\n", 95 | "numbers = [random() for __ in range(randint(50, 100))]" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "What is the 42nd element of this list?" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "What is the value of the element that is tenth from the end of the list?" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "What happens when you try to access the value of an index that doesn't exist?" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "_This is quite a tough challenge, but try to persevere. You can always check out the solutions if you're unsure_\n", 150 | "\n", 151 | "The following code will create a reordering of the numbers 0 to 99" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 5, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "[26, 3, 89, 48, 83, 21, 1, 95, 54, 33, 35, 90, 92, 94, 17, 50, 59, 28, 56, 74, 53, 45, 82, 65, 27, 14, 60, 20, 66, 67, 8, 70, 69, 40, 24, 72, 84, 80, 19, 85, 9, 98, 6, 76, 78, 68, 25, 16, 97, 99, 58, 4, 51, 31, 62, 2, 81, 42, 46, 41, 77, 34, 71, 79, 18, 47, 13, 12, 7, 52, 87, 43, 5, 61, 38, 96, 44, 10, 57, 30, 11, 73, 91, 75, 32, 36, 23, 22, 86, 39, 15, 88, 29, 64, 93, 0, 49, 63, 37, 55]\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "from random import shuffle, seed\n", 169 | "mapping = [i for i in range(100)]\n", 170 | "seed(1729)\n", 171 | "shuffle(mapping)\n", 172 | "print(mapping)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "You can think of this as a sort of mapping, where the value of a given index tells you which index to look at next. For example index zero contains the value $26$ so we'd next look at index $26$. This has value $60$ so we'd then look at index $60$, etc.\n", 180 | "\n", 181 | "It is not hard to show that if you continue following this rule, you'll eventually get back to where you started. It would be interesting to know how many steps this takes.\n", 182 | "\n", 183 | "Ask the user for an index to start from and then follow the rule until you get back to the start, keeping track of how many steps this takes.\n", 184 | "\n", 185 | "You will want to define a variable `steps` to keep track of how many steps you've taken as well as a variable `curr_value`, initial set to the starting value, to hold the current value you are looking at. You should keep looping until `curr_value == start_value` and `steps >= 1`. The last condition is needed to ensure that you don't immediately exit the loop. On each loop iteration you can increment the step count and update the current value to be `mapping[curr_value]`. Have a go at tying these ideas together into a solution." 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": null, 191 | "metadata": {}, 192 | "outputs": [], 193 | "source": [] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "(Fun fact: for a mapping with $n$ elements, the average chain length is roughly equal to $\\frac{n}{\\log(n)}$)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "## Slicing Lists" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "Using the variable `numbers` from above, extract a sub-list containing the 5th to 15th elements" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "Extract a sub-list containing all of the 5 last elements except the actual last" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "If I use the slice `m:n` with $m\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session One Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Using Google Colab" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Whenever you write a `#` in your code, Python will ignore the rest of the line. This lets us write comments to document our code. Print the phrase `\"Hello World\"` and end the line with the comment: \n", 36 | "\n", 37 | "`# see https://en.wikipedia.org/wiki/%22Hello,_World!%22_program`" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "## Printing and Arithmetic" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "What is the difference between $5423$ and $2683$? Use Python to find out (hint: use `-` for subtraction)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "Suppose I have 2000ml of solvent and I need to divide this evenly into 17 test tubes. Roughly how many millilitres should be added to each test tube. Use Python to find out" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "Print a maths equation followed by the solution. For example `54 * 93 + 4 = 5026`. Do this using print with two inputs. The first input should be the left-hand side of the equation and the equals sign, as a string, and the second should be a mathematical expression for Python to evaluate to give the answer" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "Does Python obey the order of operations (you might know this as BIDMAS, BODMAS or PEMDAS)? Try evaluating a few expressions to see" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "By default, `print()` separates your arguments by spaces. You can change this by adding a named input called `sep` somewher in your inputs (though we usually put it at the end). For example:" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 6, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "15 -> 46 -> 23 -> 70\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "print(\"15\", \"46\", \"23\", \"70\", sep = \" -> \")" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Use an empty string (`\"\"`) as your separating character to combine all the numbers above into one long number" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "## Variables" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "Create one variable containing your house number and other containing the number of pets you own. Add these variables together and print the result" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "Run the following two cells in reverse order, then the third. What value do you expect to be printed?" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "# run me second\n", 193 | "x = 2" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "# run me first\n", 203 | "x = 1" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "# run me last\n", 213 | "print(x)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "Create 3 variables storing the number of chickens, cows, and pigs you have on an imaginary farm" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": null, 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "Use these variables to print how many total legs there are on the farm (including yours!)" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": null, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "## Variable Types" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "What is the type of the result of adding an integer to a float?" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "Whenever we divide one number by another using `/`, even if the result is a whole number, the resulting type will be a float. Confirm this by creating a variable with value `4 / 2` and checking its type" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": null, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": {}, 292 | "source": [ 293 | "The reason Python does this is for consistency. It can be a pain though since the output will print with a `.0` at the end. You can get around this problem by converting to an integer. Take the variable above and convert it to an integer using `int()`, overwriting its current value. Print the result" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | } 303 | ], 304 | "metadata": { 305 | "kernelspec": { 306 | "display_name": "Python 3", 307 | "language": "python", 308 | "name": "python3" 309 | }, 310 | "language_info": { 311 | "codemirror_mode": { 312 | "name": "ipython", 313 | "version": 3 314 | }, 315 | "file_extension": ".py", 316 | "mimetype": "text/x-python", 317 | "name": "python", 318 | "nbconvert_exporter": "python", 319 | "pygments_lexer": "ipython3", 320 | "version": "3.7.3" 321 | } 322 | }, 323 | "nbformat": 4, 324 | "nbformat_minor": 2 325 | } 326 | -------------------------------------------------------------------------------- /session_one/session_one_presentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/THargreaves/beginners-python/2fb59b3ae1518db129897019274a56b2e50d0ee5/session_one/session_one_presentation.pptx -------------------------------------------------------------------------------- /session_one/session_one_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session One Homework Solutions" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Using Google Colab" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Whenever you write a `#` in your code, Python will ignore the rest of the line. This lets us write comments to document our code. Print the phrase `\"Hello World\"` and end the line with the comment:\n", 36 | "\n", 37 | "`# see https://en.wikipedia.org/wiki/%22Hello,_World!%22_program`" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "Hello World\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "print(\"Hello World\") # see https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "## Printing and Arithmetic" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "What is the difference between $5423$ and $2683$? Use Python to find out (hint: use `-` for subtraction)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 2, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "2740\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "print(5423 - 2683)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "Suppose I have 2000ml of solvent and I need to divide this evenly into 17 test tubes. Roughly how many millilitres should be added to each test tube. Use Python to find out" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 3, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "117.6470588235294\n" 105 | ] 106 | } 107 | ], 108 | "source": [ 109 | "print(2000 / 17)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "About 115–120ml per test tube should do" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "Print a maths equation followed by the solution. For example `54 * 93 + 4 = 5026`. Do this using print with two inputs. The first input should be the left-hand side of the equation and the equals sign, as a string, and the second should be a mathematical expression for Python to evaluate to give the answer" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 4, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "75 + 4 * 53 = 287\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "print(\"75 + 4 * 53 =\", 75 + 4 * 53)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "Does Python obey the order of operations (you might know this as BIDMAS, BODMAS or PEMDAS)? Try evaluating a few expressions to see" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 5, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "10\n", 160 | "14\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "print(4 + 3 * 2)\n", 166 | "print((4 + 3) * 2)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "Python does obey the order of operations" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "By default, `print()` separates your arguments by spaces. You can change this by adding a named input called `sep` somewher in your inputs (though we usually put it at the end). For example:" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 2, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "15 -> 46 -> 23 -> 70\n" 193 | ] 194 | } 195 | ], 196 | "source": [ 197 | "print(\"15\", \"46\", \"23\", \"70\", sep = \" -> \")" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "Use an empty string (`\"\"`) as your separating character to combine all the numbers above into one long number" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 3, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "name": "stdout", 214 | "output_type": "stream", 215 | "text": [ 216 | "15462370\n" 217 | ] 218 | } 219 | ], 220 | "source": [ 221 | "print(\"15\", \"46\", \"23\", \"70\", sep = \"\")" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "## Variables" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "Create one variable containing your house number and other containing the number of pets you own. Add these variables together and print the result" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 6, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [ 244 | "house_number = 4\n", 245 | "num_pets = 0 # how sad :(" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "Run the following two cells in reverse order, then the third. What value do you expect to be printed?" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 8, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "# run me second\n", 262 | "x = 2" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 7, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [ 271 | "# run me first\n", 272 | "x = 1" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 9, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "name": "stdout", 282 | "output_type": "stream", 283 | "text": [ 284 | "2\n" 285 | ] 286 | } 287 | ], 288 | "source": [ 289 | "# run me last\n", 290 | "print(x)" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "2, since this was the last value `x` was set to" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "Create 3 variables storing the number of chickens, cows, and pigs you have on an imaginary farm" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 14, 310 | "metadata": {}, 311 | "outputs": [], 312 | "source": [ 313 | "num_chickens = 3\n", 314 | "num_cows = 6\n", 315 | "num_pigs = 1" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "Use these variables to print how many total legs there are on the farm (including yours!)" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 15, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "name": "stdout", 332 | "output_type": "stream", 333 | "text": [ 334 | "There are 36 legs on the farm\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "print(\"There are\",\n", 340 | " 2 + num_chickens * 2 + (num_cows + num_pigs) * 4,\n", 341 | " \"legs on the farm\")" 342 | ] 343 | }, 344 | { 345 | "cell_type": "markdown", 346 | "metadata": {}, 347 | "source": [ 348 | "## Variable Types" 349 | ] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "What is the type of the result of adding an integer to a float?" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 1, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "\n" 368 | ] 369 | } 370 | ], 371 | "source": [ 372 | "print(type(1 + 1.0))" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "metadata": {}, 378 | "source": [ 379 | "A float. This is because Python wants to be consistent. An integer plus a float _could_ be represented as an integer in rare cases but Python plays it safe and always returns a float so you can be sure. If you want to convert the result to an integer, just use `int()`" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "Whenever we divide one number by another using `/`, even if the result is a whole number, the resulting type will be a float. Confirm this by creating a variable with value `4 / 2` and checking its type" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 12, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "\n" 399 | ] 400 | } 401 | ], 402 | "source": [ 403 | "num = 4 / 2\n", 404 | "print(type(num))" 405 | ] 406 | }, 407 | { 408 | "cell_type": "markdown", 409 | "metadata": {}, 410 | "source": [ 411 | "The reason Python does this is for consistency. It can be a pain though since the output will print with a `.0` at the end. You can get around this problem by converting to an integer. Take the variable above and convert it to an integer using `int()`, overwriting its current value. Print the result" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 13, 417 | "metadata": {}, 418 | "outputs": [ 419 | { 420 | "name": "stdout", 421 | "output_type": "stream", 422 | "text": [ 423 | "\n" 424 | ] 425 | } 426 | ], 427 | "source": [ 428 | "num = int(num)\n", 429 | "print(type(num))" 430 | ] 431 | } 432 | ], 433 | "metadata": { 434 | "kernelspec": { 435 | "display_name": "Python 3", 436 | "language": "python", 437 | "name": "python3" 438 | }, 439 | "language_info": { 440 | "codemirror_mode": { 441 | "name": "ipython", 442 | "version": 3 443 | }, 444 | "file_extension": ".py", 445 | "mimetype": "text/x-python", 446 | "name": "python", 447 | "nbconvert_exporter": "python", 448 | "pygments_lexer": "ipython3", 449 | "version": "3.7.3" 450 | } 451 | }, 452 | "nbformat": 4, 453 | "nbformat_minor": 2 454 | } 455 | -------------------------------------------------------------------------------- /session_seven/session_seven_additional_notes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook will breakdown the example shown at the end of session seven. We start by importing two additional functions." 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "from urllib.request import urlopen\n", 17 | "from json import load" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "We can then define a function to extra the title and lead author for any ISBN. We will break down how this works after." 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 4, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "def get_info_from_isbn(isbn):\n", 34 | " url = ('https://www.googleapis.com/' +\n", 35 | " 'books/v1/volumes?q=isbn:' + isbn)\n", 36 | " response = urlopen(url)\n", 37 | " all_info = load(response)\n", 38 | " book_info = all_info['items'][0]['volumeInfo']\n", 39 | " return book_info['title'], book_info['authors'][0]" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "We can use this function as so." 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 5, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "Python Data Science Handbook was written by Jacob T. Vanderplas\n", 59 | "R for Data Science was written by Garrett Grolemund\n", 60 | "Data Science with Julia was written by Paul D. McNicholas\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "isbns = ['9781491912058', '1491910399', '1138499986']\n", 66 | "for isbn in isbns:\n", 67 | " title, author = get_info_from_isbn(isbn)\n", 68 | " print(title, \"was written by\", author)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "So how does this work? It's first worth noting that we can use the API manually, without Python. Visit the following link, for example." 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "[https://www.googleapis.com/books/v1/volumes?q=isbn:9781491912058](https://www.googleapis.com/books/v1/volumes?q=isbn:9781491912058)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "This is a JSON file, which you will notice looks remarkably similar to a dictionary. We can load this into a Python dictionary using the following commands." 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "response = urlopen('https://www.googleapis.com/books/v1/volumes?q=isbn:9781491912058')\n", 99 | "all_info = load(response)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 9, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "{'kind': 'books#volumes',\n", 111 | " 'totalItems': 1,\n", 112 | " 'items': [{'kind': 'books#volume',\n", 113 | " 'id': 'yijjwAEACAAJ',\n", 114 | " 'etag': 'szI9j43M8cY',\n", 115 | " 'selfLink': 'https://www.googleapis.com/books/v1/volumes/yijjwAEACAAJ',\n", 116 | " 'volumeInfo': {'title': 'Python Data Science Handbook',\n", 117 | " 'subtitle': 'Essential Tools for Working with Data',\n", 118 | " 'authors': ['Jacob T. Vanderplas', 'Jake VanderPlas'],\n", 119 | " 'publisher': \"O'Reilly Media\",\n", 120 | " 'publishedDate': '2016',\n", 121 | " 'description': \"For many researchers, Python is a first-class tool mainly because of its libraries for storing, manipulating, and gaining insight from data. Several resources exist for individual pieces of this data science stack, but only with the Python Data Science Handbook do you get them all--IPython, NumPy, Pandas, Matplotlib, Scikit-Learn, and other related tools. Working scientists and data crunchers familiar with reading and writing Python code will find this comprehensive desk reference ideal for tackling day-to-day issues: manipulating, transforming, and cleaning data; visualizing different types of data; and using data to build statistical or machine learning models. Quite simply, this is the must-have reference for scientific computing in Python. With this handbook, you'll learn how to use: IPython and Jupyter: provide computational environments for data scientists using Python NumPy: includes the ndarray for efficient storage and manipulation of dense data arrays in Python Pandas: features the DataFrame for efficient storage and manipulation of labeled/columnar data in Python Matplotlib: includes capabilities for a flexible range of data visualizations in Python Scikit-Learn: for efficient and clean Python implementations of the most important and established machine learning algorithms\",\n", 122 | " 'industryIdentifiers': [{'type': 'ISBN_10', 'identifier': '1491912057'},\n", 123 | " {'type': 'ISBN_13', 'identifier': '9781491912058'}],\n", 124 | " 'readingModes': {'text': False, 'image': False},\n", 125 | " 'pageCount': 529,\n", 126 | " 'printType': 'BOOK',\n", 127 | " 'categories': ['Computers'],\n", 128 | " 'averageRating': 5,\n", 129 | " 'ratingsCount': 1,\n", 130 | " 'maturityRating': 'NOT_MATURE',\n", 131 | " 'allowAnonLogging': False,\n", 132 | " 'contentVersion': 'preview-1.0.0',\n", 133 | " 'panelizationSummary': {'containsEpubBubbles': False,\n", 134 | " 'containsImageBubbles': False},\n", 135 | " 'imageLinks': {'smallThumbnail': 'http://books.google.com/books/content?id=yijjwAEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api',\n", 136 | " 'thumbnail': 'http://books.google.com/books/content?id=yijjwAEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api'},\n", 137 | " 'language': 'en',\n", 138 | " 'previewLink': 'http://books.google.co.uk/books?id=yijjwAEACAAJ&dq=isbn:9781491912058&hl=&cd=1&source=gbs_api',\n", 139 | " 'infoLink': 'http://books.google.co.uk/books?id=yijjwAEACAAJ&dq=isbn:9781491912058&hl=&source=gbs_api',\n", 140 | " 'canonicalVolumeLink': 'https://books.google.com/books/about/Python_Data_Science_Handbook.html?hl=&id=yijjwAEACAAJ'},\n", 141 | " 'saleInfo': {'country': 'GB',\n", 142 | " 'saleability': 'NOT_FOR_SALE',\n", 143 | " 'isEbook': False},\n", 144 | " 'accessInfo': {'country': 'GB',\n", 145 | " 'viewability': 'NO_PAGES',\n", 146 | " 'embeddable': False,\n", 147 | " 'publicDomain': False,\n", 148 | " 'textToSpeechPermission': 'ALLOWED',\n", 149 | " 'epub': {'isAvailable': False},\n", 150 | " 'pdf': {'isAvailable': False},\n", 151 | " 'webReaderLink': 'http://play.google.com/books/reader?id=yijjwAEACAAJ&hl=&printsec=frontcover&source=gbs_api',\n", 152 | " 'accessViewStatus': 'NONE',\n", 153 | " 'quoteSharingAllowed': False},\n", 154 | " 'searchInfo': {'textSnippet': 'With this handbook, you'll learn how to use: IPython and Jupyter: provide computational environments for data scientists using Python NumPy: includes the ndarray for efficient storage and manipulation of dense data arrays in Python Pandas: ...'}}]}" 155 | ] 156 | }, 157 | "execution_count": 9, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "all_info" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "After a bit of experimenting, we notice that the book infomation is stored in `all_info['items'][0]['volumeInfo']`. If a book had a second volume, we could access this by replacing `0` with `1` etc." 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 10, 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [ 179 | "book_info = all_info['items'][0]['volumeInfo']" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 11, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "data": { 189 | "text/plain": [ 190 | "{'title': 'Python Data Science Handbook',\n", 191 | " 'subtitle': 'Essential Tools for Working with Data',\n", 192 | " 'authors': ['Jacob T. Vanderplas', 'Jake VanderPlas'],\n", 193 | " 'publisher': \"O'Reilly Media\",\n", 194 | " 'publishedDate': '2016',\n", 195 | " 'description': \"For many researchers, Python is a first-class tool mainly because of its libraries for storing, manipulating, and gaining insight from data. Several resources exist for individual pieces of this data science stack, but only with the Python Data Science Handbook do you get them all--IPython, NumPy, Pandas, Matplotlib, Scikit-Learn, and other related tools. Working scientists and data crunchers familiar with reading and writing Python code will find this comprehensive desk reference ideal for tackling day-to-day issues: manipulating, transforming, and cleaning data; visualizing different types of data; and using data to build statistical or machine learning models. Quite simply, this is the must-have reference for scientific computing in Python. With this handbook, you'll learn how to use: IPython and Jupyter: provide computational environments for data scientists using Python NumPy: includes the ndarray for efficient storage and manipulation of dense data arrays in Python Pandas: features the DataFrame for efficient storage and manipulation of labeled/columnar data in Python Matplotlib: includes capabilities for a flexible range of data visualizations in Python Scikit-Learn: for efficient and clean Python implementations of the most important and established machine learning algorithms\",\n", 196 | " 'industryIdentifiers': [{'type': 'ISBN_10', 'identifier': '1491912057'},\n", 197 | " {'type': 'ISBN_13', 'identifier': '9781491912058'}],\n", 198 | " 'readingModes': {'text': False, 'image': False},\n", 199 | " 'pageCount': 529,\n", 200 | " 'printType': 'BOOK',\n", 201 | " 'categories': ['Computers'],\n", 202 | " 'averageRating': 5,\n", 203 | " 'ratingsCount': 1,\n", 204 | " 'maturityRating': 'NOT_MATURE',\n", 205 | " 'allowAnonLogging': False,\n", 206 | " 'contentVersion': 'preview-1.0.0',\n", 207 | " 'panelizationSummary': {'containsEpubBubbles': False,\n", 208 | " 'containsImageBubbles': False},\n", 209 | " 'imageLinks': {'smallThumbnail': 'http://books.google.com/books/content?id=yijjwAEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api',\n", 210 | " 'thumbnail': 'http://books.google.com/books/content?id=yijjwAEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api'},\n", 211 | " 'language': 'en',\n", 212 | " 'previewLink': 'http://books.google.co.uk/books?id=yijjwAEACAAJ&dq=isbn:9781491912058&hl=&cd=1&source=gbs_api',\n", 213 | " 'infoLink': 'http://books.google.co.uk/books?id=yijjwAEACAAJ&dq=isbn:9781491912058&hl=&source=gbs_api',\n", 214 | " 'canonicalVolumeLink': 'https://books.google.com/books/about/Python_Data_Science_Handbook.html?hl=&id=yijjwAEACAAJ'}" 215 | ] 216 | }, 217 | "execution_count": 11, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "book_info" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "We can look at the keys by using the `.keys()` method." 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 12, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "dict_keys(['title', 'subtitle', 'authors', 'publisher', 'publishedDate', 'description', 'industryIdentifiers', 'readingModes', 'pageCount', 'printType', 'categories', 'averageRating', 'ratingsCount', 'maturityRating', 'allowAnonLogging', 'contentVersion', 'panelizationSummary', 'imageLinks', 'language', 'previewLink', 'infoLink', 'canonicalVolumeLink'])\n" 243 | ] 244 | } 245 | ], 246 | "source": [ 247 | "print(book_info.keys())" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "Finally, we can use this to extract details." 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 18, 260 | "metadata": {}, 261 | "outputs": [ 262 | { 263 | "name": "stdout", 264 | "output_type": "stream", 265 | "text": [ 266 | "Authors: ['Jacob T. Vanderplas', 'Jake VanderPlas']\n", 267 | "Page Count: 529\n", 268 | "Rating: ★★★★★\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "print(\"Authors:\", book_info['authors'])\n", 274 | "print(\"Page Count:\", book_info['pageCount'])\n", 275 | "\n", 276 | "# Number of stars\n", 277 | "rating = round(book_info['averageRating'])\n", 278 | "print(\"Rating:\", '★' * rating + '☆' * (5 - rating))" 279 | ] 280 | } 281 | ], 282 | "metadata": { 283 | "kernelspec": { 284 | "display_name": "Python 3", 285 | "language": "python", 286 | "name": "python3" 287 | }, 288 | "language_info": { 289 | "codemirror_mode": { 290 | "name": "ipython", 291 | "version": 3 292 | }, 293 | "file_extension": ".py", 294 | "mimetype": "text/x-python", 295 | "name": "python", 296 | "nbconvert_exporter": "python", 297 | "pygments_lexer": "ipython3", 298 | "version": "3.7.3" 299 | } 300 | }, 301 | "nbformat": 4, 302 | "nbformat_minor": 2 303 | } 304 | -------------------------------------------------------------------------------- /session_seven/session_seven_blank_template.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Seven Template" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "-Ceh1HnZIsVO" 29 | }, 30 | "source": [ 31 | "## Dictionaries" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "6AZ6H-6jKoSt" 39 | }, 40 | "source": [ 41 | "### Introduction\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "Create a dictionary containing an integer, a string, and a list" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/", 57 | "height": 35 58 | }, 59 | "colab_type": "code", 60 | "id": "D4l_2DsZIlgB", 61 | "outputId": "e63ad5b1-2062-4d8c-9ee7-b03a25d72001" 62 | }, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": { 69 | "colab": { 70 | "base_uri": "https://localhost:8080/", 71 | "height": 35 72 | }, 73 | "colab_type": "code", 74 | "id": "9lLW387eKgNN", 75 | "outputId": "860dcf3b-f3e5-402d-f58f-4d28fb69a6b1" 76 | }, 77 | "source": [ 78 | "Access one of the values from this dictionary" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/", 87 | "height": 35 88 | }, 89 | "colab_type": "code", 90 | "id": "bIr0l5kJI_VV", 91 | "outputId": "c7891804-4acf-4856-e51a-271914a8dc60" 92 | }, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Try to access a non-existant key to confirm you recieve an error" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "Use the `in` keyword to safely access a dictionary key that may not exist" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "### Standard Puzzles" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "Create a dictionary with some information about you and print a few of the values" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "What do you think the code in the presentation will do? Try it" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": null, 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "### Bonus Puzzles" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "Experiment with using the `.get()` method of dictionaries. What are the advantages of using this?" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "## Manipulating Dictionaries" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "### Introduction" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "Add a new item to a dictionary" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "Overwrite the value of a dictionary item" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": null, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "Delete an item from a dictionary using its key" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "### Standard Puzzles" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "Create the dictionary as defined in the presentation and manipulate it as described" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": null, 270 | "metadata": {}, 271 | "outputs": [], 272 | "source": [] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "Create an empty dictionary called `square`" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": null, 284 | "metadata": {}, 285 | "outputs": [], 286 | "source": [] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "Fill this dictionary with key-value pairs for the squares of the integers -5 to 5" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "### Bonus Puzzles" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "Create a nested dictionary and access a value from an inner dictionary" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "## Looping Through Dictionaries" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "### Introduction" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": {}, 340 | "source": [ 341 | "Create a simple dictionary to practise looping through" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": null, 347 | "metadata": {}, 348 | "outputs": [], 349 | "source": [] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "Loop through a dictionary by key" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": null, 361 | "metadata": {}, 362 | "outputs": [], 363 | "source": [] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "Loop through a dictionary by value" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": null, 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "metadata": {}, 382 | "source": [ 383 | "Loop through a dictionary be item" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": null, 389 | "metadata": {}, 390 | "outputs": [], 391 | "source": [] 392 | }, 393 | { 394 | "cell_type": "markdown", 395 | "metadata": {}, 396 | "source": [ 397 | "### Standard Puzzles" 398 | ] 399 | }, 400 | { 401 | "cell_type": "markdown", 402 | "metadata": {}, 403 | "source": [ 404 | "Create a function that can count the occurances of words in a list and print these in a readable fashion" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": null, 410 | "metadata": {}, 411 | "outputs": [], 412 | "source": [] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "metadata": {}, 417 | "source": [ 418 | "### Bonus Puzzles" 419 | ] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "metadata": {}, 424 | "source": [ 425 | "Tidy up the grammar" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": null, 431 | "metadata": {}, 432 | "outputs": [], 433 | "source": [] 434 | } 435 | ], 436 | "metadata": { 437 | "colab": { 438 | "name": "Code Club Session 1", 439 | "provenance": [] 440 | }, 441 | "kernelspec": { 442 | "display_name": "Python 3", 443 | "language": "python", 444 | "name": "python3" 445 | }, 446 | "language_info": { 447 | "codemirror_mode": { 448 | "name": "ipython", 449 | "version": 3 450 | }, 451 | "file_extension": ".py", 452 | "mimetype": "text/x-python", 453 | "name": "python", 454 | "nbconvert_exporter": "python", 455 | "pygments_lexer": "ipython3", 456 | "version": "3.7.3" 457 | } 458 | }, 459 | "nbformat": 4, 460 | "nbformat_minor": 1 461 | } 462 | -------------------------------------------------------------------------------- /session_seven/session_seven_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Seven Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "_As we are moving deeper into the world of Python, the tasks we can complete with our skills can become more and more complicated. For that reason, there are fewer exercises to complete this week but of a slightly harder standard. Don't be put off; these are meant to challenge you._" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## Dictionaries" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Create a dictionary with two keys, `x` and `y` the value of each being a numeric list of length 5. Notice, that this is essentially a dataframe/table with two variables." 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "Ask the user for their name, age, and whether they own a dog. Assign the responses to variables `name`, `age`, and `has_dog`. Remember to convert the inputted age to an integer. The response for the last question should be either `yes` or `no` which should then be converted to `True` or `False` respectively. You can use the example from near the end of the session three presentation as a basis for this." 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "Create a dictionary containing the users information using the three variables defined above. Print out these values in a meaningful sentence using this dictionary." 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "Dictionary keys are not limited to strings although there are limits. Try to create dictionaries with keys of type integer, float, Boolean, list, tuple, and dictionary, respectively. Which are allowed and what error do you recieve when a key's type is invalid?" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 4, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "Dictionaries are also advantagous over lists when storing _sparse_ data (data with lots of missing or default values).\n", 139 | "\n", 140 | "Suppose there is a race with one thousand participants. We know that the first place was awarded to 'Ann' and last place to 'Bob'. Create an 1000 element list (by starting from an empty list and appending) to store this information using the string 'Unknown' for any race position for which we don't know the winner." 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "Store this information in a two item dictionary and create a function `get_name(i)` to access the name of the person in the ith place. Remember, you can use `in` to check for the existance of a key in a dictionary." 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "Consider the amount of storage required for each solution—there should be a clear winner!" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "## Manipulating Dictionaries" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "The following code creates a list of the letters of the English alphabet (called `letters`)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 10, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "from string import ascii_uppercase\n", 192 | "letters = list(ascii_uppercase)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "Create an empty dictionary called `encode` and loop through the list using enumeration to add key-value pairs to the dictionary mapping each letter to its position in the alphabet (`A -> 1, B -> 2, ..., Z -> 26`)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "Loop through the list of letters (without enumeration this time) to decrease the value of each key by one so that the new mapping is (`A -> 0, ..., Z -> 25`)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "Given an uppercase string, loop through its letters and use this dictionary to encode the text into a numeric list. For example the word 'BAG' would be encoded as `[1, 0, 6]`." 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "Create a function that when given a dictionary and a key will delete the item associated with that key only if the key exists" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 3, 247 | "metadata": {}, 248 | "outputs": [], 249 | "source": [] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "## Looping Through Dictionaries" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "As promised, we will look at removing a key by value. We can do this by looping through the items of a dictionary and deleting a corresponding key if the value matches. Use this to remove all instances with the value `Homer` in the following dictionary" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [ 271 | "father = {\n", 272 | " 'Bart': 'Homer',\n", 273 | " 'Homer': 'Abe',\n", 274 | " 'Lisa': 'Homer'\n", 275 | "}" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "metadata": {}, 282 | "outputs": [], 283 | "source": [] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "How could modify the above code to only delete the first matching instance, as is the case with `.delete()` for lists?" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Create an empty dictionary called `decode`. Take the letter mapping dictionary from above and loop through its items and using them to fill `decode` with keys and values reversed" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": {}, 314 | "source": [ 315 | "Take the encoded string from above and decode it by looping through the list and using this dictionary. You will want to use `''.join(list_of_characters)` to combine the decoded letters back into one string. This is the key step in a large amount of machine learning with text data and cryptography" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": null, 321 | "metadata": {}, 322 | "outputs": [], 323 | "source": [] 324 | } 325 | ], 326 | "metadata": { 327 | "kernelspec": { 328 | "display_name": "Python 3", 329 | "language": "python", 330 | "name": "python3" 331 | }, 332 | "language_info": { 333 | "codemirror_mode": { 334 | "name": "ipython", 335 | "version": 3 336 | }, 337 | "file_extension": ".py", 338 | "mimetype": "text/x-python", 339 | "name": "python", 340 | "nbconvert_exporter": "python", 341 | "pygments_lexer": "ipython3", 342 | "version": "3.7.3" 343 | } 344 | }, 345 | "nbformat": 4, 346 | "nbformat_minor": 2 347 | } 348 | -------------------------------------------------------------------------------- /session_seven/session_seven_presentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/THargreaves/beginners-python/2fb59b3ae1518db129897019274a56b2e50d0ee5/session_seven/session_seven_presentation.pptx -------------------------------------------------------------------------------- /session_six/session_six_blank_template.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Six Template" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "-Ceh1HnZIsVO" 29 | }, 30 | "source": [ 31 | "## Functions" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "6AZ6H-6jKoSt" 39 | }, 40 | "source": [ 41 | "### Introduction\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "Confirm that the type of `print` is `builtin_function_or_method`" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/", 57 | "height": 35 58 | }, 59 | "colab_type": "code", 60 | "id": "D4l_2DsZIlgB", 61 | "outputId": "e63ad5b1-2062-4d8c-9ee7-b03a25d72001" 62 | }, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": { 69 | "colab": { 70 | "base_uri": "https://localhost:8080/", 71 | "height": 35 72 | }, 73 | "colab_type": "code", 74 | "id": "9lLW387eKgNN", 75 | "outputId": "860dcf3b-f3e5-402d-f58f-4d28fb69a6b1" 76 | }, 77 | "source": [ 78 | "Create a function that will print text when ran and run it twice" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/", 87 | "height": 35 88 | }, 89 | "colab_type": "code", 90 | "id": "bIr0l5kJI_VV", 91 | "outputId": "c7891804-4acf-4856-e51a-271914a8dc60" 92 | }, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Create a function which returns a number and run it, storing the returned value in a variable" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "### Standard Puzzles" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "Create a function to print the string 'Hello!' and run this" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "Create a function which calls the above function five times" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "Create a function to calculate and return the value of $2 + 2$" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "## Function Arguments" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "### Introduction" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "Create a function to square an inputted number" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "Create a function to add two numbers, with the second parameter taking a default value of $1$" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "### Standard Puzzles" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "Create a function to greet a named person, with fallback default behaviour" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "Create a function to calculate the area of a rectangle" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": null, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "### Bonus Puzzles" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "Improve the greeter function above by using `None` as the default value for the name parameter" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "## Recursion" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "### Introduction" 269 | ] 270 | }, 271 | { 272 | "cell_type": "markdown", 273 | "metadata": {}, 274 | "source": [ 275 | "Create a function to calculate the nth Fibonacci number using recursion" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "metadata": {}, 282 | "outputs": [], 283 | "source": [] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "### Standard Puzzles" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "Create a function to calculate $n!$ using recursion" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": null, 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": {}, 309 | "source": [ 310 | "### Bonus Puzzles" 311 | ] 312 | }, 313 | { 314 | "cell_type": "markdown", 315 | "metadata": {}, 316 | "source": [ 317 | "Refactor the above function using an iterative paradigm" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "## Variable Scope" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": {}, 337 | "source": [ 338 | "### Introduction" 339 | ] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": {}, 344 | "source": [ 345 | "_The examples given in the presentation are not the most suitable to use as exercises, so instead just make sure you've gave them a good look over_" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": {}, 351 | "source": [ 352 | "### Standard Puzzles" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": {}, 358 | "source": [ 359 | "Predict what values will be printed when running the presentation code. Verify this" 360 | ] 361 | }, 362 | { 363 | "cell_type": "markdown", 364 | "metadata": {}, 365 | "source": [] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": null, 370 | "metadata": {}, 371 | "outputs": [], 372 | "source": [] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "Rewrite the supplied code to avoid referencing global variables" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": null, 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [] 387 | } 388 | ], 389 | "metadata": { 390 | "colab": { 391 | "name": "Code Club Session 1", 392 | "provenance": [] 393 | }, 394 | "kernelspec": { 395 | "display_name": "Python 3", 396 | "language": "python", 397 | "name": "python3" 398 | }, 399 | "language_info": { 400 | "codemirror_mode": { 401 | "name": "ipython", 402 | "version": 3 403 | }, 404 | "file_extension": ".py", 405 | "mimetype": "text/x-python", 406 | "name": "python", 407 | "nbconvert_exporter": "python", 408 | "pygments_lexer": "ipython3", 409 | "version": "3.7.3" 410 | } 411 | }, 412 | "nbformat": 4, 413 | "nbformat_minor": 1 414 | } 415 | -------------------------------------------------------------------------------- /session_six/session_six_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Six Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "_As we are moving deeper into the world of Python, the tasks we can complete with our skills can become more and more complicated. For that reason, there are fewer exercises to complete this week but of a slightly harder standard. Don't be put off; these are meant to challenge you._" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## Functions" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Create a function called `pi` which returns the value `3.14159`" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "Create two functions, `circumference` and `area` which each have a single parameter `radius`. These should return the circumference and area of a circle with radius `radius` respectively. (See session one for the formulae for these)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "Create a function `describe_list` which prints the min, mean, and max of a list. (Bonus: print the median too with the helped of the `sorted()` function)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "Evaluate the following code cell to define the contained function" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 3, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "def print_row(r, n):\n", 94 | " w = n - 2 * abs((n - 1) // 2 - r)\n", 95 | " print(('*' * w).center(n))" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "Create a function, `print_diamond` which has parameter `size`. If `size` is even, print an error message. Otherwise loop through the numbers 0 to `size-1` using the counter variable `row` and call `print_row(row, size)` each iteration" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 9, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "## Recursion" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "What happens when we create a function that calls itself with no condition to eventually stop the recursion? Here's an example:" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "def recurse():\n", 133 | " return recurse()\n", 134 | "recurse()" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "Use recursion to create a function `product` which calculates the product of all items in a list. This should take a list as its single argument. There are then two cases. If the list is of length 1, return that number. If the list is of length 2 or more, return the product of the first element and the result of passing the rest of the list to `product()`" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "The greatest common divisor (GCD) of two integers is the largest integer which divides both of them. Greek mathematician Euclid, is famous for discovering the following recursive definition of the GCD.\n", 161 | "\n", 162 | "$$\\textrm{GCD}(a, b) = \\textrm{GCD}(b, a \\text{ mod } b)$$\n", 163 | "\n", 164 | "Where $\\text{mod}$ is the modulus operator given by `%` in Python. We also have that $\\textrm{GCD}(a, 0)=a$ for any integer $a$. Use this to write a recursive function to calculate the GCD of two integers." 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 3, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "## Variable Scope" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "Exercises for variable scope don't make much sense, so instead read [this short article](https://matthew-brett.github.io/teaching/global_scope.html) on variable scope to re-cap" 186 | ] 187 | } 188 | ], 189 | "metadata": { 190 | "kernelspec": { 191 | "display_name": "Python 3", 192 | "language": "python", 193 | "name": "python3" 194 | }, 195 | "language_info": { 196 | "codemirror_mode": { 197 | "name": "ipython", 198 | "version": 3 199 | }, 200 | "file_extension": ".py", 201 | "mimetype": "text/x-python", 202 | "name": "python", 203 | "nbconvert_exporter": "python", 204 | "pygments_lexer": "ipython3", 205 | "version": "3.7.3" 206 | } 207 | }, 208 | "nbformat": 4, 209 | "nbformat_minor": 2 210 | } 211 | -------------------------------------------------------------------------------- /session_six/session_six_presentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/THargreaves/beginners-python/2fb59b3ae1518db129897019274a56b2e50d0ee5/session_six/session_six_presentation.pptx -------------------------------------------------------------------------------- /session_six/session_six_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Six Homework Solutions" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "_As we are moving deeper into the world of Python, the tasks we can complete with our skills can become more and more complicated. For that reason, there are fewer exercises to complete this week but of a slightly harder standard. Don't be put off; these are meant to challenge you._" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## Functions" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Create a function called `pi` which returns the value `3.14159`" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 1, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "3.14159\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "def pi():\n", 60 | " return 3.14159\n", 61 | "print(pi())" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "Create two functions, `circumference` and `area` which each have a single parameter `radius`. These should return the circumference and area of a circle with radius `radius` respectively. (See session one for the formulae for these)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 3, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "A circle with radius 4 has circumference 25.13272 and area 50.26544\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "def circumference(radius):\n", 86 | " return 2 * pi() * radius\n", 87 | "\n", 88 | "def area(radius):\n", 89 | " return pi() * radius ** 2\n", 90 | "\n", 91 | "r = 4\n", 92 | "c = circumference(r)\n", 93 | "a = area(r)\n", 94 | "print(\"A circle with radius\", r,\n", 95 | " \"has circumference\", c,\n", 96 | " \"and area\", a)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "Create a function `describe_list` which prints the min, mean, and max of a list. (Bonus: print the median too with the helped of the `sorted()` function)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "Min: 4\n", 116 | "Mean: 6.5\n", 117 | "Max: 9\n", 118 | "Median: 6.0\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "def describe_list(lst):\n", 124 | " print(\"Min:\", min(lst))\n", 125 | " print(\"Mean:\", sum(lst) / len(lst))\n", 126 | " print(\"Max:\", max(lst))\n", 127 | " ### Bonus ###\n", 128 | " ordered_lst = sorted(lst)\n", 129 | " l = len(lst)\n", 130 | " if l % 2 == 0:\n", 131 | " med = (lst[l // 2] + lst[l // 2 - 1]) / 2\n", 132 | " else:\n", 133 | " med = lst[(l - 1) // 2]\n", 134 | " print(\"Median:\", med)\n", 135 | " \n", 136 | "describe_list([6, 7, 4, 8, 9, 5])" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Evaluate the following code cell to define the contained function" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 7, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "def print_row(r, n):\n", 153 | " w = n - 2 * abs((n - 1) // 2 - r)\n", 154 | " print(('*' * w).center(n))" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Create a function, `print_diamond` which has parameter `size`. If `size` is even, print an error message. Otherwise loop through the numbers 0 to `size-1` using the counter variable `row` and call `print_row(row, size)` each iteration" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 11, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | " * \n", 174 | " *** \n", 175 | " ***** \n", 176 | "*******\n", 177 | " ***** \n", 178 | " *** \n", 179 | " * \n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "def print_diamond(size):\n", 185 | " if size % 2 == 0:\n", 186 | " print(\"`size` must be odd\")\n", 187 | " else:\n", 188 | " for row in range(size):\n", 189 | " print_row(row, size)\n", 190 | "\n", 191 | "print_diamond(7)" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "## Recursion" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "What happens when we create a function that calls itself with no condition to eventually stop the recursion? Here's an example:" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 12, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "ename": "RecursionError", 215 | "evalue": "maximum recursion depth exceeded", 216 | "output_type": "error", 217 | "traceback": [ 218 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 219 | "\u001b[1;31mRecursionError\u001b[0m Traceback (most recent call last)", 220 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 221 | "\u001b[1;32m\u001b[0m in \u001b[0;36mrecurse\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 222 | "... last 1 frames repeated, from the frame below ...\n", 223 | "\u001b[1;32m\u001b[0m in \u001b[0;36mrecurse\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mrecurse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 224 | "\u001b[1;31mRecursionError\u001b[0m: maximum recursion depth exceeded" 225 | ] 226 | } 227 | ], 228 | "source": [ 229 | "def recurse():\n", 230 | " return recurse()\n", 231 | "recurse()" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "Python will give us a recursion error. This occurs when the depth of a recursion has surpassed a given threshold. This can be altered using the following code." 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 13, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "import sys\n", 248 | "sys.setrecursionlimit(1500)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "Use recursion to create a function `product` which calculates the product of all items in a list. This should take a list as its single argument. There are then two cases. If the list is of length 1, return that number. If the list is of length 2 or more, return the product of the first element and the result of passing the rest of the list to `product()`" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 15, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "336" 267 | ] 268 | }, 269 | "execution_count": 15, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "def product(lst):\n", 276 | " if len(lst) == 1:\n", 277 | " return lst[0]\n", 278 | " else:\n", 279 | " return lst[0] * product(lst[1:])\n", 280 | "\n", 281 | "product([3, 7, 2, 8])" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "The greatest common divisor (GCD) of two integers is the largest integer which divides both of them. Greek mathematician Euclid, is famous for discovering the following recursive definition of the GCD.\n", 289 | "\n", 290 | "$$\\textrm{GCD}(a, b) = \\textrm{GCD}(b, a \\text{ mod } b)$$\n", 291 | "\n", 292 | "Where $\\text{mod}$ is the modulus operator given by `%` in Python. We also have that $\\textrm{GCD}(a, 0) = a$ for any integer $a$. Use this to write a recursive function to calculate the GCD of two integers." 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 20, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "data": { 302 | "text/plain": [ 303 | "8" 304 | ] 305 | }, 306 | "execution_count": 20, 307 | "metadata": {}, 308 | "output_type": "execute_result" 309 | } 310 | ], 311 | "source": [ 312 | "def gcd(a, b):\n", 313 | " if b == 0:\n", 314 | " return a\n", 315 | " else:\n", 316 | " return gcd(b, a % b)\n", 317 | " \n", 318 | "gcd(24, 32)" 319 | ] 320 | }, 321 | { 322 | "cell_type": "markdown", 323 | "metadata": {}, 324 | "source": [ 325 | "## Variable Scope" 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "metadata": {}, 331 | "source": [ 332 | "Exercises for variable scope don't make much sense, so instead read [this short article](https://matthew-brett.github.io/teaching/global_scope.html) on variable scope to re-cap" 333 | ] 334 | } 335 | ], 336 | "metadata": { 337 | "kernelspec": { 338 | "display_name": "Python 3", 339 | "language": "python", 340 | "name": "python3" 341 | }, 342 | "language_info": { 343 | "codemirror_mode": { 344 | "name": "ipython", 345 | "version": 3 346 | }, 347 | "file_extension": ".py", 348 | "mimetype": "text/x-python", 349 | "name": "python", 350 | "nbconvert_exporter": "python", 351 | "pygments_lexer": "ipython3", 352 | "version": "3.7.3" 353 | } 354 | }, 355 | "nbformat": 4, 356 | "nbformat_minor": 2 357 | } 358 | -------------------------------------------------------------------------------- /session_three/session_three_blank_template.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Three Template" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "-Ceh1HnZIsVO" 29 | }, 30 | "source": [ 31 | "## Comparison Operators" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "6AZ6H-6jKoSt" 39 | }, 40 | "source": [ 41 | "### Introduction\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "Use comparison operators to create two Boolean variables, on `True` and one `False`" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/", 57 | "height": 35 58 | }, 59 | "colab_type": "code", 60 | "id": "D4l_2DsZIlgB", 61 | "outputId": "e63ad5b1-2062-4d8c-9ee7-b03a25d72001" 62 | }, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": { 69 | "colab": { 70 | "base_uri": "https://localhost:8080/", 71 | "height": 35 72 | }, 73 | "colab_type": "code", 74 | "id": "9lLW387eKgNN", 75 | "outputId": "860dcf3b-f3e5-402d-f58f-4d28fb69a6b1" 76 | }, 77 | "source": [ 78 | "Check for equality of two numbers using comparisons" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/", 87 | "height": 35 88 | }, 89 | "colab_type": "code", 90 | "id": "bIr0l5kJI_VV", 91 | "outputId": "c7891804-4acf-4856-e51a-271914a8dc60" 92 | }, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "### Standard Puzzles" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "Answer the three comparison questions from the presentation" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "Check whether a user inputted number is positive" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "Create a list and check if the smallest element is equal to two" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "### Bonus Puzzles" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "Experiment with using comparison operators on strings. What are you findings?" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "## Boolean Operators" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "### Introduction" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "Use the `and` operator to combine two Boolean values" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "Likewise, use `or`" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "Negate a Boolean value using `not`" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "### Standard Puzzles" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "What do you think the Boolean expressions will evaluate to. Make your guess then check" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "Create two Boolean expressions, one `True` and one `False`" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": {}, 270 | "source": [ 271 | "### Bonus Puzzles" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "Guess and check the value of the complex Boolean expression" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": null, 289 | "metadata": {}, 290 | "outputs": [], 291 | "source": [] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "## Control Flow" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "### Introduction" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "Create an `if` statement to check if a number is positive" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [] 320 | }, 321 | { 322 | "cell_type": "markdown", 323 | "metadata": {}, 324 | "source": [ 325 | "Create an `if` statement with an `elif` statement following it" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": null, 331 | "metadata": {}, 332 | "outputs": [], 333 | "source": [] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": {}, 338 | "source": [ 339 | "Create an `if-elif-else` statement to check the sign of a number" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": null, 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": {}, 352 | "source": [ 353 | "### Standard Puzzles" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "Ask for a users age and use a control flow sequence with two `elif` statements to print an appropriate response" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": null, 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": {}, 373 | "source": [ 374 | "Create a random number between one and ten" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": null, 380 | "metadata": {}, 381 | "outputs": [], 382 | "source": [] 383 | }, 384 | { 385 | "cell_type": "markdown", 386 | "metadata": {}, 387 | "source": [ 388 | "Ask the user to guess the number and print an appropriate response" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": null, 394 | "metadata": {}, 395 | "outputs": [], 396 | "source": [] 397 | }, 398 | { 399 | "cell_type": "markdown", 400 | "metadata": {}, 401 | "source": [ 402 | "### Bonus Puzzles" 403 | ] 404 | }, 405 | { 406 | "cell_type": "markdown", 407 | "metadata": {}, 408 | "source": [ 409 | "Ask the user for a decimal number and round it. Print in which direction it was rounded in using control flow" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": null, 415 | "metadata": {}, 416 | "outputs": [], 417 | "source": [] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": {}, 422 | "source": [ 423 | "## While Loops" 424 | ] 425 | }, 426 | { 427 | "cell_type": "markdown", 428 | "metadata": {}, 429 | "source": [ 430 | "### Introduction" 431 | ] 432 | }, 433 | { 434 | "cell_type": "markdown", 435 | "metadata": {}, 436 | "source": [ 437 | "Use a loop to print numbers starting at $4$ and increasing by $5$ until this surpasses $20$" 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": null, 443 | "metadata": {}, 444 | "outputs": [], 445 | "source": [] 446 | }, 447 | { 448 | "cell_type": "markdown", 449 | "metadata": {}, 450 | "source": [ 451 | "Use a `while` loop to enforce that a user inputs a positive number" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": null, 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [] 460 | }, 461 | { 462 | "cell_type": "markdown", 463 | "metadata": {}, 464 | "source": [ 465 | "### Standard Puzzles" 466 | ] 467 | }, 468 | { 469 | "cell_type": "markdown", 470 | "metadata": {}, 471 | "source": [ 472 | "Count down from 10 then shout \"Blast off!\"" 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": null, 478 | "metadata": {}, 479 | "outputs": [], 480 | "source": [] 481 | }, 482 | { 483 | "cell_type": "markdown", 484 | "metadata": {}, 485 | "source": [ 486 | "### Bonus Puzzles" 487 | ] 488 | }, 489 | { 490 | "cell_type": "markdown", 491 | "metadata": {}, 492 | "source": [ 493 | "Repeat the above puzzle but allow the user to specify a starting point (to avoid an infinite loop, you likely want to validate the input first)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": null, 499 | "metadata": {}, 500 | "outputs": [], 501 | "source": [] 502 | }, 503 | { 504 | "cell_type": "markdown", 505 | "metadata": {}, 506 | "source": [ 507 | "Repeat your above code using assignment operators" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": null, 513 | "metadata": {}, 514 | "outputs": [], 515 | "source": [] 516 | }, 517 | { 518 | "cell_type": "markdown", 519 | "metadata": {}, 520 | "source": [ 521 | "Change your code so only even numbers in the countdown are printed" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": null, 527 | "metadata": {}, 528 | "outputs": [], 529 | "source": [] 530 | } 531 | ], 532 | "metadata": { 533 | "colab": { 534 | "name": "Code Club Session 1", 535 | "provenance": [] 536 | }, 537 | "kernelspec": { 538 | "display_name": "Python 3", 539 | "language": "python", 540 | "name": "python3" 541 | }, 542 | "language_info": { 543 | "codemirror_mode": { 544 | "name": "ipython", 545 | "version": 3 546 | }, 547 | "file_extension": ".py", 548 | "mimetype": "text/x-python", 549 | "name": "python", 550 | "nbconvert_exporter": "python", 551 | "pygments_lexer": "ipython3", 552 | "version": "3.7.3" 553 | } 554 | }, 555 | "nbformat": 4, 556 | "nbformat_minor": 1 557 | } 558 | -------------------------------------------------------------------------------- /session_three/session_three_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Three Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Comparisons and Boolean Arithmetic" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Confirm that applying `or` to two `True` values does indeed return `True`" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "_Note: Creating interesting questions for Boolean variables is difficult since there's not much to them, and so the following questions are a bit more abstract in nature. The first gives a useful application of comparisons for data analysis, and the second offers a way of dealing with the shortcomings of how computers store numbers. Feel free to skip to the next section if you only want to know the bare bones of Python._" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "Create a list of seven numbers called `nums` and find their mean (sum divided by length), saving this in the variable `mean`" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "Run the code below which will calculate the median value of the list (the middle value of the list when they are placed in order)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "median = sorted(nums)[3]" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "Use a Boolean expression to see which of the mean and median is larger" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "A rule-of-thumb for estimating how skewed a collection of numbers is to compare the mean and the median. When the mean is larger than the median then your data is likely positively skewed. For the reverse, a negative skew is more likely. Is your data more positively or negatively skewed?" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "![Skewness](https://upload.wikimedia.org/wikipedia/commons/f/f8/Negative_and_positive_skew_diagrams_%28English%29.svg)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Computers are not able to store decimal numbers with an infinite number of decimal places. This means that as we manipulate decimal numbers, small rounding errors can occur. For example:" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 5, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "0.9999999999999999\n", 132 | "False\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "print(1 / 49 * 49)\n", 138 | "print(1 / 49 * 49 == 1)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "This can be incredibly annoying. We know $\\frac{1}{49} \\times 49$ and $1$ are the same thing yet \"computer says no\". We can get around this by checking that two numbers are near each other, rather than exactly equal. We typically do this by taking the difference of two numbers, making this difference positive using the `abs()` function, and then checking that this postive difference is less than some small tolerance that we set. To warm up, start my using `abs()` on the numbers $1$ and $-1$ to see that it does indeed make any value you pass into it positive" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "Now, use the tolerance defined below to check if $\\frac{1}{49}\\times 49$ and $1$ are indeed the same value (within said tolerance)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "tolerance = 10 ** (-8) # 0.000000001 - i.e. very small tolerance" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "## Control Flow" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "Ask for user input. If it lies between 8 and 12, print \"Approximately 10\" (if you want to show off, you can do this with one condition using `abs()`)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "Ask the user whether it will rain, requesting that they answer either \"yes\" or \"no\". If their answer is \"yes\" print \"Better bring an umbrella\", and if it's no print \"That's great to hear\". If they answer anything but these two options, print \"I don't understand\"" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "Use `or` twice to check whether a number inputted by the user is one of $6$, $28$, or $496$. If it is, print \"Perfect...\"" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "Python has a useful command `in`, which lets you check if a value is in a list. For example, `5 in [4, 5, 6]` will return `True`. Use this to simplify the previous solution" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": {}, 238 | "outputs": [], 239 | "source": [] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "Ask the user for a number and store this as a float called `num`. Check whether the number could be stored as an integer by seeing if `num` and `int(num)` have the same value. If so, convert `num` to an integer type. (Recall: running `int()` on a decimal number will round it down to the nearest integer)" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": null, 251 | "metadata": {}, 252 | "outputs": [], 253 | "source": [] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "## While Loops" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "Print the first ten squared numbers (you can either times a number by itself to get a square or use `x ** 2`)" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": null, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "Define a string `password` containing a password (not a real one, obviously!). Use a while loop to allow a user, 3 guesses at this password. You might want to keep the number of guesses in a variable called `guesses`, initial set to $0$. You can also use a variable `correct` to keep track of whether they guessed correctly. This should most likely default to `False` and then get set to `True` if they guess correctly" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "The game FizzBuzz goes as follows:\n", 295 | "\n", 296 | "* Count up from one\n", 297 | "* Replace each multiple of 3 with the word \"Fizz\"\n", 298 | "* Replace each multiple of 5 with the word \"Buzz\"\n", 299 | "* Replace each multiple of 15 with the word \"FizzBuzz\"\n", 300 | "* Otherwise, print the number\n", 301 | "\n", 302 | "For example, the sequence begins:\n", 303 | "\n", 304 | "```\n", 305 | "1\n", 306 | "2\n", 307 | "Fizz\n", 308 | "4\n", 309 | "Buzz\n", 310 | "Fizz\n", 311 | "7\n", 312 | "...\n", 313 | "14\n", 314 | "FizzBuzz\n", 315 | "16\n", 316 | "...\n", 317 | "```\n", 318 | "\n", 319 | "Ask for a user inputted number and place FizzBuzz up to that number. Note you can check if `x` is divible by `y` using `x % y == 0`. For example `5 % 2 == 0` is `False` but `9 % 3` is `True`" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": null, 325 | "metadata": {}, 326 | "outputs": [], 327 | "source": [] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "The Collatz conjecture goes as follows\n", 334 | "\n", 335 | "> Take a number $n$. If it is even, halve it, if it is odd, times it by $3$ and add $1$. Repeat this process. You will eventually reach the number $1$\n", 336 | "\n", 337 | "We have not proved that this is definitely true but we have good reason to believe it is. You Python to take any starting number and print out the sequence of numbers generated by the Collatz conjecture, stopping when you reach one" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": null, 343 | "metadata": {}, 344 | "outputs": [], 345 | "source": [] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "What is the first power of two to surpass one million? Use a while loop to find out (Hint: start with a variable `n = 1` and set `n = n * 2` at each iteration, stopping when this goes above one million)" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": {}, 358 | "outputs": [], 359 | "source": [] 360 | } 361 | ], 362 | "metadata": { 363 | "kernelspec": { 364 | "display_name": "Python 3", 365 | "language": "python", 366 | "name": "python3" 367 | }, 368 | "language_info": { 369 | "codemirror_mode": { 370 | "name": "ipython", 371 | "version": 3 372 | }, 373 | "file_extension": ".py", 374 | "mimetype": "text/x-python", 375 | "name": "python", 376 | "nbconvert_exporter": "python", 377 | "pygments_lexer": "ipython3", 378 | "version": "3.7.3" 379 | } 380 | }, 381 | "nbformat": 4, 382 | "nbformat_minor": 2 383 | } 384 | -------------------------------------------------------------------------------- /session_three/session_three_presentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/THargreaves/beginners-python/2fb59b3ae1518db129897019274a56b2e50d0ee5/session_three/session_three_presentation.pptx -------------------------------------------------------------------------------- /session_two/session_two_blank_template.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Two Template" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": { 27 | "colab_type": "text", 28 | "id": "-Ceh1HnZIsVO" 29 | }, 30 | "source": [ 31 | "## User Input" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "colab_type": "text", 38 | "id": "6AZ6H-6jKoSt" 39 | }, 40 | "source": [ 41 | "### Introduction\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "Use the input function to ask the user a question and print the response" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/", 57 | "height": 35 58 | }, 59 | "colab_type": "code", 60 | "id": "D4l_2DsZIlgB", 61 | "outputId": "e63ad5b1-2062-4d8c-9ee7-b03a25d72001" 62 | }, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": { 69 | "colab": { 70 | "base_uri": "https://localhost:8080/", 71 | "height": 35 72 | }, 73 | "colab_type": "code", 74 | "id": "9lLW387eKgNN", 75 | "outputId": "860dcf3b-f3e5-402d-f58f-4d28fb69a6b1" 76 | }, 77 | "source": [ 78 | "Ask for numeric user input and assign this to a variable. Check this variables type and then convert it to an integer. Check the type again, perform some maths on the variable, and print the result" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/", 87 | "height": 35 88 | }, 89 | "colab_type": "code", 90 | "id": "bIr0l5kJI_VV", 91 | "outputId": "c7891804-4acf-4856-e51a-271914a8dc60" 92 | }, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "### Standard Puzzles" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "Remake the farm yard animal puzzle from session one using user input" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "### Bonus Puzzles" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "Try adding `\\r\\n` to the end of your question in `input()`" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "## Variables Types" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "### Introduction" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "Add two strings together to concatenate their contents" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "Multiply a string by an integer" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "Try to multiply a string by a whole number stored as a float type to confirm that you recieve an error" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "### Standard Puzzles" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "What could go wrong with the presentation code?" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "## Summation" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "### Introduction" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "Create a list of numbers" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": null, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "Calculate the sum of these numbers and store this as a variable, then print this value" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "### Standard Puzzles" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": {}, 272 | "source": [ 273 | "Create a list with the values 193, 452, 67, 891, and 10" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "metadata": {}, 280 | "outputs": [], 281 | "source": [] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "Find the sum of the items in the list and store it as a variable" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Use this variable to print the sum as part of a meaningful statement" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": {}, 314 | "source": [ 315 | "### Bonus Puzzles" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "Confirm that lists can contain negative and decimal numbers and that `sum()` behaves as you would expect with these" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "## Min & Max" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "### Introduction" 344 | ] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": {}, 349 | "source": [ 350 | "Create a list of numbers" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": null, 356 | "metadata": {}, 357 | "outputs": [], 358 | "source": [] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "Calculate the minimum of this list and print it in a meaningful statement" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": null, 370 | "metadata": {}, 371 | "outputs": [], 372 | "source": [] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "Repeat this for the maximum" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": null, 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "### Standard Puzzles" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "Create a list of numbers and find the minimum and maximum" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": null, 405 | "metadata": {}, 406 | "outputs": [], 407 | "source": [] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "metadata": {}, 412 | "source": [ 413 | "Use these to find the range of the data and print this in a meaningful statement" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": null, 419 | "metadata": {}, 420 | "outputs": [], 421 | "source": [] 422 | }, 423 | { 424 | "cell_type": "markdown", 425 | "metadata": {}, 426 | "source": [ 427 | "## Length" 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "metadata": {}, 433 | "source": [ 434 | "### Introduction" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "Create a list of numbers and print the number of items it contains" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "metadata": {}, 448 | "outputs": [], 449 | "source": [] 450 | }, 451 | { 452 | "cell_type": "markdown", 453 | "metadata": {}, 454 | "source": [ 455 | "Create a string and print out the number of characters it contains" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": null, 461 | "metadata": {}, 462 | "outputs": [], 463 | "source": [] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "### Standard Puzzles" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": {}, 475 | "source": [ 476 | "Ask the user for a string input" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": null, 482 | "metadata": {}, 483 | "outputs": [], 484 | "source": [] 485 | }, 486 | { 487 | "cell_type": "markdown", 488 | "metadata": {}, 489 | "source": [ 490 | "Output the length of this string" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": null, 496 | "metadata": {}, 497 | "outputs": [], 498 | "source": [] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "metadata": {}, 503 | "source": [ 504 | "### Bonus Puzzles" 505 | ] 506 | }, 507 | { 508 | "cell_type": "markdown", 509 | "metadata": {}, 510 | "source": [ 511 | "Use the supplied code to let the user input the values of a list" 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": null, 517 | "metadata": {}, 518 | "outputs": [], 519 | "source": [] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "Print the length of this list" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": null, 531 | "metadata": {}, 532 | "outputs": [], 533 | "source": [] 534 | } 535 | ], 536 | "metadata": { 537 | "colab": { 538 | "name": "Code Club Session 1", 539 | "provenance": [] 540 | }, 541 | "kernelspec": { 542 | "display_name": "Python 3", 543 | "language": "python", 544 | "name": "python3" 545 | }, 546 | "language_info": { 547 | "codemirror_mode": { 548 | "name": "ipython", 549 | "version": 3 550 | }, 551 | "file_extension": ".py", 552 | "mimetype": "text/x-python", 553 | "name": "python", 554 | "nbconvert_exporter": "python", 555 | "pygments_lexer": "ipython3", 556 | "version": "3.7.3" 557 | } 558 | }, 559 | "nbformat": 4, 560 | "nbformat_minor": 1 561 | } 562 | -------------------------------------------------------------------------------- /session_two/session_two_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Two Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## User Input" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Ask the user for the year, month, and day and use these to print out the date in the following format:\n", 36 | "\n", 37 | "`Your date is 1 January, 1970`\n", 38 | "\n", 39 | "(Hint: if you want to get the comma looking exactly right, you might have to use string addition)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Ask the user to input a decimal number (remember you'll need to use `float()` to convert it from a string). Multiply this $5$, subtract $1.2$ and print the result" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "## Variable Types" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "What happens when you pass a decimal number (such as $3.14$) to `int()`?" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "The `min()` and `max()` functions are versatile. As well being able to input a list of numbers, you are able to pass multiple inputs to these functions, separated by commas, provided they are all of the same type. For example:" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 12, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "1" 105 | ] 106 | }, 107 | "execution_count": 12, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "min(2, 5, 1)" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "The versitility doesn't stop there. What happens if we put in two strings as the inputs to either `min()` or `max()`. Experiment and try to find out.\n", 121 | "\n", 122 | "(Hint: Try comparing each pair of `Cat`, `Dog`, and `Donkey`)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "Say you wanted to add the string `\"Time is 12:\"` to the integer `30` to get the string `\"Time is 12:30\"`. How can you use type conversion to achieve this?" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "## Sum, Min, Max and Length" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "Your teammates are aged 20, 33, 45, 57, and 62. Use `input()` to get Python to ask you your age and then make a list containing the 5 ages above and your age" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "Find the average age of your team and print it\n", 177 | "\n", 178 | "(Hint: The average age is the `sum` of all ages divided by the `len`gth of your list of ages)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "Run the following lines of code to create two lists containing the scores of 30 students in each class" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 13, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "from random import randrange, seed\n", 202 | "seed(1729)\n", 203 | "class_A_scores = [randrange(101) for __ in range(30)]\n", 204 | "class_B_scores = [randrange(101) for __ in range(30)]" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "Use `max()` to find which class contained the highest scoring student" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "As mentioned above, you can find the minimum or maximum of a collection of numbers without having to first put them in a list. Use this approach to find the minumum and maximum of the numbers 4, 5, 2, and 8" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [ 244 | "Ask the user for two strings and print the length of the shortest" 245 | ] 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.7.3" 272 | } 273 | }, 274 | "nbformat": 4, 275 | "nbformat_minor": 2 276 | } 277 | -------------------------------------------------------------------------------- /session_two/session_two_presentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/THargreaves/beginners-python/2fb59b3ae1518db129897019274a56b2e50d0ee5/session_two/session_two_presentation.pptx -------------------------------------------------------------------------------- /session_two/session_two_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\"Open" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "
Spotted a mistake? Report it here
" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "# Beginner's Python—Session Two Homework Exercises" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## User Input" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "Ask the user for the year, month, and day and use these to print out the date in the following format:\n", 36 | "\n", 37 | "`Your date is 1 January, 1970`\n", 38 | "\n", 39 | "(Hint: if you want to get the comma looking exactly right, you might have to use string addition)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 1, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "What is the year? 1970\n", 52 | "What is the month? January\n", 53 | "What is the day? 1\n", 54 | "Your date is 1 January, 1970\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "year = input(\"What is the year? \")\n", 60 | "month = input(\"What is the month? \")\n", 61 | "day = input(\"What is the day? \")\n", 62 | "print(\"Your date is\",\n", 63 | " day, month + \",\", year)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "Ask the user to input a decimal number (remember you'll need to use `float()` to convert it from a string). Multiply this $5$, subtract $1.2$ and print the result" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 2, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "Input a decimal number: 2.718\n", 83 | "12.39\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "number = float(input(\"Input a decimal number: \"))\n", 89 | "print(5 * number - 1.2)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "## Variable Types" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "What happens when you pass a decimal number (such as $3.14$) to `int()`?" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "3\n", 116 | "3\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "print(int(3.14))\n", 122 | "print(int(3.99))" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "The number is rounded **down** to the integer below it. Note, we can round to the nearest integer using `round()`" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "The `min()` and `max()` functions are versatile. As well being able to input a list of numbers, you are able to pass multiple inputs to these functions, separated by commas, provided they are all of the same type. For example:" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 12, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "1" 148 | ] 149 | }, 150 | "execution_count": 12, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "min(2, 5, 1)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "The versitility doesn't stop there. What happens if we put in two strings as the inputs to either `min()` or `max()`. Experiment and try to find out.\n", 164 | "\n", 165 | "(Hint: Try comparing each pair of `Cat`, `Dog`, and `Donkey`)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 5, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "Dog\n", 178 | "Donkey\n", 179 | "Donkey\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "print(max(\"Cat\", \"Dog\"))\n", 185 | "print(max(\"Dog\", \"Donkey\"))\n", 186 | "print(max(\"Cat\", \"Donkey\"))" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "When `max()` or `min()` act on strings, they order items alphabetically. Therefore, `max()` returns the word you'd find last in the dictionary, and `min()` the first" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "Say you wanted to add the string `\"Time is 12:\"` to the integer `30` to get the string `\"Time is 12:30\"`. How can you use type conversion to achieve this?" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 6, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "'Time is 12:30'" 212 | ] 213 | }, 214 | "execution_count": 6, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "\"Time is 12:\" + str(30)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "## Sum, Min, Max and Length" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "Your teammates are aged 20, 33, 45, 57, and 62. Use `input()` to get Python to ask you your age and then make a list containing the 5 ages above and your age" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 8, 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "What is your age? 21\n" 247 | ] 248 | } 249 | ], 250 | "source": [ 251 | "your_age = int(input(\"What is your age? \"))\n", 252 | "team_ages = [20, 33, 45, 57, 62, your_age]" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "Find the average age of your team and print it\n", 260 | "\n", 261 | "(Hint: The average age is the `sum` of all ages divided by the `len`gth of your list of ages)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 9, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "Average team age: 39.666666666666664\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "print(\"Average team age:\", sum(team_ages) / len(team_ages))" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "You could round this to two decimal places using `round(..., 2)`" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "Run the following lines of code to create two lists containing the scores of 30 students in each class" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 10, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "from random import randrange, seed\n", 302 | "seed(1729)\n", 303 | "class_A_scores = [randrange(101) for __ in range(30)]\n", 304 | "class_B_scores = [randrange(101) for __ in range(30)]" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "Use `max()` to find which class contained the highest scoring student" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 11, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "name": "stdout", 321 | "output_type": "stream", 322 | "text": [ 323 | "Best score in class A: 97\n", 324 | "Best score in class B: 92\n" 325 | ] 326 | } 327 | ], 328 | "source": [ 329 | "print(\"Best score in class A: \", max(class_A_scores))\n", 330 | "print(\"Best score in class B: \", max(class_B_scores))" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "The top student was in class A with a score of $97$" 338 | ] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": {}, 343 | "source": [ 344 | "As mentioned above, you can find the minimum or maximum of a collection of numbers without having to first put them in a list. Use this approach to find the minumum and maximum of the numbers 4, 5, 2, and 8" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 12, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "name": "stdout", 354 | "output_type": "stream", 355 | "text": [ 356 | "Max: 8\n", 357 | "Min: 2\n" 358 | ] 359 | } 360 | ], 361 | "source": [ 362 | "print(\"Max:\", max(4, 5, 2, 8))\n", 363 | "print(\"Min:\", min(4, 5, 2, 8))" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "Ask the user for two strings and print the length of the shortest" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 14, 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "What is your first string? A short string\n", 383 | "What is your second string? An incredibly, astonishingly, uncommonly, tremendously, long string\n", 384 | "The shortest string has 14 characters\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "string_1 = input(\"What is your first string? \")\n", 390 | "string_2 = input(\"What is your second string? \")\n", 391 | "print(\"The shortest string has\",\n", 392 | " min(len(string_1), len(string_2)),\n", 393 | " \"characters\")" 394 | ] 395 | } 396 | ], 397 | "metadata": { 398 | "kernelspec": { 399 | "display_name": "Python 3", 400 | "language": "python", 401 | "name": "python3" 402 | }, 403 | "language_info": { 404 | "codemirror_mode": { 405 | "name": "ipython", 406 | "version": 3 407 | }, 408 | "file_extension": ".py", 409 | "mimetype": "text/x-python", 410 | "name": "python", 411 | "nbconvert_exporter": "python", 412 | "pygments_lexer": "ipython3", 413 | "version": "3.7.3" 414 | } 415 | }, 416 | "nbformat": 4, 417 | "nbformat_minor": 2 418 | } 419 | --------------------------------------------------------------------------------