Python ships with a big standard library — modules for JSON, dates, regex, math, random, and dozens more. To use one, you import it.
import json
data = json.loads('{"a": 1, "b": [2, 3]}')
print(data["b"]) # [2, 3]import json makes the module available; then I use json.something(...) to call its functions.
Right. The module is a bundle of functions, classes, and constants. Once imported, you reach into it with the dot — json.loads, json.dumps. The dot tells the reader "this comes from the json module."
Are there other forms of import?
Three you'll see:
import json # bring in module; use as json.loads
from json import loads # bring in the name directly
from json import loads as parse_json # rename on importWeek 3 will use from datetime import datetime (extracting one class from the module). The first form is the most common — keeps the namespace tidy.
And imports go at the top of the file?
Yes. PEP 8 says imports come first, before any other code. Easier to scan dependencies. Python doesn't require it (you can import inside a function for lazy loading), but unless you have a specific reason, top of the file.
import — bring a module into scopeThree forms:
import json # use as json.loads
from json import loads # use as loads (no prefix)
from json import loads as parse_json # use under a renamed aliasimport moduleThe canonical form. Adds the module to your namespace; access its contents with the dot:
import json
import datetime
import re
data = json.loads('{"a": 1}')
now = datetime.datetime.now()
match = re.search(r"\d+", "x42y")The prefix json., datetime., re. makes the source of each function obvious.
from module import nameBrings just one name in:
from json import loads, dumps
from datetime import datetime
data = loads('{"a": 1}')
now = datetime.now()Use this when you'll call the name many times and the prefix would be noise — and when there's no ambiguity. Don't from X import * in normal code; you lose track of where names come from.
from module import name as aliasUseful when names collide or are too long:
from datetime import datetime as dt
from collections import defaultdict as ddictCommon convention you'll see: import numpy as np, import pandas as pd. Stick to community conventions when they exist.
PEP 8: top of the file, in this order:
json, re, datetime)requests, numpy)from .utils import ...)Blank line between groups.
| Module | What |
|---|---|
json | JSON parse + serialize (lesson 17) |
csv | CSV read + write (lesson 16) |
re | regex (lesson 18) |
datetime | dates and times (lesson 20) |
pathlib | filesystem paths (week 4) |
collections | Counter, defaultdict (week 4) |
math, random, os, sys | the rest of the standard kit |
import is notimport json works offline.requests, pandas, etc. need pip install first. Pyodide (this track's runtime) ships only stdlib + a curated subset of packages; we'll only use stdlib.Use import json and json.loads to parse a JSON string. Then index into the result like any dict.
Python ships with a big standard library — modules for JSON, dates, regex, math, random, and dozens more. To use one, you import it.
import json
data = json.loads('{"a": 1, "b": [2, 3]}')
print(data["b"]) # [2, 3]import json makes the module available; then I use json.something(...) to call its functions.
Right. The module is a bundle of functions, classes, and constants. Once imported, you reach into it with the dot — json.loads, json.dumps. The dot tells the reader "this comes from the json module."
Are there other forms of import?
Three you'll see:
import json # bring in module; use as json.loads
from json import loads # bring in the name directly
from json import loads as parse_json # rename on importWeek 3 will use from datetime import datetime (extracting one class from the module). The first form is the most common — keeps the namespace tidy.
And imports go at the top of the file?
Yes. PEP 8 says imports come first, before any other code. Easier to scan dependencies. Python doesn't require it (you can import inside a function for lazy loading), but unless you have a specific reason, top of the file.
import — bring a module into scopeThree forms:
import json # use as json.loads
from json import loads # use as loads (no prefix)
from json import loads as parse_json # use under a renamed aliasimport moduleThe canonical form. Adds the module to your namespace; access its contents with the dot:
import json
import datetime
import re
data = json.loads('{"a": 1}')
now = datetime.datetime.now()
match = re.search(r"\d+", "x42y")The prefix json., datetime., re. makes the source of each function obvious.
from module import nameBrings just one name in:
from json import loads, dumps
from datetime import datetime
data = loads('{"a": 1}')
now = datetime.now()Use this when you'll call the name many times and the prefix would be noise — and when there's no ambiguity. Don't from X import * in normal code; you lose track of where names come from.
from module import name as aliasUseful when names collide or are too long:
from datetime import datetime as dt
from collections import defaultdict as ddictCommon convention you'll see: import numpy as np, import pandas as pd. Stick to community conventions when they exist.
PEP 8: top of the file, in this order:
json, re, datetime)requests, numpy)from .utils import ...)Blank line between groups.
| Module | What |
|---|---|
json | JSON parse + serialize (lesson 17) |
csv | CSV read + write (lesson 16) |
re | regex (lesson 18) |
datetime | dates and times (lesson 20) |
pathlib | filesystem paths (week 4) |
collections | Counter, defaultdict (week 4) |
math, random, os, sys | the rest of the standard kit |
import is notimport json works offline.requests, pandas, etc. need pip install first. Pyodide (this track's runtime) ships only stdlib + a curated subset of packages; we'll only use stdlib.Use import json and json.loads to parse a JSON string. Then index into the result like any dict.
Create a free account to get started. Paid plans unlock all tracks.