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.

Module importing

# 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()

In Python, you can import and use the content of another file using import filename, provided that it is in the same folder as the current file you are writing.

Aliasing with ‘as’ keyword

# 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()

In Python, the as keyword can be used to give an alternative name as an alias for a Python module or function.

Date and Time in Python

# 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()

Python provides a module named datetime to deal with dates and times.

It allows you to set date ,time or both date and time using the date(),time()and datetime() functions respectively, after importing the datetime module .

random.randint() and random.choice()

# 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()

In Python, the random module offers methods to simulate non-deterministic behavior in selecting a random number from a range and choosing a random item from a list.

The randint() method provides a uniform random selection from a range of integers. The choice() method provides a uniform selection of a random element from a sequence.

Modules: 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

    […] is just the beginning. There are hundreds of Python modules that you can use. Another one of the most commonly used is […] which allows you to generate numbers or select items at random…

  3. 3

    Notice that when we want to invoke the […] function we call […] . This is default behavior where Python offers a namespace for the module. A namespace isolates the functions, classes, and …

  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 - …