Area of a Circle

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to calculate the area of a circle based on a given radius using Python. This is a fundamental programming task that introduces the use of command-line arguments and the math module in Python.

👀 Preview

$ python CircleArea.py 8
## Output
201.0619298297

$ python CircleArea.py 10
## Output
314.1592653590

🎯 Tasks

In this project, you will learn:

  • How to create a Python script to calculate the area of a circle
  • How to use command-line arguments to pass input data to a Python script
  • How to use the math module to access the value of pi and perform mathematical calculations

🏆 Achievements

After completing this project, you will be able to:

  • Write a Python script that can calculate the area of a circle given a radius
  • Understand how to use command-line arguments to pass input data to a Python script
  • Apply the math module to perform mathematical calculations in Python

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302687{{"`Area of a Circle`"}} python/variables_data_types -.-> lab-302687{{"`Area of a Circle`"}} python/numeric_types -.-> lab-302687{{"`Area of a Circle`"}} python/type_conversion -.-> lab-302687{{"`Area of a Circle`"}} python/lists -.-> lab-302687{{"`Area of a Circle`"}} python/tuples -.-> lab-302687{{"`Area of a Circle`"}} python/importing_modules -.-> lab-302687{{"`Area of a Circle`"}} python/standard_libraries -.-> lab-302687{{"`Area of a Circle`"}} python/math_random -.-> lab-302687{{"`Area of a Circle`"}} python/os_system -.-> lab-302687{{"`Area of a Circle`"}} python/build_in_functions -.-> lab-302687{{"`Area of a Circle`"}} end

Create the CircleArea.py File

In this step, you will create the CircleArea.py file in the ~/project directory. Follow the steps below to complete this step:

  1. Open a terminal and navigate to the ~/project directory:
cd ~/project
  1. Create the CircleArea.py file using the touch command:
touch CircleArea.py
  1. The CircleArea.py file has been created in the ~/project directory.

Implement the Circle Area Calculation

In this step, you will implement the code to calculate the area of a circle based on the given radius. Follow the steps below to complete this step:

  1. Open the CircleArea.py file in a text editor.
  2. Add the following code to the file:
import sys
import math

## Get command line arguments
radius = float(sys.argv[1])
## Calculate the area of the circle
area = math.pi * radius * radius
area_str = format(area, ".10f")
## Print the result
print(area_str)
  1. In this code:

    • The sys.argv list contains the command-line arguments passed to the script. The first element (sys.argv[0]) is the script name, and the second element (sys.argv[1]) is the radius value.
    • The math.pi value is used to calculate the area of the circle using the formula area = π * radius^2.
    • The format() function is used to round the area value to 10 decimal places.
    • The calculated area is then printed to the console.
  2. Save the CircleArea.py file.

Test the Circle Area Calculation

In this step, you will test the CircleArea.py script by running it with different radius values. Follow the steps below to complete this step:

  1. Open a terminal and navigate to the ~/project directory:
cd ~/project
  1. Run the CircleArea.py script with a radius of 5:
python CircleArea.py 5

The output should be:

78.5398163397
  1. Run the CircleArea.py script with a radius of 8:
python CircleArea.py 8

The output should be:

201.0619298297
  1. Run the CircleArea.py script with a radius of 10:
python CircleArea.py 10

The output should be:

314.1592653590

Congratulations! You have successfully implemented the Circle Area Calculation project.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like