Homework 1 - Data Types and Math Operators
Due Date: September 8, 2023
Upload your .py
file using the Student File Upload link by 11:59 pm on the due date. For each problem, start your solution by using the characters #%%
on the first line of your solution. These three characters create a cell in Spyder. For questions that do not require you to write a program, write your answers using Python comments which start with the hash symbol #
. For example, if you were asked to explain what the int
function does then you would type the following in the Spyder editor:
#%% Problem 1
# The int() function is a built-in function that returns
# an integer object constructed from a number
# or string, or returns 0 if no arguments are given.
Or you can use triple quotes:
#%% Problem 1
""" The int() function is a built-in function that returns
an integer object constructed from a number
or string, or returns 0 if no arguments are given."""
Problems
- What are the data types introduced in Chapter 1? Give an explicit code example of when it is not possible to convert one data type into another.
- Do a Google search for "python built-in functions" and list four functions not already discussed in Chapter 1 of the textbook. Briefly explain the purpose of each function and use each one in an explicit code example.
- Why does the expression below cause an error? How can you fix it?
"On average, the Bills score" + 27 + "points at a home game."
- What are the seven basic math operators supported by Python? List each one and for each one give an explicit code example.
- Write a program that asks the user to input a distance in kilometers and outputs the distance in miles. For example, if the user inputs 230 for kilometers, your program would print to the screen:
Remember that the function"230 kilometers equals 143 miles."
input
returns a string and you cannot do math operations with strings. - Write a program that asks the user to input two numbers, the first number is "the numerator" and the second is "the denominator", and outputs the corresponding fraction as a string and the nearest integer to the fraction. For example, if the user inputs 5 as the numerator and 2 as the denominator, then the printed string should be:
"The fraction 5/2 rounded to the nearest integer is 2."