Key Concepts

Review core concepts you need to learn to master this subject

Import Python Modules

# Three different ways to import modules: # First way import module module.function() # Second way from module import function function() # Third way from module import * function()

The Python import statement can be used to import Python modules from other files.

Modules can be imported in three different ways: import module, from module import functions, or from module import *. from module import * is discouraged, as it can lead to a cluttered local namespace and can make the namespace unclear.

Modules in Python
Lesson 1 of 1
  1. 1
    In the world of programming, we care a lot about making code reusable. In most cases, we write code so that it can be reusable for ourselves. But sometimes we share code that’s helpful across a br…
  2. 2
    datetime is just the beginning. There are hundreds of Python modules that you can use. Another one of the most commonly used is random which allows you to generate numbers or select items at random…
  3. 3
    Notice that when we want to invoke the randint() function we call random.randint(). This is default behavior where Python offers a namespace for the module. A namespace isolates the functions, cl…
  4. 4
    Let’s say you are writing software that handles monetary transactions. If you used Python’s built-in floating-point arithmetic to calculat…
  5. 5
    You may remember the concept of scope from when you were learning about functions in Python. If a variable is defined inside of a function, it will not be accessible outside of the function. …
  6. 6
    You’ve learned: - what modules are and how they can be useful - how to use a few of the most commonly used Python libraries - what namespaces are and how to avoid polluting your local namespace - …