A Deep Dive into Python’s Built-In Functions

  • 13 mins read

A Deep Dive into Python’s Built-In Functions

Getting the hang of Python built-in functions is vital to understanding the language. Many programmers love Python as a back-end programming language because it’s easy to learn, and its syntax can be understood by any level programmer who knows the English language. This makes it incredibly useful for a wide range of applications, including web development, data analysis, artificial intelligence, and much, much more. Python also stands out as a frontrunner because of its many available built-in functions; at the time this article was published, there were sixty-eight.

These many and varied built-in functions are there because of the DRY programming philosophy, which stands for “don’t repeat yourself.” This philosophy is an important one to get behind because you’ll be able to code faster and smarter, and you’ll ensure any programmer can come after you and work with your code easily. The concept of a function was made specifically for this purpose because it allows you to put reusable code in function blocks, allowing you to execute the contents with a short line of code.

If you’re not sure where to start with Python’s many built-in functions, you’ve come to the right place. Over the course of this article, we’ll walk you through how to use the most popular built-in functions available to you as a Python developer. You’ll also learn how to use these functions with easy code examples that put these functions into action.

Before we begin, here is a list of the built-in functions you’ll learn over the course of our time together:

  1. upper() and lower() 
  2. round() 
  3. print() 
  4. input() 
  5. int() 
  6. range() 

Formatting Strings with Upper() and Lower()

These two functions have a variety of use cases; however, to learn how it’s used, we’ll talk about the easiest use for it—formatting output on the console. If you’re still a beginning Python developer, it’s likely you’re either getting your data by creating variables yourself or through user input (we’ll talk more about how to do user input a little later), but as you build your skills, your data will likely come from other sources, such as APIs, databases, or web scraping. When dealing with both outside sources and user input, you have very little control over how the data is stored in your program. That’s where these two simple yet might built-in functions come in to save the day.

Let’s look at an example:

To start out this code example, we created three different variables that had varying cases yet used the same two words in the strings. One mixes both upper and lower, the second has only lowercase, and the third has all uppercase.

For shorter code, we also decided to do the function call within the print statement, which incidentally is another built-in function Python has that we’ll discuss a little later. This function doesn’t change the original string, but it does return the result. We could have also saved these commands into their own variables, but that wasn’t necessary.

When we execute this program, here is the result:

hello world

Out in the real world, your use of these functions will be much more complex, but we’re setting up the foundational knowledge today that will allow you to build to that skill level. So, if all you can do with it right now is display your own variables in the console, then we think that’s a huge win.

Simplify Calculations with Round()

This is another function that’s also helpful when you want to format data that gets sent to the console. If you’re familiar with basic math, you probably already assumed that this Python function will round a decimal number. If so, your assumption is correct. It allows you to work with floating point numbers with less precision. For instance, if you’re working with financial transactions, you might need to round the final total that gets displayed on the console. When you use round() in this case, you would essentially be telling it to round the result to only two decimal places.

Let’s look and see how that works in code:

python built-in functions

Here, we have three different floating point numbers that you can probably imagine might have too much precision for a financial application since the smallest denomination of money we have (at least in the United States) is .01. So, adding these three numbers together would give us one too many decimal points. 

What can we do about that?

Well, let’s look at the round function in the code: it rounds the subtotal to two decimal places and returns that value to the finalSubtotal variable so we can access it later for printing. If it was applicable to our use case, we could have also rounded this number to three or more decimal places by changing the “2” in our code.

Here is what prints on the console:

Subtotal: 560.038

Final Subtotal: 560.04

The point of using this function in our code is to make this calculation more useful to the application itself. So, if this final subtotal was used to send deposits to a U.S. bank account, it would have no issues since it was in the expected format—with the two decimal places.

Display Data on the Console with Print()

With this Python built-in function, what you see is what you get—this function’s role is to print data to the console. There are many reasons why you might need to print your data to the console:

  • Displaying a welcome message to the user
  • Giving additional information or instructions for input
  • Outputting results of an algorithm
  • Displaying information that helps you debug your code
  • Giving the user more information about an exception

If you’re just beginning your journey as a Python developer, you might only use it to display a string of characters, but you can do more than that:

  • Pass it other functions to display the result of those functions
  • Insert multiple variables into your string
  • Display the results of formulas or calculations
  • Concatenate several strings together 

So, let’s look at some code to see how it works:

python built-in functions

If you look at the first print command on line 8, you’ll notice that it looks like a simple print statement. There’s nothing fancy here; we simply put the string we wanted to display in double quotes. However, if you look at line 9, you’ll see that we can also do that with single quotes. Python doesn’t care which one you use.

However, it would on line 10 if we didn’t put in the escape characters. If you’re using double quotes and want to print a string with double quotes in it, you’ll need to use the \” escape character. You can also use the following escape characters in a print string:

  • \’ — displays a single quote
  • \\ — displays a backslash
  • \n — adds a new line
  • \t — adds a tab

One thing you’ll want to learn how to do is format a string that includes multiple variables. To do this, you need to add the “f” before the opening quote. And, once you do that, you can easily add {variables} in your string. You can see this accomplished on line 12. Then, on line 13, you can see an alternate way to achieve this.

Lastly, we have string concatenation on line 15. It looks a bit complicated because we had to add the spaces in-between the variables, but it’s essentially accomplishing the same thing we did on lines 12 and 13—it’s just not as easy to read. For that reason, we prefer using the formatted syntax.

Interact with Users with Input()

What would a program be if it didn’t allow you to accept user input? This is likely one of the first things you should learn as you grow your Python development skills because it allows you to ease into designing real-world uses for your software. The input function itself does two things:

  1. It prints a message to the console, prompting a user to input some data.
  2. It saves that data into a string.

Let’s look at a simple code example for this function:

python built-in functions

As you can see in lines 3 through 7, the input statement uses a syntax very similar to the print function we just learned about in the last section. It prints one at a time, then waits for the user to enter their data, saving it into the respective variable. Then, on line 9, it formats the data in a specific way that makes it more readable.

However, there is one thing we haven’t yet talked about with the input function: What happens if the data you need to store in a variable is an integer? We’ll explore the answer to that with the next function.

Cast Strings into Integers with Int()

The input function is extremely useful and necessary when working with programs that require interaction from the user. However, it wouldn’t be a very useful function if all you could ever do with it is store strings. What if you needed to perform calculations with the data? That’s where int() comes in and saves the day.

We can see how this works with the following code example:

python built-in functions

To illustrate the difference, let’s look at what happens without casting. Lines 3 through 6 take input from the user, store them as strings, then appear to calculate the sum of the two variables. However, that’s not exactly what’s happening. If you run this code, you will get the following result (where a = ‘70’ and b = ‘80’):

70 + 80 = 7080

When it prints the result, it looks like a normal calculation; however, what it’s really doing is concatenating the strings. It’s not actually giving you the sum of the numbers.

But, in lines 8 through 11, we can fix that by casting the two variables as integers. So, here, instead of saving them as strings, we’re saving them as integers. If we give it the same input (a = 70 and b = 80), we get this result instead:

70 + 80 = 150

And this is the correct result.

An Overview of the Other Python Built-In Functions

Like we discussed early in this article, there are many more functions you can use that are built into Python. Here is a list of the other functions and what they can do:

abs()Returns the absolute value of a number.
aiter()Returns an asynchronous iterator for an asynchronous iterable.
all()Returns True if all elements in an iterable are True.
anext()Returns the next item from an asynchronous iterator.
any()Returns True if any of the elements in an iterable are True.
ascii()Returns a string containing a printable representation of a specific object, but it escapes the non-ASCII characters.
bin()Converts an integer to a binary string.
bool()Returns a boolean value, True or False.
breakpoint()Drops you into the debugger at the call site.
bytearray()Returns a new array of bytes.
bytes()Returns a new bytes object.
callable()Returns True if the object is callable.
chr()Returns the character that represents the Unicode passed to it.
@classmethod()Transforms a method into a class method.
compile()Compiles into a code or AST object.
complex()Returns a complex number.
delattr()Deletes an object attribute.
dict()Creates a new dictionary.
dir()Returns a list of valid attributes for an object.
divmod()Returns a pair of numbers consisting of their quotient and remainder.
enumerate()Returns an enumerate object.
eval()Returns the result of an evaluated expression.
exec()Dynamically executes Python code.
filter()Constructs an iterator from elements of an iterable that evaluate as True.
float()Casts a string into a floating point number.
frozenset()Returns a new frozenset object.
getattr()Returns the value of an object’s attribute.
global()Returns a dictionary that implements the module’s namespace.
hasattr()Returns True if an object has the attribute.
hash()Returns the hash value of an object.
help()Invokes Python’s built-in help system.
hex()Converts an integer to a hexadecimal.
id()Returns the id of an object.
isintance()Returns True if the object is an instance of the argument.
issubclass()Returns True if the class is a subclass of the argument.
iter()Returns an iterator object.
len()Returns the length of an object.
locals()Updates and returns a dictionary representing the current local symbol table.
map()Returns an iterator that applies function to every item of iterable.
max()Returns the largest item in an iterable.
memoryview()Returns a memory view object.
min()Returns the smallest item in an iterable.
next()Retrieves the next item from the iterator.
object()Returns a new featureless object.
oct()Converts an integer to an octal string.
open()Opens a file.
ord()Returns an integer representing the Unicode of a character.
pow()Returns the base to the specified power.
property()Returns a property attribute.
repr()Returns a string of a printable representation of an object.
reversed()Returns a reversed iterator.
set()Returns a new set object.
setattr()Assigns a value to an object’s attribute.
slice()Returns a slice of an object, given a range to slice.
sorted()Returns a new sorted list.
@staticmethodTransforms a method into a static method.
str()Returns a string version of an object.
sum()Sums the values within an iterable.
super()Refers to the parent class of an object.
type()Returns the type of an object.
vars()Returns the __dict__ attribute for an object.
zip()Iterates over multiple iterables in parallel fashion and returns a tuple containing all elements.
__import__()Imports a module using arguments to determine how to process the import.

Wrapping up Our Deep Dive into Python Built-In Functions

The examples we worked with in this article are quite simple, but as you gain more knowledge of the Python programming language, you’ll find more complex uses for them. You’ll also have the knowledge you need to tackle the more complex built-in functions listed in the above table.

Once you understand how to use the easier functions, you can move onto the others with the following principles in mind:

  • Read through Python’s documentation to understand the purpose of the function and how to use it properly.
  • If you find multiple functions that accomplish the same result, experiment with them to see which provides you with the best solution.
  • When you get stuck—or run into errors or exceptions—use your debugging skills to figure out what went wrong. If all else fails, do the proper research to extend your knowledge on the particular function.
  • You can ask AI tools for help, but we don’t recommend always relying on their complete solution. This often skips over the learning process and doesn’t always teach you what you need to know.
  • Experiment with different built-in functions to deepen your experience coding with Python.