Basic Python tutorials, python data structures and modules

data structure


In Python, lists are variable, which is the most important feature that distinguishes them from strings and tuples. In a word, lists can be modified, while strings and tuples cannot. Here are the methods of listing in Python:

list.append(x)Adding an element to the end of the list is equivalent to a[len(a):] = [x].
list.extend(L)Expand the list by adding all elements of the specified list, which is equivalent to a[len(a):] = L.
list.insert(i, x)Inserts an element at the specified location. The first parameter is the index of the element to be inserted before it. For example, a.insert(0, x) will be inserted before the whole list, and a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x)Delete the first element with a value of x in the list. If there is no such element, an error will be returned.
list.pop([i])Removes an element from the specified location in the list and returns it. If no index is specified, a.pop() returns the last element. The element is removed from the list. (the square brackets around i in the method indicate that this parameter is optional, rather than requiring you to enter a square bracket. You will often encounter such a mark in the Python library reference manual.)
list.clear()Remove all items from the list, equal to del a [:].
list.index(x)Returns the index of the first element in the list whose value is x. If there is no matching element, an error will be returned.
list.count(x)Returns the number of times x appears in the list.
list.sort()Sort the elements in the list.
list.reverse()Invert the elements in the list.
list.copy()Returns a shallow copy of the list, equal to a [:].


>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))2 1 0
>>> a.insert(2, -1)>>> a.append(333)
>>> a[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)1
>>> a.remove(333)
>>> a[66.25, -1, 333, 1, 1234.5, 333]>
>> a.reverse()
>>> a[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a[-1, 1, 66.25, 333, 333, 1234.5]

Note: methods such as insert, remove, or sort that modify the list do not return a value.

Use list as stack

The list method makes it easy to use the list as a stack. As a specific data structure, the first entry element is released last (LIFO). You can add an element to the top of the stack with the append() method. A pop() method that does not specify an index can release an element from the top of the stack. For example:


>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
>>> stack.pop()
>>> stack
[3, 4]

Use lists as queues

You can also use the list as a queue, just take out the first element added in the queue; However, it is not efficient to use lists for such purposes. Adding or popping elements at the end of the list is fast, but inserting or popping elements from the head of the list is not fast (because all other elements have to move one by one).


>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
>>> queue.popleft()                 # The second to arrive now leaves
>>> queue                           # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])

del statement

Using the Del statement, you can delete an element from a list by index rather than by value. This is different from using pop() to return a value. You can delete a cut from the list with del statement or empty the whole list (the method we introduced earlier is to assign an empty list to the cut). For example:


>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a

You can also delete entity variables with del:

>>> del a

Tuples and Sequences

Tuples consist of several comma separated values, such as:

>>> t = 12345, 54321, 'hello!'
>>> t[0]
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested:
... u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))

As you can see, tuples are always output with parentheses to correctly express nested structures. There may or may not be parentheses when entering, but parentheses are usually required (if the tuple is part of a larger expression).


A collection is an unordered set of non repeating elements. Basic functions include relationship testing and de duplication.

You can create a collection with braces ({}). Note: if you want to create an empty set, you must use set() instead of {}; The latter creates an empty dictionary. We will introduce this data structure in the next section.

Here is a simple demonstration:

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)                      # Delete duplicate
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket                 # Test member
>>> 'crabgrass' in basket

>>> # The following demonstrates the operation of the two sets
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # The only letter in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # The letter in a, but not in b
{'r', 'd', 'b'}
>>> a | b                              # Letters in a or b
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # Letters in both a and b
{'a', 'c'}
>>> a ^ b                              # A letter in a or b, but not in both a and b
{'r', 'd', 'b', 'm', 'z', 'l'}


Another very useful Python built-in data type is a dictionary.

Sequences are indexed by continuous integers. Unlike this, dictionaries are indexed by keywords, which can be of any immutable type, usually string or numeric value.

The best way to understand a dictionary is to think of it as an unordered set of key = > value pairs. Keywords must be different from each other within the same dictionary.

A pair of braces creates an empty dictionary: {}.

This is a simple example of dictionary use:

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
>>> 'jack' not in tel

The constructor dict() builds a dictionary directly from the list of key value pair tuples. If there is a fixed mode, the list derivation specifies a specific key value pair:

>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}

Traversal skill

When traversing the dictionary, the keyword and the corresponding value can be interpreted simultaneously using the items() method:

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
...     print(k, v)
gallahad the pure
robin the brave


In the previous chapters, our script is programmed with Python interpreter. If you exit from the Python interpreter and enter again, all the methods and variables you defined will disappear.

To this end, Python provides a way to store these definitions in a file for some scripts or interactive interpreter instances. This file is called a module.

A module is a file that contains all the functions and variables you define. Its suffix is py. The module can be introduced by other programs to use the functions in the module. This is also the way to use the python standard library.

The following is an example of using modules in the python standard library.

# File name:
import sys
print('The command line parameters are as follows:')
for i in sys.argv:
print('\n\nPython The path is:', sys.path, '\n')

The example results are as follows:

$ python Parameter 1 Parameter 2
 The command line parameters are as follows:
 Parameter 1
 Parameter 2

Python The path is: ['/root', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages'] 
  • 1. import sys introduces sys. In the python standard library Py module; This is the first mock exam.
  • 2,sys.argv is a list of command line arguments.
  • 3,sys.path contains a list of paths where the Python interpreter automatically finds the required modules.

import statement

To use a Python source file, simply execute the import statement in another source file. The syntax is as follows:

import module1[, module2[,... moduleN]

When the interpreter encounters an import statement, if the module is in the current search path, it will be imported.

The search path is a list of all directories that the interpreter will search first. If you want to import the module support, you need to put the command at the top of the script:

# Filename:
def print_func( par ):
    print ("Hello : ", par)
    return import support module

# Filename:
# Import module
import support
# Now you can call the functions contained in the module

Output results of the above examples:

$ python3 
Hello :  Michael

A module can only be imported once, no matter how many times you execute import. This prevents the import module from being executed over and over again.

When we use the import statement, how does the Python interpreter find the corresponding file?

This involves the python search path, which is composed of a series of directory names, and the Python interpreter looks for the introduced modules from these directories in turn.

This looks a lot like environment variables. In fact, you can also determine the search path by defining environment variables.

The search path is determined during Python compilation or installation, and the installation of new libraries should also be modified. The search path is stored in the path variable in the sys module. Do a simple experiment. In the interactive interpreter, enter the following code:

>>> import sys
>>> sys.path
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']

sys. The path output is a list, in which the first item is the empty string '', which represents the current directory (if printed from a script, you can see which directory it is more clearly), that is, the directory where we execute the python interpreter (for a script, it is the directory where the running script is located).

Therefore, if a file with the same name as the module to be imported exists in the current directory like me, the module to be imported will be shielded.

Once you understand the concept of search path, you can modify sys. Net in the script Path to introduce some modules that are not in the search path.

Now, in the current directory of the interpreter or sys Path to create a Fibo Py file, the code is as follows:

# Fibonacci sequence module
def fib(n):    # Fibonacci sequence defined to n
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
def fib2(n): # Return to the Fibonacci sequence of n
    result = []
    a, b = 0, 1
    while b < n:
        a, b = b, a+b
    return result

Then enter the Python interpreter and import this module with the following command:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__

Deep module

In addition to method definitions, modules can also include executable code. These codes are generally used to initialize this module. This code is only executed when it is first imported.

Each module has its own independent symbol table, which is used as a global symbol table for all functions within the module.

Therefore, the author of the module can safely and boldly use these global variables inside the module without worrying about confusing the global variables of other users.

On the other hand, when you really know what you're doing, you can also use modname Use a notation like itemname to access functions within a module.

Modules can be imported into other modules. Use import at the beginning of a module (or script, or elsewhere) to import a module. Of course, this is only a convention, not mandatory. The name of the imported module will be placed in the symbol table of the module of the current operation.

There is also an import method. You can use import to directly import the names in the module (functions and variables) into the current operation module. For example:

>>> from fibo import fib, fib2
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

This import method does not put the name of the imported module in the current character table (so in this example, the name fibo is undefined).

There is another way to import all (function and variable) names in the module into the character table of the current module at one time:

>>> from fibo import *
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

This will import all names, but those are underlined () The first name is not in this example. In most cases, Python programmers do not use this method, because the names of other sources introduced are likely to overwrite the existing definitions.

dir() function

The built-in function dir() can find all the names defined in the module. Return as a list of strings:

>>> import fibo, sys
>>> dir(fibo)
['__name__', 'fib', 'fib2']
>>> dir(sys)  
['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__',
 '__package__', '__stderr__', '__stdin__', '__stdout__',
 '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe',
 '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv',
 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder',
 'call_tracing', 'callstats', 'copyright', 'displayhook',
 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
 'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
 'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount',
 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit',
 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
 'thread_info', 'version', 'version_info', 'warnoptions']

Standard module

Python itself comes with some standard module libraries, which will be introduced in the python library reference document (that is, the "library reference document" later).

Some modules are built directly in the parser. Although these are not built-in functions of some languages, they can be used efficiently, even system level calls.

These components will be configured in different forms according to different operating systems. For example, winreg module will only be provided to Windows system.

It should be noted that there is a special module sys, which is built into every Python parser. Variable sys PS1 and sys PS2 defines the string corresponding to the main prompt and secondary prompt:

>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '


Package is a form of managing Python module namespace, which adopts "point module name".

For example, if the name of a module is A.B, it represents sub module B in package a.

Just as you don't have to worry about the interaction of global variables between different modules when using modules, you don't have to worry about the duplication of module names between different libraries in the form of point module names.

In this way, different authors can provide NumPy module or Python graphics library.

Suppose you want to design a module (or a "package") that processes sound files and data uniformly.

There are many different audio file formats (they are basically distinguished by suffix names, such as:. wav,: file:.aiff,:,), so you need to have a set of increasing modules to convert between different formats.

And for these audio data, there are many different operations (such as mixing, adding echo, adding equalizer function, creating artificial stereo effect), so you also need a set of endless modules to deal with these operations.

Here is a possible package structure (in a hierarchical file system):

sound/                          Top package               initialization sound package
      formats/                  File format conversion sub package
      effects/                  Sound effect sub package
      filters/                  filters subpackage 

When importing a package, Python will import it according to sys Path to find the subdirectories contained in the package.

The directory contains only one called init Py file will be regarded as a package, mainly to avoid some vulgar names (such as string) inadvertently affecting the effective modules in the search path.

In the simplest case, put an empty file: init Py is OK. Of course, this file can also contain some initialization code or as (described later)__ all__ Variable assignment.

Users can import only specific modules in one package at a time, such as:

import sound.effects.echo

This will import the sub module: sound effects. echo. He must use his full name to access:

sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)

Note that when using the form of from package import item, the corresponding item can be a sub module (sub package) in the package or other names defined in the package, such as functions, classes or variables.

The import syntax will first treat item as the name of a package definition. If it is not found, then try to import it according to a module. If it is not found, throw an exception: exc:ImportError.

On the contrary, if you use the shape such as import item subitem. The import form of subitem, except the last item, must be a package, and the last item can be a module or package, but can not be the name of a class, function or variable.

Tags: Python

Posted by mouli on Sat, 16 Apr 2022 10:15:54 +0930