From a71880fbbea44f879631f20cfa5d995f882a2777 Mon Sep 17 00:00:00 2001 From: Bryan Holman Date: Mon, 15 Aug 2022 06:33:23 -0700 Subject: [PATCH 1/3] update table of contents --- python_101/_toc.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_101/_toc.yml b/python_101/_toc.yml index fe70809..1f7b9d1 100644 --- a/python_101/_toc.yml +++ b/python_101/_toc.yml @@ -7,6 +7,6 @@ parts: - file: intro/installing_python - file: intro/printing_output - file: intro/collecting_user_input - - file: intro/variables - file: intro/basic_data_types - file: intro/math_operations + - file: intro/variables From ace77c2e685b40e33d500a28bb9dc1736203beff Mon Sep 17 00:00:00 2001 From: Bryan Holman Date: Mon, 15 Aug 2022 10:35:22 -0700 Subject: [PATCH 2/3] continued work on printing output --- python_101/intro/printing_output.ipynb | 468 ++++++++++++++++++++++++- 1 file changed, 467 insertions(+), 1 deletion(-) diff --git a/python_101/intro/printing_output.ipynb b/python_101/intro/printing_output.ipynb index 8437e63..7f9e142 100644 --- a/python_101/intro/printing_output.ipynb +++ b/python_101/intro/printing_output.ipynb @@ -6,7 +6,465 @@ "source": [ "# Printing output\n", "\n", - "Nothing here yet!" + "## Objectives\n", + "\n", + "After working through this page, you should be able to answer these questions:\n", + "\n", + "* How do you display output to the user in Python?\n", + "* What is a function?\n", + "* What is an argument?\n", + "* How can you print multiple statements?\n", + "* How do you print variable values?\n", + "* What options keyword arguments can you pass the `print()` function? What do they do?\n", + "\n", + "## Introducing the `print()` function" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The first programming concept we'll cover is printing output from Python code to you the user. This ability is useful for several reasons, which we'll cover in the examples throughout this introductory section. To print output, use the [`print()`](https://docs.python.org/3/library/functions.html#print) _function_. For example," + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World!\n" + ] + } + ], + "source": [ + "print('Hello World!')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We'll cover functions in more detail in a later section. For now, think of a function as a tool in your coding toolbelt that is waiting to be used when you need it. It is a piece of code that performs a specific task, and it will perform that task only when you tell it to do so. (Much like a hammer to a carpenter.) In Python we use (_call_) a function by typing it's name followed by open and closed parentheses. \n", + "\n", + ":::{admonition} Definition\n", + "A **function** is a piece of code that performs a specific task, and is run only when called.\n", + ":::\n", + "\n", + "The `print()` function's single purpose is outputting, to the user, whatever value it is passed. Most functions accept _arguments_, or values passed to the function that modify it's behavior. In the example above we passed the string `'Hello World!'`, so that is what it printed back to you.\n", + "\n", + ":::{admonition} Definition\n", + "An **argument** is a value that is passed to a function that can modify the function's behavior.\n", + ":::\n", + "\n", + "If we call `print()` without passing arguments, nothing would be output but a new line:\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "print()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can string together multiple calls to `print()`. Imagine you work for an investments firm and you are desigining an app where clients can sign in and view the value of their portfolio. When a user returns to the app, you might want to update them on how the value of their portfolio has changed since the last time they signed in.\n", + "\n", + "We can do this by calling `print()` as many times as necessary to get the user up to speed.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome back, Jane!\n", + "You last visited 26 days ago.\n", + "Since then, the value of your investments has increased 3.4%.\n" + ] + } + ], + "source": [ + "print('Welcome back, Jane!')\n", + "print('You last visited 26 days ago.')\n", + "print('Since then, the value of your investments has increased 3.4%.')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Printing multiple arguments\n", + "\n", + "Continuing our example, imagine that, when a user is signing up, you'd like to inform if their desired username is available. Supposing someone wants `user1234`, you could return the following:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Congratulations user1234 is available!\n" + ] + } + ], + "source": [ + "print('Congratulations,', 'user1234', 'is available!')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here we passed multiple arguments to `print()`. When passed multiple arguments, its default behavior is to return whatever it is passed, separated by a space." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Every argument we've passed `print()` so far has been surrounded by single quotes. These are called _strings_, or sequences of unicode characters. In Python there are various data types that are used to represent different types of values. We'll get into these basic data types in a future lesson. \n", + "\n", + "For now it is sufficient to know that we can pass `print()` not only strings (which represent text values), but also other data types like integers (whole numbers) or floats (numbers with decimals)." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome back, Jane!\n", + "You last visited 26 days ago.\n", + "Since then, the value of your investments has increased 3.4 %.\n" + ] + } + ], + "source": [ + "print('Welcome back, Jane!')\n", + "print('You last visited', 26, 'days ago.')\n", + "print('Since then, the value of your investments has increased', 3.4, '%.')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ":::{admonition} Definition\n", + "A **string** is a built-in data type that holds a sequence of unicode characters.\n", + ":::" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Printing variable values\n", + "\n", + "Looking at the above print statements, in an actual application we'd have code that interacted with a database that would determine the last time Jane visited the app and the change in portfolio value since that visit. After calculating those values they would be stored as *variables*, or names that point to an object or value. \n", + "\n", + ":::{admonition} Definition\n", + "A **variable** is a name that points to an object or value.\n", + ":::\n", + "\n", + "We'll cover variables with more depth soon. For this exercise we'll show that you can print a variable's value by passing the variable to the `print()` function, and it will know what to do from there.\n", + "\n", + "For example, suppose our application had variables that stored the user's name (Jane), the number of days since the last visit (26), and the portfolio value change (+3.4%)." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "user_name = 'Jane'\n", + "days_since_last_visit = 26\n", + "portfolio_change_percent = 3.4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can make the previous print statement dynamic (reuseable) by adding these variables as arguments." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome back, Jane !\n", + "You last visited 26 days ago.\n", + "Since then, the value of your investments has increased 3.4 %.\n" + ] + } + ], + "source": [ + "print('Welcome back,', user_name, '!')\n", + "print('You last visited', days_since_last_visit, 'days ago.')\n", + "print('Since then, the value of your investments has increased', portfolio_change_percent, '%.')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can reuse these same print statements _for all users_ and only need to modify the variable values elsewhere in the application (something we'll learn to do later)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Modifying `print()`'s default behavior\n", + "\n", + "Perhaps you noticed that, in lines one and three in the output from our last `print()` statement, there is a space between the final word and the punctuation. We wouldn't want sentences with grammatical errors being displayed to users of our application, so we'll learn how to fix that next.\n", + "\n", + "### Changing what separates each argument\n", + "\n", + "As we stated earlier, `print()`'s default behavior is to take whatever arguments you pass it, separate those arguments with a space, and print them back to you, ending with a new line. What if we wanted to separate the arguments by something other than a space? Or we didn't want a new line to begin following the output?\n", + "\n", + ":::{admonition} Coding tip\n", + ":class: tip\n", + "An _essential_ tool in every coder's toolbelt is knowing how to use a search engine to get answers to your programming questions.\n", + ":::\n", + "\n", + "If we Google _python print change what separates arguments_, the first result that displays as of this writing is a [codingem article](https://www.codingem.com/python-print-function-parameters/) titled _Python print() Function Parameters Explained_. Near the top of the article we learn that the `print()` function accepts an argument named `sep`, which has a default value of `' '`. \n", + "\n", + "If we modify this _keyword argument_ (a named argument that comes with a default value) we can change what separates the values of the various arguments we pass `print()`.\n", + "\n", + ":::{admonition} Definition\n", + "A **keyword argument** is a named argument, which holds a default value, that can be used when calling a function.\n", + ":::\n", + "\n", + "If we go back to the last thing we printed and add `sep=' '` to the arguments we're passing:" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome back, Jane !\n", + "You last visited 26 days ago.\n", + "Since then, the value of your investments has increased 3.4 %.\n" + ] + } + ], + "source": [ + "print('Welcome back,', user_name, '!', sep=' ')\n", + "print('You last visited', days_since_last_visit, 'days ago.', sep=' ')\n", + "print('Since then, the value of your investments has increased', portfolio_change_percent, '%.', sep=' ')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We get the same output! That is because, if we don't specify an alternative value for `sep`, the `print()` function _behaves as though_ we did pass `sep=' '`. We'll learn a lot more about keyword arguments in a later lesson.\n", + "\n", + "To fix the issue we're having here, we can specify that we don't want spaces between arguments. If we set `sep=''` (an empty string), we get:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome back,Jane!\n", + "You last visited26days ago.\n", + "Since then, the value of your investments has increased3.4%.\n" + ] + } + ], + "source": [ + "print('Welcome back,', user_name, '!', sep='')\n", + "print('You last visited', days_since_last_visit, 'days ago.', sep='')\n", + "print('Since then, the value of your investments has increased', portfolio_change_percent, '%.', sep='')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We no longer have a space between the final word of our sentence and the punctuation! But we introduced a new readability issue. `sep=''` removed spaces between _all_ our arguments. To fix this problem we can manually add spaces in our strings so the text displays properly." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome back, Jane!\n", + "You last visited 26 days ago.\n", + "Since then, the value of your investments has increased 3.4%.\n" + ] + } + ], + "source": [ + "print('Welcome back, ', user_name, '!', sep='')\n", + "print('You last visited ', days_since_last_visit, ' days ago.', sep='')\n", + "print('Since then, the value of your investments has increased ', portfolio_change_percent, '%.', sep='')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Perfect!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing what follows the output\n", + "\n", + "The [codingem article](https://www.codingem.com/python-print-function-parameters/) also discusses the `end` keyword argument. `end` dictates what `print()` will output _after_ it has printed the values of all the arguments. It's default value is `'\\n'`, which is Python's character for a new line.\n", + "\n", + "To reinforce what is meant by a default value, note that the output of these two `print()` calls is the same. " + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome back, Jane!\n", + "Welcome back, Jane!\n" + ] + } + ], + "source": [ + "print('Welcome back, ', user_name, '!', sep='', end='\\n')\n", + "print('Welcome back, ', user_name, '!', sep='')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "An important thing to note here. The `print()` function can handle changes to the default `sep` and `end` values _at the same time_. This means we can play with these keyword arguments to meet our needs.\n", + "\n", + "Let's suppose that, when Jane returns to the application, we want all her details to be displayed on one line (instead of multiple lines). We could do this, using multiple print statements, if we modify `end` to be a space instead of a new line. We'll retain the modifications we made earlier, inserting spaces into the string arguments and keeping `sep=''` to maintain readability." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome back, Jane! You last visited 26 days ago. Since then, the value of your investments has increased 3.4%. " + ] + } + ], + "source": [ + "print('Welcome back, ', user_name, '!', sep='', end=' ')\n", + "print('You last visited ', days_since_last_visit, ' days ago.', sep='', end=' ')\n", + "print('Since then, the value of your investments has increased ', portfolio_change_percent, '%.', sep='', end=' ')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Conclusion\n", + "\n", + "Being able to deliver output to a user is an essential piece to any application. It is also helpful to you, as a programmer, when you are debugging and testing your code (which we'll cover in a later lesson). Python makes printing output easy with the `print()` function. It works intuitively, outputting whatever you pass it, even if you pass it multiple arguments.\n", + "\n", + "As we explored `print()`'s capabilities, we introduced the concept of functions, which are blocks of code that are standing by to perform a specific task for you. We pass functions arguments, which can take the shape of an on-the-fly value (e.g., `print('Hello World!')`), or some variable we've defined. `print()` handles arguments from all sorts of data types, not just strings.\n", + "\n", + "The `print()` function doesn't require that an argument be passed, but it is more helpful when at least one is. And it has pre-defined keyword arguments called `sep` and `end` which can be modified to change how the values of arguments are separated and how the output ends, respectively.\n", + "\n", + "In the next section we'll explore the flip side of printing output: gathering input from the user. Before moving on, test what you've learned by answering the test questions below and work through the included exercises." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test questions\n", + "\n", + "* How do you display output to the user in Python?\n", + "* What is a function?\n", + "* What is an argument?\n", + "* How can you print multiple statements?\n", + "* How do you print variable values?\n", + "* What options keyword arguments can you pass the `print()` function? What do they do?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercises" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Glossary\n", + "\n", + "* An **argument** is a value that is passed to a function that can modify the function's behavior.\n", + "* A **function** is a piece of code that performs a specific task, and is run only when called.\n", + "* A **keyword argument** is a named argument, which holds a default value, that can be used when calling a function.\n", + "* The **string** is a built-in data type that holds a sequence of unicode characters.\n", + "* A **variable** is a name that points to an object or value." ] } ], @@ -17,7 +475,15 @@ "name": "python3" }, "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", "version": "3.9.7" }, "orig_nbformat": 4, From 2ad8f11cdd1ab9ab60a76ec00b8f9eafda735dd2 Mon Sep 17 00:00:00 2001 From: Bryan Holman Date: Tue, 16 Aug 2022 06:40:35 -0700 Subject: [PATCH 3/3] finish main section of print page --- python_101/intro/printing_output.ipynb | 55 ++++++++++++++++++++------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/python_101/intro/printing_output.ipynb b/python_101/intro/printing_output.ipynb index 7f9e142..698ae4c 100644 --- a/python_101/intro/printing_output.ipynb +++ b/python_101/intro/printing_output.ipynb @@ -6,6 +6,12 @@ "source": [ "# Printing output\n", "\n", + "## Introduction\n", + "\n", + "Using code to get a computer to talk back to you is [typically the first thing you'll learn](https://www.thesoftwareguild.com/blog/the-history-of-hello-world/) when studying a programming language. Not only is it typically a simple task, and therefore perfect for newcomers, but learning this skill allows for an easy introduction to core coding concepts such as functions, data types, variables, and more.\n", + "\n", + "Following in the footsteps of others, the first programming concept we'll cover is printing output to the user. This ability is useful for several reasons, which we'll cover in the examples throughout this introductory section. Since Python uses a function to print output, we'll also introduce functions and arguments along the way.\n", + "\n", "## Objectives\n", "\n", "After working through this page, you should be able to answer these questions:\n", @@ -17,14 +23,14 @@ "* How do you print variable values?\n", "* What options keyword arguments can you pass the `print()` function? What do they do?\n", "\n", - "## Introducing the `print()` function" + "## The `print()` function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The first programming concept we'll cover is printing output from Python code to you the user. This ability is useful for several reasons, which we'll cover in the examples throughout this introductory section. To print output, use the [`print()`](https://docs.python.org/3/library/functions.html#print) _function_. For example," + "To print output in Python, use the [`print()`](https://docs.python.org/3/library/functions.html#print) _function_. We'll cover functions in more detail in a later section. For now, think of a function as a tool in your coding toolbelt that is waiting to be used when you need it. It is a piece of code that performs a specific task, and it will perform that task only when you tell it to do so. (Much like a hammer to a carpenter.) In Python we use, or _call_, a function by typing it's name followed by open and closed parentheses. For example," ] }, { @@ -48,8 +54,6 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We'll cover functions in more detail in a later section. For now, think of a function as a tool in your coding toolbelt that is waiting to be used when you need it. It is a piece of code that performs a specific task, and it will perform that task only when you tell it to do so. (Much like a hammer to a carpenter.) In Python we use (_call_) a function by typing it's name followed by open and closed parentheses. \n", - "\n", ":::{admonition} Definition\n", "A **function** is a piece of code that performs a specific task, and is run only when called.\n", ":::\n", @@ -430,7 +434,7 @@ "\n", "The `print()` function doesn't require that an argument be passed, but it is more helpful when at least one is. And it has pre-defined keyword arguments called `sep` and `end` which can be modified to change how the values of arguments are separated and how the output ends, respectively.\n", "\n", - "In the next section we'll explore the flip side of printing output: gathering input from the user. Before moving on, test what you've learned by answering the test questions below and work through the included exercises." + "In the next section we'll explore the flip side of printing output: gathering input from the user. Before moving on, test what you've learned by answering the questions below and work through the included exercises." ] }, { @@ -439,19 +443,46 @@ "source": [ "## Test questions\n", "\n", - "* How do you display output to the user in Python?\n", - "* What is a function?\n", - "* What is an argument?\n", - "* How can you print multiple statements?\n", - "* How do you print variable values?\n", - "* What options keyword arguments can you pass the `print()` function? What do they do?" + "Consider each question below and write an answer based on what you've learned. Once you've responded to each question, check your knowledge by clicking the dropdown to see a solution.\n", + "\n", + ":::{dropdown} 1. How do you display output to the user in Python?\n", + "Use the `print()` function! Call the function and pass an argument of any data type, and its value will be output to the user.\n", + ":::\n", + "\n", + ":::{dropdown} 2. What is a function?\n", + "A **function** is a piece of code that performs a specific task, and is run only when called.\n", + ":::\n", + "\n", + ":::{dropdown} 3. What is an argument?\n", + "An **argument** is a value that is passed to a function that can modify the function's behavior.\n", + ":::\n", + "\n", + ":::{dropdown} 4. How can you print multiple statements?\n", + "The `print()` function accepts as many arguments as you like! To have it output the values of multiple objects, pass it multiple arguments!\n", + "\n", + "Alternatively, you can call `print()` multiple times.\n", + ":::\n", + "\n", + ":::{dropdown} 5. How do you print variable values?\n", + "Pass the `print()` function a variable as an argument, and it will print the variable's value.\n", + ":::\n", + "\n", + ":::{dropdown} 6. What options keyword arguments can you pass the `print()` function? What do they do?\n", + "`print()` accepts the `sep` and `end` keyword arguments.\n", + "\n", + "`sep` determines how values of multiple arguments will be separated in the output, and defaults to a blank space (`' '`).\n", + "\n", + "`end` determines what is output _after_ all the argument values. It defaults to a new line (`'\\n'`).\n", + ":::" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Exercises" + "## Exercises\n", + "\n", + "In progress." ] }, {