All categories
    PythonIntermediate

    Modules & Imports

    Python modules and imports: import standard library modules, use from imports, create aliases, and work with math and random.

    01Import a module

    main.py
    import math
    print(math.sqrt(16))
    print(math.floor(3.7))
    Output
    4.0
    3

    02From import

    main.py
    from math import pi, sqrt
    print(round(pi, 4))
    print(sqrt(25))
    Output
    3.1416
    5.0

    03Import with alias

    main.py
    import math as m
    print(m.pow(2, 10))
    Output
    1024.0

    04Seeded random values

    main.py
    import random
    random.seed(42)
    print(round(random.random(), 4))
    Output
    0.6394