You have been writing everything yourself from scratch.
Every function, every loop, every piece of logic. That is exactly right for learning. But out in the real world, nobody builds everything from zero.
Python has been around since 1991. In that time, developers have written and shared hundreds of thousands of packages. Tools for working with data. Tools for making web requests. Tools for generating random numbers. Tools for working with dates. Tools for training neural networks.
All of it free. All of it one command away.
Knowing how to find, install, and use other people's code is one of the most valuable skills you will develop. This post is where that starts.
The Standard Library: Already Installed
Python ships with a large collection of built-in modules called the standard library. You do not need to install them. They are already there.
import brings a module into your file.
import math
print(math.pi)
print(math.sqrt(16))
print(math.ceil(4.2))
print(math.floor(4.9))
Output:
3.141592653589793
4.0
5
4
math is a module. A module is just a Python file with useful functions inside it. When you write import math, Python finds that file and makes everything in it available under the math. prefix.
More Standard Library Modules Worth Knowing
import random
print(random.randint(1, 10)) # random integer between 1 and 10
print(random.choice(["a", "b", "c"])) # random item from a list
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers) # list in random order
Output (yours will differ, it's random):
7
b
[3, 1, 5, 2, 4]
import datetime
today = datetime.date.today()
print(today)
now = datetime.datetime.now()
print(now)
Output:
2024-11-15
2024-11-15 14:32:07.482910
import os
print(os.getcwd()) # current working directory
files = os.listdir(".") # list files in current folder
print(files)
os is important for file system work. Checking if paths exist, creating folders, listing directory contents. You'll use it a lot when building data pipelines.
Importing Specific Things
Sometimes you only need one function from a module. No need to import the whole thing.
from math import sqrt, pi
print(sqrt(25))
print(pi)
Output:
5.0
3.141592653589793
Now you call sqrt directly without math. in front. Cleaner for functions you use constantly.
You can also give imports a shorter name.
import numpy as np
import pandas as pd
You haven't installed these yet but when you do, this is how every single tutorial and professional uses them. np for NumPy. pd for Pandas. It is so universal that using any other name would confuse people reading your code.
pip: Installing Packages From the Internet
The standard library is big but it doesn't have everything. The Python Package Index, PyPI, hosts over 500,000 additional packages contributed by developers worldwide.
pip is the tool that downloads and installs them.
Open your terminal. Not inside Python. Your regular terminal or VS Code's integrated terminal.
pip install requests
That's it. Python downloads the requests package and installs it. Now you can use it in any Python file on your machine.
Try a few packages that you'll use constantly in this series.
pip install requests
pip install numpy
pip install pandas
pip install matplotlib
Each one downloads and installs. Takes a few seconds each.
Now use requests to make a real web request.
import requests
response = requests.get("https://api.github.com")
print(response.status_code)
print(type(response.json()))
Output:
200
<class 'dict'>
Your Python code just talked to GitHub's server and got a response back. Status code 200 means success. The response came back as a dictionary. Real data from the real internet.
Virtual Environments: Keep Projects Separate
Here is a problem that will bite you eventually.
Project A needs version 1.0 of some package. Project B needs version 2.0 of the same package. If you install packages globally, one of those projects breaks.
Virtual environments solve this. Each project gets its own isolated space with its own packages.
python -m venv myenv
This creates a folder called myenv with a complete isolated Python installation inside.
Activate it:
# Windows
myenv\Scripts\activate
# Mac and Linux
source myenv/bin/activate
Your terminal prompt changes to show the environment name. Now any pip install goes into that environment only, not the global Python.
When you're done:
deactivate
For every serious project you start from this series onward, create a virtual environment first. It is a small habit that prevents big headaches.
requirements.txt: Sharing Your Setup
When you share a project with someone or deploy it to a server, they need to know which packages to install.
requirements.txt is the standard way.
pip freeze > requirements.txt
This saves every installed package and its version to a text file. Open it and it looks like this:
numpy==1.26.0
pandas==2.1.1
requests==2.31.0
matplotlib==3.8.0
Anyone who gets your project runs one command to install everything at once:
pip install -r requirements.txt
Start doing this now. Even for small projects. It is a professional habit that costs nothing.
Writing Your Own Module
Any Python file you write is a module that other files can import.
Create a file called helpers.py:
def greet(name):
return f"Hello, {name}!"
def add(a, b):
return a + b
PI = 3.14159
Now in a different file in the same folder:
import helpers
print(helpers.greet("Alex"))
print(helpers.add(5, 3))
print(helpers.PI)
Output:
Hello, Alex!
8
3.14159
Python found helpers.py in the same folder and imported everything from it. Your own reusable code, organized into modules, is how larger programs stay manageable.
Try This
Create modules_practice.py.
Part one: use the random module to build a simple dice roller. Roll two dice, each between 1 and 6. Print the result of each die and the total. Run it five times using a loop to make sure randomness is working.
Part two: use the datetime module to print today's date in a nice readable format like "Today is November 15, 2024." Look up strftime to format it properly. The search term is "python datetime strftime format codes."
Part three: create a file called calculator.py with four functions: add, subtract, multiply, and divide. Import that file in modules_practice.py and use each function at least once.
Part four: if you haven't already, install requests using pip and make a GET request to https://jsonplaceholder.typicode.com/users/1. Print the name and email from the response. It comes back as a dictionary.
What's Next
Phase 1 is almost done. Two more posts left. Next is list comprehensions, a cleaner way to build lists that you will see everywhere in Python code and will want to use immediately once you understand it.
Top comments (0)