Imports & 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.
- 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
- Import the
math
module and print the value of π; - Import
randint
from therandom
module and generate a number from1
to6
; - Use
import datetime as dt
and print today's date.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 5
Imports & Standard Library
Desliza para mostrar el menú
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.
- 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
- Import the
math
module and print the value of π; - Import
randint
from therandom
module and generate a number from1
to6
; - Use
import datetime as dt
and print today's date.
¡Gracias por tus comentarios!