Python round

This Python math function rounds the specified expression or number to the nearest integer, and the syntax of this is

round(Number, Digits);

If the number argument is a positive or negative number, the Python round function returns the nearest decimal number. And if it is not a number, it returns TypeError.

Digits (Optional): If you omit this argument, Python round function displays the number to the nearest integer value. Otherwise, the Number prints the nearest to the decimal number specified in the Digits arguments.

Python round Function Example

This math function rounds the number to the nearest integer. In this example, we are going to find the nearest values of different data types. In the first two, we used Python round function directly on both the Positive decimals and negative decimals.

Tup = (10.98, 20.26, 30.05, -40.95 , 50.85) # Tuple Declaration

Lis = [-10.98, 32.65, -39.29, -42.15, -39.97] # List Declaration

  • Next two statements, we used the round Function with two arguments, so they display the nearest decimal fractions in the third position.
  • Following four, we used this on Tuple and List items. If you observe the Python screenshot, this Math function is working correctly on them.
  • Then, We tried directly on multiple values.
  • Last, We tried on the String value, and it returns TypeError.
Python ROUND Function

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.