Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Imports & Standard Library | Functions & Modularity
Introduction to Python with AI

bookImports & Standard Library

Python's Standard Library is a collection of built-in modules with ready-made functions, classes, and constants.

By importing them, you can use tools for math, random numbers, files, dates, and more β€” saving time and effort without extra installations.

Note
Example Prompts
  • How do you import a standard module in Python and use one of its functions?
  • How do you import a specific function from a module in Python?
  • What is aliasing in imports? Show code example.

Importing a Module

Use the import keyword to bring in a module, e.g. import math. Then access its functions or constants with dot notation: math.sqrt(25), math.pi.

Modules work like toolboxes β€” import one to use all its tools.

Importing Specific Items

Use from ... import ... to bring in only what you need from a module. For example: from math import sqrt lets you call sqrt(25) directly.

This keeps code shorter and cleaner, especially if you use the same function often.

Aliasing

You can rename a module when importing it with as. Example: import random as rnd lets you call rnd.randint(1, 10) instead of random.randint(1, 10).

Aliasing shortens code and avoids name conflicts. It's also common in conventions, like import numpy as np.

Useful Standard Modules

Here are some commonly used modules from the standard library with documentation links:

  • math β€” for square roots, constants, and trigonometry;
  • random β€” for generating random numbers, shuffling, and choices;
  • datetime β€” for working with dates, times, and timestamps;
  • os β€” for managing files and directories on the operating system;
  • sys β€” for interacting with the Python interpreter and command-line arguments;
  • statistics β€” for calculating mean, median, and other statistical values.

Summary

  • Python has many built-in modules, ready to import with import;
  • You can import a full module or just a specific function;
  • Use as to assign a shorter alias for convenience;
  • The standard library gives you access to tools for math, randomness, dates, files, and more.

Try It Yourself

  1. Import the math module and print the value of Ο€\pi;
  2. Import randint from the random module and generate a number from 1 to 6;
  3. Use import datetime as dt and print today's date.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What are some examples of using these standard library modules?

Can you explain the difference between importing a whole module and importing specific items?

How do I choose when to use an alias for a module?

Awesome!

Completion rate improved to 5

bookImports & Standard Library

Swipe to show menu

Python's Standard Library is a collection of built-in modules with ready-made functions, classes, and constants.

By importing them, you can use tools for math, random numbers, files, dates, and more β€” saving time and effort without extra installations.

Note
Example Prompts
  • How do you import a standard module in Python and use one of its functions?
  • How do you import a specific function from a module in Python?
  • What is aliasing in imports? Show code example.

Importing a Module

Use the import keyword to bring in a module, e.g. import math. Then access its functions or constants with dot notation: math.sqrt(25), math.pi.

Modules work like toolboxes β€” import one to use all its tools.

Importing Specific Items

Use from ... import ... to bring in only what you need from a module. For example: from math import sqrt lets you call sqrt(25) directly.

This keeps code shorter and cleaner, especially if you use the same function often.

Aliasing

You can rename a module when importing it with as. Example: import random as rnd lets you call rnd.randint(1, 10) instead of random.randint(1, 10).

Aliasing shortens code and avoids name conflicts. It's also common in conventions, like import numpy as np.

Useful Standard Modules

Here are some commonly used modules from the standard library with documentation links:

  • math β€” for square roots, constants, and trigonometry;
  • random β€” for generating random numbers, shuffling, and choices;
  • datetime β€” for working with dates, times, and timestamps;
  • os β€” for managing files and directories on the operating system;
  • sys β€” for interacting with the Python interpreter and command-line arguments;
  • statistics β€” for calculating mean, median, and other statistical values.

Summary

  • Python has many built-in modules, ready to import with import;
  • You can import a full module or just a specific function;
  • Use as to assign a shorter alias for convenience;
  • The standard library gives you access to tools for math, randomness, dates, files, and more.

Try It Yourself

  1. Import the math module and print the value of Ο€\pi;
  2. Import randint from the random module and generate a number from 1 to 6;
  3. Use import datetime as dt and print today's date.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
some-alt