Math in Python (class slides)

CSc 110 Math in Python

The Mathematical Operators

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • // Integer Division
  • ** Exponent
  • % Modulus

The order of Operations (PEMDAS)

  1. Parentheses,
  2. Exponentiation
  3. Multiplication and Division have equal precedence
  4. Addition and Subtraction have equal precedence

Write a function

Write a function that calculates the area of a square:

  1. Its name is square_area
  2. It takes one argument: side
  3. It calculates the area of a square (a quadrilateral with two sides parallel that are of equal length). The area of a square is calculated by multiplying the side by itself (\(area = side^2\)).
  4. It returns the value for calculated square area.

Square Area

def square_area(side):
  # enter your code here

print(square_area(5)) # 25
print(square_area(6)) # 36
print(square_area(10)) # 100

Square Area

def square_area(side):
  "calculates the area of a square"
  area = side * side
  return area

print(square_area(5)) # 25
print(square_area(6)) # 36
print(square_area(10)) # 100
25
36
100

Rounding numbers with built-in function round()

round()

Use the round() function to get a floating-point number rounded to the specified number of decimals.

Syntax:

round(number, ndigits*)

The number of digits (ndigits) is optional, but we will often round number to two decimals:

round(392.68750000000006, 2)
392.69

round()

Use the round() function to get a floating-point number rounded to the specified number of decimals.

Syntax:

round(number, ndigits*)

Rounding is done toward the nearest even choice. What would these round to? (discuss with your group)

round(51.6)
round(51.5)
round(50.5)
round(51.4)

round()

Use the round() function to get a floating-point number rounded to the specified number of decimals.

Syntax:

round(number, ndigits*)

Rounding is done toward the nearest even choice. What would these round to:

print(round(51.6))
print(round(51.5))
print(round(50.5))
print(round(51.4))
52
52
50
51

Write a function

Write a function that calculates the volume of a sphere:

  1. Its name is sphere_volume
  2. It takes one argument: radius
  3. It calculates the volume of the sphere (use 3.1415 for \(\pi\)):

\[ v = {4 / 3} \cdot \pi \cdot radius^3 \]

  1. It returns the value for the calculated sphere volume
  2. Test case: sphere_volume(.75) should return 1.77.

Write a function – sphere volume

def sphere_volume(radius):
  "calculates the volume of a sphere of given radius"
  volume = (4 / 3) * 3.1415 * radius**3
  return round(volume, 2)

print(sphere_volume(.75)) # 1.77
print(sphere_volume(2)) # 33.51
print(sphere_volume(5.5)) # 696.89
1.77
33.51
696.89

Write a function

Write a function that calculates the area of a sphere:

  1. Its name is sphere_area
  2. It takes one argument: radius
  3. It calculates the volume of the sphere (use 3.1415 for \(\pi\)):

\[ a = 4 \cdot \pi \cdot radius^2 \]

  1. It returns the value for the calculated sphere area
  2. Test case: sphere_area(.75) should return 7.07.

Write a function – solutions

def sphere_volume(radius):
  "calculates the volume of a sphere of given radius"
  volume = (4 / 3) * 3.1415 * radius**3
  return round(volume, 2)


def sphere_area(radius):
  "calculates the area of a sphere of given radius"
  area = 4 * 3.1415 * radius**2
  return round(area, 2)


r = .75
v = sphere_volume(r)
a = sphere_area(r)
print(v, a)
1.77 7.07

Write a function – sphere volume and area

Submit your sphere volume and area solution as attendance on Gradescope.

Name your file sphere.py