Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Import Statements | Modules and Imports
Python Advanced Concepts
course content

Conteúdo do Curso

Python Advanced Concepts

Python Advanced Concepts

1. Modules and Imports
2. Error Handling
3. File Handling
4. Pytest Framework
5. Unittest Framework
6. Iterators and Generators

book
Import Statements

Hello again, awesome learners! 🌟 Now, let's enhance our skills by exploring different ways to import these modules. Understanding the nuances of import statements can significantly streamline your coding process and make your code cleaner and more efficient.

Python offers several ways to import modules, each with its own use cases. Let's break them down:

Standard Import

This is the most straightforward method. You import an entire module and access its functions or variables using the module name as a prefix.

12
import math print(math.sqrt(16)) # Outputs: 4.0
copy

Import with Aliases

If you find the module name long or if you want to avoid name conflicts, you can alias it to a shorter name.

12
import numpy as np print(np.array([1, 2, 3]))
copy

From...Import Statement

This method allows you to import specific functions, classes, or variables from a module, making them accessible directly without the module name prefix.

12
from datetime import datetime print(datetime.now())
copy

From...Import * Statement

Use this when you want to import everything from a module.

Caution: this can lead to unclear code and potential conflicts with names in other modules.

12
from math import * print(sqrt(25)) # Outputs: 5.0
copy

Using Aliases

Aliases can help make your code more readable and manageable, especially when dealing with modules that have long names. Here's how you can create an alias:

123
from matplotlib import pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
copy
1. What is the correct way to import the `randint` function from the `random` module so that it can be used directly without a module prefix?
2. Consider the following statement: `import os as operating_system`. What would be the correct way to use the `getcwd()` function from the `os` module after this import?
What is the correct way to import the `randint` function from the `random` module so that it can be used directly without a module prefix?

What is the correct way to import the randint function from the random module so that it can be used directly without a module prefix?

Selecione a resposta correta

Consider the following statement: `import os as operating_system`. What would be the correct way to use the `getcwd()` function from the `os` module after this import?

Consider the following statement: import os as operating_system. What would be the correct way to use the getcwd() function from the os module after this import?

Selecione a resposta correta

We've now mastered importing modules and using aliases to tidy up our code. In the next chapter, we’ll explore the extensive Python standard libraries and see how they can be incorporated into our projects! Keep up the great energy! 🚀

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt