Python time Module for Beginners

Published on: June 3, 2026
Reading time: 3 minutes
Uso do módulo time para controlar tempo em scripts Python

Time is one of the most critical resources in any application. Whether you need to measure a function’s performance, create strategic pauses in a script, or record the exact moment a transaction occurs, knowing how to work with time is essential. The time module is part of Python’s standard library, meaning no external installation is needed to get started.

The module focuses on system time and elapsed seconds. Unlike datetime (which handles calendar-aware date objects), time is lower-level and interacts directly with the operating system clock. This makes it ideal for performance measurement and automation tasks. To measure code speed in detail, also see measuring Python code speed with timeit.

Unix timestamp: time.time()

The fundamental concept in the time module is the Unix Timestamp: the number of seconds elapsed since January 1, 1970 at 00:00:00 UTC (known as “The Epoch”). Computers work more easily with a single decimal float than with months of varying lengths or leap years.

Python
import time

now = time.time()
print(f"Current timestamp: {now}")

Pausing execution: time.sleep()

time.sleep(seconds) suspends execution for the specified number of seconds. It accepts floats for sub-second precision (e.g. time.sleep(0.5) for 500ms). Useful for rate limiting API calls, creating countdowns, and spacing automated actions:

Python
import time

Reading local time: localtime()

Python
import time

Formatting dates: strftime()

strftime() converts a time struct into a formatted string. For a practical project using calendars, see generating a monthly calendar in Python.

Python
import time

Measuring execution time

Python
import time

strftime format codes reference

CodeMeaningExample
%Y4-digit year2026
%mMonth (zero-padded)06
%dDay (zero-padded)03
%HHour (24h)14
%MMinute32
%SSecond01

The full list of format codes is available in the official Python time documentation. For higher-precision benchmarking (with statistical analysis), use timeit instead of raw time.time() differences.

Frequently asked questions

What is the difference between time and datetime?

time deals with seconds and system-level time functions. datetime is higher-level and works with human-readable date/time objects including timezone support, arithmetic, and parsing. For most applications where you need to work with calendar dates, datetime is more convenient.

Can time.sleep() be interrupted?

Yes, by a signal (like KeyboardInterrupt with Ctrl+C). On some systems, sleep may return early if a signal is received. If you need guaranteed timing, check the actual elapsed time after sleep.

The time module is the foundation for any script that needs to interact with the system clock, create delays, or measure performance. Start with time.sleep() and time.time(), which cover 90% of everyday use cases.

Share:

Facebook
WhatsApp
Twitter
LinkedIn

Article content

    Related articles

    Uso da função zip para combinar listas em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python zip() Function: Beginner’s Guide

    Learn how Python zip() works: combine iterables, loop over multiple lists, handle different lengths, unzip with *, and use zip_longest

    Ler mais

    Tempo de leitura: 2 minutos
    03/06/2026
    Uso do módulo collections para estruturas avançadas em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python collections Module: namedtuple to deque

    Learn Python's collections module: namedtuple, Counter, defaultdict, and deque for better performance and readability than built-in lists and dicts.

    Ler mais

    Tempo de leitura: 4 minutos
    03/06/2026
    Criação de geradores eficientes usando yield em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python yield: Create Efficient Generators

    Learn how Python yield works to create memory-efficient generators, process large files, build infinite sequences, and use generator pipelines.

    Ler mais

    Tempo de leitura: 6 minutos
    03/06/2026
    Uso do operador walrus para atribuições inline em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python Walrus Operator (:=) Explained with Examples

    Learn how Python's walrus operator (:=) works for assignment expressions inside if, while, and list comprehensions, with practical examples and

    Ler mais

    Tempo de leitura: 3 minutos
    03/06/2026
    Geração de números aleatórios seguros usando secrets em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Generate Secure Random Numbers in Python with secrets

    Learn how Python's secrets module generates cryptographically secure random numbers, tokens, and passwords using randbelow, token_hex, token_urlsafe, and compare_digest.

    Ler mais

    Tempo de leitura: 4 minutos
    01/06/2026
    Pattern matching com match case em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python match-case: Pattern Matching Explained

    Learn how Python match-case works for structural pattern matching, including values, sequences, dictionaries, guards, and dataclasses with practical examples.

    Ler mais

    Tempo de leitura: 4 minutos
    30/05/2026