What are the values of \(a\), \(b\), \(c\) and \(d\) after the following code is run?
a = 0
b = a
d = b
a = 7
c = a
We will keep track of each value after each step using the table below:
Initial | Line 1 | Line 2 | Line 3 | Line 4 | Line 5 | |
---|---|---|---|---|---|---|
\(a\) | None | \(0\) | \(0\) | \(0\) | \(7\) | \(7\) |
\(b\) | None | None | \(0\) | \(0\) | \(0\) | \(0\) |
\(c\) | None | None | None | None | None | \(7\) |
\(d\) | None | None | None | \(0\) | \(0\) | \(0\) |
Thus, after the code is run, we have that \(a = 7\), \(b = 0\), \(c = 7\) and \(d = 0\).
Determine the data type of the following values in Python.
\(-4839.1\)
“76”
\(0\)
‘False’
float
, since numerical value has a
decimal
str
, since value is enclosed in
quotations
int
, since numerical value has no
decimal
str
, since value is enclosed in
quotations
In order to convert temperature from Celsius to Fahrenheit, first multiply the temperature in Celsius by \(1.8\) and then add \(32\). Write a Python program called \(celsius\_to\_fahrenheit\) that inputs a temperature in Celsius and outputs the corresponding temperature in Fahrenheit. What is the corresponding Fahrenheit temperature for the following?
\(0\)℃
\(100\)℃
\(18\)℃
\(-40\)℃
def celsius_to_fahrenheit(c_temp):
f_temp = (c_temp * 1.8) + 3.2
return f_temp
celsius_to_fahrenheit(0)
celsius_to_fahrenheit(100)
celsius_to_fahrenheit(18)
celsius_to_fahrenheit(-40)
\(32\)℉
\(212\)℉
\(64.4\)℉
\(-40\)℉
Determine the values of each of the variables and their data types after the following code is run.
a = 17
b = 5.0
c = b - a
d = b * c
e = (a != d)
f = not(not(e))
g = e and not(f)
b = d
Solution: We will keep track of each value after each step using the table below:
Initial | Line 1 | Line 2 | Line 3 | Line 4 | Line 5 | Line 6 | Line 7 | Line 8 | |
---|---|---|---|---|---|---|---|---|---|
\(a\) | None | \(17\) | \(17\) | \(17\) | \(17\) | \(17\) | \(17\) | \(17\) | \(17\) |
\(b\) | None | None | \(5.0\) | \(5.0\) | \(5.0\) | \(5.0\) | \(5.0\) | \(5.0\) | \(-60.0\) |
\(c\) | None | None | None | \(-12.0\) | \(-12.0\) | \(-12.0\) | \(-12.0\) | \(-12.0\) | \(-12.0\) |
\(d\) | None | None | None | None | \(-60.0\) | \(-60.0\) | \(-60.0\) | \(-60.0\) | \(-60.0\) |
\(e\) | None | None | None | None | None | \(True\) | \(True\) | \(True\) | \(True\) |
\(f\) | None | None | None | None | None | None | \(True\) | \(True\) | \(True\) |
\(g\) | None | None | None | None | None | None | None | \(False\) | \(False\) |
Thus, after the code is run, we have that \(a = 17\) (int), \(b = -60.0\) (float), \(c = -12.0\) (float), \(d = -60.0\) (float), \(e = True\) (bool), \(f = True\) (bool) and \(g = False\) (bool).
Let \(a = 25\), \(b = 4\) and \(c = 9\). Determine the following.
a + b - c
a * (c / b)
(b ** b) + (9 // 4)
(a % b) - (a * b * c)
Solution:
a+b-c
\(\Longrightarrow\) 25+4-9
\(\Longrightarrow\) 29-9
\(\Longrightarrow\) 20
a*(c/b)
\(\Longrightarrow\) 25*(9/4)
\(\Longrightarrow\) 25*(2.25)
\(\Longrightarrow\) 56.25
(b**b)+(c//a)
\(\Longrightarrow\) (4**4)+(9//4)
\(\Longrightarrow\) 256+2
\(\Longrightarrow\) 258
(a%b)+(a*b*c)
\(\Longrightarrow\) (25%4)+(25*4*9)
\(\Longrightarrow\) 1-(100*9)
\(\Longrightarrow\) 1-900
\(\Longrightarrow\) -899
Let \(a = 3\), \(b = -8\), \(c = 3.0\) and \(d = 0\). Determine the following.
not(a == c)
(b <= c) and (d > b)
not((a != b) or (d == 0))
Solution:
not(a==c)
\(\Longrightarrow\) not(3==3.0)
\(\Longrightarrow\) not(True)
\(\Longrightarrow\) False
(b<=c) and (d>b)
\(\Longrightarrow\) (-8<=3.0) and (0>-8)
\(\Longrightarrow\) True and True
\(\Longrightarrow\) True
not((a!=b) or (d==0))
\(\Longrightarrow\) not((3!=-8) or (0==0))
\(\Longrightarrow\) not(True or True)
\(\Longrightarrow\) not(True)
\(\Longrightarrow\) False
The volume of a rectangular prism is determined by multiplying
the length, width and height of the
rectangular prism by each other. Write a program called
rec_prism_volume
that inputs the length, width and height of a
rectangular prism, and outputs the volume of the rectangular prism.
Solution:
def rec_prism_volume(length, width, height):
volume = length * width * height
return volume
There are approximately \(1.609344\) kilometres in a mile. Write two programs:
km_to_miles
, inputs a
distance in kilometres and outputs the equivalent distance in
miles
miles_to_km
, inputs a
distance in miles and outputs the equivalent distance in
kilometres
Use these two programs to make the following conversions. Round to \(4\) decimal places.
\(1\) km to miles
\(10\) miles to km
\(120\) km to miles
\(54.0592937\) miles to km
Solution:
def km_to_miles(km_distance):
mile_distance = km_distance / 1.609344
return mile_distance
def miles_to_km(mile_distance):
km_distance = mile_distance * 1.609344
return km_distance
\(1\) km \(= 0.6214\) miles
\(10\) miles \(= 16.0934\) km
\(120\) km \(= 74.5645\) miles
\(54.0592937\) miles \(= 87\) km
Suppose a person writes code that prints the sum of any two inputted numbers. Their code is shown below, but it contains an error. Determine if it is a syntax error or a semantic error, and specify where it occurs. How could this code be fixed? (Hint: Run it through Python Tutor).
num1 = input('Enter a number:')
num2 = input('Enter another number:')
sum = num1 + num2
print('The sum of', num1, 'and', num2, 'is', sum)
Solution: This code contains a semantic error because it uses incorrect logic. The error occurs at the line
sum = num1 + num2
Since the values of num1
and
num2
are user inputs, the data
types of these values is str
. So, when the third line
of code runs, we have two strings being added together, which results in
the value of sum
being the value of
num1
followed by the value of num2
. (e.g. If num1 = "7"
and num2 = "6"
, then sum = "76"
.
We can fix this error by adding two lines of code, before sum
is defined, where the values of num1
and num2
are converted to int
. The correct code is shown below.
num1 = input('Enter a number:')
num2 = input('Enter another number:')
num1 = int(num1)
num2 = int(num2)
sum = num1 + num2
print('The sum of', num1, 'and', num2, 'is', sum)
The following code contains a total of \(3\) syntax errors. Identify each of them.
a == input("Enter a number:")
b = int(a)
c = a + b
a = int(a)
d = 0
e = a / d
Solution: Here are the syntax errors in the code:
The first line uses ==
and not =
, which means that instead of
defining the value of \(a\) to be the
user inputted value, we are comparing the value of \(a\) to the user inputted value. Since \(a\) hasn’t been defined yet, we get a
syntax error.
The third line adds the value of \(a\), which is str
, and the
value of \(b\), which is
int
, which causes a syntax error.
The sixth line divides the value of \(a\) by the value of \(d\), which is \(0\), thus a division by \(0\) causes a syntax error.