In this course, we're going to cover built-in functions and review the categories for these functions so that you get a sense of what they do. This course is part of a series of content designed to help you learn to program with the Python programming language.
- When the Python runtime starts up it bootstraps several built-in functions. These functions fall into some basic categories including:
- Two built-in functions of note at this stage are:
- The print function, which is used to display text-based output in the console
- The input function, which is used to get text-based input from the console
This course was designed for first-time developers wanting to learn Python.
This is an introductory course and doesn’t require any prior programming knowledge.
Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language. Should you wish to ask me a specific question, you can do that with the contact details on screen. You can also reach support by using the email address: email@example.com. And one of our cloud experts will reply.
When the Python runtime starts up it bootstraps some basic functionality for us to use. That includes object types such as strings, integers, booleans, and floats. It also includes a small collection of functions. These built-in functions are going to be our topic for this lesson. We’re going to review the categories for these functions so that you get a sense for what they do. We’re not going to cover all of Python’s built-in functions. Many of these are rabbit holes for another time. So we’re only going to cover a couple useful functions for new developers.
The built-in functions break down into different categories based on their purpose. First up are functions designed to interact with sequences. A sequence is an object that has multiple - sequential values. One example is the sum function which adds each number in the sequence and returns results. Also in this category are functions for finding the minimum and maximum objects in a sequence.
There’s another function, which determines the length of a sequence. Everything in this category interacts in some way with sequences. This next category relates to object types. These functions are named after built-in object types. They’re used to convert objects from one type into the type for which the function is named. Or to create a new object of the given type.
Here’s an example:
Let’s bind the name number to the string with the text ‘100’. Let’s attempt to use the add operator to add our number plus one. This throws an error because we’re attempting to add a string to a number and Python doesn’t know what to do in this scenario. This is common for developers to get data with one data type and have to convert it to another. The name number is bound to the string with the text ‘100’. Using the built-in int function we can provide the string as input and we’ll be returned an integer.
These functions will become familiar to you over time. You’ll likely use at least some of these often. Being able to convert data from one type to another is common. The next function category is used for data encoding. This category revolves around unicode.
Here’s how wikipedia describes unicode:
[Unicode] ...is an information technology standard for the consistent encoding, representation, and handling of text - expressed in most of the world's writing systems.
Basically, unicode is a giant lookup table; where numbers are placeholders for specific characters. The next function category relates to objects. Python provides quite a few functions which are able to inspect and interact with objects. The callable function accepts an object as input and it returns a boolean. True if the object is callable, False if not.
The hasattr function accepts an object and a name. When called it checks if the provided object includes the named attribute. This next category revolves around name bindings. These functions are all used to inspect which names the runtime has bound. As an example, the dir function accepts an object as input and returns a list containing all of the attributes and methods bound to the object.
The final category that we’ll cover revolves around input and output.
Most application users interact with the app using a graphical user interface. For example, using text boxes and buttons. Text boxes, buttons, sliders, etc, make for user-friendly input methods. However, those all require a lot more effort to create. So, developers often end up using console-based applications. The built-in input and print functions are used for console input and output. These built-in functions are called like any other callable. We use the function’s name, followed by any arguments wrapped in parentheses.
The first built-in function I want to cover a bit more is the print function. Print is often used for displaying some text in a console window. In fact, using print to display the phrase “Hello, World!” is commonly a developer’s first line of code. This tradition of printing hello world goes back decades. It’s a tradition experienced by developers of many different programming languages.
Let’s use the built-in print function to join in on that tradition. In Python, we type the function name print. Then we call it by providing the input. In this case, the string object which says Hello, World!
Print provides a rudimentary mechanism for displaying text in the console. Another built-in function that I want to introduce is named input. Input is used to prompt a person to type some input into a console window. For example, select option 1 or 2. So, Python’s built-in input function provides us with a crude console-based text input mechanism. And the print function provides us with a crude console-based text output.
Check out this example. The built-in input function expects us to provide a prompt to display in the console. This function displays the prompt and then waits for us to enter some text and hit return or enter. The input function returns a string containing the text that we entered. The name greeting is bound to the string object returned from the input function. We can use the print function to display the text we entered. In this example, we combine the string hello to the greeting string by using the add operator. This results in a single string object being returned and printed.
All of these categories are intended to help you create a mental model for these built-in functions. You don’t need to memorize all of these individual functions. However, it’s worth reading through Python’s documentation for each. As it summarizes the use case for each. The purpose of these built-ins is to help developers get the most out of Python. These functions are built-in because they’re commonly required. Or at least they once were.
Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
When the Python runtime starts up it bootstraps several built-in functions. These functions fall into some basic categories including:
Two built-in functions of note at this stage are:
The print function, which is used to display text-based output in the console
The input function, which is used to get text-based input from the console
That's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.