Program to Calculate Percentage in PythonComprehending 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 PythonExample 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
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:
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
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
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
This code uses list comprehension and len() to provide the percentage of even entries in the new input list. Benefits:
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. Next TopicConvert python app into apk |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India