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

    Programador pensando enquanto analisa código em dois monitores
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Learn Programming from Scratch: Beginner Guide

    Learn programming from scratch with a practical roadmap covering logic, languages, tools, exercises, projects, debugging, Git, and consistent study habits.

    Ler mais

    Tempo de leitura: 6 minutos
    10/07/2026
    ícone de loop com o texto 'For' abaixo
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python for Loops: Complete Beginner Guide

    Learn Python for loops with sequences, range, enumerate, zip, dictionaries, nested loops, break, continue, comprehensions, and practical examples.

    Ler mais

    Tempo de leitura: 4 minutos
    10/07/2026
    Manipulação e formatação de strings em Python
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python Strings: Complete Beginner Guide

    Learn Python strings with creation, indexing, slicing, methods, formatting, Unicode, searching, validation, splitting, joining, and practical examples.

    Ler mais

    Tempo de leitura: 4 minutos
    10/07/2026
    código Python de uma tupla com o texto separado formando a frase "O que são tuplas?"
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python Tuples: Complete Beginner Guide

    Learn Python tuples with creation, packing, unpacking, indexing, immutability, methods, named records, function returns, and practical examples.

    Ler mais

    Tempo de leitura: 5 minutos
    10/07/2026
    símbolo de PI
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python Floats: Complete Beginner Guide

    Learn Python floats with decimal values, arithmetic, conversion, rounding, precision limits, comparisons, Decimal, math functions, and practical examples.

    Ler mais

    Tempo de leitura: 4 minutos
    10/07/2026
    notebook mostrando tela a teclado
    Fundamentals
    Foto de perfil de Leandro Hirt da Academify

    Python Input and Output: Complete Guide

    Learn Python input and output with input(), print(), conversions, validation, formatting, files, command-line arguments, and practical interactive programs.

    Ler mais

    Tempo de leitura: 4 minutos
    10/07/2026