Python 3 comes with many built-in functions that you can readily use in any program that you’re working on. Some functions enable you to convert data types, and others are specific to a certain type, like strings.

This tutorial will go through a few of the built-in functions that can be used with numeric data types in Python 3. We’ll go over the following functions:

`abs()`for absolute value`divmod()`to find a quotient and remainder simultaneously`pow()`to raise a number to a certain power`round()`to round a number to a certain decimal point`sum()`to calculate the sum of the items in an iterable data type

Introduction

The built-in function `abs()` will return the absolute value of a number that you pass to it. In mathematics, **absolute value** refers to the distance that a number is on the number line from 0. Absolute value does not take into consideration which direction from zero the number lies, meaning that negative numbers will be represented with positive numbers.

To give some examples, the absolute value of `15` is `15`, the absolute value of `-74` is `74`, and the absolute value of `0` is `0`.

Absolute value is an important concept for calculus and real analysis, but it also makes sense when we think about everyday situations like distance travelled. For example, if we are trying to get somewhere that is 58 miles away but we travel 93 miles instead, we overshot our original destination. If we want to calculate now how many miles left to travel to get to the intended destination, we’ll end up with a negative number, but we can’t travel negative miles.

Let’s use `abs()` to solve this problem:

In the output, we see that if we don’t use the `abs()` function, in this instance we have a negative number, `-35`. Though we may be in a position where `miles_travelled` is less than `miles_from_origin`, including the `abs()` function takes the possibility of a negative number into account.

With a negative number, `abs()` will return a positive number as absolute values are always positive numbers or zero.

Let’s go through using `abs()` with a positive number and zero:

print(abs(89.9)) print(abs(0))

We’re most likely to use `abs()` with a variable that may be positive or negative in an instance when we are looking only for a positive number. To account for a negative input or result, we’ll use `abs()` to modify what is returned to be a positive number.

Absolute Value - abs()

Because both floor division (which returns a quotient), and modulo division (which returns a remainder), are closely related, it can be useful to use a function that combines both operations at once.

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

Because `divmod()` will be working with two numbers, we need to pass two numbers to it.

divmod(a,b)

With this function we are basically performing the following:

a // b a % b

Let’s say we have written a book that is 80,000 words long. With our publisher, we have the option of either 300 or 250 words per page, and we’d like to get a sense of how many pages we would have in each case. With `divmod()` we can see immediately how many pages we would have, and how many words would be spilled over onto an additional page.

In Option A, we will have 266 pages filled with words and 200 words left over (⅔ of a page) for a total of 267 pages, and in Option B we’ll have an even 320-page book. If we want to be environmentally-conscious, we can choose Option A, but if we want to look more impressive with a bigger-sized book we may choose Option B.

Because the function `divmod()` can take both integers and floats, let’s also go through an example that uses floats:

a = 985.5 b = 115.25 print(divmod(a,b))

In this example, `8.0` is the floor quotient of `985.5` divided by `115.25`, and `63.5` is the remainder.

Keep in mind that you can use the floor division operator `//` and the modulo operator `%` to verify what `divmod()` did:

print(a//b) print(a%b)

When using the `divmod()` function in Python. we get both the whole number of times the division occurs and the remainder returned.

Quotient and Remainder Simultaneously - divmod()

In Python, you can use the operator `**` to raise a number by an exponent, or you can use the built-in function `pow()` which takes in two numbers.

To see how the `pow()` function works, let’s say we are doing research on bacteria and want to see how many bacteria we’ll have at the end of the day if we start with 1. The particular bacteria we’re working with doubles each hour, so we’ll be calculating 2 (doubling) to the power of the total number of hours (24 in our case).

hours = 24 total_bacteria = pow(2,hours) print(total_bacteria)

We passed two integers to the `pow()` function and determined that by the end of this 24-hour period, we’ll have over 16 million bacteria.

In mathematics, if we want to calculate 3 to the power of 3, it is generally written like this:

3³

The computation that we are completing is 3 x 3 x 3, which is equal to 27.

To calculate 3³ in Python, we would type out `pow(3,3)`.

The function `pow()` will take both integers and floats, and provides an alternative to using the `**` operator when you intend to raise numbers to a certain power.

Power

Being able to quickly and readily round numbers becomes important when working with floats that have a lot of decimal places. The built-in Python function `round()` takes in two numbers, one to be rounded, and one that specifies the number of decimal places to include.

We’ll use the function to take a float with more than 10 decimal places and use the `round()` function to reduce decimal places to 4:

i = 17.34989436516001 print(round(i,4))

In the example above, the float `17.34989436516001` is rounded to `17.3499` because we have specified that the number of decimal places should be limited to `4`.

Note also that the `round()` function rounds numbers up, so instead of providing `17.3498` as the output, it has provided `17.3499` because the number following the decimal number `8` is the number `9`. Any number that is followed by the number `5` or greater will be rounded up to the next whole number.

Let’s break down the syntax for `round()`:

round(number to round,number of decimal places)

In everyday life, rounding numbers happens often, especially when working with money; we can’t split up a penny evenly among several friends.

Let’s go through an example of a simple program that can calculate a tip. Here we’ll provide figures, but we could rewrite the program to bring in user-provided numbers instead. In this example, 3 friends went to a restaurant who want to split a bill of £87.93 evenly, along with adding a 20% tip.

In this program, we ask first for output of the number after we calculate the total bill plus tip divided by 3, which evaluates to a number with a lot of decimal places: `35.172000000000004`. Since this number doesn’t make sense as a monetary figure, we use the `round()` function and limit the decimal places to 2, so that we can provide an output that the 3 friends can actually work with: `35.17`.

If you would prefer to round to a number with only 0 as a decimal value, you can do so by using 0 as the second parameter in the `round()` function:

round(345.9874590348545304636,0)

This would evaluate to `346.0`.

You can also pass integers into `round()` without receiving an error, in case you receive user input in the form of an integer rather than a float. When an integer is passed as the first parameter, an integer will be returned.

Rounding Numbers

The `sum()` function is used for calculating sums of numeric compound data types, including lists, tuples, and dictionaries.

We can pass a list to the `sum()` function to add all the items in the list together in order from left to right:

some_floats = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9] print(sum(some_floats))

This will work similarly with tuples and dictionaries:

The `sum()` function can take up to 2 arguments, so you can add an additional number in integer or float form to add to the numbers that make up the argument in the first position:

When you don’t include a second argument, the `sum()` function defaults to adding 0 to the iterable compound data type.

Calculating a Sum

Write a program that demonstrates `sum()`.

Write a program that demonstrates `pow()`, `round()` and `sum()`.

Write a program that demonstrates `abs()`, `divmod()`, `pow()`, `round()` and `sum()`.

1.3 Maths in Python Task

Submit your completed task...

If you need to get in touch with Mr McG then this is the way….

What's your name?

What's your name?

Thank you [name], that's great.

Can I ask where you are from?

Can I ask where you are from?

Hey [name], can I ask where you are from?

Hey [name], which class are you in?

OK [name], thanks for that.

I will need your email address so I can get back to you!!

I will need your email address so I can get back to you!!

Thanks [name], nearly there.

What can I help you with?

What can I help you with?

Thanks [name] from [whichclass][location], Mr McG will get your message…"

[message]"…and will get back to you ASAP via [email]. Catch you soon.

Thank you [name], Mr McG got your message.

Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.