Key Concepts

Review core concepts you need to learn to master this subject

Date and Time in Python

import datetime feb_16_2019 = datetime.date(year=2019, month=2, day=16) feb_16_2019 = datetime.date(2019, 2, 16) print(feb_16_2019) #2019-02-16 time_13_48min_5sec = datetime.time(hour=13, minute=48, second=5) time_13_48min_5sec = datetime.time(13, 48, 5) print(time_13_48min_5sec) #13:48:05 timestamp= datetime.datetime(year=2019, month=2, day=16, hour=13, minute=48, second=5) timestamp = datetime.datetime(2019, 2, 16, 13, 48, 5) print (timestamp) #2019-01-02 13:48:05

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 .

Aliasing with ‘as’ keyword

import datetime feb_16_2019 = datetime.date(year=2019, month=2, day=16) feb_16_2019 = datetime.date(2019, 2, 16) print(feb_16_2019) #2019-02-16 time_13_48min_5sec = datetime.time(hour=13, minute=48, second=5) time_13_48min_5sec = datetime.time(13, 48, 5) print(time_13_48min_5sec) #13:48:05 timestamp= datetime.datetime(year=2019, month=2, day=16, hour=13, minute=48, second=5) timestamp = datetime.datetime(2019, 2, 16, 13, 48, 5) print (timestamp) #2019-01-02 13:48:05

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

Import Python Modules

import datetime feb_16_2019 = datetime.date(year=2019, month=2, day=16) feb_16_2019 = datetime.date(2019, 2, 16) print(feb_16_2019) #2019-02-16 time_13_48min_5sec = datetime.time(hour=13, minute=48, second=5) time_13_48min_5sec = datetime.time(13, 48, 5) print(time_13_48min_5sec) #13:48:05 timestamp= datetime.datetime(year=2019, month=2, day=16, hour=13, minute=48, second=5) timestamp = datetime.datetime(2019, 2, 16, 13, 48, 5) print (timestamp) #2019-01-02 13:48:05

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.

random.randint() and random.choice()

import datetime feb_16_2019 = datetime.date(year=2019, month=2, day=16) feb_16_2019 = datetime.date(2019, 2, 16) print(feb_16_2019) #2019-02-16 time_13_48min_5sec = datetime.time(hour=13, minute=48, second=5) time_13_48min_5sec = datetime.time(13, 48, 5) print(time_13_48min_5sec) #13:48:05 timestamp= datetime.datetime(year=2019, month=2, day=16, hour=13, minute=48, second=5) timestamp = datetime.datetime(2019, 2, 16, 13, 48, 5) print (timestamp) #2019-01-02 13:48:05

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.

Module importing

import datetime feb_16_2019 = datetime.date(year=2019, month=2, day=16) feb_16_2019 = datetime.date(2019, 2, 16) print(feb_16_2019) #2019-02-16 time_13_48min_5sec = datetime.time(hour=13, minute=48, second=5) time_13_48min_5sec = datetime.time(13, 48, 5) print(time_13_48min_5sec) #13:48:05 timestamp= datetime.datetime(year=2019, month=2, day=16, hour=13, minute=48, second=5) timestamp = datetime.datetime(2019, 2, 16, 13, 48, 5) print (timestamp) #2019-01-02 13:48:05

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.

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