Python's Built-in Functions: A Complete Exploration :
by:
blow post content copied from Real Python
click here to view original post
Python has many built-in functions that you can use directly without importing anything. These functions cover a wide variety of common programming tasks that include performing math operations, working with built-in data types, processing iterables of data, handling input and output in your programs, working with scopes, and more.
In this tutorial, you’ll:
- Get to know Python’s built-in functions
- Learn about common use cases of Python’s built-in functions
- Use these functions to solve practical problems
To get the most out of this tutorial, you’ll need to be familiar with Python programming, including topics like working with built-in data types, functions, classes, decorators, scopes, and the import system.
Get Your Code: Click here to download the free sample code that shows you how to use Python’s built-in functions.
Take the Quiz: Test your knowledge with our interactive “Python's Built-in Functions: A Complete Exploration” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Python's Built-in Functions: A Complete ExplorationTake this quiz to test your knowledge of the available built-in functions in Python. By taking this quiz, you'll deepen your understanding of how to use these functions and the common programming problems they cover, from mathematical computations to Python-specific features.
Built-in Functions in Python
Python has several functions available for you to use directly from anywhere in your code. These functions are known as built-in functions and they cover many common programming problems, from mathematical computations to Python-specific features.
Note: Many of Python’s built-in functions are classes with function-style names. Good examples are str
, tuple
, list
, and dict
, which are classes that define built-in data types. These classes are listed in the Python documentation as built-in functions and you’ll find them in this tutorial.
In this tutorial, you’ll learn the basics of Python’s built-in functions. By the end, you’ll know what their use cases are and how they work. To kick things off, you’ll start with those built-in functions related to math computations.
Using Math-Related Built-in Functions
In Python, you’ll find a few built-in functions that take care of common math operations, like computing the absolute value of a number, calculating powers, and more. Here’s a summary of the math-related built-in functions in Python:
Function | Description |
---|---|
abs() |
Calculates the absolute value of a number |
divmod() |
Computes the quotient and remainder of integer division |
max() |
Finds the largest of the given arguments or items in an iterable |
min() |
Finds the smallest of the given arguments or items in an iterable |
pow() |
Raises a number to a power |
round() |
Rounds a floating-point value |
sum() |
Sums the values in an iterable |
In the following sections, you’ll learn how these functions work and how to use them in your Python code.
Getting the Absolute Value of a Number: abs()
The absolute value or modulus of a real number is its non-negative value. In other words, the absolute value is the number without its sign. For example, the absolute value of -5 is 5, and the absolute value of 5 is also 5.
Note: To learn more about abs()
, check out the How to Find an Absolute Value in Python tutorial.
Python’s built-in abs()
function allows you to quickly compute the absolute value or modulus of a number:
>>> from decimal import Decimal
>>> from fractions import Fraction
>>> abs(-42)
42
>>> abs(42)
42
>>> abs(-42.42)
42.42
>>> abs(42.42)
42.42
>>> abs(complex("-2+3j"))
3.605551275463989
>>> abs(complex("2+3j"))
3.605551275463989
>>> abs(Fraction("-1/2"))
Fraction(1, 2)
>>> abs(Fraction("1/2"))
Fraction(1, 2)
>>> abs(Decimal("-0.5"))
Decimal('0.5')
>>> abs(Decimal("0.5"))
Decimal('0.5')
In these examples, you compute the absolute value of different numeric types using the abs()
function. First, you use integer numbers, then floating-point and complex numbers, and finally, fractional and decimal numbers. In all cases, when you call the function with a negative value, the final result removes the sign.
For a practical example, say that you need to compute the total profits and losses of your company from a month’s transactions:
>>> transactions = [-200, 300, -100, 500]
>>> incomes = sum(income for income in transactions if income > 0)
>>> expenses = abs(
... sum(expense for expense in transactions if expense < 0)
... )
>>> print(f"Total incomes: ${incomes}")
Total incomes: $800
>>> print(f"Total expenses: ${expenses}")
Total expenses: $300
>>> print(f"Total profit: ${incomes - expenses}")
Total profit: $500
In this example, to compute the expenses, you use the abs()
function to get the absolute value of the expenses, which results in a positive value.
Finding the Quotient and Remainder in Division: divmod()
Python provides a built-in function called divmod()
that takes two numbers as arguments and returns a tuple with the quotient and remainder that result from the integer division of the input numbers:
Read the full article at https://realpython.com/python-built-in-functions/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
July 01, 2024 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment