Abs() and Round() in Python | Python for Data Science |Day 10
Hello, Python gang!
The Data Monk is back with a new agenda for today. Let’s stick to the abs() and round() in Python functions for the discussion today. The two functions, abs() and round() in Python are used in the mathematical aspects of Python. They are very easy to understand. Just follow along as we hop on our journey.
As you must have guessed already from the meme, the absolute value of a number is the modulus of a number, which implies that it can never be negative.
In Python, we use the abs() function to find the modulus of integers, floating point numbers and even complex numbers.
Please open your IDEs or notebooks to try the lines of code below and compare your outputs with ours:
Positive float number:
Negative float number:
For the last line of code, the absolute value of the complex number is taken as its magnitude.
z = a + ib
i.e. square root of a and b.
The round() function rounds the given number to the specified number of digits and returns a float number.
We can also specify the number of digits upto which it should be rounded. Look at the line of code below:
Attempt the questions below to understand abs() and round(). You can also refer to https://www.geeksforgeeks.org/abs-in-python/ for the functions.
Do practice these questions on your own before seeking aid from our provided solutions.
Alright, Pyfam. That’s the end of this tutorial. We will be back with another interesting topic. Stay with us for the rest of the series. Happy coding!
We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert
Intern | The Data Monk