0
0
PythonProgramBeginner · 2 min read

Python Program to Find Percentage of a Number

To find the percentage in Python, use the formula percentage = (part / total) * 100, for example: percentage = (50 / 200) * 100.
📋

Examples

Inputpart = 50, total = 200
OutputPercentage: 25.0%
Inputpart = 23, total = 50
OutputPercentage: 46.0%
Inputpart = 0, total = 100
OutputPercentage: 0.0%
🧠

How to Think About It

To find the percentage, you divide the part value by the total value to get a fraction, then multiply by 100 to convert it to a percentage. This is like finding how much of a whole something is, expressed out of 100.
📐

Algorithm

1
Get the part value (the portion you want to find the percentage of).
2
Get the total value (the whole or maximum possible value).
3
Divide the part by the total to get a decimal fraction.
4
Multiply the result by 100 to convert it to a percentage.
5
Display or return the percentage value.
💻

Code

python
part = float(input('Enter the part value: '))
total = float(input('Enter the total value: '))
percentage = (part / total) * 100
print(f'Percentage: {percentage}%')
Output
Enter the part value: 50 Enter the total value: 200 Percentage: 25.0%
🔍

Dry Run

Let's trace the example where part = 50 and total = 200 through the code.

1

Input part value

User inputs 50, so part = 50.0

2

Input total value

User inputs 200, so total = 200.0

3

Calculate percentage

percentage = (50.0 / 200.0) * 100 = 0.25 * 100 = 25.0

4

Print result

Output is 'Percentage: 25.0%'

StepparttotalpercentageOutput
150.0---
250.0200.0--
350.0200.025.0-
450.0200.025.0Percentage: 25.0%
💡

Why This Works

Step 1: Divide part by total

Dividing part by total gives the fraction of the whole that the part represents.

Step 2: Multiply by 100

Multiplying the fraction by 100 converts it to a percentage, which is out of 100.

Step 3: Display the result

Printing the result shows the percentage value clearly to the user.

🔄

Alternative Approaches

Using a function
python
def find_percentage(part, total):
    return (part / total) * 100

print(f'Percentage: {find_percentage(50, 200)}%')
This approach makes the code reusable for different inputs.
Using integer inputs and rounding
python
part = int(input('Enter part: '))
total = int(input('Enter total: '))
percentage = round((part / total) * 100, 2)
print(f'Percentage: {percentage}%')
Rounding limits decimal places for cleaner output.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation involves only basic arithmetic operations without loops, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store inputs and the result, so space complexity is O(1).

Which Approach is Fastest?

All approaches perform the same constant-time calculation; using a function adds readability but no significant overhead.

ApproachTimeSpaceBest For
Direct calculationO(1)O(1)Simple scripts
Function-basedO(1)O(1)Reusable code
Rounded outputO(1)O(1)Cleaner display
💡
Always convert inputs to float to handle decimal values correctly when calculating percentages.
⚠️
Forgetting to multiply by 100 after dividing part by total, which results in a decimal fraction instead of a percentage.