Calculating Circular Values with Python's Math Modules

30 minutes
• 2 Learning Objectives

There are so many things that we can model while we’re programming, and this includes things that we learned about in mathematics classes, such as shapes. These skills can be useful when we’re working on graphics software or something similar to help engineers model their design structures. In this hands-on lab, we’ll use some of the tools provided to us by Python’s `math` module to make geometric calculations about circles.

To feel comfortable completing this lab, you’ll want to know how to use Python’s `math` module. Watch the “Common `math` Functions and Constants” video from the [Using Python’s Math, Science, and Engineering Libraries](https://linuxacademy.com/cp/modules/view/id/621) course.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Implement the `circle_circumference` function.

Implement the `circle_circumference` function such that running the automated tests for that function in `test_calcs.py` all pass. You’ll need to utilize the `math` module and think outside of the box a little bit.

Implement the `circle_area` function.

Implement the `circle_area` function such that running the automated tests for that function in `test_calcs.py` all pass. You’ll need to utilize the `math` module and think outside of the box a little bit.

We've been tasked with writing a few simple functions to calculate the circumference and area of some shapes. This code will be used in a larger application used to calculate material costs for buildings, but for now we only need to create the functions for working with the specific shape that we care about: circles. We'll need to use a few different things from Python's `math` module to calculate these values and ensure that the result is accurate given that we're working with PI and our starting measurements can be floats. We want our calculations to be accurate to the 1/100th of a unit.

We'll be writing our code in `calcs.py` and we'll be able to check our work by running the automated tests within the `test_calcs.py` file by running the following command:

``````\$ python3.8 test_calcs.py
FF
======================================================================
FAIL: test_circle_area (__main__.TestCalcs)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_calcs.py", line 29, in test_circle_area
AssertionError: None != 326.85

======================================================================
FAIL: test_circle_circumference (__main__.TestCalcs)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_calcs.py", line 17, in test_circle_circumference
AssertionError: None != 64.09

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=2)
\$``````

These tests can help guide our implementation of the `circle_circumference` and `circle_area` functions.

Logging In

There are a couple of ways to get in and work with the code. One is to use the credentials provided in the hands-on lab overview page, log in with SSH, and use a text editor in the terminal.

The other is using VS Code in the browser. If you'd like to go this route, then you will need to navigate to the public IP address of the workstation server (provided in the hands-on lab overview page) on port `8080` (example: `http://PUBLIC_IP:8080`). Your password will be the same password that you'd use to connect over SSH.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.