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

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.

âœĻ Check Solution and Practice

Summary

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

Other Python Tutorials you may like