Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python round() — A Simple Guide with Video

#1
Python round() — A Simple Guide with Video

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.

https://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" />

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 brand-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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python round() — A Simple Guide with Video first appeared on Finxter.



https://www.sickgaming.net/blog/2021/02/...ith-video/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 14 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 27 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 20 02-18-2021, 04:10 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 27 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] Python repr() Function — A Helpful Guide with Example xSicKxBot 0 31 02-15-2021, 02:40 PM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 26 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 29 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 33 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How to Send UDP Multicast in Python? xSicKxBot 0 31 02-03-2021, 01:31 PM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 28 02-01-2021, 04:09 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
News - Destiny 2021 Update: The Road to ...
Last Post: xSicKxBot
Today 03:03 AM
» Replies: 0
» Views: 0
News - Hot Wheels Unleashed Speeds Onto ...
Last Post: xSicKxBot
Today 03:02 AM
» Replies: 0
» Views: 0
Xbox Wire - The Future of Game Accessibi...
Last Post: xSicKxBot
Today 03:02 AM
» Replies: 0
» Views: 0
News - Get a job: Join Sealost Interacti...
Last Post: xSicKxBot
Today 03:02 AM
» Replies: 0
» Views: 1
[1.07] BO4 Zombie Trainer (Semi-Patched)
Last Post: trioxyn
Yesterday 08:14 PM
» Replies: 12
» Views: 469
HOW TO: Get all steam games. (Green Luma...
Last Post: trioxyn
Yesterday 07:28 PM
» Replies: 5
» Views: 1688
(Indie Deal) Castlevania, Pqube, Fantasy...
Last Post: xSicKxBot
Yesterday 07:15 PM
» Replies: 0
» Views: 5
Microsoft - Digital good: How to have mo...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 5
News - A Super Mario Lenticular Magnet S...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 5
News - Halo Infinite's Open World Honors...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 13

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020