percent calculator in python

To create a percentage calculator in Python, you can use the following code:

main.py
def calculate_percentage(value, total):
    percentage = (value / total) * 100
    return percentage

# Example usage:
value = 75
total = 100
percentage = calculate_percentage(value, total)
print(f"The percentage is {percentage}%")
232 chars
10 lines

In this code, we define a function calculate_percentage that takes two arguments: the value and the total. It calculates the percentage by dividing the value by the total and multiplying by 100. The result is then printed using f-string formatting.

You can replace the value and total variables with your own values to calculate different percentages.

related categories

gistlibby LogSnag