Practice Exercises for Variables and Assignments#
Given a template that pre-defines a variable
miles
, write an assignment statement that defines a variablefeet
whose value is the number of feet inmiles
miles. Miles to feet template — Miles to feet solution — Miles to feet (Checker)miles = 13 miles = 57 miles = 82.67 feet = miles * 5280 print(f'{miles} miles equals {feet} feet.')
In Python3 you do not need to convert integers to strings.
f-strings in Python3 are very convenient compared to concatenating strings using +
Given a template that pre-defines three variables
hours
,minutes
andseconds
, write an assignment statement that updates the variabletotal_seconds
to have a value corresponding to the total number of seconds forhours
hours,minutes
minutes andseconds
seconds. Hours to second template — Hours to second solution — Hours to second (Checker)hours = 7 minutes = 21 seconds = 37 total_seconds = (hours * 60 + minutes) * 60 + seconds print(f'{hours} hours, {minutes} minutes, and {seconds} seconds totals to', f'{total_seconds} seconds')
Given a template that pre-defines the variables
width
andheight
that are the lengths of the sides of a rectangle, write an assignment statement that defines a variableperimeter
whose value is the perimeter of the rectangle in inches. Perimeter of rectangle template — Perimeter of rectangle solution — Perimeter of rectangle (Checker)width = 4 height = 7 perimeter = (width + height)*2 print(f'A rectangle {width} inches wide and {height} inches high has a perimeter of {perimeter} inches.')
Given a template that pre-defines the variables
width
andheight
that are the lengths of the sides of a rectangle, write an assignment statement that defines a variablearea
whose value is the area of the rectangle in square inches. Area of rectangle template — Area of rectangle solution — Area of rectangle (Checker)width = 4 height = 7 area = width * height print(area)
Given a template that pre-defines the constant
PI
and the variableradius
corresponding to the radius of a circle in inches, write an assignment statement that defines a variablecircumference
whose value is the circumference of a circle with radiusradius
in inches. Circumference of circle template — Circumference of circle solution — Circumference of circle (Checker)PI = 3.14 radius = 8 circumference = 2 * PI * radius print(circumference)
Given a template that pre-defines the constant
PI
and the variableradius
corresponding to the radius of a circle in inches, write an assignment statement that defines a variablearea
whose value is the area of a circle with radiusradius
in square inches. Area of circle template — Area of circle solution — Area of circle (Checker)PI = 3.14 radius = 8 area = PI * radius**2 print(area)
Given the pre-defined variables
present_value
,annual_rate
andyears
, write an assignment statement that define a variablefuture_value
whose value ispresent_value
dollars invested atannual_rate
percent interest, compounded annually foryears
years. Future value template — Future value solution — Future value (Checker)present_value = 1000 annual_rate = 7 years = 10 future_value = present_value * (1 + 0.01 * 7)**10 print(future_value)
Give the pre-defined variables
first_name
andlast_name
, write an assignment statement that defines the variablename_tag
whose value is the string “My name is % %.” where the percents should be replaced byfirst_name
andlast_name
. Note that, in Python, you can use the+
operator on strings to concatenate (i.e. join) them together into a single string. Name tag template — Name tag solution — Name tag (Checker)first_name = "Joe" last_name = "Warren" name_tag = f'My name is {first_name} {last_name}' print(name_tag) name_tag = 'My name is ' + first_name + ' ' + last_name print(name_tag) name_tag = 'My name is', first_name, last_name print(*name_tag) # argument list unpacking
You can also use f-strings
if you use
+
then pay attention to spaces, because+
concatenates the strings without any spaces in between.also argument list unpacking possible
Given the pre-defined variables
name
(a string) andage
(a number), write an assignment statement that defines a variablestatement
whose value is the string"% is % years old."
where the percents should be replaced byname
and the string form ofage
. Name and age template — Name and age solution — Name and age (Checker)name = "Joe Warren" age = 52 statement = f'{name} is {age} years old' print(statement)
Given the variables
x0
,y0
,x1
, andy1
, write an assignment statement that defines a variabledistance
whose values is the distance between the points $(x0,y0)$ and $(x1, y1)$. Point distance template — Point distance solution — Point distance (Checker)x0 = 2 y0 = 2 x1 = 5 y1 = 6 distance = ( (x0 - y0)**2 + (x1 - y1)**2 )**.5 print(distance)
Challenge: Heron’s formula states the area of a triangle is $\sqrt{s(s-a)(s-b)(s-c)}$ where $a$, $b$ and $c$ are the lengths of the sides of the triangle and $s = \frac{1}{2}(a + b +c)$ is thesemi-perimeterof the triangle. Given the variables
x0
,y0
,x1
,y1
,x2
, andy2
, write a Python program that computes a variablearea
whose value is the area of the triangle with vertices $(x0,y0)$, $(x1, y1)$ and $(x2, y2)$. (Hint: our solution uses five assignment statements.) Triangle area template — Triangle area solution — Triangle area (Checker)x0, y0 = 0, 0 x1, y1 = 3, 4 x2, y2 = 1, 1 a = ( (x0 - x1)**2 + (y0 - y1)**2 )**.5 b = ( (x1 - x2)**2 + (y1 - y2)**2 )**.5 c = ( (x2 - x0)**2 + (y2 - y0)**2 )**.5 s = (a + b + c)/2 area = (s * (s-a) * (s-b) * (s-c))**.5 print(area)