# Practice Exercises for Variables and Assignments#

Given a template that pre-defines a variable

`miles`

, write an assignment statement that defines a variable`feet`

whose value is the number of feet in`miles`

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`

and`seconds`

, write an assignment statement that updates the variable`total_seconds`

to have a value corresponding to the total number of seconds for`hours`

hours,`minutes`

minutes and`seconds`

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`

and`height`

that are the lengths of the sides of a rectangle, write an assignment statement that defines a variable`perimeter`

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`

and`height`

that are the lengths of the sides of a rectangle, write an assignment statement that defines a variable`area`

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 variable`radius`

corresponding to the radius of a circle in inches, write an assignment statement that defines a variable`circumference`

whose value is the circumference of a circle with radius`radius`

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 variable`radius`

corresponding to the radius of a circle in inches, write an assignment statement that defines a variable`area`

whose value is the area of a circle with radius`radius`

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`

and`years`

, write an assignment statement that define a variable`future_value`

whose value is`present_value`

dollars invested at`annual_rate`

percent interest, compounded annually for`years`

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`

and`last_name`

, write an assignment statement that defines the variable`name_tag`

whose value is the string “My name is % %.” where the percents should be replaced by`first_name`

and`last_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) and`age`

(a number), write an assignment statement that defines a variable`statement`

whose value is the string`"% is % years old."`

where the percents should be replaced by`name`

and the string form of`age`

. 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`

, and`y1`

, write an assignment statement that defines a variable`distance`

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`

, and`y2`

, write a Python program that computes a variable`area`

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)