Python NamedTuple example: Triangle area calculation
Write a Python program that defines a NamedTuple named "Triangle" with fields 'side1', 'side2', and 'side3'. Now write a function that takes a "Triangle" NamedTuple as input and calculates its area.
Sample Solution:
Code:
Output:
Sides of the triangle: 4 , 5 , 7 Triangle Area: 9.797958971132712
In the exercise above, we define a "Triangle" NamedTuple with 'side1', 'side2', and 'side3' fields. We also define a function "triangle_area()" that takes a "Triangle" NamedTuple as input and calculates its area using Heron's formula. Next, we create an instance of the "Triangle" NamedTuple and use the "triangle_area()" function to calculate its area.
Flowchart:
Previous: Python NamedTuple example: Circle attributes.
Next: Python NamedTuple example: Car attributes.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics