yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Importing modules | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

If you were building a bike, you would probably go off and get a seat, a set of handlebars, a set of tires, and then assemble those pieces together. You wouldn't harvest your own rubber and try and forge a tire from scratch. With programming, we do the same; we can reuse components that other people have already built and made available. As programmers, then, our job is to assemble these components in unique and interesting ways, rather than spend time resolving the same problems over and over again.

In Python, programmers can package code that they've written for other people to use in what's called a module. You may also hear them refer to as libraries or packages. A module is essentially just a file of code, generally organized into a set of functions. Modules can quickly uplevel what we're able to accomplish in a program because we can use that functionality without having to understand the details of how that code works.

We've used functions written by other people before in the form of built-in functions like print, input, round, and len. These are different from modules because they're loaded into Python by default. We can access the print function anywhere we write Python code. With modules, we have to explicitly import the module into our code first before we can access any of its functions.

That's because the computer has limited memory; it doesn't have space to pre-memorize the thousands of modules that are out there. In the rare event that we might use a specific one, instead, it waits for us to tell it that we need a specific module. Only then does it bother to go read that file and memorize all those functions for us.

To load a module into our program, we use the import statement. This is just the keyword import followed by the module name. Import statements go at the very top of our program. Then, we can access any function defined inside that module with the syntax module_name.function_name. This tells the computer to look for that function inside that module and then run that code.

Let's take a look at an example built-in module. Here we have the documentation for the math module. The math module provides functions for a lot of common mathematical operations. We see here that we have a function to get the square root of a number. It takes one input or argument and returns out the square root.

So, if we open up our code editor, we can add an import statement at the top to import the math module, and then we can call the square root function using math.sqrt. Note that if we forgot the import statement or the preceding module name syntax, we would get a name error because the computer wouldn't know where to find the square root function.

Now that we've imported the math module, we can use any of its other functions in our program, like perm or factorial, which saves us from having to write code to calculate factorial ourselves. We can take advantage of the many other built-in Python modules like datetime, random, and unittest to expand the functionality of our programs.

Built-in modules can be pre-installed with the Python programming language, so they're available for us to import anywhere we write Python code. There are many other modules out there that programmers have made available for general use, but they don't come pre-installed with Python. We have to download the module code off the internet and install it to make it available to import into our programs.

With these modules, we need to be careful about plagiarism. We can't just use any code we want as if it's our own without giving credit to the original creator. Programmers can decide how other people can use their module through a license agreement. Some common licenses, like MIT and Apache, are open source, which means they largely allow anyone to use, modify, or distribute the code as they please.

Other licenses may be more restrictive, where they don't allow people to modify the code or use it in commercial projects. As with downloading anything off the internet, we also need to think about security. One, it could just be a virus masquerading as a Python module; and two, that module could have bugs or other security vulnerabilities. We don't have any guarantees from the people who wrote it, so we're using it at our own risk, which ultimately makes any programs we build on top of it vulnerable to the same problems.

But note that there are many popular and trusted modules out there, like Pandas for data analysis, Pygame for game development, and TensorFlow for machine learning, that you may find fun to experiment with as you become more comfortable with Python. Just note that the Khan Academy IDE doesn't support downloading arbitrary modules off the internet for these same security reasons. So, for now, let's stick to the built-in modules because we still have so much to explore.

More Articles

View All
Great White and Oceanic Whitetip Sharks: Photographing Top Ocean Predators (Part 3) | Nat Geo Live
The third in the four species of sharks that I wanna share with you tonight is the Oceanic White Tip shark, a shark that has been listed as the fourth most dangerous species of sharks if you pay attention to such lists. It’s an animal that is a true pelag…
What’s Hiding at the Most Solitary Place on Earth? The Deep Sea
Sometimes the world feels… hmm, boring. We’ve visited all the remote islands, conquered the Arctic, and penetrated the deepest jungles. But there is still one place to explore. It’s a wet and deadly desert inhabited by mysterious creatures living in total…
VMware Cofounder Diane Greene with Jessica Livingston at the Female Founders Conference
Let’s, I’m going to stay here. Oh, okay, right there. Come over. Alright, well, we’ll just get into this because I have a whole list of questions and how many we’ll get through. So I’m going to selfishly ask a question because I’m very interested. When yo…
Subtracting a 1-digit number with regrouping | 2nd grade | Khan Academy
So we have the number 35, which is 3 tens, because we have a 3 in the tens place, so we have 3 tens, and we have a 5 in the ones place. So, this is the ones place, I’ll do the ones place in that same purple color. This is the ones place, and we see it rep…
Got Bees? Meet a Swarm Chaser Up for the Challenge | Short Film Showcase
[Music] [Music] [Music] Hello! Oh gosh, do you have some bees? Okay, so are the bees outside? Are they hanging on a tree, or like, where are the bees at? You know, as a child, I spent a lot of time crafting and making miniatures. I think that that ties i…
The Poor Man's Rolex? | Kevin & Teddy Baldassarre Tudor Watches
It’s a bit of a funky look. It is. Well, you have to hand it to Tudor; they’ve really, really focused on great dials, great value. If there was ever a brand that could encroach on a Rolex, it would be Tudor. Absolutely, it’s its own standalone brand. This…