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 Cursor

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.

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.
question mark

Which syntax correctly imports the math module in Python?

Select the correct answer

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 standard modules I can use in Python?

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

How do I use aliasing when importing modules?

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.

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.
question mark

Which syntax correctly imports the math module in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
some-alt