w3resource
Python Exercises

Python: Compute the area of the polygon

Python Basic - 1: Exercise-59 with Solution

A convex polygon is a simple polygon in which no line segment between two points on the boundary ever goes outside the polygon. Equivalently, it is a simple polygon whose interior is a convex set. In a convex polygon, all interior angles are less than or equal to 180 degrees, while in a strictly convex polygon all interior angles are strictly less than 180 degrees.

Write a Python program that compute the area of the polygon . The vertices have the names vertex 1, vertex 2, vertex 3, ... vertex n according to the order of edge connections.

However, n is 3 or more and 20 or less. You can also use the following formula to calculate the area S from the lengths a, b, and c of the triangle's three sides.

Python: Compute the area of the polygon rules

Pictorial Presentation:

Python: Compute the area of the polygon

Sample Solution:

Python Code:

def poly_area(c):
  add = []
  for i in range(0, (len(c) - 2), 2):
    add.append(c[i] * c[i + 3] - c[i + 1] * c[i + 2])
    add.append(c[len(c) - 2] * c[1] - c[len(c) - 1] * c[0])
    return abs(sum(add) / 2)

print(poly_area([1, 0, 0, 0, 1, 1, 2, 0, -1, 1]))

Sample Output:

0.5

Flowchart:

Flowchart: Python - Compute the area of the polygon

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to restore the original string by entering the compressed string with this rule. However, the # character does not appear in the restored character string.
Next: Write a Python program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.

What is the difficulty level of this exercise?