Class Coding Lab: Variables And Types

The goals of this lab are to help you to understand:

  1. Python data types
  2. Getting input as different types
  3. Formatting output as different types
  4. Basic arithmetic operators
  5. How to create a program from an idea.

Variable Types

Every Python variable has a type. The Type determines how the data is stored in the computer's memory:


In [ ]:
a = "4"
type(a) # should be str

In [ ]:
a = 4
type(a) # should be int

Types Matter

Python's built in functions and operators work differently depending on the type of the variable.:


In [ ]:
a = 4
b = 5
a + b # this plus in this case means add so 9

In [ ]:
a = "4"
b = "5"
a + b # the plus + in this case means concatenation, so '45'

Switching Types

there are built-in Python functions for switching types. For example:


In [ ]:
x = "45" # x is a str
y = int(x)  # y is now an int
z = float(x) # z is a float
print(x,y,z)

Inputs type str

When you use the input() function the result is of type str:


In [ ]:
age = input("Enter your age: ")
type(age)

We can use a built in Python function to convert the type from str to our desired type:


In [ ]:
age = input("Enter your age: ")
age = int(age)
type(age)

We typically combine the first two lines into one expression like this:


In [ ]:
age = int(input("Enter your age: "))
type(age)

Now Try This:

Write a program to:

  • input your age, convert it to an int and store it in a variable
  • add one to your age, store it in another variable
  • print out your current age and your age next year.

For example:

Enter your age: 45
Today you are 45 next year you will be 46

In [ ]:
# TODO: Write your code here

Format Codes

Python has some string format codes which allow us to control the output of our variables.

  • %s = format variable as str
  • %d = format variable as int
  • %f = format variable as float

You can also include the number of spaces to use for example %5.2f prints a float with 5 spaces 2 to the right of the decimal point.


In [ ]:
name = "Mike"
age = 45
gpa = 3.4
print("%s is %d years old. His gpa is %.3f" % (name, age,gpa))

Formatting with F-Strings

The other method of formatting data in Python is F-strings. As we saw in the last lab, F-strings use interpolation to specify the variables we would like to print in-line with the print string.

You can format an f-string

  • {var:d} formats var as integer
  • {var:f} formats var as float
  • {var:.3f} formats var as float to 3 decimal places.

Example:


In [ ]:
name ="Mike"
wage = 15
print(f"{name} makes ${wage:.2f} per hour")

Now Try This:

Write a print statement using F-strings. print GPA to 3 decimal places, rest of the output should appear just like the example above


In [ ]:
name = "Mike"
age = 45
gpa = 3.4
print("%s is %d years old. His gpa is %.3f" % (name, age,gpa))
## TODO: Rewrite the above line to use an F-string instead

Now Try This:

Print the PI variable out 3 times. Once as a string, once as an int and once as a float to 4 decimal places. Use either F-strings for Format Codes.


In [ ]:
PI = 3.1415927
#TODO: Write Code Here

Putting it all together: Fred's Fence Estimator

Fred's Fence has hired you to write a program to estimate the cost of their fencing projects. For a given length and width you will calculate the number of 6 foot fence sections, and the total cost of the project. Each fence section costs $23.95. Assume the posts and labor are free.

Program Inputs:

  • Length of yard in feet
  • Width of yard in feet

Program Outputs:

  • Perimeter of yard ( 2 x (Length + Width))
  • Number of fence sections required (Permiemer divided by 6 )
  • Total cost for fence ( fence sections multiplied by $23.95 )

NOTE: All outputs should be formatted to 2 decimal places: e.g. 123.05


In [ ]:
#TODO:
# 1. Input length of yard as float, assign to a variable
# 2. Input Width of yard as float, assign to a variable
# 3. Calculate perimeter of yard, assign to a variable
# 4. calculate number of fence sections, assign to a variable 
# 5. calculate total cost, assign to variable
# 6. print perimeter of yard
# 7. print number of fence sections
# 8. print total cost for fence.

Now Try This

Based on the provided TODO, write the program in python in the cell below. Your solution should have 8 lines of code, one for each TODO.

HINT: Don't try to write the program in one sitting. Instead write a line of code, run it, verify it works and fix any issues with it before writing the next line of code.


In [ ]:
# TODO: Write your code here

Metacognition

Please answer the following questions. This should be a personal narrative, in your own voice. Answer the questions by double clicking on the question and placing your answer next to the Answer: prompt.

Questions

  1. Record any questions you have about this lab that you would like to ask in recitation. It is expected you will have questions if you did not complete the code sections correctly. Learning how to articulate what you do not understand is an important skill of critical thinking.

Answer:

  1. What was the most difficult aspect of completing this lab? Least difficult?

Answer:

  1. What aspects of this lab do you find most valuable? Least valuable?

Answer:

  1. Rate your comfort level with this week's material so far.

1 ==> I can do this on my own and explain how to do it.
2 ==> I can do this on my own without any help.
3 ==> I can do this with help or guidance from others. If you choose this level please list those who helped you.
4 ==> I don't understand this at all yet and need extra help. If you choose this please try to articulate that which you do not understand.

Answer:


In [ ]:
# SAVE YOUR WORK FIRST! CTRL+S
# RUN THIS CODE CELL TO TURN IN YOUR WORK!
from ist256.submission import Submission
Submission().submit()