▲ python Grammar
in # the statement at the beginning is a comment, an interpreter ( CPython ) will ignore the comment
when the statement : at the end, the downward indented statement is treated as a block of code. Default indent of 4 spaces
indent: Force the writing of formatted, less indented code
The disadvantage of indentation: the copy-and-paste function fails when refactoring code
Python , the constants are usually represented in all uppercase variable names
▲ Escape Character \
\ n represents a line break, \ t represents a tab character, \\ represents \ , \ ' represents ordinary characters ' , \" represents ordinary characters "
▲ formatting the output of a string
inside the string, %s represents a string substitution, %d represents an integer substitution, %f represents the substitution with floating-point numbers, %x represents the substitution with a hexadecimal integer. The sample code is as follows:
' Hi,%s, you have $%d. '% (' wddoer ', 10000) |
not sure what to use when %s always works, it converts any data type to a string
if % is a normal character in a string, using the %% represents a %
▲ Meta-group tuple
tuple is an ordered list, with () indicates that it cannot be changed once it has been initialized. If the elements in a tuple have a list, the elements in this list can be changed
▲ List List
List is an ordered set of elements that can be added, modified, and deleted at will.
▲ Dictionary Dict
Dict using keys - VALUES ( Key-value ), the internal key-value pairs are unordered and have a very fast query speed. key is unique and cannot be changed, the algorithm that computes value by key is called a hash ( Hash ) Algorithm
▲ Set
Set is a group Key collection, but does not store value . To create a set, you need to provide a list as an input collection
Set can be seen as a set of unordered and non-repeating elements in a mathematical sense, so you can do the intersection & , and set | and other operations
Set and the Dict The only difference is that there is no storage of the corresponding value , but Set the principle and Dict The same. Therefore, it is also not allowed to fit mutable objects. Because it is not possible to determine whether two mutable objects are equal.
in the Python in, String, Integer, None are immutable. For non-mutable objects, any method that invokes the object itself, such as replace,does not alter the contents of the object itself, but these methods create new objects and return them. This guarantees that the immutable object itself will always be immutable.
▲ function
The function name is actually a reference to a function object, which can be assigned to a variable, which is equivalent to an alias for the function.
The statement inside the function body executes when it executes to the return , the function finishes executing and returns the result. Therefore, it is possible to implement very complex logic through conditional judgment and loop inside the function.
If not return statement, the result is returned after the function is executed, and the result is None
return None can be shortened to return
function Body only Pass function represents an empty function. In fact,pass can be used as a placeholder for the code to run first. Add code to the function body later as needed
Python a function returning a multivalued value is actually returning a tuple . However, in syntax, atuple can omit parentheses. Multiple variables can receive a tuple at the same time , assigned to the corresponding value by location
▲ default parameters for functions
When setting default parameters, there are a few things to note:
- The required parameters are before the default parameters. Otherwise the python interpreter will error
- When a function has more than one parameter, the parameter with large change is placed in front, and the parameter with small change can be used as the default parameter.
- The default value of the default parameter must be an immutable object. Otherwise the function is called once, and the default value of the default parameter is changed once
The default parameter can reduce the difficulty of calling the function. The function only needs to define one, whether it is a simple call or a complex call. The sample code is as follows:
def power (x, n=2): s = 1 While n > 0: n = n-1 s = S * x return s Res1 = Power (4) Print (RES1) Res2 = Power (4, 3) Print (Res2) |
▲ Variable Parameters
list tuple parameter, just precede the argument with a Span style= "font-family:arial;" >* #. Variable parameters allow you to pass in 0 or any of the arguments, These mutable parameters are automatically assembled as a tuple
def cal (*numbers): sum = 0 For x in numbers: sum = sum + x * x return sum Print (CAL (1, 2)) Print (CAL (1, 2, 3)) L = [1, 2, 3, 4] Print (CAL (*L)) |
▲ keyword Parameters
The keyword parameter allows you to pass in 0 or any parameter with parameter names, preceded by a parameter of two ** parameters, which are automatically assembled inside the function as a Dict . Similar to variable parameters, you can also assemble a dict, and then convert the dict into a keyword parameter to pass in. The sample code is as follows:
def person (name, age, **KV): Return ' name: ', Name, ' Age: ', age, ' other: ', KV Print (Person (' wddoer ', 26)) Print (Person (' wddoer ', city=, ' Hefei ')) KV = {' City ': ' Hefei ', ' job ': ' Coder '} Print (Person (' wddoer ', +, **KV)) |
Note: in Python A required parameter, a default parameter, a variable parameter, and a keyword parameter can all be used together when a function is defined in. Note, however, that the order in which the parameters are defined is: Required parameters, default parameters, mutable parameters, keyword parameters.
for any function, you can use the function (*args, **KV) the form (a tuple a Dict call it, regardless of its arguments when it is defined.
▲ Recursive Functions
a function calls itself inside a function body, and such a function is a recursive function. In theory, all recursive functions can be written in the form of loops, but the logic of the loops is not as clear as recursion. Using recursive functions requires preventing stackoverflow. The method of solving stack overflow is optimized by tail recursion. But the python interpreter does not optimize for tail recursion.
Python notes 1#python data types, syntax, and functions