Practice Exercises for Expressions

Practice Exercises for Expressions#

  1. There are $5280$ feet in a mile. Write a Python statement that calculates and prints the number of feet in $13$ miles. Miles to feet templateMiles to feet solutionMiles to feet (Checker)

    print(5280 * 13)
    
    • In Python3 print() is a statement.

  2. Write a Python statement that calculates and prints the number of seconds in $7$ hours, $21$ minutes and $37$ seconds. Hours to seconds templateHours to seconds solutionHours to seconds (Checker)

    print( (7*60 + 21)*60 +37) 
    
  3. The perimeter of a rectangle is $2 w + 2 h$, where $w$ and $h$ are the lengths of its sides. Write a Python statement that calculates and prints the length in inches of the perimeter of a rectangle with sides of length $4$ and $7$ inches. Perimeter of rectangle templatePerimeter of rectangle solutionPerimeter of rectangle (Checker)

    print((4+7) *2)
    
  4. The area of a rectangle is $w h$, where $w$ and $h$ are the lengths of its sides. Note that the multiplication operation is not shown explicitly in this formula. This is standard practice in mathematics, but not in programming. Write a Python statement that calculates and prints the area in square inches of a rectangle with sides of length $4$ and $7$ inches. Area of rectangle template Area of rectangle solution Area of rectangle (Checker)

    print(4*7)
    
  5. The circumference of a circle is $2\pi r$ where $r$ is the radius of the circle. Write a Python statement that calculates and prints the circumference in inches of a circle whose radius is $8$ inches. Assume that the constant $\pi = 3.14$. Circumference of circle templateCircumference of circle solutionCircumference of circle (Checker)

    print(2 * 3.14 * 8) 
    
  6. The area of a circle is $\pi r ^2$ where $r$ is the radius of the circle. (The raised $2$ in the formula is an exponent.) Write a Python statement that calculates and prints the area in square inches of a circle whose radius is $8$ inches. Assume that the constant $\pi = 3.14$. Area of circle templateArea of circle solutionArea of circle (Checker)

    print(3.14 * 8**2)
    
  7. Given $p$ dollars, the future value of this money when compounded yearly at a rate of $r$ percent interest for $y$ years is $ p (1 + 0.01 r)^y$. Write a Python statement that calculates and prints the value of $1000$ dollars compounded at $7$ percent interest for $10$ years. Future value templateFuture value solutionFuture value (Checker)

    print( 1000 * (1 + 0.01 * 7)**10 )
    
  8. Write a single Python statement that combines the three strings "My name is"{.py}, "Joe"{.py} and "Warren"{.py} (plus a couple of other small strings) into one larger string "My name is Joe Warren."{.py} and prints the result. Name tag templateName tag solutionName tag (Checker)

    print('My name is ' + 'Joe ' + 'Warren')
    print('My name is', 'Joe', 'Warren')
    
    • In Python3, the arguments to print() will be separated by a space as default.

  9. Write a Python expression that combines the string "Joe Warren is 52 years old."{.py} from the string "Joe Warren"{.py} and the number $52$ and then prints the result (Hint: Use the function str{.py} to convert the number into a string.) Name and age templateName and age solutionName and age (Checker)

    print('Joe Warren is', 52, 'years old')
    
    • In Python3, you do not need to use str().

  10. The distance between two points $(x_0, y_0)$ and $(x_1, y_1)$ is $\sqrt{(x_0-x_1)^2 + (y_0-y_1)^2}$. Write a Python statement that calculates and prints the distance between the points $(2, 2)$ and $(5, 6)$. Point distance templatePoint distance solutionPoint distance (Checker)

    print( ( (2-5)**2 + (2-6)**2 )**.5 )