Python The language is spoken by the Dutch Guido Van Rossum On 1989 The invention of ,1991 The first public release was released in ;Python It is the mainstream language in the field of machine learning ;Python2.x It is already a legacy ,Python3.x Is the present and future of this language . Is a high-level general-purpose scripting language Compiler language , Such as C、C++、C# etc. , The source code must be converted by the compiler into the machine object code , Run the executable directly , No more source code and compilers . Explanatory language , Such as Python、MATLAB、Java Script etc. , The interpreter interprets and executes the source program line by line , That is, the runtime needs source code and interpreter .
Python The interpreter executes for the first time py Source program error , Will convert the source program into bytecode , The execution speed of bytecode is much faster than that of source code , And it's not about the platform . When the source program is executed again , The interpreter will automatically load the bytecode and interpret the bytecode . The interpreter will automatically check the timestamp of the source program and bytecode , If you find inconsistencies , Will convert the source program to bytecode again .
Python There is no independent compilation system , Just with some compilation features , The bytecode is still interpreted and executed item by item .
Implement the same function ,Python The number of lines of code in this language is only equivalent to that in other languages 1/10~1/5.
It can run without modification in any computer environment where the interpreter is installed
Is often referred to as “ Glue language ”
Excellent scalability , Can be integrated C、C++、Java And other languages , Through interfaces and function libraries, etc “ Stick it together ”. One common application is , First use Python Quickly prototype the program ( Even the final interface of the program ), Then rewrite the parts with special requirements in more appropriate languages . For example, the parts requiring high operating speed , available c/c++ Rewrite and encapsulate into Python Extension class libraries that can be invoked .
For advanced programmers ,Python The open source interpreter and function library of the language have a strong attraction , More importantly ,Python The open source software concept advocated by the language has laid a solid mass foundation for the development of the language .
It can be used to write applications in various fields .
Python Language by forced indentation ( Like the first line of a paragraph in an article ) To reflect the logical relationship between sentences , The readability and maintainability of the program are significantly improved
Even though Python3.x The interpreter is implemented in an object-oriented manner , but Python The syntax level supports both process oriented and object-oriented programming .
Has formed a thriving Python Machine learning ecosystem .
Single-line comments :#
Multiline comment : Triple quotes ( Single or double quotation marks )
Tends to identify the end of a statement by wrapping
Semicolons are also supported as statement end identifiers . If you write multiple statements on one line , You need to add a semicolon to distinguish these statements .
1. The... Of the identifier 1 Characters must be letters or underscores
2. Other characters can be composed of letters 、 Underline 、 Or numbers make up , Chinese characters are not recommended
3. The identifier length is arbitrary test_data,test_data_1,y,, correct ; 1test,1_test error
4. Identifiers are case sensitive
5. Identifier cannot be associated with Python Duplicate key name .(help(“keywords”))
6. Generally, you don't start with an underscore , Because the system stipulates ,_xxx Represents the protected variable name in the class ,__xxx Represents the private variable name in the class ,__xxx__ System defined identifier .
(1) Integers (int): Don't worry about overflowing due to insufficient digits
(2) Floating point numbers (float):3.14,-0.28,1.392E9, 1.392e9
(3) Boolean value (bool):True,False
(4) The plural (complex):1+2i,1+2j
Use pairs of single quotation marks 、 Double quotation marks form a single line string ,
Use pairs of triple quotes ( Single or double quotation marks Number ) Cover up , Form a multiline string .
Numbers 、 character string 、 Boolean value 、 Null value, etc , for example 2,-10086,3.5, “Python” ,True、False,None
Python There are no named constants in , A constant cannot be given a name
Python You don't need to declare , The data type is determined by the assigned value
Different types of numerical data operations , It will automatically perform type conversion :
bool<int<float<complex
Automatic type conversion , Only exists between numeric data
for example :
int、float、complex Is the class name. ,a2 It is actually an instantiated object of an integer class ,
d It's actually accessing complex objects c Of imag Attribute or data member
Continuous assignment :a=b=c=1
Multiple assignments :(x,y,z)=(1,2,“Python”),x,y,z=1,2,“Python”
An operator : Operate on the binary bits of an integer
(1) Tuples 、 list 、 The string is a sequence type .
The elements have order , repeatable , Through square brackets and subscripts ( Indexes ) Access elements . The element index of the sequence type adopts the forward increasing or reverse decreasing sequence number . Common operators and functions are 12 individual , Commonly used :①sn or ns, The sequence of s Copy n Time ;②s[i], A single index returns sequence number i Elements ;③s[i:j], section , Back to page i To j-1 Elements ;④s[i:j:k], Step slicing , Back to page i To j-1 The first element is k For stepping ;⑤len(s)、min(s)、max(s)
(2) The dictionary is of mapping type , Each element is a key value pair , Elements in no order , Can't repeat , The corresponding values are accessed through square brackets and keys . key key Numbers or strings are often used , value value It can be any type .
Tuples (tuple [ˈtʌpl] ) Is a read-only sequence type , Element types can be different , Tuple elements cannot be reassigned after initialization . Use commas and parentheses ( Parentheses can be omitted without confusing semantics ) Express . In expressing fixed data items 、 Function returns multiple values 、 Multivariable assignment 、 Loop traversal is very useful .
list (list) Is the sequence type , Element types can be different , Variable length and content .
keyword if、else、elif; Each condition determines ( Brackets can be omitted ) Ends with a colon ; Indent spaces to distinguish code blocks .
Extract the elements from the iteratable object one by one, put them in the loop variable, and execute the loop body
for < Loop variable > in < Iteratable object >:
< The loop body >
range Function to generate an integer range :range(3) return 02;range(1,4) return 13;range(0,6,2) return 0,2,4;range(6,0,-2) return 6,4,2.
range Objects are lazy objects , Can't show directly . Built in functions zip Expandable inert objects , You can also pair the elements of multiple list objects into meta groups .
while < Conditions >:
< The loop body >
The loop structure has two reserved words break and continue,break Used to jump out of its place for or while loop ,continue Used to end the current cycle .
The parameter can be zero 、1 One or more , Keep parentheses when there are no parameters , Multiple parameters are separated by commas ; The order of formal parameters is : General parameters ( Only formal parameter names )、 Optional parameters ( The name of the parameter = The default value )、 Variable parameters (* The name of the parameter ).
The return value can be zero 、1 One or more , Multiple return values are separated by commas and returned as tuples , When there is no return value, there can be no return sentence .
When a function is called , General parameter must be given an actual parameter value ; By default, arguments are passed in the order of formal parameters , And variable parameters can only be passed by position ; To enhance readability , You can also pass arguments by parameter name , The order of parameters can be arbitrary . If mixed , Then the person passing by name needs to pass by location
1. Method 1
2. Method 2
1. Customize Python The parent class of a class is usually object, Multiple inheritance is possible ; Class members include function members ( Or method ) And data members ( Or attribute ).
2. When defining member functions , The first parameter must be self, Prefix can be used self. Call properties or other member functions ;
3. When instantiating a class to generate an object , The constructor will be called automatically for initialization , The name of the constructor is __init__,init There are two underscores before and after , no return value ; Omission
4. When clearing objects , The system calls the destructor automatically , Reclaim and release the resources occupied by the object. Its name is __del__. It can be omitted .
5. All member functions and properties are public by default ( That is, you can call... Through an object ), If you want to make it private , Add two underscores before the name . Use del Object name , You can delete objects .
example :
All things can be python Later I'm going to share 8.10 piece python Topic sharing , Will expand some knowledge of machine learning direction , You are welcome to support me .