Python’s built-in `round()`

function takes two input arguments: a `number`

and an optional `precision`

in decimal digits. It rounds the number to the given precision and returns the result. The return value has the same type as the input number—or integer if the `precision`

argument is omitted. Per default, the precision is set to 0 digits, so `round(3.14)`

results in `3`

.

Table of Contents

## Usage

Learn by example! Here are some examples of how to use the `round()`

built-in function.

>>> round(3.14) 3 >>> round(3.14, ndigits=1) 3.1 >>> round(3.13, ndigits=-1) 0.0 >>> round(4458.1242, ndigits=-1) 4460.0 >>> round(3.14159, ndigits=3) 3.142

## Syntax round()

You can use the `round()`

method as follows:

Syntax:round(number, precision) # rounds the number to the given precision and returns the result

Argument | `number` | Numerical value—in most cases a float. |

Argument | `ndigits` | (Optional) Precision in number of decimal digits. Default 0. |

Return Value | `number` | Numerical value rounded to the given precision. |

## Video round()

## Interactive Shell Exercise: Understanding round()

Consider the following interactive code:

* Exercise: *Guess the output before running the code.

**But before we move on, I’m excited to present you my new Python book Python One-Liners** (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a **single line of Python code.** But it’s also an **introduction to computer science**, data science, machine learning, and algorithms. *The universe in a single line of Python!*

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners

## How to Round All Values in a List in Python?

**Problem Formulation: **Given a list of numerical values. How to round all values in the list to a given precision?

To round all values of a list, use list comprehension `[round(x, ndigits=precision) for x in list]`

to apply the `round()`

function to all values in the list and place the resulting numerical values into a new list:

lst = [3.14, 3.44444, 4.424242] rounded = [round(x, ndigits=2) for x in lst] print(rounded) # [3.14, 3.44, 4.42]

## Python round() vs NumPy round()

Python’s `round()`

function rounds a single numerical value to a single rounded value, given a certain precision. NumPy‘s `np.round()`

function can do the same but more: if you pass an array or list of numerical values, it’ll round all values in the passed array-like data structure. In addition to that, in contrast to Python’s built-in `round()`

function that returns an integer if no precision argument is given, the function `np.round()`

always returns a float.

>>> import numpy as np >>> lst = [3.14, 3.44444, 4.424242] >>> round(lst[0]) 3 >>> np.round(lst[0]) 3.0 >>> np.round(lst) array([3., 3., 4.])

## Summary

Python’s built-in `round()`

function takes two input arguments:

- a
`number`

, and - an optional
`precision`

in decimal digits.

It rounds the number to the given precision and returns the result.

>>> round(3.14, ndigit=1) 3.1 >>> round(3.14) 3

The return value has the same type as the input number—or integer if the `precision`

argument is omitted. Per default, the precision is set to 0 digits, so `round(3.14)`

results in `3`

.

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

## Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

**You build high-value coding skills by working on practical coding projects!**

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is ** YES!**, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.