Project Brief
Today we have an easy task to make sure you know how to use Python functions well. Your task is to create five functions:
circle_area: gets the radius and returns the area
circle_circumference: gets the radius and returns the circumference
rectangle_area: gets length and width and returns the area
rectangle_perimeter: gets length and width and returns the perimeter
triangle_area: gets base and height and returns the area
triangle_perimeter: gets the three triangle sides and returns the perimeter
sphere_surface_area: gets the radius and returns the area
sphere_surface_volume: gets the radius and returns the volume
Once you define the functions, call them with some sample values.
Expected Output
Place all the functions in one script and call and print the outputs at the end of the script.
Environment Setup Instructions
You don’t need to install any libraries.
Resources
Functions in Python are reusable blocks of code that perform specific tasks. They take input, process it, and return output. For example, the input can be the birth year and the current_year and the output could be the age of the person:
def calculate_age(birth_year, current_year):
age = current_year - birth_year
return age
By encapsulating code into functions, you can organize your code better, make it more modular, and reuse it throughout your program. Functions are defined using the def
keyword, followed by a name and optional parameters. They can be called multiple times with different arguments, making them versatile and efficient. In our example, we could call the function like following:
# Example usage:
birth_year = 1990
current_year = 2024
age = calculate_age(birth_year, current_year)
print("Age of the person:", age)
And here are the geometry formulas for your reference:
Area of a Rectangle:
Area = length × width
Perimeter of a Rectangle:
Perimeter = 2 × (length+width)
Area of a Triangle:
Area = 1/2 × base × height
Perimeter of a Triangle:
Perimeter = side1 + side2 + side3
Area of a Circle:
Area=π×radius2Area=π×radius2
Circumference of a Circle:
Circumference = 2 × π × radius
Surface Area of a Sphere:
Surface Area = 4 × π × radius*radius
Volume of a Sphere:
Volume = 4/3 × π × radius*radius*radius