Dictionaries
Objects that can store multiple elements become containers, and the list and tuple mentioned above belong to the container class, which introduces a new container: a dictionary.
Basic concepts
Common basic methods for creating dictionaries:
dic={' key ': value, ' key ': value, ' key ': Value ...}
The dictionary contains two parts, a key and a value, where the key is usually a string, or it can be a number or a truth (immutable object can be used as a key). The value can be any object, and the key and value one by one correspond.
The dictionary has a special place where the elements in the table are unordered, and therefore cannot be accessed by subscript. Dictionaries are referenced by keys.
To build a new empty dictionary:
DIC = {}
method to add a new element to the dictionary:
Circular invocation of dictionary elements
You can see the last addition of a "GCG" but it is not in the last one, that is, in the dictionary there is no order.
Common methods of dictionaries
Delete one of the pairs:
(Note:del is a reserved keyword in python for deleting objects.)
"High.__delitem__ (Key)" equivalent to "Del high[key")
The total number of elements in the query dictionary:
input and output of text file
Python has basic text file read and write capabilities. Python's standard library provides richer read and write capabilities. The reading and writing of a text file is implemented primarily through the file objects that are built by open ().
Create a File object
We open a file and use an object to represent the file:
Object name = open (filename, mode)
The most common patterns are: R opens a read-only file, which must exist. r+ open a file that can be read and write, the file must exist. W Open Write-only file, if the file exists, the file length is 0, that is, the file content will disappear. If the file does not exist, the file is created. w+ open a writable file, if the file exists, the file length is zero, that is, the file content will disappear. If the file does not exist, the file is created. A opens the write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. A + opens the writable file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. The above morphological strings can be added to a B-character, such as RB, W+b or ab+, add a B-character to tell the function library to open the file as a binary file, not a plain text file.
Method of File Object
Read
If there are already two lines of data in the file:
Write
Since the content is a multiline read, Python stores each line as an element in a list, so we can write the two read lines again after the file.
Results:
Module
Introducing Modules
We saw the functions and objects before. In essence, they are all for the better organization of existing procedures to facilitate reuse. Modules (module) are also intended for the same purpose. In Python, a. py file forms a module. Through modules, you can invoke programs in other files.
We first create a move.py file, place it in the same directory as the main.py file, and then import the move.py file in main.py.
Then import in main.py:
After the module is introduced, we can invoke the object in the module, including functions and global variables, by means of the module.
Python also has a few other useful ways to import:
Rename Module Name:
Specifies that only specific objects in the module are imported and can be accessed directly, as is the case with global objects in main.py:
(Because the function move is only imported, the content is not accessible.) )
Imports all objects from a module and can be accessed directly as a global variable:
Search Path
The above move.py file can be found by the main.py file because move.py files and main.py files are in the same directory, so main.py can still find the imported modules under which path. The folder operating system environment variable Pythonpath contains the path standard library installation path (Windows system is the Site-packages directory in the Lib folder in the Python installation directory, the Linux system is in the/usr/local/ In Lib/python2.7/dist-packages)
Module Pack
The module package, as the name implies, is a collection of modules, which is a folder where we put the move.py file above in a folder called Module_bag, which is in the same directory as mian.py:
Note that a __init__.py file must be included in the folder Module_bag, reminding Python that the folder is a module package and__init__.py can be an empty file .
parameter passing of a function
In general, we pass arguments to a function by the position of the parameter in the function, for example:
At this time the actual participation in the ABC one by one corresponds.
Key word Delivery
For easy viewing, we write a function:
The above is passed using the default location-appropriate method.
Sometimes feel too rigid according to the position, so want to according to their own convenience, according to the parameter name to pass parameters to each parameter:
Keyword passing can be mixed with position passing. But the positional arguments are preceded by the keyword parameter:
Parameter Default value
When you define a function, you can assign a default value to the parameter using the form a=19. If the parameter is not eventually passed the value, the default value is used:
Take a look at the following example:
Note: It is not output 2 4 10, because if a parameter has a default value, subsequent arguments must also have a default value, otherwise there will be an error.
Parcel Pass
When defining a function, we sometimes do not know how many arguments are passed when invoked. At this point, the parcel (packing) positional parameters, or the package keyword parameters, are useful for parameter passing.
Let's look at an example:
You can see that this parameter is a collection of parameters, all of which are passed to parameters. Two calls, although the number of parameters are different, are based on the same Func definition. In the Func parameter table, all the parameters are collected by parameters and merged into a tuple (tuple) according to the position, which is the parcel position transfer.
In fact, in front of the parameters does not add * also can achieve this function:
As you can see, the front *parameters is that Python automatically converts some of the column parameters into a tuple and assigns a parameters, and the latter approach is that we programmers think of manually passing a tuple to parameter when the argument is passed.
Pass a dictionary through a parcel:
Notice that a B C in the argument is not quoted.
Solution Package
* and * *, can also be used at the time of the call, that is, the solution package (unpacking), the following example:
Accordingly, there is also the solution package for the dictionary,
Dict = {' A ': 1, ' B ': 2, ' C ': 3}
func (**dict)
Mixed
When you define or invoke a parameter, several ways in which the parameters are passed can be mixed. But be careful in the process before and after the order. The basic principle is: first position, then keyword, and then parcel position, and then parcel the keyword, and according to the principle of the above said to carefully distinguish
Resources:
Https://www.shiyanlou.com/courses/214/labs/650/document
NOTE: Reprint please indicate the original source:
Author: CUG_UESTC
Source: http://blog.csdn.net/qq_31192383/article/details/53729331