Program to Calculate Percentage in Python

Comprehending percentages is an essential component of mathematics that has applications in a variety of domains, including science, finance, and daily life. Percentage calculations are a typical job in programming, particularly in data analysis, budgeting, and grading systems. With its ease of use and adaptability, Python is a great platform for developing programs that calculate percentages. In this article, we'll look at how to write a Python program that can easily calculate percentages.

Comprehending Percentages:

Let's go over the idea of percentages before delving into the Python code. A fraction of 100 can be expressed as a percentage. '%' is a common symbol used to indicate it. If you have 30 apples out of 50, for example, the proportion of apples you have is (30/50) * 100, or 60%.

Using filter(), lambda, and len() to calculate a percentage in Python

Example 1:

Input:

Output:

New List : [10, 15, 20, 25, 30, 35, 40]
Percentage of numbers greater than or equal to 20 in the new list : 71.42857142857143
  • new_input_list: The updated list of integers is this one.
  • Printing the new input list: The terminal is filled with the contents of the new_input_list.
  • filter(), lambda: We filter entries from new_input_list that are larger than or equal to 20 by using filter() in conjunction with a lambda function. As a filter, the lambda x: x >= 20 function chooses just the elements that meet the criteria.
  • filtered_list: This variable contains the list of entries from the new_input_list that the lambda function filtered such that they are either larger than or equal to 20.
  • Finding the %: To find the percentage, divide the length of the filtered list by the length of the new input list, and then multiply the result by 100.
  • Printing the result: An explanation message and the computed percentage are displayed on the console.

This code shows how to filter a list based on a criterion and determine the percentage of elements that fulfill that condition in Python using filter(), lambda, and len().

Example 2 :

Input:

Output:

New List : [5, 12, 25, 40, 55, 60, 75]
Percentage of numbers greater than or equal to 40 in the new list : 57.14285714285714

Benefits:

  1. Readability: This method is simple for anyone who is acquainted with functional programming.
  2. Functional Programming: Some people consider functional programming to be more beautiful and clear. Filter() and lambda enable this approach.

Drawbacks: Perhaps less obvious to novices or anyone unfamiliar with the ideas of functional programming.

Python Percentage calculation Using list(), map(), and find()

Example1:

Input:

Output:

New List : [13, 67, 62, 36, 19, 66, 14, 63, 60]
Percentage of elements starting with the digit 6 in the new list : 66.66666666666666
  • new_input_list: The updated list of integers is this one.
  • Printing the new input list: The terminal is filled with the contents of the new_input_list.
  • The use of locate(), map(), and list() Using map() and str(), we turn every number in new_input_list into a string. Next, we use the find() method to cycle through each string representation (item) in the list and determine if it includes the digit "6". The matching integer is appended to filtered_list if the string contains the number "6".
  • Finding the %: To find the percentage, divide the length of the filtered list by the length of the new input list, and then multiply the result by 100.
  • Printing the result: An explanation message and the computed percentage are displayed on the console.

This code determines the proportion of the list's elements that begin with the number "6" by filtering out the other elements.

Example 2:

Input:

Output:

List : [13, 67, 62, 36, 19, 66, 14, 63, 60]
Percentage of elements starting with the digit 6 in the list : 66.66666666666666

Using List Comprehension and len()

Example 1:

Input:

Output:

New List : [13, 67, 62, 36, 19, 66, 14, 63, 60]
Percentage of elements starting with the digit 6 in the new list : 55.55555555555556
  • new_input_list: The updated list of integers is this one.
  • Printing the new input list: The terminal is filled with the contents of the new_input_list.
  • Using list comprehension, we iterate over each element (num) in new_input_list to build a new list (filtered_list). We determine if the number's string representation begins with the digit "6" by using the startswith() function. Should it do so, the number appears in filtered_list.
  • Finding the %: To find the percentage, divide the length of the filtered list by the length of the new input list, and then multiply the result by 100.
  • Printing the result: An explanation message and the computed percentage are displayed on the console.

This code now provides the proportion of entries in the new input list that begin with the digit "6" and is compatible with the updated input list.

Example 2:

Input:

Output:

New List : [10, 15, 20, 25, 30, 35, 40]
Percentage of even elements in the new list : 57.14285714285714
  • new_input_list: The updated list of integers is this one.
  • Printing the new input list: The terminal is filled with the contents of the new_input_list.
  • Using list comprehension, we iterate over each element (num) in new_input_list to build a new list (filtered_list). We determine if the number's string representation begins with the digit "6" by using the startswith() function. Should it do so, the number appears in filtered_list.
  • Finding the %: To find the percentage, divide the length of the filtered list by the length of the new input list, and then multiply the result by 100.
  • Printing the result: An explanation message and the computed percentage are displayed on the console.

This code uses list comprehension and len() to provide the percentage of even entries in the new input list.

Benefits:

  1. Efficiency: Because list comprehensions are Python-optimized, they are typically quicker and need less memory than similar loops.
  2. Readability: List comprehensions are readable because they are clear and concise.

Drawbacks: Those who are not familiar with list comprehensions could find it less understandable.

Finally, this post shows how to write a Python program that computes percentages. Comprehending percentages and knowing how to compute them programmatically are useful abilities for a variety of uses. Python's readability and simplicity make implementing such programs simple, making jobs requiring percentages more efficient and manageable.