- Two modes of operation for Python:
- command-line mode, run Python, and then enter the python command on the command line
- Program script, enter the./hello.py run on the command line
- Python is an explanatory form language, but it can be packaged as a binary executable file by tool
- The comment is #, the multiline comment begins with ' ', ends with '
- Variables do not need to be declared type, can be deduced automatically, type () function can get variable types
- A sequence is a set of elements that are ordered, each element type can be different, and the sequence is divided into two
- Tuple (tuple): elements in a tuple cannot be changed, defined with (), or omitted (), a string is a special tuple
- List: Individual elements can be changed, defined with []
- A sequence can be an element of another sequence, a sequence element can be accessed with [], or a range reference, and the expression is [lower: Upper limit: Step]
- Function-defined keyword: def, return returns multiple values, such as return a,b,c, equivalent to return (A,B,C)
- Package parameter passing: When defining a function, add * or * * before the corresponding tuple or dictionary, such as
- func (*arr): arr is a tuple, called when Func (1,2,3,4)
- func (**dic): DIC is a dictionary that is called when Func (a=1,b=2,c=3)
- solution package: The function definition does not use the package parameters, the call is passed in the package parameters, the function will be automatically disassembled, note that when called, the parameter value to add * or * *, corresponding to the tuple and dictionary
-
- Python also uses class to define classes, and the first argument of a class must be self, which refers to the object itself, similar to this
- Inherited syntax: Class Subclass (ParentClass), ParentClass is the parent class, subclass is a subclass
- __init__ () is a special method that Python automatically calls this method when creating an object, which is equivalent to the initialization process. such as: Human =new Human ("male"); Parameter mail is passed to the __init__ () method
- Two important built-in functions:
- Dir () queries all properties of a class or object
- Help () for querying the description document
- Dictionary type definition Dic={key1:value1,key2,value2},key can make strings, numbers, bool type, etc., immutable objects can do keys, the dictionary loop as follows, Note the key value of the loop
For key in DIC:
Print (Dic[key])
- Common functions of the Dictionary: keys (), values (), items (), clear (), and a common usage is del dic[' Tom '), deleting the key is Tom's element, Del is a keyword in python, not a function, for deleting an object
- Module: A. py file is a module that uses the Import keyword to introduce other modules, using modules. Objects to access the objects in the ingest module
- Some uses of import:
- Import a as B: Introduce module A and rename it to B
- From a import func1: The Func1 object is introduced from module A, which can then be used directly without the use of the func1 a.func1
- From a import *: All objects are introduced from module a so that you can use the objects in a directly without using the A. Object
- Python will search for the module it wants in the following path:
- The folder where the program resides
- Installation path of the standard library
- The path that the operating system environment variable Pythonpath contains
- Module packages: Modules with similar functionality placed in the same folder, such as Dir, constitute a module package, which must contain a __int__.py file (which can be empty) to inform Python that the folder is a module package, through:
Import dir.module referencing the module in the Dir folder
- Functions for looping:
- Range
- Enumerate (): can get subscript and element at the same time in each loop, for (Index,value) in enumerate (arr)
- Zip (): Used to loop through multiple equal-length sequences, each of which takes one element from each sequence, for (a,b,c) in Zip (ARR1,ARR2,ARR3). The function of zip is to take an element from each sequence sequentially, to synthesize a tuple
- Loop object: Contains a __next__ method that is intended to loop the next result until the last Stopiteration error is thrown
- Generator (Generator): Build a user-defined loop object, write a method similar to a function, just return to yield, you can have multiple yield,generator when the yield is encountered will pause the return of yield after the value, When the generator is called again, it continues to run from where it was paused, returning the next yield value. Generator Example:
g= (x for X in range (4)), G is a generator that accesses values in the __next__ () method
- Table derivation (table comprehension): A quick way to generate a table (list), example:
L= (x**2 for X in range (10)), which is similar to the builder expression, except that the brackets are used
- Lambda: Example Func=lambda x,y:x+y call is like a normal function, func (3,5)
- Common functions:
- Map (Function object, List ... ): The function is to function objects in order to each element of the list, the result of each action is stored in the returned loop object, if the function object has more than one parameter, then you can have more than one list,map function each time from all the list to take a value, as the parameters of the function
- Filter (Function object, List ...): The function is to function object to more than one element, if the function object returns True, the element that returns that time is stored in the Loop object
- Reduce (Function object, list): The function object can only accept two parameters, it is possible to take two values from the list in a progressive parameter. Need to introduce Functools package in 3.x
- The exception syntax is as follows and throws the style itself using the Raise keyword
Try: ... exceptException1: ...except exception2: ... except : ... Else: ... finally: ...
If there is no exception, the Else statement is executed, and if there is no corresponding exception type, an exception is thrown to the upper layer
- Context Manager: Used to specify an object's scope of use, syntax: With ...., any object that defines the __enter__ () and __exit__ () methods can be used in the context manager
With Open ( "new.txt" , "w" ) as F: Print (f.closed) f.write ( "Hello world! ") Print(f.closed)
- The properties of the object are stored in the object's __dict__ property , with the property name key and the property value
- Closures: A closure in Python is a function object containing the value of an environment variable, and the value of the environment variable is stored in the __closure__ property of the function object.
- Adorner: The processing of a function, method, or class, @decorator by a decoration function or class ,The square_sum is actually passed to decorator, and the new callable object returned by decorator is assigned to the original function name
Def
defNew_f (A, B):
Print( "input" , A, b)
returnF (A, B)
returnNew_fdef return a**2 + b**2
- formatted string: Python uses a string as a template with format characters such as print ("I am%s,i am%d years old"% (' Tom ', 10)), and the template is separated from the tuple with a% number, It represents a formatting operation that can be further controlled in the following ways:
%[(name)][flags][width]. [Precision]typecode
Print( "I ' m% (name) s. I ' m% (age) D-old" % { 'name' : 'vamei' , 'age' : 99}) (Use a dictionary to pass real values)
Python basic syntax (based on 3.0)