Python’s Built-in Number Methods: Simple Functions, Great Power

In the last post, we discovered the different python number types and how to use them for counting, or calculating things.

But Python not only gives us numbers. It also provides us with a toolbox filled with built-in functions that work beautifully with them.

Let’s walk through the ones that every beginner should know.

Convert to Numbers

Sometimes, a number looks like a number but it’s actually a string. If you’ve got “42” and want to do math with it, you’ll need to convert it first.

We use methods like int() and float() to do just that.

We can convert a string to integer or a float with methods like int() and float().
(click on the image to open in a new tab)

Round floats

Regarding floats, you can round them to the nearest whole number using the round() method

Note how the first number is rounded down, while the second is rounded up.
(click on the image to open in a new tab)

If you want more control, you can pass a second argument:

The second parameter dictates how many decimal places will be in the number.
(click on the image to open in a new tab)

Absolute Value, Power, and Beyond

Math sometimes calls for more than just adding and subtracting.

Python can help in such cases natively.

The built-in method abs() give you the absolute value of a number.

The built-in method pow() raises a number to a power, like 2 to the 3rd power.

We see how abs() gives the absolute value of the number, while the pow() method raises the number to the power of three.
(click on the image to open in a new tab)

You can also use the ** operator for powers, but pow() has an optional third argument for modulo math (used in cryptography and algorithms).

The pow() method first raises 2 to the power of 3 and then returns the remainder after the power of the number is divided by 7.
(click on the image to open in a new tab)

The above is the equivalent of:

This is the traditional, two step, way, of doing what the pow() method did in the previous example.
(click on the image to open in a new tab)

Summing, sorting and picking numbers from collections

Python loves working with collections of numbers. You don’t need a loop just to find the biggest value.

Given a specific list, we can easily retrieve the maximum and minimum numbers, find the sum of all numbers or put them in order by using the suitable method.
(click on the image to open in a new tab)

These functions work on any iterable be it lists, tuples, even ranges.

Calculating Quotient and Remainder Simultaneously. The divmod() method

The Python built-in function divmod() combines floor division and modulo division, returning first the quotient that comes from floor division, then the remainder.

Because divmod() will be working with two parameters, we need to pass two argument numbers to it. The first represents the dividend, and the second represents the divider.

The divmod() method returns the quotient, and the remainder with that order.
(click on the image to open in a new tab)

This method is perfect for when you’re working with things like currency breakdowns or formatting time.

Example: Breaking Down Cents into Coins

Suppose you have 117 cents, and you want to break it down into quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢). You can use divmod() to handle this cleanly.

A simple example of breaking down currency.

And we get both the number of coins and the remainder.

Breaking down a currency is easy with Python.

For all these you just utilize the standard library

All these methods are built-in, so you don’t need to import anything. That’s part of Python’s beauty. It keeps things readable and efficient without cluttering your screen.

These methods help build complex programs by reducing code length, improving logic clarity, and guaranteeing precise results.

Although my blog doesn’t support comments, feel free to reply via email or X.