In [ ]:
a = "4"
type(a) # should be str
In [ ]:
a = 4
type(a) # should be int
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'
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)
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)
In [ ]:
# TODO: Write your code here
Python has some string format codes which allow us to control the output of our variables.
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))
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")
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
In [ ]:
PI = 3.1415927
#TODO: Write Code Here
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:
Program Outputs:
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.
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
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.
Answer:
Answer:
Answer:
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()