Problem Formulation and Solution Overview
- A Python list of numerical elements (float or integer), and
- A percentage value.
Apply the percentage value to each list element and print the result to the Python shell.
- 15% to
[100, 1000, 10000]
yields[15, 150, 1500]
- 50% of
[10.0, 20.0, 30.0]
yields[5.0, 10.0, 15.0]
In the following article, we round to the full integer number for clarity of presentation—in practice, you can skip the rounding step if needed.
Gil, the owner of GatorXpress, has four employees. Their food/bar sales are totaled each week, and a 15% gratuity is issued. Gil has asked you to write code to calculate these percentages rounded to the nearest dollar.
π¬ Question: How would we write Python code to calculate the gratuities?
We can accomplish this task by one of the following options:
- Method 1: Use List Comprehension
- Method 2: Use Lambda and
map()
- Method 3: Use
zip()
- Method 4: Use a Custom Function
Method 1: Use List Comprehension
This example uses List Comprehension and the round()
function to calculate the percentages.
emp = ['Art', 'Sandy', 'Micah', 'Jen'] totals = [245.19, 145.75, 197.87, 196.72] percentage = 15 tips = [round(x*percentage/100) for x in totals] for e, t in zip(emp, tips): print(f'{e}\t ${round(t)}.00')
This code declares two (2) lists containing employee names and their total food/bar sales for the current week. Then, each element of totals
is accessed using List Comprehension to calculate the percentage owed to the employee rounded to the nearest dollar. The result saves to tips
.
A for
loop is instantiated using zip()
to combine the lists. Finally, the formatted output containing the employee names and gratuities owed is sent to the terminal.
Output
Art | $37.00 |
Sandy | $22.00 |
Micah | $30.00 |
Jen | $30.00 |
Method 2: Use Lambda and map()
In this example, a Lambda is used in conjunction with the map()
function to calculate the percentages.
emp = ['Art', 'Sandy', 'Micah', 'Jen'] totals = [245.19, 145.75, 197.87, 196.72] percentage = 15 tips = list(map(lambda x : round(x*percentage/100), totals)) for e, t in zip(emp, tips): print(f'{e}\t ${round(t)}.00')
This code declares two (2) lists containing employee names and their total food/bar sales for the current week. Then, the map()
function creates an iterable object that the Lambda accesses and performs the appropriate calculations on. Finally, the result is converted to a list and saved to tips
.
A for
loop is instantiated using zip()
to combine the lists. Finally, the formatted output containing the employee names and gratuities owed is sent to the terminal.
Output
Art | $37.00 |
Sandy | $22.00 |
Micah | $30.00 |
Jen | $30.00 |
Method 3: Use zip()
In this example, zip()
is used to navigate the lists and calculate the percentages. This is an excellent alternative if the calculations do not need to be saved.
emp = ['Art', 'Sandy', 'Micah', 'Jen'] totals = [245.19, 145.75, 197.87, 196.72] percentage = 15 for e, t in zip(emp, totals): print(f'{e}\t ${round(t*percentage/100)}.00')
This code declares two (2) lists containing employee names and their total food/bar sales for the current week.
Then a for
loop is instantiated using zip()
to combine the lists. Finally, each element is accessed, the percentage calculated, formatted, and sent to the terminal.
Output
Art | $37.00 |
Sandy | $22.00 |
Micah | $30.00 |
Jen | $30.00 |
Method 4: Use a Custom Function
This example creates a new function that takes each employee, calculates, and returns the formatted percentages. A good solution if you need to re-use the code.
emp = ['Art', 'Sandy', 'Micah', 'Jen'] totals = [245.19, 145.75, 197.87, 196.72] percentage = 15 def calc_amt(e, t): return (f'{e}\t ${round(t*percentage/100)}.00') for e, t in zip(emp, totals): print(f'{calc_amt(e, t)}')
This code declares two (2) lists containing employee names and their total food/bar sales for the current week. Then, a function is created and is passed one element from the emp
list and one element from the totals
list.
Then a for
loop is instantiated using zip()
to combine the lists. Finally, each element is accessed, and the function calc_amt
is called. Finally, the formatted output is sent to the terminal.
Output
Art | $37.00 |
Sandy | $22.00 |
Micah | $30.00 |
Jen | $30.00 |
Summary
These four (4) methods to calculate percentages should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!