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
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:
- Open a terminal and navigate to the ~/project directory:
cd ~/project
- Create the CircleArea.py file using the touch command:
touch CircleArea.py
- 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:
- Open the
CircleArea.pyfile in a text editor. - 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)
In this code:
- The
sys.argvlist 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.pivalue is used to calculate the area of the circle using the formulaarea = π * 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.
- The
Save the
CircleArea.pyfile.
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:
- Open a terminal and navigate to the ~/project directory:
cd ~/project
- Run the CircleArea.py script with a radius of 5:
python CircleArea.py 5
The output should be:
78.5398163397
- Run the CircleArea.py script with a radius of 8:
python CircleArea.py 8
The output should be:
201.0619298297
- 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.



