Get startedGet started for free

Building with built-ins

1. Building with built-ins

Python comes with a number of built-in components that you can think of as a "batteries included" concept. Although these aren't exhaustive, they give us more than enough to start using Python out-of-the-box. Let's explore a few built-in components that help write efficient Python code.

2. The Python Standard Library

Built-in components are referred to as the Python Standard Library. This library comes with every Python installation and is commonly cited as one of Python's greatest strengths. Python has a number of built-in types. We'll be focusing on specific data structure types like lists, tuples, sets, and dictionaries. Python also comes with built-in functions that provide a variety of problem-solving features. Listed here are a few of the built-in functions we'll discuss and use in the course. It's worth noting that Python's built-ins have been optimized to work within the Python language itself. Therefore, we should default to using a built-in solution (if one exists) rather than developing our own.

3. Built-in function: range()

Let's explore some notable built-in functions; starting with range. This is a handy tool whenever we want to create a sequence of numbers. Suppose we wanted to create a list of integers from zero to ten. We could explicitly type out each integer, but that is not very efficient.

4. Built-in function: range()

Instead, let's use range to accomplish this task. We can provide range with a start and stop value to create this sequence. Or, we can provide just a stop value assuming that we want our sequence to start at zero. Notice that the stop value is exclusive, or up to but not including this value. Also note the range function returns a range object, which we can convert into a list and print.

5. Built-in function: range()

range can also accept a start, stop, and step value (in that order). In this block of code, we tell range to create a sequence of numbers starting at two, ending at ten, and incrementing by two.

6. Built-in function: enumerate()

Another useful built-in function is enumerate. enumerate creates an index item pair for each item in the object provided. For example, calling enumerate on the list letters produces a sequence of indexed values. Similar to range, enumerate returns an enumerate object, which can also be converted into a list and printed.

7. Built-in function: enumerate()

We can also specify the starting index of enumerate with the keyword argument start. Here, we tell enumerate to start the index at five by passing start equals five into the function call.

8. Built-in function: map()

The last notable built-in function we'll cover is map. map applies a function to each element in an object. Notice that map takes two arguments; first, the function you'd like to apply and second, the object you'd like to apply that function on. Here, we use map to apply the built-in function round to each element of the nums list.

9. Built-in function: map()

map can also be used with a lambda, or, an anonymous function. Notice here, that we can use map and a lambda expression to apply a function, which we've defined on the fly, to our original list nums. The map function provides a quick and clean way to apply a function to an object iteratively without writing a for loop.

10. Let's start building with built-ins!

Now that we've covered a few key components of the Python Standard Library let's start building with these built-ins.