Python count between two numbers

Calculate Percentage between two numbers in Python #

To calculate the percentage between two numbers, divide one number by the other and multiply the result by 100, e.g. (30 / 75) * 100. This shows what percent the first number is of the second. In the example, 30 is 40% of 75.

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(30, 75)) # 👉️ 40.0 print(is_what_percent_of(20, 75)) # 👉️ 26.6666... print(round(is_what_percent_of(20, 75), 2)) # 👉️ 26.67 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(50, 30)) # 👉️ 66.6666... print(get_percentage_increase(50, 100)) # 👉️ -50.0 # -------------------------------------------------- def get_percentage_difference(num_a, num_b): # 👇️ use abs() function to always get positive number return (abs(num_a - num_b) / num_b) * 100 print(get_percentage_difference(50, 30)) # 👉️ 66.6666... print(get_percentage_difference(50, 100)) # 👉️ 50.0

The first function takes 2 numbers and returns what percent the first number is of the second.

For example, 25 / 50 * 100 shows that 25 is 50% of 50.

Copied!

print((25 / 50) * 100) # 👉️ 50.0

When calculating percentages between two numbers, you might need to round to a specific number of digits after the decimal.

The round function takes the following 2 parameters:

NameDescription
number the number to round to ndigits precision after the decimal
ndigits the number of digits after the decimal the number should have after the operation (optional)

Copied!

print(round((23 / 43) * 100, 2)) # 👉️ 53.49

The round function returns the number rounded to ndigits precision after the decimal point.

If ndigits is omitted, the function returns the nearest integer.

The second function shows how to get the percentage increase / decrease between two numbers.

Copied!

def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(50, 30)) # 👉️ 66.6666... print(get_percentage_increase(50, 100)) # 👉️ -50.0

The first example shows that the percentage increase from 30 to 50 is 66.6666...%.

And the second shows that the percentage increase from 100 to 50 is -50%.

If you always need to get a positive number, use the abs() function.

Copied!

def get_percentage_increase(num_a, num_b): return (abs(num_a - num_b) / num_b) * 100 print(get_percentage_increase(50, 30)) # 👉️ 66.6666... print(get_percentage_increase(50, 100)) # 👉️ 50.0

The abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned, and if the number is negative, the negation of the number is returned.

This way we are always guaranteed to get a positive number calculating the difference in percentage between two numbers.

Something you might want to handle is division by 0. Division by 0 raises a ZeroDivisionError in Python.

We can handle the error in a try/except block.

Copied!

def get_percentage_increase(num_a, num_b): try: return (abs(num_a - num_b) / num_b) * 100 except ZeroDivisionError: return float('inf') print(get_percentage_increase(50, 0)) # 👉️ inf print(get_percentage_increase(50, 50)) # 👉️ 0.0 print(get_percentage_increase(50, 100)) # 👉️ 50.0

If we get a ZeroDivisionError error, we return Infinity, however you can handle the error in any other way that suits your use case.

How do you count the number of two numbers in Python?

You can also use a combination of value_counts() and pd. cut() to help you get the job done.

How do you count numbers in a range in Python?

range(a, b) returns a list of b - a values, while (a, b) is a tuple with only two values. To solve your problem you could do e.g. for x in range(a, b + 1): # +1 to include the end of the range in the list x = x - a + 1; # Make x start at 1 ... Save this answer.

How do you count the number of numbers in Python?

The count() is a built-in function in Python. It will return you the count of a given element in a list or a string. In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element. The count() method returns an integer value.