#!/usr/bin/env python # coding: utf-8 # # Lab Exercises # # ## Introduction to Python, Part 1 # # ### Questions/Office Hours # # 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. # # 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. # ---- # ## Exercises # # 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. # # * [Click here to read Chapter 2 - Variables](https://www.py4e.com/html3/02-variables) # * [Click here to read Chapter 3 - Conditionals](https://www.py4e.com/html3/03-conditional) # # ### A brief note on these lab assignments # # 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). # # These three steps will get you through these assignments, and the habit will help you throughout your life coding. They're basically as follows: # # 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. # 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. # 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. # # # Read-Search-Ask is going to be helpful; keep it in the back of your mind as you're doing these assignments. # ---- # #### **Exercise 1** # # ###### sections to reference: 2.1, 2.4 # # Using the code cell directly below and the `type()` command, tell me the *type* of the following items: # # * `184` # * `'The quick brown fox jumps over the lazy dog.'` # * `63.0` # * `'October 1917'` # * `'1776'` # * `'83.4'` # * `83.4` # * `'six'` # * `True` # * `False` # * `"True"` # # 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. # In[ ]: # Code here! # #### **Exercise 2** # # ###### sections to reference: 2.2, 2.3 # # 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. # # * my_fun_variable # * item84 # * 1600pennsylvania # * whereru@ # * the_class # * class # * True # * val_true # #### **Exercise 3** # # ###### sections to reference: 2.4, 2.5, 2.6 # # 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. # # 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! # # 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. # # Here are the test math problems! Predict what you think they'll evaluate to, test them out, and write about what happened! # # * `25+75` # * `52-8` # * `20*5` # * `55+9` # * `55.0+9` # * `55.0+9.0` # * `18.5-2` # * `61.2-3.2` # * `5+15+20` # * `8*20.0` # * `50/2` # * `50/2.0` # * `50//2.0` # * `50//2` # * `2**2` # * `4**0.5` # * `2**2**2` # * `5==5` # * `6==5` # * `8!=9` # * `42.0 > 30` # * `800 <= 800` # In[ ]: # #### **Exercise 4** # # ###### sections to reference: 2.5, 2.10 # # 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. # # 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. # In[ ]: # #### **Exercise 5** # # ###### sections to reference: 2.2, 2.3, 2.5, 2.6 # # This one is pretty similar to number 2, but with some complications. Let's say we define the following variables: # In[1]: length = 20 width = 42.0 # If you happened to have cleared all inputs from this notebook, run the above code cell to make sure the variables are assigned. # # 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! # # * `width//9` # * `width/9.0` # * `length/3` # * `1 + 4 * length` # #### **Exercise 6** # # ###### sections to reference: 2.5, 2.10, 3.1, 3.3, 3.4, 3.5 # # 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. # # 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. # In[ ]: # #### **Exercise 7** # # ###### sections to reference: 2.8, 2.10, 3.1, 3.3, 3.4 # # 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. # # 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. # In[ ]: # #### **Exercise 8** # # ###### sections to reference: 2.10, 3.1, 3.3, 3.4, 3.5 # # 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*. # In[ ]: # #### **Exercise 9** # # ###### section to reference: 3.7 # # 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.) # #### **Exercise 10** # # ###### sections to reference: 2.10, 3.1, 3.2, 3.3, 3.6, 3.7 # # 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. # # Use the following table to do your conversions: # # Score | Grade # -------|------- # 94-100 | A # 85-93 | B # 74-84 | C # 63-73 | D # ≤62 | F # # (Note: this *isn't* the scale we're using in this class. Just an example. :D) # # 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. # In[ ]: # #### **Exercise 11** # # ###### sections to reference: 2.9, 2.10 # # Last week, you did this: # In[ ]: name = input(prompt="What is your name? ") print("Hello", name, "!") # Run that code again, just to remind yourself. # # 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. # # 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.) # 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 !`"? # 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! # 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. # 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: # # > `What's your name?` (you type in your name, say "Julie") # > `How old are you?` (you type in your age, say "19") # > `Hi, Julie! You are 19 years old.` # #        Obviously, `Julie` and `19` would be replaced with whatever you entered. # # **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. # #### **Exercise 12** # # ###### sections to reference: your mind *(whoa)* # # 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. # # Thanks!