Standard library highlights

Computer Science · Python

Question 1 of 10

0

    The module for creating temporary files and directories

    Capital letters and accents are ignored.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Standard library highlights explained

    The Python standard library is the set of modules that comes with every Python installation, so many common tasks need nothing extra. Useful examples include math, random, datetime, json, os and collections.

    math provides functions such as sqrt and the constant pi. random produces pseudo-random numbers, for example random.randint(1, 6) for a die roll, but it is not suitable for security secrets, for which the secrets module is used. datetime handles dates and times, and json converts between Python objects and JSON text. os and pathlib work with files and folders. collections provides Counter, which counts items, and defaultdict, which supplies a default value for missing keys. Check the official documentation before relying on a function, since each module has many of them.

    from collections import Counter
    import json
    
    words = ["cat", "dog", "cat", "bird", "cat"]
    counts = Counter(words)
    print(json.dumps(counts))
    

    Worked example

    Trace the code above.

    1. Counter counts each distinct word: cat appears 3 times, dog once and bird once.
    2. json.dumps turns the counts into JSON text, with the words as string keys.
    3. print displays {"cat": 3, "dog": 1, "bird": 1}.

    What you will practise

    96 flashcards. Type your answer and get instant feedback.

    Example questions

    • The module for random numbers
    • The module for dates and times
    • The module for OS interactions
    • The module for JSON
    • The module for regular expressions
    • The module for mathematical functions
    • The module for system-specific parameters and functions
    • The module for writing and reading CSV files

    …and 88 more.