module 1

Cards (91)

  • A code which is not able to respond to users' needs will be forgotten quickly, and instantly replaced with a new, better, and more flexible code.
  • Each of these parts can be (most likely) divided into smaller ones, and so on. Such a process is often called decomposition.
  • module
    a file containing Python definitions and statements, which can be later imported and used when necessary.
  • The handling of modules consists of two different issues:
    • the first (probably the most common) happens when you want to use an already existing module, written by someone else, or created by yourself during your work on some complex project - in this case you are the module's user;
    • the second occurs when you want to create a brand new module, either for your own use, or to make other programmers' lives easier - you are the module's supplier.
  • a module is identified by its name. If you want to use any module, you need to know the name. A (rather large) number of modules is delivered together with Python itself.
  • Python standard library 
    a special sort of library where modules play the roles of books (we can even say that folders play the roles of shelves). If you want to take a look at the full list of all "volumes" collected in that library, you can find it here: https://docs.python.org/3/library/index.html.
  • Each module consists of entities (like a book consists of chapters). These entities can be functions, variables, constants, classes, and objects.
  • To make a module usable, you must import it (think of it like of taking a book off the shelf). Importing a module is done by an instruction named import. Note: import is also a keyword (with all the consequences of this fact). It also lets you import modules into a Python script
  • The simplest way to import a particular module is to use the import instruction as follows:
    • the import keyword;
    • the name of the module which is subject to import.
  • The import instruction may be located anywhere in your code, but it must be placed before the first use of any of the module's entities.
  • If you want to (or have to) import more than one module, you can do it by repeating the import clause (preferred) or by listing the modules after the import keyword
  • namespace
    is a space (understood in a non-physical context) in which some names exist and the names don't conflict with each other (i.e., there are not two different objects of the same name). 
  • Inside a certain namespace, each name must remain unique. This may mean that some names may disappear when any other entity of an already known name enters the namespace.
  • If the module of a specified name exists and is accessible(a module is in fact a Python source file), Python imports its contents, i.e., all the names defined in the module become known, but they don't enter your code's namespace.
  • import math
    print(math.sin(math.pi/2))
  • It's simple, you put:
    • the name of the module (e.g., math)
    • a dot (i.e., .)
    • the name of the entity (e.g., pi)
    Such a form clearly indicates the namespace in which the name exists.
  • In the second method, the import's syntax precisely points out which module's entity (or entities) are acceptable in the code:
    from math import pi
    • the listed entities (and only those ones) are imported from the indicated module;
    • the names of the imported entities are accessible without qualification.
    Note: no other entities are imported. Moreover, you cannot import additional entities using a qualification
  • As you can see, the name of an entity (or the list of entities' names) is replaced with a single asterisk (*).
    Such an instruction imports all entities from the indicated module.
    Is it convenient? Yes, it is, as it relieves you of the duty of enumerating all the names you need.
    Is it unsafe? Yes, it is - unless you know all the names provided by the module, you may not be able to avoid name conflicts. Treat this as a temporary solution, and try not to use it in regular code.
  • Aliasing causes the module to be identified under a different name than the original. This may shorten the qualified names, too.
  • If you need to change the word math, you can introduce your own name, just like in the example:
    import math as m
    print(m.sin(m.pi/2))
    Note: after successful execution of an aliased import, the original module name becomes inaccessible and must not be used.
  • If you want to import a module as a whole, you can do it using the import module_name statement. You are allowed to import more than one module at once using a comma-separated list.
    import mod1
    import mod2, mod3, mod4
  • You are allowed not only to import a module as a whole, but to import only individual entities from it. In this case, the imported entities must not be prefixed when used.
  • dir()
    A function that reveals all the names provided through a particular module
  • import math

    for name in dir(math):
    print(name, end="∖t")


    The module has to have been previously imported as a whole (i.e., using the import module instruction - from module is not enough)
  • dir() has nothing to do with the dir command you know from Windows and Unix consoles, as dir() doesn't show the contents of a disk directory/folder
  • dir() does something really similar to the dir command from Windows and Unix consoles
  • random module
    It delivers some mechanisms allowing you to operate with pseudorandom numbers.
  • A random number generator takes a value called a seed, treats it as an input value, calculates a "random" number based on it (the method depends on a chosen algorithm) and produces a new seed value.
  • The initial seed value, set during the program start, determines the order in which the generated values will appear
  • The most general function named random() (not to be confused with the module's name) produces a float number x coming from the range (0.0, 1.0) - in other words: (0.0 <= x < 1.0).
  • The seed() function is able to directly set the generator's seed. We'll show you two of its variants:
    • seed() - sets the seed with the current time;
    • seed(int_value) - sets the seed with the integer value int_value.
    • randrange(end)
    • randrange(beg, end)
    • randrange(beg, end, step)
    • randint(left, right)
  • The first three invocations will generate an integer taken (pseudorandomly) from the range (respectively):
    • range(end)
    • range(beg, end)
    • range(beg, end, step)
  • from random import randint
    for i in range(10)
    print(randint(1,10), end=',')
  • As you can see, this is not a good tool for generating numbers in a lottery. Fortunately, there is a better solution than writing your own code to check the uniqueness of the "drawn" numbers.
    It's a function named in a very suggestive way - choice:
    • choice(sequence)
    • sample(sequence, elements_to_choose)
  • Layers of the software stack
    1. Your (running) code
    2. Python (runtime environment)
    3. Operating system
    4. Hardware
  • Your (running) code
    • Located at the top of the software stack
  • Python (runtime environment)

    • Lies directly below the running code
  • Operating system
    • Provides services that Python uses to process files or communicate with physical devices
    • Python's environment provides some of its functionalities using the operating system's services
  • Hardware
    • The bottom-most layer of the software stack
    • Includes the processor(s), network interfaces, human interface devices (mice, keyboards, etc.)
    • The operating system knows how to drive the hardware and uses tricks to conduct all parts in a consistent rhythm