Back

Activity 1.9: Math Functions

divider

Activity 1.9

Math Functions

Key Concepts

Modules

The math module

Not Everything Is Built In

Some tools aren't part of core Python — you bring them in with import. A collection of tools you import like this is called a module.

Today's Module: math

Constants and functions for more advanced math than +, -, *, and / can do alone.

import math

math.pi

A constant — no parentheses, because you're not calling a function, just reading a stored value.

radius = float(input("Enter radius: "))
circumference = 2 * math.pi * radius
print(f"Circumference: {circumference}")

math.pi

Terminal window
Enter radius: 42 [Enter]
Circumference: 263.89378290154264

That Number Is Ugly. Fix It With round()

round() is built in — no math. needed. One argument rounds to a whole number; a second argument rounds to that many decimal places.

print(round(263.8938))
print(round(263.8938, 2))

That Number Is Ugly. Fix It With round()

Terminal window
264
263.89

Wait — Haven't We Done Exponents Already?

Activity 1.4 taught ** for exponents. math.pow() does the exact same job.

side = 2
print(side ** 3)
print(math.pow(side, 3))

One Real Difference

Terminal window
8
8.0

math.pow() always returns a float, even when the answer is a whole number. ** doesn't. In your own code, ** is still the more common choice — but you'll see math.pow() in other people's code, so it's worth recognizing.

math.sqrt()

Square roots — no exponent trick required.

print(math.sqrt(45))

math.sqrt()

Terminal window
6.708203932499369

Today's Objectives

  • Importing a module for the first time
  • Using the math.pi constant
  • Rounding with round()
  • Comparing math.pow() to **
  • Calculating square roots with math.sqrt()

Key Terms

Module
A collection of tools not built directly into Python — you bring it in with import.
Constant
A named value, like math.pi, that doesn't change — read directly, not called with ().
round()
Built-in function that rounds a number, optionally to a specific number of decimal places.
math.pow()
Raises a number to a power, same job as **, but always returns a float.
math.sqrt()
Returns the square root of a non-negative number.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-9-math-functions.


Task 1: Circumference Calculator

  • Import the math module.
  • Ask the user for a radius, then calculate and print the circumference using math.pi.
  • Print the circumference again, rounded to 2 decimal places with round().
Math Functions
import math
radius = float(input("Enter radius: "))
circumference = 2 * math.pi * radius
print(f"Circumference: {circumference}")
print(f"Rounded: {round(circumference, 2)}")

Task 2: ** vs. math.pow()

  • Ask for a side length and an exponent.
  • Print the result using **, then print the result using math.pow(), each labeled.
Math Functions
import math
radius = float(input("Enter radius: "))
circumference = 2 * math.pi * radius
print(f"Circumference: {circumference}")
print(f"Rounded: {round(circumference, 2)}")
side = float(input("Enter a side length: "))
exponent = int(input("Enter an exponent: "))
print(f"{side} ** {exponent} is {side ** exponent}")
print(f"math.pow({side}, {exponent}) is {math.pow(side, exponent)}")

Task 3: Square Roots

  • Ask for a positive number and print its square root using math.sqrt().
Math Functions
import math
radius = float(input("Enter radius: "))
circumference = 2 * math.pi * radius
print(f"Circumference: {circumference}")
print(f"Rounded: {round(circumference, 2)}")
side = float(input("Enter a side length: "))
exponent = int(input("Enter an exponent: "))
print(f"{side} ** {exponent} is {side ** exponent}")
print(f"math.pow({side}, {exponent}) is {math.pow(side, exponent)}")
number = float(input("Enter a positive number: "))
print(f"Square root of {number} is {math.sqrt(number)}")

Challenge (Optional): Ceiling and Floor

  • Look up math.ceil() and math.floor() in the Python docs.
  • Print a decimal number three ways: rounded, ceiling, and floor, and compare all three.
divider

Checkpoint

Verify your program works correctly.

Example Output
Enter radius: 42 [Enter]
Circumference: 263.89378290154264
Rounded: 263.89
Enter a side length: 2 [Enter]
Enter an exponent: 3 [Enter]
2.0 ** 3 is 8.0
math.pow(2.0, 3) is 8.0
Enter a positive number: 45 [Enter]
Square root of 45.0 is 6.708203932499369
divider

Reflection

Answer the following questions before submitting your work.

  1. What is a module, and why does math need to be imported when round() doesn't?
  2. What's the one real difference between ** and math.pow(), since they do the same job?
  3. Why does math.pi get used without parentheses, while math.sqrt() needs them?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete