Program to Calculate Percentage in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

A percentage is the proportion of the totality. In other words, a percentage is a figure or ratio that is written as a fraction of 100. In this article, we calculate the percentage using the Python programming language in three ways.

  1. Calculating Percentage in Python using List Comprehension and len()
  2. Calculating Percentage in Python using filter(), lambda, and len()
  3. Calculating Percentage in Python using list(), map() and find()

Calculating Percentage in Python

We are going to use the input_list and calculate the percentage of the number of even elements in the list.

  • input_list = [2, 7, 6, 3, 19, 8, 14, 26]
  • Number of even elements: 5
  • Percentage of number of even elements in the list: 62.5
  • Explanation: (5/8)100=62.5(5/8)*100 = 62.5

Method 1: Using List Comprehension and len()

Output:

Here, we create a list of even elements using list comprehension and calculate the length of the list using len(). Then divide by the length of the original list and multiply the result by 100 to obtain the percentage count.

Method 2: Calculating Percentage in Python Using filter(), lambda and len()

Output

Using filter() and lambda, we carry out tasks to obtain even elements in the list and then calculate the length of the list using len(). Then divide by the length of the original list and multiply the result by 100 to obtain the percentage count.

Method 3: Calculating Percentage in Python with the use of list(), map(), and find()

Output

Convert all the list elements to string and then check if the numbers in the list start with the digit 6 and add them to the output list. Then divide the lengths of both lists to find the percentage of the count of even values in the input list.

Similar Python Programs

Conclusion

We used three methods to calculate percentages in the Python programming language.

  1. Using List Comprehension and len()
  2. Using filter(), lambda and len()
  3. Using list(), map() and find()