1) list and tuple are Python built-in ordered sets, one variable, one immutable. Choose to use them as needed.
.) List is an ordered set of elements that can be added and removed at any time
List:classmates = [' Michael ', ' Bob ', ' Tracy ']
Specific operation:
.) You can append elements to the list to the end: Classmates.append (' Adam ')
.) Inserts an element into the specified position, such as the index number 1: Classmates.insert (1, ' Jack ')
.) To delete the element at the end of the list, use the Pop () method: Classmates.pop ()
.) To delete the element at the specified position, use the pop (i) method
.) To replace an element with another element, you can assign a value directly to the corresponding index position
Tuple:classmates = (' Michael ', ' Bob ', ' Tracy ')
.) Tuple and list are very similar, but a tuple cannot be modified once initialized
.) Only 1 elements of a tuple definition must be added with a comma, to disambiguate: T = (1,)
2) Dict and set
Python built-in dictionary: dict support, Dict full name dictionary, also known as map in other languages, using key-value (Key-value) storage
.) D = {' Michael ': Up, ' Bob ': +, ' Tracy ': 85}
.) To delete a key, use the Pop (key) method, and the corresponding value will be removed from the dict.
.) Add a key-value, directly assign the value, classnates["AAA"]=34
.) If you take a non-existent key will have an exception, to avoid the key does not exist in the error, there are two ways, one is through
In to determine if key exists ' Thomas ' in D
.) s = Set ([1, 2, 3])
Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, in set, there is no heavy
Complex key. To create a set, you need to provide a list as the input collection:
Add (Key)
Remove (key)
------Loop:
。 ) for X in ...
。 Python provides a range () function that generates a sequence of integers, such as a range (5) that generates a sequence that starts at 0 and is less than
An integer of 5
. ) while loop, as long as the condition is met, keeps circulating
3) How to import modules in Python
#导入modules, the difference between import and from...import is that it simply says:
# If you want to represent SYS.ARGV in the program with ARGV,
# You can use: from sys import argv
# Generally, you should avoid using from. Import and use the import statement,
# because it will make your program easier to read and avoid conflicting names
4) Adorner:
Now, let's say we're going to enhance the function of the present () function, for example, to automatically print the log before and after a function call, but you don't want to modify the definition of the now () function.
This way of dynamically adding functionality during code runs, called "adorners" (Decorator)
5) There are two special cases in the function definition of Python, that is, the form of *,** appears.
such as: Def myfun1 (username, *keys) or Def myfun2 (username, **keys), etc.
Explain:
* Used to pass any of the parameterless parameters, which are accessed in the form of a tuple.
* * Used to process the passing of any parameter with a name, these parameters are accessed using Dict.
Example:
def fun1 (*keys):
Print type (keys)
Print "keys=" +str (keys)
For I in range (0,len (keys)):
Print I,keys[i]
FUN1 (2,3,4,5)
6) Python allows you to precede a list or tuple with an * number, and change the elements of a list or tuple into variable parameters.
7) Class Access restrictions
.) Within class, you can have properties and methods, and external code can manipulate the data by invoking the instance variable directly, which
, it hides the complex logic inside.
.) If you want the internal properties to be inaccessible externally, you can add two underscores to the name of the property, in Python, the instance's
Variable name if it starts with __, it becomes a private variable (private), which can only be accessed internally and cannot be accessed externally.
.) If external and internal private variables are to be manipulated, the corresponding method get_,set_ method can be declared;
.) It is important to note that in Python, the variable name is similar to __xxx__, which starts with a double underscore and is double-underlined
At the end of a particular variable, a special variable is directly accessible, not a private variable,
Therefore, variable names such as __name__ and __score__ cannot be used.
.) Sometimes you'll see an instance variable name that starts with an underscore, such as _name, where an instance variable can be
Accessed, however, by the customary rules, when you see such variables, meaning, "although I can be interviewed
Ask, but please treat me as a private variable and don't feel free to access "
.) is an instance variable that starts with a double underscore not necessarily externally accessible? Actually, it's not. No direct access to __name is due to
The __name variable is changed to _student__name for the Python interpreter, so it can still be
_student__name to access the __name variable. (Note: Student is the corresponding L-class name).
However, it is highly recommended that you do not do this because different versions of the Python interpreter may change the __name to a different variable name.
In general, Python itself has no mechanism to prevent you from doing bad things, all by self-awareness.
8) How to determine the type of a variable, how to get all the methods and properties of an object:
.) The base type can be judged by the type (): Type (123),
.) The use of type () is inconvenient for class inheritance relationships. We want to determine the type of class that can be used
The Isinstance () function. isinstance (h, dog), such as judging H is not an instance of Dog
.) Basic types that can be judged by type () can also be judged by isinstance (), such as: isinstance (' a ', str)
。 If you want to get all the properties and methods of an object, you can use the Dir () function, which returns a string containing the
List
。 It is not enough to simply list properties and methods, with GetAttr (), SetAttr (), and hasattr (), we can directly
The state of an object is then manipulated
9) How to refer to the classes in the Python module that you wrote;
In two cases:
。 If the corresponding py file is in the same directory, the direct import can be used, as follows:
From A import Animal
A = Animal ()
A.run ()
This introduces the animal class in a.py (also called a module).
。 If the imported module a.py is not in the same directory as the py file you are calling, running the above code directly will cause an error;
You need to create a file under the Lib/site-packages directory in the Python installation directory mymodule.,pth
The file content is the directory where your a.py files are located, such as:
a.py file in D:\, then write d:\ in this file,
When the Python compiler executes the code, it will load the files in the D:\ directory into memory for compiling;
。 Reference: HTTP://BLOG.CSDN.NET/HYHOLINE/ARTICLE/DETAILS/8581740#T3
How your own modules are managed through the package:
。 Package) can be seen as a collection of modules, as long as there is a __init__.py file under a folder, then this file
A clip can be seen as a package.
The folder under the package can also be a package (child package); Further, multiple smaller packages can be aggregated into a larger package,
Through the package of this structure, convenient for the management and maintenance of the class, but also facilitate the user's use
The role of PYc and pyo in Python
.) The PYc file, which is a python-compiled bytecode (bytecode) file. As soon as you run the py file, the Python compiler will automatically generate a corresponding PYC bytecode file. This PYC bytecode file, which passes through the Python interpreter, generates machine code to run (which is why PYC files can be deployed across platforms, Java-like cross-platform, bytes run by JVM in Java
code files). The next call calls PYc directly, without invoking the py file. Until you have changed this py file. Python explanation
Check the build time in the PYc file, compare the modification time of the Py file, and if the PY is updated, a new PYC is generated.
.) PYO file, which is a python compiled optimized bytecode file
This article from "Small Li Guangzhi Blog" blog, declined reprint!
Python study notes