Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 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?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 4.  3
some-alt