diff --git a/Day_1/00_Intro.md b/Day_1/00_Intro.md deleted file mode 100644 index 3f0f66d..0000000 --- a/Day_1/00_Intro.md +++ /dev/null @@ -1,69 +0,0 @@ -# Python Basics: 0-1 Introduction. - -This unit provides a basic introduction to Python. By the end of the series, you should be able to: - -1. Run Python from the Anaconda Navigator in a Jupyter Notebook -2. Write basic commands using Python syntax -3. Grasp the major Python [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) [types](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#type), including [integers](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#integer), [floats](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#floating-point-number), [strings](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string), lists, sets, and dictionaries -4. Operate and manipulate those objects -5. Integrate choices into your programs using [conditionals](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#conditional-statement) - - -# What is Programming - -> ## Learning Objectives -> -> * Explain the difference between knowing a programming language and knowing how to program. -> * Explain how programming languages can differ. -> * Give useful debugging tips. -> * Offer helpful resource websites. -> * Explain how to google an error. - -### What it means to "know how to program" - -Most programmers can program in more than one language. That's because they know *how to program* generally, as opposed to "knowing" Python, R, Ruby, or whatever. - -In other words, programming is an extendible skill. Basic programming concepts -- conditionals, for loops, functions -- can be found in almost any programming language. - -That being said, programming languages differ from one another in the following ways: - -1. **Syntax**: whether to add a semicolon at the end of each line, etc. -2. **Usage**: JavaScript is for building websites, R is for statistics, Python is general purpose, etc. -3. **Level**: how close you are to the hardware. 'C' is often considered to be the lowest (or one of the lowest) level languages. -4. **Object-oriented:** "objects" are data + functions. Some programming languages are object-oriented (e.g. Python) and some aren't (e.g. C). -5. **Many more**: Here's a [list](https://en.wikipedia.org/wiki/List_of_programming_languages_by_type) of all the types of programming languages out there. - -So what should your first programming language be? That is, what programming language should you use to learn *how to program*? At the end of the day, the answer depends on what you want to get out of programming. Many people recommend Python because its fun, easy, and multi-purpose. Here's an [article](http://lifehacker.com/which-programming-language-should-i-learn-first-1477153665) that can offer more advice. - -Regardless of what you choose, you will probably grow 'comfortable' in one language while learning the basic concepts and skills that allow you to 'hack' your way into other languages. - -Thus "knowing how to program" means learning how to *think* like a programmer, not necessarily knowing all the language-specific commands off the top of your head. **Don't learn specific programming languages; learn how to program.** - -### What programming is like - -![xkcd](http://sslimgs.xkcd.com/comics/wisdom_of_the_ancients.png) - -Here's the sad reality: When you're programming, 80% or more of your time will be spent debugging, looking stuff up (like program-specific syntax, [documentation](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#documentation) for packages, useful functions, etc.), or testing. This does not just apply to beginner or intermediate programmers, although you will grow more "fluent" over time. - -If you're a good programmer, you're a good detective! - -### Debugging - -1. Read the errors! -2. Read the documentation -2. Make it smaller -3. Figure out what changed -4. Check your syntax -5. Print statements are your friend - -### Googling Errors - -* google: name-of-program + text in error message -* Remove user- and data-specific information first! -* See if you can find examples that do and don’t produce the error - -### Stack Overflow Example - -Often when you google something, the most relevant and helpful result will be someone asking a similar question on Stack Overflow. Just to give you an example of what this looks like, [this](https://stackoverflow.com/questions/1228299/change-one-character-in-a-string) is the first result if you google "python change one character in a string". When we talk about strings and lists, you'll see why this might be a question you have! - -You'll see that someone asks a question, with an example of their question or problem, and then other users respond. If you look carefully, you'll see that some answers are more detailed than others, and that there is more than one way to go about doing something. diff --git a/Day_1/madlib.py b/Day_1/madlib.py deleted file mode 100644 index a130c8a..0000000 --- a/Day_1/madlib.py +++ /dev/null @@ -1,45 +0,0 @@ -from __future__ import print_function - -""" -String Substitution for a Mad Lib -Adapted from code by Kirby Urner -""" - -story = """ -Once upon a time, deep in an ancient jungle, -there lived a %(animal)s. This %(animal)s -liked to eat %(food)s, but the jungle had -very little %(food)s to offer. One day, an -explorer found the %(animal)s and discovered -it liked %(food)s. The explorer took the -%(animal)s back to %(city)s, where it could -eat as much %(food)s as it wanted. However, -the %(animal)s became homesick, so the -explorer brought it back to the jungle, -leaving a large supply of %(food)s. - -The End -""" - - -def tellStory(): - userPicks = dict() - - print() - - addPick('animal', userPicks) - addPick('food', userPicks) - addPick('city', userPicks) - print(story % userPicks) - - -def addPick(cue, dictionary): - prompt = "Enter a specific example for %s: " % cue - try: - dictionary[cue] = str(raw_input(prompt)) - except: - dictionary[cue] = str(input(prompt)) - -tellStory() - -print() diff --git a/lessons/Part1/00_Introduction.md b/lessons/Part1/00_Introduction.md new file mode 100644 index 0000000..076ed65 --- /dev/null +++ b/lessons/Part1/00_Introduction.md @@ -0,0 +1,75 @@ +# Python Basics: Introduction + +This unit provides a basic introduction to Python. By the end of the series, you should be able to: + +1. Run Python from the Anaconda Navigator in a Jupyter Notebook. +2. Write basic commands using Python syntax. +3. Grasp the major Python [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) [types](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#type), including [integers](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#integer), [floats](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#floating-point-number), [strings](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string), lists, sets, and dictionaries +4. Operate and manipulate those objects. +5. Control the flow of your programs using [conditionals](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#conditional-statement). + +# What is Programming? + +> ## Learning Objectives +> +> * Explain the difference between knowing a programming language and knowing how to program. +> * Explain how programming languages can differ. +> * Give useful debugging tips. +> * Offer helpful resource websites. +> * Explain how to Google an error. + +## What it means to "know how to program" + +Most programmers can program in more than one language. That's because they know *how to program* generally, as opposed to "knowing" Python, R, Ruby, or any other language. + +In other words, programming is an extendible skill. Basic programming concepts - conditionals, for loops, functions - can be found in almost any programming language. + +That being said, programming languages differ from one another in the following ways: + +1. **Syntax**: The precise rules of how to structure code in a language. For example, whether to add a semicolon at the end of each line. +2. **Usage**: Different programming languages are designed for different goals. For example, JavaScript is generally for building websites, R is primarily a statistical programming language, Python is a general purpose programming lanaguage, etc. +3. **Level**: How close you are to the hardware. Any programming code ultimately ends up as machine code. The difference between languages lies in how they are represented to the user. For example, C is often considered lower level language than others, because certain concepts - such as memory management - are not abstracted away in C. +4. **Object-oriented:** Many programming languages are object-oriented in that they provide functionality to create objects, which allow the user to organize code and functions in useful modules. +5. **Many more**: Here's a [list](https://en.wikipedia.org/wiki/List_of_programming_languages_by_type) of all the types of programming languages out there. + +So what should your first programming language be? That is, what programming language should you use to learn *how to program*? At the end of the day, the answer depends on what you want to get out of programming. Many people recommend Python because its fun, easy, and multi-purpose. Here's an [article](http://lifehacker.com/which-programming-language-should-i-learn-first-1477153665) that can offer more advice. + +Regardless of what you choose, you will probably grow "comfortable" in one language while learning the basic concepts and skills that allow you to "hack" your way into other languages. + +Thus "knowing how to program" means learning how to *think* like a programmer, not necessarily knowing all the language-specific commands off the top of your head. **Don't learn specific programming languages; learn how to program.** + +## What programming is like + +![xkcd](http://sslimgs.xkcd.com/comics/wisdom_of_the_ancients.png) + +Here's the sad reality: When you're programming, 80% or more of your time will be spent debugging, looking stuff up (like program-specific syntax, [documentation](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#documentation) for packages, useful functions, etc.), or testing. This does not just apply to beginner or intermediate programmers, although you will grow more "fluent" over time. + +If you're a good programmer, you're a good detective! + +## Debugging + +Here's a useful mental workflow to keep in mind when you want to try and debug an error: + +1. Read the errors! +2. Read the documentation. +3. Make it smaller. +4. Figure out what changed. +5. Check your syntax. +6. Print statements are your friend. +7. Even better than print statements - built-in debuggers! These are more advanced, but worth learning how to use when you're ready. + +## Googling Errors + +Here are some tips on how to use Google to resolve errors you run might into: + +* Enter in Google the name of the computer language and the text in error message. +* Be sure to remove user- and data-specific information first! +* See if you can find examples that do and don’t produce the error. + +## Stack Overflow Example + +Often when you Google something, the most relevant and helpful result will be someone asking a similar question on StackOverflow. Just to give you an example of what this looks like, [this](https://stackoverflow.com/questions/1228299/change-one-character-in-a-string) is one of the top results if you Google "python change one character in a string". When we talk about strings and lists, you'll see why this might be a question you have! + +StackOverflow is a great resource by which people can ask and answer questions. In the above case, there's a lot of different answers to the question -- some more detailed than others -- and you can go with the approach that works best for you. + +Don't reinvent the wheel -- learning how to find the answer to the issues you run into is a critical part of becoming a capable programmer! \ No newline at end of file diff --git a/Day_1/01_Running-Python.md b/lessons/Part1/01_Running_Python.md similarity index 75% rename from Day_1/01_Running-Python.md rename to lessons/Part1/01_Running_Python.md index 89b1777..3c97ec4 100644 --- a/Day_1/01_Running-Python.md +++ b/lessons/Part1/01_Running_Python.md @@ -1,26 +1,25 @@ -# Python Basics: 1 Running Python +# Python Basics: Running Python > ## Questions > How can I run Python programs? > > ## Learning Objectives > -> * Running Python in Anaconda IDE +> * Running Python in IDEs > * Jupyter Notebooks > * Download the training material > * Open Jupyter Notebook -## Running Python in Anaconda IDE +## Running Python in Integrated Development Environments -It's common to write python programs using either a text editor or an interactive/integrated development environment (IDE). An IDE is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools, and a debugger. Some of them come with package managers and other features, too. - -There are many Python IDE's. You can see a comparison [here](https://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments#Python) +It's common to write Python programs using either a text editor or an integrated development environment (IDE). An IDE is a software application that provides a comprehensive suite of tools to programmers for software development. An IDE normally consists of a source code editor, build automation tools, and a debugger. Some of them come with package managers and other features, too. +There are many Python IDE's. You can see a comparison [here](https://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments#Python). ## Jupyter Notebooks -This course will be using a Jupyter Notebook to interact with Python. The bit of extra setup is well worth it because the Notebook provides code completion and other helpful features. +In this course, we interact with Python using Jupyter Notebooks. Using Jupyter Notebooks will require some extra setup. It will be well worth it, however, as Jupyter Notebooks are among the most powerful (and heavily used) tools for conducting data science in Python. Jupyter Notebooks are included in the Anaconda distribution which you installed. Notebook files have the extension ".ipynb" to distinguish them from plain-text Python programs. diff --git a/Day_1/02_Jupyter Notebooks.ipynb b/lessons/Part1/02_Jupyter Notebooks.ipynb similarity index 83% rename from Day_1/02_Jupyter Notebooks.ipynb rename to lessons/Part1/02_Jupyter Notebooks.ipynb index e77b827..03eedc4 100644 --- a/Day_1/02_Jupyter Notebooks.ipynb +++ b/lessons/Part1/02_Jupyter Notebooks.ipynb @@ -26,24 +26,24 @@ "source": [ "## Introduction\n", "\n", - "You've just got a basic intro to programming in python. Now, we are going to get our hands dirty, but...\n", + "We've just covered a basic introduction to programming in Python. Now, we are going to get our hands dirty, but...\n", "\n", "**Don't panic!**\n", "\n", - "*You can't break anything!* You can always get another copy of any notebook from the workshop on the [github repo](https://github.com/dlab-berkeley/python-intensive)." + "*You can't break anything!* You can always get another copy of any notebook from the workshop on the [Github repo](https://github.com/dlab-berkeley/python-intensive)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Navigating in Jupyter Notebook\n", + "## Navigating in Jupyter Notebooks\n", "\n", - "Jupyter Notebooks have more useful features for interactive use than the standard python interpreter, but they work in the same basic way: you type things and then execute them.\n", + "Jupyter Notebooks have more useful features for interactive use than the standard Python interpreter, but they work in the same basic way: you type things and then execute them.\n", "\n", - "Unlike many graphical systems, there is no button to run your code! Instead, **you run code using Shift-Enter**. This also moves you to the next box (or \"cell\") for code below the one you just ran (but this may change in the future).\n", + "In Jupyter Notebooks, code is typed into \"cells\". Cells are individual units in which code can be separately run. In contrast to many IDEs, running code is done with a keyword combination: `Shift+Enter`. Running `Shift+Enter` on a selected cell will run the code in the cell and then automatically move your cursor to the following cell.\n", "\n", - "Try to **run the following code using Shift-Enter** now!" + "Try to run the following code using `Shift+Enter` now!" ] }, { @@ -61,7 +61,7 @@ "source": [ "If you hit **Enter** only, Jupyter Notebook gives you another line in the current cell.\n", "\n", - "This allows you to [compose](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#compose) multi-line commands and submit them to python all at once." + "This allows you to [compose](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#compose) multi-line commands and submit them to Python all at once." ] }, { @@ -78,7 +78,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Control-Enter** executes the cell and does not move to the next cell.\n", + "`Control+Enter` executes the cell and does not move to the next cell.\n", "\n", "You can enter the same line over and over again into the interpreter." ] @@ -96,7 +96,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Try entering this cell a few times:" + "Try using `Control+Enter` to run this cell a few times:" ] }, { @@ -116,11 +116,18 @@ "If you want to create new empty cells, you can use Insert -> Insert Cell Below or use the Insert Cell Below button at the top of the notebook. Try entering a new cell below this one." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "You can also split cells by putting your cursor on the line you want to split, and clicking on Edit --> Split cell. Split the cell below after the `c = 2` line" + "You can also split cells by putting your cursor on the line where you want to split, and clicking on Edit -> Split cell. Split the cell below after the `c = 2` line. How the location of the cursor impact the split? Try it different places and find out!" ] }, { @@ -132,6 +139,7 @@ "a = 1\n", "b = 2\n", "c = 2\n", + "\n", "d = a + b + c" ] }, @@ -139,7 +147,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You merge cells by clicking the Edit --> Merge cell Above / Below. Go back to the cells you split and merge them back into one cell." + "You merge cells by clicking the Edit -> Merge cell Above / Below. Go back to the cells you split and merge them back into one cell." ] }, { @@ -148,7 +156,7 @@ "source": [ "## Markdown\n", "\n", - "Jupyter notebooks allow you type in markdown as well as code. In fact, this very cell is written in Markdown!\n", + "Jupyter notebooks allow you type in Markdown as well as code. In fact, this very cell is written in Markdown!\n", "\n", "Markdown is a lightweight markup language with plain text formatting syntax designed so that it can be converted to HTML and many other formats. Markdown has its own syntax, but it's easy to learn. Here's a [cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) that can help.\n", "\n", @@ -190,9 +198,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "By default, cells in jupyter notebook are code cells, which means it's expecting you to enter Python code. But you can change the cell format by changing the dropdown item on the top from \"Code\" to \"Markdown\".\n", + "By default, cells in Jupyter notebook are code cells, which means it's expecting you to enter Python code. But you can change the cell format by changing the dropdown item on the top from \"Code\" to \"Markdown\".\n", "\n", - "Enter some markdown text in the cell below, and execute it. Be sure to change the cell from code to markdown!" + "Enter some Markdown text in the cell below, and execute it. Be sure to change the cell from code to Markdown!" ] }, { @@ -209,10 +217,11 @@ "## Keyboard shortcuts\n", "\n", "There are many useful keyboard shortcuts in Jupyter Notebooks. Check some of them out [here](https://www.cheatography.com/weidadeyue/cheat-sheets/jupyter-notebook/)! Below are five essential ones. \n", - "* When you click a **code** cell, the border is green (\"editing mode\") and means you can edit code in that cell. \n", - "* We first need to turn this green border to blue (\"command mode\") before we can use the shortcuts (**markdown** cell borders are blue by default). \n", "\n", - "Press the `Esc` key to ensure that a code cell border is blue, or simply click a markdown cell: \n", + "* When you click a **code** cell, the border is green (\"editing mode\") which means you can edit code in that cell. \n", + "* We first need to turn this green border to blue (\"command mode\") before we can use the shortcuts (Markdown cell borders are blue by default). \n", + "\n", + "Press the `Esc` key to ensure that a code cell border is blue, or simply click a Markdown cell: \n", "\n", "1. Press the `a` key to add a cell above the current one\n", "2. Press `b` to add a cell below the current one\n", @@ -225,13 +234,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Error Reporting" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ + "## Error Reporting\n", + "\n", "What happens if you run this cell?" ] }, @@ -255,13 +259,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## History" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ + "## History\n", + "\n", "In order to see all the commands you've run so far in the notebook, use the `%history` built-in command. This is unique to Jupyter Notebook. See more [here](http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-history)." ] }, @@ -289,7 +288,7 @@ "\n", "Jupyter remembers everything it executed, **even if it's not currently displayed in the notebook**.\n", "\n", - "To clear everything from Jupyter use Kernel-->Restart in the menu." + "To clear everything from Jupyter use Kernel -> Restart in the menu." ] }, { @@ -307,7 +306,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now use Kernel-->Restart in the menu!" + "Now use Kernel -> Restart in the menu! You can also press the \"Reset\" button in the icon bar." ] }, { @@ -455,14 +454,12 @@ "\n", "When you close your Jupyter notebook window, all of your values will be lost. But you can save your code for a later time.\n", "\n", - "First go to File --> Close and Halt in order to shutdown the notebook you are using. Once all notebooks are shutdown, to end the Jupyter server, go back to the Anaconda Navigator and shut it down. You may get a warning dialog box alerting you that Jupyter Notebook is still running. Just click **Quit** to shut everything down." + "First go to File -> Close and Halt in order to shutdown the notebook you are using and close its window. Once all notebooks are shutdown, to end the Jupyter server, go back to the Anaconda Navigator and shut it down. You may get a warning dialog box alerting you that Jupyter Notebook is still running. Just click **Quit** to shut everything down." ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": true - }, + "metadata": {}, "source": [ "## Challenge 1" ] @@ -474,7 +471,7 @@ "- Clear this notebook again.\n", "- Save the notebook.\n", "- Close this notebook correctly.\n", - "- Shutdown Jupyter notebook. Reopen Jupyter notebook and open the notebook 03_Variables_Assignment." + "- Shutdown Jupyter notebook. Reopen Jupyter notebook and open the notebook `03_Variables_Assignment.ipynb`." ] }, { @@ -487,7 +484,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -501,7 +498,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.9.7" }, "toc": { "base_numbering": 1, @@ -547,5 +544,5 @@ } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 } diff --git a/Day_1/03_Variables_Assignment.ipynb b/lessons/Part1/03_Variables_Assignment.ipynb similarity index 66% rename from Day_1/03_Variables_Assignment.ipynb rename to lessons/Part1/03_Variables_Assignment.ipynb index 2b1650c..9875982 100644 --- a/Day_1/03_Variables_Assignment.ipynb +++ b/lessons/Part1/03_Variables_Assignment.ipynb @@ -2,9 +2,7 @@ "cells": [ { "cell_type": "markdown", - "metadata": { - "collapsed": true - }, + "metadata": {}, "source": [ "# Variables and Assignment\n", "\n", @@ -13,25 +11,23 @@ "- Challenges: 10 min\n", "\n", "**Questions**\n", - "- \"How can I store data in programs?\"\n", + "- How can I store data in programs?\n", "\n", "**Learning Objectives**\n", - "- \"Write programs that [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) scalar values to variables and perform calculations with those values.\"\n", - "- \"Correctly trace value changes in programs that use scalar assignment.\"\n", + "- Write programs that [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) scalar values to variables and perform calculations with those values.\n", + "- Correctly trace value changes in programs that use scalar assignment.\n", "\n", "* * * * *" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": true - }, + "metadata": {}, "source": [ - "## Use variables to store values.\n", + "## Use variables to store values\n", "\n", - "* Variables are names for values.\n", - "* In Python the `=` symbol assigns the value on the right to the name on the left.\n", + "* Variables are placeholders for useful values. They have meaningful names, which makes it easier to reuse those values.\n", + "* In Python, the `=` symbol assigns the value on the right to the name on the left.\n", "* The variable is created when a value is assigned to it.\n", "* Here's Python code that assigns an age to a variable `age`\n", " and a name in quotation marks to a variable `first_name`.\n" @@ -51,16 +47,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Variable names:\n", - " * cannot start with a digit\n", - " * cannot contain spaces, quotation marks, or other punctuation\n", - " * *may* contain an underscore (typically used to separate words in long variable names)\n", - "* Underscores at the start like `__alistairs_real_age` have a special meaning\n", - " so we won't do that until we understand the convention.\n", + "* Variable names must follow a few rules:\n", + " * They cannot start with a digit.\n", + " * They cannot contain spaces, quotation marks, or other punctuation.\n", + " * They *may* contain an underscore (typically used to separate words in long variable names).\n", + "* Underscores at the beginning of a variable name, such as `__first_name`, have a special meaning.\n", + " We won't use this format until we understand the convention.\n", "\n", - "## Use `print` to display values.\n", + "## Use `print` to display values\n", "\n", - "* Python has a built-in function called `print` that prints things as text.\n", + "* Python has a built-in function called `print` that prints output given some inputs.\n", "* Call the function (i.e., tell Python to run it) by using its name.\n", "* Provide values to the function (e.g., things to print) in parentheses.\n" ] @@ -78,17 +74,17 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* `print` automatically puts a single space between items to separate them.\n", - "* And wraps around to a new line at the end.\n", + "* `print` automatically puts a single space between different inputs to separate them.\n", + "* It also adds a new line at the end. Try entering multiple `print` statements to see this.\n", "\n", - "## Variables persist between cells.\n", + "## Variables persist between cells\n", "\n", "* Variables defined in one cell exist in all following cells.\n", "* Notebook cells are just a way to organize a program:\n", " as far as Python is concerned,\n", " all of the source code is one long set of instructions.\n", "\n", - "## Variables must be created before they are used.\n", + "## Variables must be created before they are used\n", "\n", "* If a variable doesn't exist yet, or if the name has been mis-spelled,\n", " Python reports an error.\n" @@ -110,7 +106,7 @@ "* The last line of an error message is usually the most informative.\n", "* We will look at error messages in detail [later](https://github.com/dlab-berkeley/python-intensive/blob/master/Day_3/15_Errors.ipynb).\n", "\n", - "## Python is case-sensitive.\n", + "## Python is case-sensitive\n", "\n", "* Python thinks that upper- and lower-case letters are different,\n", " so `Name` and `name` are different variables.\n", @@ -118,7 +114,7 @@ " there are conventions around using upper-case letters at the start of variable names\n", " so we will use lower-case letters for now.\n", "\n", - "## Use meaningful variable names.\n", + "## Use meaningful variable names\n", "\n", "* Python doesn't care what you call variables as long as they obey the rules\n", " (alphanumeric characters and the underscore).\n" @@ -145,7 +141,7 @@ "## Variables can be used in calculations.\n", "\n", "* We can use variables in calculations just as if they were values.\n", - " * Remember, we assigned 42 to `age` a few lines ago." + "* Remember, we assigned 42 to `age` a few lines ago." ] }, { @@ -158,56 +154,14 @@ "print('Age in three years:', age)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Namespace\n", - "\n", - "To display the scope namespace of variables we can use the `dir` function:\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dir()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To get a dictionary of local variables, we can call `locals`:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "locals()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With both functions, we see a lot of predefined variables in the Python and Jupyter environment, but also `age`, `first_name`, `flabadab`, and `ewr_422_yY`, which we created above." - ] - }, { "cell_type": "markdown", "metadata": {}, "source": [ "# `%who` and `%whos`\n", "\n", - "If `dir()` and `locals()` are too confusing, try these [magic](https://towardsdatascience.com/top-10-magic-commands-in-python-to-boost-your-productivity-1acac061c7a9) [commands](https://ipython.readthedocs.io/en/stable/interactive/magics.html) instead. \n", - "\n", - "`%who` will return the variables you have saved:" + "* There are a couple \"magic\" commands you can run to help you keep track of your variables.\n", + "* `%who` will return the variables you have saved:" ] }, { @@ -223,7 +177,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "`%whos` will display the variables, their type, and information about the data they contain:" + "* `%whos` will display the variables, their type, and information about the data they contain:" ] }, { @@ -239,10 +193,10 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Challenge 1: Making and Printing Variables\n", + "## Challenge 1: Creating and Printing Variables\n", "\n", - "1. Make 3 variables: `name` (with your full name), `city` (where you were born) and `year` (when you were born).\n", - "2. Print these three variables so that it prints `[your name] was born in [city] in [year]`" + "1. Create 3 variables: `name` (with your full name), `city` (where you were born) and `year` (when you were born).\n", + "2. Print these three variables with the `print` function so that it prints `[your name] was born in [city] in [year]`" ] }, { @@ -258,7 +212,7 @@ "source": [ "## Challenge 2: Swapping Values\n", "\n", - "Draw a table showing the values of the variables in this program after each statement is executed.\n", + "Create a table showing the values of the variables in this program after each statement is executed.\n", "\n", "In simple terms, what do the last three lines of this program do?" ] @@ -350,29 +304,22 @@ "source": [ "*****\n", "\n", - "## Keypoints\n", + "## Key Points\n", "\n", - "1. \"Use variables to store values.\"\n", - "2. \"Use `print` to display values.\"\n", - "3. \"Variables persist between cells.\"\n", - "4. \"Variables must be created before they are used.\"\n", - "5. \"Python is case-sensitive.\"\n", - "6. \"Variables can be used in calculations.\"\n", - "7. \"Use meaningful variable names.\"" + "1. Use variables to store meaningful values.\n", + "2. Use `print` to display values.\n", + "3. Variables persist between cells.\n", + "4. Variables must be created before they are used.\n", + "5. Python is case-sensitive.\n", + "6. Variables can be used in calculations.\n", + "7. Use meaningful variable names." ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -386,7 +333,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.9.7" }, "toc": { "base_numbering": 1, @@ -432,5 +379,5 @@ } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 } diff --git a/Day_1/04_Types_Conversion.ipynb b/lessons/Part1/04_Types_Conversion.ipynb similarity index 72% rename from Day_1/04_Types_Conversion.ipynb rename to lessons/Part1/04_Types_Conversion.ipynb index 16f5a8f..99bb040 100644 --- a/Day_1/04_Types_Conversion.ipynb +++ b/lessons/Part1/04_Types_Conversion.ipynb @@ -11,13 +11,13 @@ "- Challenges: 15 min\n", "\n", "**Questions**\n", - "- \"What kinds of data do programs store?\"\n", - "- \"How can I convert one [type](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#type) to another?\"\n", + "- What kinds of data do programs store?\n", + "- How can I convert one [type](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#type) to another?\n", "\n", "**Learning Objectives**\n", - "- \"Explain key differences between [integers](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#integer) and [floating point numbers](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#floating-point-number).\"\n", - "- \"Explain key differences between numbers and [character strings](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string).\"\n", - "- \"Use built-in functions to convert between integers, floating point numbers, and strings.\"\n", + "- Explain key differences between [integers](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#integer) and [floating point numbers](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#floating-point-number).\n", + "- Explain key differences between numbers and [character strings](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string).\n", + "- Use built-in functions to convert between integers, floating point numbers, and strings.\n", "* * * * *" ] }, @@ -25,21 +25,19 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Every value has a type.\n", + "## Every value has a type\n", "\n", - "* Every value in a program has a specific type.\n", - "* Integer (`int`): counting numbers like 3 or -512.\n", - "* Floating point number (`float`): fractional numbers like 3.14159 or -2.5.\n", - " * Integers are used to count, floats are used to measure.\n", - "* Character string (usually just called \"string\", `str`): text.\n", - " * Written in either single quotes or double quotes (as long as they match).\n", - " * The quotation marks aren't printed when the string is displayed.\n", + "* Every value in a program has a specific **type**.\n", + " * Integers (`int`): whol numbers such as 3 or -512.\n", + " * Floating point numbers (`float`): decimal numbers such as 3.14159 or -2.5.\n", + " * Character strings (usually just called \"string\", `str`): text.\n", + " * Written in either single quotes or double quotes (as long as they match).\n", + " * The quotation marks aren't printed when the string is displayed.\n", "\n", - "## Use the built-in function `type` to find the type of a value.\n", + "## Use the built-in function `type` to find the type of a variable\n", "\n", - "* Use the built-in function `type` to find out what type a value has.\n", - "* Works on variables as well.\n", - " * But remember: the *value* has the type --- the *variable* is just a label.\n" + "* Use the built-in function `type` to find out what type a value or variable has.\n", + "* But remember: the *value* has the type - the *variable* is just a label.\n" ] }, { @@ -75,9 +73,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Types control what operations can be done on values.\n", + "## Types control what operations can be done on values\n", "\n", - "* A value's type determines what the program can do to it." + "* A value's type determines how different functions and operations impact it." ] }, { @@ -102,7 +100,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Strings can be added and multiplied.\n", + "## Addition and multiplication operations can be applied to strings\n", "\n", "* \"Adding\" character strings concatenates them.\n" ] @@ -121,8 +119,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Multiplying a character string by an [integer](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#integer) replicates it.\n", - " * Since multiplication is just repeated addition.\n" + "* Multiplying a character string by an [integer](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#integer) replicates it. This follows from multiplication as \"repeated addition\" - in this case, it is repeated concatenation.\n", + "* Notice, however, that the multiplication only works between a string and an integer - not between two different strings." ] }, { @@ -135,11 +133,20 @@ "print(separator)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "times = '=' * 'x'" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Strings have a length (but numbers don't).\n", + "## Strings have a length (but numbers don't)\n", "\n", "* The built-in function `len` counts the number of characters in a string." ] @@ -173,9 +180,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Must convert numbers to strings or vice versa when operating on them.\n", + "## Addition on string and numeric types requires casting\n", "\n", - "* Cannot add numbers and strings." + "* The addition operator cannot be applied to different types (in contrast to multiplication)." ] }, { @@ -191,9 +198,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Not allowed because it's ambiguous: should `1 + '2'` be `3` or `'12'`?\n", - "* Use the name of a type as a function to convert a value to that type.\n", - "* This conversion is also called *casting* a value." + "* This is not allowed because it's ambiguous. Should `1 + '2'` be `3` or `'12'`?\n", + "* We have to force the values to be the same type - either the int becomes a string, or vice versa.\n", + "* This conversion is also called *casting*. It can be done with either the `int` function or the `str` function." ] }, { @@ -210,10 +217,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Can mix integers and floats freely in operations.\n", + "## Integers and floats can be freely mixed in arithmetic.\n", "\n", - "* Integers and floating-point numbers can be mixed in arithmetic.\n", - " * Python automatically converts integers to floats as needed." + "* Python automatically converts integers to floats as needed when performing arithmetic." ] }, { @@ -230,7 +236,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Variables only change value when something is assigned to them.\n", + "## Integers, strings, and floats only change value when re-assigned\n", "\n", "* If we make one cell in a spreadsheet depend on another,\n", " and update the latter,\n", @@ -256,25 +262,16 @@ "source": [ "* The computer reads the value of `first` when doing the multiplication,\n", " creates a new value, and assigns it to `second`.\n", - "* After that, `second` does not remember where it came from." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* The computer reads the value of `first` when doing the multiplication,\n", - " creates a new value, and assigns it to `second`.\n", - "* After that, `second` does not remember where it came from." + "* After that, `second` does not update since `first` changed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Challenge 1: Making and Casting Variables\n", + "## Challenge 1: Creating and Casting Variables\n", "\n", - "1. Make a variable `year` and [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) it as the year you were born\n", + "1. Create a variable `year` and [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) it to the year you were born.\n", "2. Cast that variable to a float, and assign it to a new variable `year_float`\n", "3. Cast `year_float` to a string, and assign it to a new variable `year_string`\n", "4. Someone in your class says they were born in 1997. Find out what your age difference is, using only `year_string`.\n", @@ -304,21 +301,23 @@ "metadata": {}, "outputs": [], "source": [ - "print('5 // 3:', 5//3)\n", - "print('5 % 3:', 5%3)" + "print('5 // 3:', 5 // 3)\n", + "print('5 % 3:', 5 % 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "Now, consider the following scenario.\n", + "\n", "You are ordering pizza for a campus event.\n", "\n", "You have ordered 8 pizzas, with 8 slices each, for a total of 64 slices.\n", "\n", "You know that everyone attending will eat exactly 3 slices of pizza. You want to make sure that everyone who comes gets enough pizza, even if that means a slice or two will be left over.\n", "\n", - "Write an expression that calculates how many students you can satisfactorily feed with 64 slices of pizza, provided that each will eat 3 slices." + "Write an expression that calculates how many students you can satisfactorily feed with 64 slices of pizza, provided that each will eat 3 slices. Then, calculate how many slices will be left over." ] }, { @@ -340,7 +339,7 @@ "source": [ "## Challenge 3: Strings to Numbers\n", "\n", - " `float` will convert a string to a floating point number, and `int` will convert a floating point number to an integer:\n" + "The `float` function will convert a string to a floating point number, while the `int` function will convert a floating point number to an integer:\n" ] }, { @@ -357,7 +356,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Given that, what do you expect this program to do? What does it actually do? Why do you think it does that?" + "Given that, what do you expect this code block to do? What does it actually do? Why do you think it does that?" ] }, { @@ -375,7 +374,7 @@ "source": [ "## Challenge 4: Arithmetic with Different Types\n", "\n", - "Which of the following will print 2.0?\n", + "Which of the following will print the value 2.0?\n", "\n", "Note: there may be more than one right answer." ] @@ -460,8 +459,8 @@ "metadata": {}, "outputs": [], "source": [ - "print(8/4)\n", - "type(8/4)" + "print(8 / 4)\n", + "type(8 / 4)" ] }, { @@ -470,8 +469,8 @@ "metadata": {}, "outputs": [], "source": [ - "print(8/5)\n", - "type(8/5)" + "print(8 / 5)\n", + "type(8 / 5)" ] }, { @@ -480,8 +479,8 @@ "metadata": {}, "outputs": [], "source": [ - "print(8*4)\n", - "type(8*4)" + "print(8 * 4)\n", + "type(8 * 4)" ] }, { @@ -490,15 +489,15 @@ "metadata": {}, "outputs": [], "source": [ - "print(8*4.5)\n", - "type(8*4.5)" + "print(8 * 4.5)\n", + "type(8 * 4.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Change the following code to make the output `True`" + "Change the following code to make the output `True`." ] }, { @@ -565,29 +564,22 @@ "source": [ "*****\n", "\n", - "# Keypoints\n", + "# Key Points\n", "\n", - "- \"Every value has a type.\"\n", - "- \"Use the built-in function `type` to find the type of a value.\"\n", - "- \"Types control what operations can be done on values.\"\n", - "- \"Strings can be added and multiplied.\"\n", - "- \"Strings have a length (but numbers don't).\"\n", - "- \"Must convert numbers to strings or vice versa when operating on them.\"\n", - "- \"Can mix integers and floats freely in operations.\"\n", - "- \"Variables only change value when something is assigned to them.\"\n" + "- Every value has a type.\n", + "- Use the built-in function `type` to find the type of a value.\n", + "- Types control what operations can be done on values.\n", + "- Strings can be added and multiplied.\n", + "- Strings have a length (but numbers don't).\n", + "- Must convert numbers to strings or vice versa when operating on them.\n", + "- Can mix integers and floats freely in operations.\n", + "- Variables only change value when something is assigned to them." ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -601,7 +593,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.7" }, "toc": { "base_numbering": 1, diff --git a/Day_1/05_Strings.ipynb b/lessons/Part1/05_Strings.ipynb similarity index 83% rename from Day_1/05_Strings.ipynb rename to lessons/Part1/05_Strings.ipynb index 36739b3..83e6906 100755 --- a/Day_1/05_Strings.ipynb +++ b/lessons/Part1/05_Strings.ipynb @@ -11,7 +11,7 @@ "- Exercises: 20 min\n", "\n", "**Questions**:\n", - "- \"How do I manipulate strings (text)?\"\n", + "- How do I manipulate strings (text)?\n", "\n", "**Learning Objectives**:\n", "- Become familiar with the [string](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string) [type](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#type) and its methods\n", @@ -22,9 +22,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## We can do things with strings\n", + "## Operations can be applied to strings\n", "\n", - "* We've already seen some operations that can be done with strings." + "* We've already seen some operations, such as addition, that can be applied to strings." ] }, { @@ -43,7 +43,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Remember that computers don't understand context." + "* The addition operation doesn't come with a space. We'll need to insert it on our own." ] }, { @@ -63,7 +63,7 @@ "## Strings are made up of sub-strings\n", "\n", "* You can think of strings as a [sequence](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#sequence) of smaller strings or characters. \n", - "* We can access a piece of that sequence using `[]`." + "* We can access a piece of that sequence using bracket notation, `[]`." ] }, { @@ -79,7 +79,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Gotcha** - Python (and many other langauges) start counting from 0." + "**Important**: Python (and many other langauges) start counting from 0. They are **zero-indexed**." ] }, { @@ -106,7 +106,7 @@ "source": [ "## You can slice strings using `[ : ]`\n", "\n", - "* if you want a range (or \"slice\") of a sequence, you get everything *before* the second index:" + "* If you want a range (or \"slice\") of a sequence, you get everything *before* the second index:" ] }, { @@ -131,7 +131,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* You can see some of the logic for this when we consider implicit indices." + "* You can see some of the logic for this when we consider implicit indices. If we want to start from the beginning of the string, or get all values until the end, we can just omit one end of the slice." ] }, { @@ -156,7 +156,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## String Have Methods" + "## Strings have built-in methods" ] }, { @@ -164,7 +164,7 @@ "metadata": {}, "source": [ "* There are other operations defined on string data. These are called **string [methods](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#method)**. \n", - "* IPython lets you do tab-completion after a dot ('.') to see what methods an [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) (i.e., a defined variable) has to offer. Try it now!" + "* Jupyter lets you do tab-completion after a dot ('.') to see what methods an [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) (i.e., a defined variable) has to offer. Try it now!" ] }, { @@ -180,15 +180,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Let's look at the upper method. What does it do? Lets take a look at the documentation. IPython lets us do this with a question mark ('?') before *or* after an object (again, a defined variable)." + "* Let's look at the upper method. What does it do? Lets take a look at the documentation. Jupyter lets us do this with a question mark ('?') before *or* after an object (again, a defined variable)." ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "str.upper?" @@ -198,7 +196,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "So we can use it to upper-caseify a string. " + "We can use this function to convert a string to upper case." ] }, { @@ -216,7 +214,7 @@ "source": [ "You have to use the parenthesis at the end because upper is a method of the string class.\n", "\n", - "Don't forget, simply calling the method does not change the original variable, you must reassign the variable:" + "Don't forget: simply calling the method does not change the original variable. You must reassign the variable:" ] }, { @@ -267,7 +265,7 @@ "source": [ "## Challenge 1: Write your name\n", "\n", - "1. Make two string variables, one with your first name and one with your last name.\n", + "1. Create two string variables, one with your first name and one with your last name.\n", "2. Concatenate both strings to form your full name and [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) it to a variable.\n", "3. Assign a new variable that has your full name in all upper case.\n", "4. Slice that string to get your first name again." @@ -276,9 +274,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -286,21 +282,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Challenge 2: Try seeing what the following string methods do:\n", + "## Challenge 2: String methods\n", + "\n", + "Create a string variable, and apply the following methods to it to see what they do:\n", "\n", - " * `split`\n", - " * `join`\n", - " * `replace`\n", - " * `strip`\n", - " * `find`" + "* `split`\n", + "* `join`\n", + "* `replace`\n", + "* `strip`\n", + "* `find`" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -321,9 +317,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "poem = '''Take this kiss upon the brow!\n", @@ -377,9 +371,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -393,9 +385,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -405,26 +395,17 @@ "collapsed": true }, "source": [ - "# Keypoints\n", + "# Key Points\n", "\n", "- Some mathematical operators can be used on strings\n", - "- Strings can be indexed, Python indexing always starts at 0!\n", + "- Strings can be indexed. Python indexing always starts at 0!\n", "- Strings, and other types, have their own methods, which are called using dots after the variable, and then the method name." ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -438,7 +419,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.9.7" } }, "nbformat": 4, diff --git a/Day_1/06_Built-ins.ipynb b/lessons/Part1/06_Built-in_Functions.ipynb similarity index 57% rename from Day_1/06_Built-ins.ipynb rename to lessons/Part1/06_Built-in_Functions.ipynb index 18a8d02..7c02d8b 100644 --- a/Day_1/06_Built-ins.ipynb +++ b/lessons/Part1/06_Built-in_Functions.ipynb @@ -11,14 +11,14 @@ "- Exercises: 20 min\n", "\n", "**Questions**\n", - "- \"How can I use built-in functions?\"\n", - "- \"How can I find out what they do?\"\n", + "- How can I use built-in functions?\n", + "- How can I find out what they do?\n", "\n", "**Learning Objectives**\n", - "- \"Explain the purpose of functions.\"\n", - "- \"Correctly call built-in Python functions.\"\n", - "- \"Correctly nest calls to built-in functions.\"\n", - "- \"Use help to display [documentation](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#documentation) for built-in functions.\"\n", + "- Explain the purpose of functions.\n", + "- Correctly call built-in Python functions.\n", + "- Correctly nest calls to built-in functions.\n", + "- Use help to display [documentation](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#documentation) for built-in functions.\n", "\n", "*****" ] @@ -27,23 +27,32 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## A function may take zero or more arguments.\n", + "## A function may take zero or more arguments\n", "\n", - "* We have seen some functions already --- now let's take a closer look.\n", + "* We have seen some functions already - now let's take a closer look.\n", "* An *argument* is a value passed into a function.\n", "* `len` takes exactly one.\n", "* `int`, `str`, and `float` create a new value from an existing one.\n", "* `print` takes zero or more.\n", "* `print` with no arguments prints a blank line.\n", - " * Must always use parentheses, even if they're empty,\n", - " so that Python knows a function is being called." + "* Functions must always be called with parentheses, even if there are no arguments." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "before\n", + "\n", + "after\n" + ] + } + ], "source": [ "print('before')\n", "print()\n", @@ -54,19 +63,30 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Commonly-used built-in functions include `max`, `min`, and `round`.\n", + "## Commonly-used built-in functions include `max`, `min`, and `round`\n", "\n", + "* Built-in functions are those that already exist in Python (in contrast to custom functions that a user can create).\n", + "* Some common built-in functions include `max`, `min`, and `round`.\n", "* Use `max` to find the largest value of one or more values.\n", "* Use `min` to find the smallest.\n", "* Both work on character strings as well as numbers.\n", - " * \"Larger\" and \"smaller\" use (0-9, A-Z, a-z) to compare letters." + "* \"Larger\" and \"smaller\" use (0-9, A-Z, a-z) to compare letters." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "0\n" + ] + } + ], "source": [ "print(max(1, 2, 3))\n", "print(min('a', 'A', '0'))" @@ -76,18 +96,51 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Functions may only work for certain (combinations of) arguments.\n", + "## Functions may operate on certain combinations of arguments\n", "\n", "* `max` and `min` must be given at least one argument.\n", - " * \"Largest of the empty set\" is a meaningless question.\n", - "* And they must be given things that can meaningfully be compared.\n" + "* For example, \"Largest of the empty set\" is a meaningless question.\n", + "* Furthermore, `max` requires types that are comparable (i.e., strings and ints can't be compared to each other)." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "max expected at least 1 argument, got 0", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/6h/ms_dpkl536d1w6qqd_wnckrw0000gn/T/ipykernel_40851/3540540054.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: max expected at least 1 argument, got 0" + ] + } + ], + "source": [ + "print(max())" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'>' not supported between instances of 'str' and 'int'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/6h/ms_dpkl536d1w6qqd_wnckrw0000gn/T/ipykernel_40851/2220240766.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'a'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'" + ] + } + ], "source": [ "print(max(1, 'a'))" ] @@ -96,10 +149,10 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Functions may have default values for some arguments.\n", + "## Functions may have default values for some arguments\n", "\n", - "* `round` will round off a floating-point number.\n", - "* By default, rounds to zero decimal places." + "* `round` will round off a floating-point number. It accepts two arguments: the number, and the number of decimal places to round off to.\n", + "* By default, it rounds to zero decimal places." ] }, { @@ -120,9 +173,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.7" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "round(3.712, 1)" ] @@ -138,9 +202,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function round in module builtins:\n", + "\n", + "round(number, ndigits=None)\n", + " Round a number to a given precision in decimal digits.\n", + " \n", + " The return value is an integer if ndigits is omitted or None. Otherwise\n", + " the return value has the same type as the number. ndigits may be negative.\n", + "\n" + ] + } + ], "source": [ "help(round)" ] @@ -149,14 +228,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## The Jupyter Notebook has two ways to get help.\n", + "## The Jupyter Notebook has two ways to get help\n", "\n", "* Place the cursor inside the parenthesis of the function,\n", - " hold down `shift`,\n", - " and press `tab`.\n", + " hold down `Shift`,\n", + " and press `Tab`.\n", "* Or type a function name with a question mark after it.\n", "\n", - "## Every function returns something.\n", + "## Every function returns a value\n", "\n", "* Every [function call](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#function-call) produces some result.\n", "* If the function doesn't have a useful result to return,\n", @@ -165,9 +244,18 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "example\n", + "result of print is None\n" + ] + } + ], "source": [ "result = print('example')\n", "print('result of print is', result)" @@ -177,18 +265,18 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Difference between Function, Method, Object\n", + "## Functions, objects, and methods\n", " \n", - "A **function** is a piece of code that is called by name. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).\n", + "A **function** is a block of code that can be reused. It can be passed data to operate on (ie. the arguments) and can optionally return data (the return value).\n", "\n", - "A **method** is a function which is tied to a particular object. Each of an object's methods typically implements one of the things it can do, or one of the questions it can answer. It is called using the dot notation: e.g. `object.method()`\n", + "An **object** is a collection of conceptually related variables and functions using those variables. Every [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) is an instance of a `class`, which is like a blueprint for an object. \n", "\n", - "An **object** is a collection of conceptually related grouping of variables (called \"members\") and functions using those variables (called \"methods\"). Every [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) is an instance of a `class`, which is like a blueprint for an object. \n", + "A **method** is a function which is tied to a particular object. Each of an object's methods typically implements one of the things it can do, or one of the questions it can answer. It is called using the dot notation: e.g. `object.method()`.\n", "\n", - " - Everything that exists is an object.\n", - " - Everything that happens is a function call.\n", + "- In Python, everything is an object.\n", + "- In Python, everything that happens is a function call.\n", " \n", - "Read more about objects, classes and methods [here](https://docs.python.org/3/tutorial/classes.html)\n", + "Read more about objects, classes and methods [here](https://docs.python.org/3/tutorial/classes.html).\n", "\n", "Check out our Python glossary [here](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md)." ] @@ -209,7 +297,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } }, "outputs": [], "source": [ @@ -221,7 +312,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } }, "outputs": [], "source": [] @@ -276,7 +370,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } }, "outputs": [], "source": [ @@ -395,34 +492,24 @@ }, { "cell_type": "markdown", - "metadata": { - "collapsed": true - }, + "metadata": {}, "source": [ "*****\n", - "# Keypoints:\n", - "- \"A function may take zero or more arguments.\"\n", - "- \"Commonly-used built-in functions include `max`, `min`, and `round`.\"\n", - "- \"Functions may only work for certain (combinations of) arguments.\"\n", - "- \"Functions may have default values for some arguments.\"\n", - "- \"Use the built-in function `help` to get help for a function.\"\n", - "- \"The Jupyter Notebook has two ways to get help.\"\n", + "# Key Points\n", + "\n", + "- A function may take zero or more arguments.\"\n", + "- Commonly-used built-in functions include `max`, `min`, and `round`.\"\n", + "- Functions may only work for certain (combinations of) arguments.\"\n", + "- Functions may have default values for some arguments.\"\n", + "- Use the built-in function `help` to get help for a function.\"\n", + "- The Jupyter Notebook has two ways to get help.\"\n", "- \"Every function returns something.\"" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -436,9 +523,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.9.7" } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 } diff --git a/Day_1/Day_1_Answers/03_Answers.ipynb b/lessons/Part1/Day_1_Answers/03_Answers.ipynb similarity index 100% rename from Day_1/Day_1_Answers/03_Answers.ipynb rename to lessons/Part1/Day_1_Answers/03_Answers.ipynb diff --git a/Day_1/Day_1_Answers/04_Answers.ipynb b/lessons/Part1/Day_1_Answers/04_Answers.ipynb similarity index 100% rename from Day_1/Day_1_Answers/04_Answers.ipynb rename to lessons/Part1/Day_1_Answers/04_Answers.ipynb diff --git a/Day_1/Day_1_Answers/05_Answers.ipynb b/lessons/Part1/Day_1_Answers/05_Answers.ipynb similarity index 100% rename from Day_1/Day_1_Answers/05_Answers.ipynb rename to lessons/Part1/Day_1_Answers/05_Answers.ipynb diff --git a/Day_1/Day_1_Answers/06_Answers.ipynb b/lessons/Part1/Day_1_Answers/06_Answers.ipynb similarity index 100% rename from Day_1/Day_1_Answers/06_Answers.ipynb rename to lessons/Part1/Day_1_Answers/06_Answers.ipynb