CS170: Programming for the World Around Us - Representing Numbers and Text with Python

Activity Goals

The goals of this activity are:
  1. To explore numeric operators and variables in Python
  2. To obtain user input in a Python program

Supplemental Reading

Feel free to visit these resources for supplemental background reading material.

The Activity

Directions

Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.

Model 1: Numeric Operations

Operator Meaning Example
+ Addition z = x + 5
- Subtraction z = a - b
* Multiplication slices = pizzas * 8
/ Division average = people / teams
** Exponentiation radius_squared = r ** 2
% Modulus (remainder) remainder = 8 % 3

Questions

  1. What is an expression for the number of semesters one attends class, assuming a 4-year college degree program?
  2. What is an expression to compute the total cost of items purchased at a store with 6 percent state sales tax?

Model 2: A First Python Program

Questions

  1. Create a project in Python, paste and run this code.
  2. Modify the program to ask for your major and where you're from, and print that.
  3. The x = int(x) function converts the variable x from text to a whole number. Ask for the year you'll graduate, convert it to a number, and subtract the current year from it. Print that value to show how many years it will be until you graduate.
  4. Write a program to ask the user to input their street address, city, state, and zip code, and print out their address like one would on an mailing envelope.
  5. Print the total cost after tax from the prior example using this print statement: print("${0:,.2f}".format(totalcost)). What do you think {0:,.2f} means? As a hint, try purchasing over $1000 worth of items in your formula!

Submission

I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.