▲python syntax
A statement beginning with # is a comment, and the Interpreter (CPython) ignores the comment
When the statement ends with:, the downward indent 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
In Python, constants are typically represented in all uppercase variable names
▲ escape character \
\ n = newline, \ t for tab, \ \ for \,\ ' for normal character ', \ ' for normal characters '
▲ output of formatted string
Inside the string,%s is replaced with a string,%d is replaced with an integer,%f is replaced with a floating-point number, and%x is replaced with a hexadecimal integer. The sample code is as follows:
' Hi,%s, you have $%d. ' % ('wddoer', 10000)
When you are unsure what to use,%s will always work, and it will convert any data type to a string
If% is an ordinary character in the string, a% is expressed as a percent
▲ Tuple tuples
A tuple is an ordered list, denoted by (), that cannot be changed once initialized. If the elements in a tuple have a list, the elements in this list can be changed
▲ List
A list is an ordered set of elements that can be added, modified, and deleted at will.
▲ Dictionary Dict
Dict uses key-value (Key-value) to store, internal key-value pairs are unordered and have a very fast query speed. Key is unique and immutable, the algorithm that computes value by key is called a hash (hash) algorithm
▲set
A set is a collection of keys, but does not store value. To create a set, you need to provide a list as an input collection
Set can be regarded as a set of unordered and non-repeating elements in mathematical sense, so we can do the intersection &
The only difference between set and dict is that the corresponding value is not stored, but the set principle is the same as Dict. Therefore, it is also not allowed to fit mutable objects. Because it is not possible to determine whether two mutable objects are equal.
In Python, strings, integers, 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.
When the statement inside the function body executes, once it executes to return, the function finishes and returns the result. Therefore, it is possible to implement very complex logic through conditional judgment and loop inside the function.
If there is no 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's 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
The Python function returns a multivalued value that actually returns a tuple. However, in syntax, a tuple 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:
1) The required parameter is before the default parameter. Otherwise the Python interpreter will error
2) When the function has more than one parameter, the change of the parameter is placed in front, the change of small parameters can be used as the default parameters
3) 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): = 1 while n > 0: = n-1 = S * x
return= Power (4)print= Power (4, 3) Print(res2)
▲ Variable Parameters
When you define a mutable parameter and define a list or tuple parameter, just precede the parameter with an * number. Variable parameters allow you to pass in 0 or any of the parameters that are automatically assembled as a tuple when the function is called. The sample code is as follows:
def Cal (*numbers): = 0 for in numbers: = sum + x * x return sum Print (Cal (1, 2)) Print (Cal (1, 2, 3= [1, 2, 3, 4]print(Cal (*l))
▲ keyword Parameters
The keyword parameter allows you to pass in 0 or any parameter with a parameter name, preceded by two * * numbers, which are automatically assembled into a dict within the function. 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:
defPerson (name, age, * *kv):return 'Name:', Name,'Age :', Age,'Other :', KVPrint(Person ('Wddoer', 26))Print(Person ('Wddoer', city=,'Hefei')) KV= {' City':'Hefei','Job':'Coder'}Print(Person ('Wddoer', **kv))
Note: When defining a function in Python, the required parameter, the default parameter, the variable parameter, and the keyword parameter can all be used together. Note, however, that the order in which the parameters are defined is: Required parameters, default parameters, mutable parameters, keyword parameters.
For any function, it can be called using the form of function (*args, **KV) (a tuple one dict), regardless of its arguments.
▲ Recursive function
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 stack overflow. 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