{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab Exercises\n", "\n", "## Introduction to Python, Part 1\n", "\n", "### Questions/Office Hours\n", "\n", "If you have questions about this week's lab **ANY** TA for this class can help you with any problems in office hours. That being said, if you need help over email it is best to email **YOUR** TA.\n", "\n", "You can also always ask the instructor for help with skills labs (even though they are not covered in lecture). You can email the instructor of come to his office hours." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises\n", "\n", "These are primarily drawn from your reading for the week, chapters 2 and 3 of *Python for Everybody*. I've remixed and extended them a bit, so blame any errata on me, not Dr. Chuck. \n", "\n", "* [Click here to read Chapter 2 - Variables](https://www.py4e.com/html3/02-variables)\n", "* [Click here to read Chapter 3 - Conditionals](https://www.py4e.com/html3/03-conditional)\n", "\n", "### A brief note on these lab assignments\n", "\n", "You're definitely going to get stuck at some point doing this assignment. When you do, there's a methodology that I think is helpful that you should try to develop. It comes from FreeCodeCamp (incidentally, a great resource for teaching yourself some coding), and it's called [Read-Search-Ask](https://medium.freecodecamp.org/read-search-dont-be-afraid-to-ask-743a23c411b4).\n", "\n", "These three steps will get you through these assignments, and the habit will help you throughout your life coding. They're basically as follows:\n", "\n", "1. *Read.* Reread the error messages you get, reread the exercise description, read the relevant parts from the textbook, read over any code that might be given to you, read over any supplementary materials you might've gotten that relate to what you're doing. Try any solutions or potential changes that come to mind after reading or were suggested while you're reading. You can do that as many times as you want; it doesn't hurt anybody to try putting in another parenthesis and hit Shift-Enter again.\n", "2. *Search.* By now, you probably have a pretty good idea of what your problem is. Google it! Look on StackExchange, coding forums, and other websites. Don't copy and paste code (you know what plagiarism is) but try to see what others did that's different from what you have.\n", "3. *Ask.* If you're still in lab and you're stuck, raise your hand! If not, drop me or the instructor an email. Come to either of our office hours. We'll help you out.\n", "\n", "\n", "Read-Search-Ask is going to be helpful; keep it in the back of your mind as you're doing these assignments." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 1**\n", "\n", "###### sections to reference: 2.1, 2.4\n", "\n", "Using the code cell directly below and the `type()` command, tell me the *type* of the following items:\n", "\n", "* `184`\n", "* `'The quick brown fox jumps over the lazy dog.'`\n", "* `63.0`\n", "* `'October 1917'`\n", "* `'1776'`\n", "* `'83.4'`\n", "* `83.4`\n", "* `'six'`\n", "* `True`\n", "* `False`\n", "* `\"True\"`\n", "\n", "You can leave your code in the cell if you want! For your answers, insert a Markdown cell below your code and make a list of the items and their type." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Code here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 2**\n", "\n", "###### sections to reference: 2.2, 2.3\n", "\n", "Are the following items valid variable names in Python? If they are, just type \"yes\" next to the item on the list. If not, in a few words, explain why not.\n", "\n", "* my_fun_variable\n", "* item84\n", "* 1600pennsylvania\n", "* whereru@\n", "* the_class\n", "* class\n", "* True\n", "* val_true" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 3**\n", "\n", "###### sections to reference: 2.4, 2.5, 2.6\n", "\n", "Below are a list of, well, math problems. (Sorry.) First, go through the list and **write what you think it will evaluate to** in Python. Create a markdown cell and **make a table** to keep track of the math problems.\n", "\n", "Then, using the code cell below this or the Python console in JupyterLabs (go to Launcher and click the \"Python 3\" box under \"Console\") below the list and **check your work**: type in the problems and see what output you get!\n", "\n", "Finally, update your markdown table with the correct values in their own column. Then, below your table, **write** if any of the problems had different answers than you wrote in the beginning. Why do you think Python interpreted the math problems differently than you, a human? If all of your predictions were accurate, come up with an example or two of a problem where the result you think you *should* get isn't the result that Python gives you.\n", "\n", "Here are the test math problems! Predict what you think they'll evaluate to, test them out, and write about what happened!\n", "\n", "* `25+75`\n", "* `52-8`\n", "* `20*5`\n", "* `55+9`\n", "* `55.0+9`\n", "* `55.0+9.0`\n", "* `18.5-2`\n", "* `61.2-3.2`\n", "* `5+15+20`\n", "* `8*20.0`\n", "* `50/2`\n", "* `50/2.0`\n", "* `50//2.0`\n", "* `50//2`\n", "* `2**2`\n", "* `4**0.5`\n", "* `2**2**2`\n", "* `5==5`\n", "* `6==5`\n", "* `8!=9`\n", "* `42.0 > 30`\n", "* `800 <= 800`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 4**\n", "\n", "###### sections to reference: 2.5, 2.10\n", "\n", "In the code cell below, write a program that asks the users for how many hours they work, their hourly wage, and what percent of their income they pay in taxes. Then, **print out** both their gross and net pay. Gross pay is what they made *before* taxes, and net pay is what they took home *after* taxes.\n", "\n", "Hint: When you ask someone for the percent of income they pay in taxes, they're going to enter an integer like 30. In order to figure out what portion of their income they pay in taxes, you're going to need to convert that into a decimal between 0 and 1, so you can multiply it by their gross pay. How would you turn 30 into 0.3? Do that for the tax percentage they enter before you calculate their net pay." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 5**\n", "\n", "###### sections to reference: 2.2, 2.3, 2.5, 2.6\n", "\n", "This one is pretty similar to number 2, but with some complications. Let's say we define the following variables:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "length = 20\n", "width = 42.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you happened to have cleared all inputs from this notebook, run the above code cell to make sure the variables are assigned.\n", "\n", "Now, what will be the results of the following statements? Like before, **write** your answers next to the statements on the list, and then **check** them using a code cell or the Python console. Add a Markdown cell with your answers and if you get any wrong, tell me why!\n", "\n", "* `width//9`\n", "* `width/9.0`\n", "* `length/3`\n", "* `1 + 4 * length`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 6**\n", "\n", "###### sections to reference: 2.5, 2.10, 3.1, 3.3, 3.4, 3.5\n", "\n", "Write a program which prompts the user for a **temperature** and then asks if it was Celsius or Fahrenheit. If it was Celsius, **convert it** to Fahrenheit; if Fahrenheit, convert to Celsius. Then, **print** the converted temperature. \n", "\n", "I'm not going to give you the conversion formula here, so you'll have to search for it and then implement it in code. Also, you don't have to ask the user to type in \"Celsius\" or \"Fahrenheit\"; your prompt can be as simple as \"Enter 1 if this temperature was Fahrenheit, or 0 if it was Celsius.\" That way, you're able to do something like `if isFahrenheit == 0` rather than exact string matching." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 7**\n", "\n", "###### sections to reference: 2.8, 2.10, 3.1, 3.3, 3.4\n", "\n", "This one's simple. Write a program that prompts the user for an integer. If the integer is even, print the integer with a message indicating that it's even. If it's odd, print it with a message saying that it's odd.\n", "\n", "There are a few ways to do this, so don't worry if yours isn't the same as something you see in the book. As long as you get the results you want, your solution is good." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 8**\n", "\n", "###### sections to reference: 2.10, 3.1, 3.3, 3.4, 3.5\n", "\n", "Another simple one! Write a program that asks the user for *two* integers. If they're equal, print something like \"`5 and 5 are equal.`\" If the first integer is greater than the second, print a statement with both of the integers, saying that. If the first integer is *less* than the second, print a statement saying *that*." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 9**\n", "\n", "###### section to reference: 3.7\n", "\n", "This exercise is a little more open-ended. In a code cell below this one, rewrite one of your previous exercises that takes user input (you're probably going to want to pick from exercises 4, 6, 7, or 8). Using `try` and `except`, **make your program \"idiot-proof\"**. I should be able to type basically whatever I want into your input prompts without crashing your program. For example, if you ask me for a number, I should be able to type in \"apples\" and, instead of your program crashing and giving me a system error, I should see a message *you* typed in, like \"Enter a number, you dummy.\" (In real life, it's probably not a good idea to insult your users, though.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 10**\n", "\n", "###### sections to reference: 2.10, 3.1, 3.2, 3.3, 3.6, 3.7\n", "\n", "You're going to make a program that takes scores on a scale of 0 to 100 and returns a letter grade. If the user enters a valid score, print out a letter from A to F. If the user enters invalid input, print out an error message.\n", "\n", "Use the following table to do your conversions: \n", " \n", "Score | Grade\n", "-------|-------\n", "94-100 | A\n", "85-93 | B\n", "74-84 | C\n", "63-73 | D\n", "≤62 | F\n", " \n", "(Note: this *isn't* the scale we're using in this class. Just an example. :D)\n", "\n", "Make sure you **test your program multiple times** with different inputs to make sure you handled errors and the different scores properly. You won't be graded on this, but **commenting** your code can make it easier for you to understand the many different statements in a row that you'll be writing." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 11**\n", "\n", "###### sections to reference: 2.9, 2.10\n", "\n", "Last week, you did this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = input(prompt=\"What is your name? \")\n", "print(\"Hello\", name, \"!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run that code again, just to remind yourself. \n", "\n", "Your task for this week is to extend this a little bit. Copy and paste the above code into a new code cell below. You're going to make three changes to it. \n", "\n", "1. Make `\"What is your name? \"` a variable. This makes it easier to reuse and change strings that you use across larger programs. After you've done that, change the line of code you use to prompt for the person's name so that, instead of saying `prompt=\"What is your name? \"`, it says `prompt=WHATEVER YOU NAMED YOUR PROMPT VARIABLE`. (Obviously, you'd put the name of your variable in there.)\n", "2. Notice how, when you ran the code cell at the beginning of the exercise, it printed your name with spaces around it, like, \"`Hello Sean !`\"? \n", "Well, that doesn't look super great, so I want you to fix it. Use string *concatenation* (remember what `+` does when you use it with strings?) to insert the user's name that they enter into a string that says \"Hello\" to them. Then, instead of saying `print(\"Hello\", name, \"!\")`, you should be able to just do `print(WHATEVER YOU NAMED YOUR HELLO VARIABLE)`. Isn't that nicer? And no ugly spaces, too!\n", "3. One last thing: I want you to ask for the user's *age*, too. So, make another prompt where you ask \"What is your age? \". Do the same thing you did in step 1, and assign that string to a variable. You want both of your prompts *before* you print anything out. \n", "So, now, you can change your print statement. Go ahead and concatenate the age onto the string you're printing out at the end. After you've done this, your program will go something like this: \n", "\n", "> `What's your name?` (you type in your name, say \"Julie\") \n", "> `How old are you?` (you type in your age, say \"19\") \n", "> `Hi, Julie! You are 19 years old.` \n", "\n", "       Obviously, `Julie` and `19` would be replaced with whatever you entered.\n", "\n", "**NOTE**: This is a lot to throw at you all at once. Consider this a bit of a challenge problem, especially if this is your first time doing any serious programming. If you don't get it all at once, or something breaks, don't feel frustrated! Come see me in office hours, or send me an email. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **Exercise 12**\n", "\n", "###### sections to reference: your mind *(whoa)*\n", "\n", "Write a little bit about this lab in a Markdown cell below. It doesn't have to be an essay, just a few sentences about your experience. Let us know if you got stuck anywhere, if you had any errors with Jupyter, if anything was boring or too easy, and if this lab was too much or not enough practice for you.\n", "\n", "Thanks!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }