1 By default, Python 3 source files are encoded in UTF-8, and all strings are Unicode strings.
2 Single-line comments in Python begin with #, and multiline comments enclose comments in three single quotation marks ("") or three double quotation marks ("" ").
3 Python's most distinctive feature is the use of indentation to represent blocks of code . The number of spaces to indent is variable, but the statement of the same code block must contain the same number of indentation spaces .
4 Natural String, by adding R or R before the string. such as R "This was a line with \ n \" will be displayed, not newline.
5 python allows processing of Unicode strings with prefix u or u
6 Compound assignment: The expression on the right is executed before the assignment changes. The execution order of the right-hand expression is left-to-right.
7 variables in Python do not need to be declared. Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.
The 8 variable is a variable, it has no type, and what we call "type" is the type of object in memory that the variable refers to.
Differences in assignment, shallow copy, deep copy:
Reference https://www.cnblogs.com/wilber2013/p/4645353.html
Shallow copy effect: Slice operation, factory function, copy.copy
Deep copy effect: Copy.deepcopy
9 Division//: Get an integer;
A backslash can be used as a continuation character, indicating that the next line is a continuance of the previous row. You can also use "" "," "" "" "" "or" "" ... "across multiple lines.
The string in each Python is indexed in two ways, the first from left to right, from 0 onwards, and the second from right to left, and from 1 to lower in turn. Slices a string to get a bunch of strings. Separate two indexes with a colon in the form of a variable [head subscript: Tail subscript].
The Python string cannot be changed . For non-mutable objects, the so-called replace operation actually returns a new data, opening a new location in memory.
The list is a comma-delimited list of elements written between square brackets. The types of elements in the list can be different. The elements in the list can be changed
the array tuple (tuple) is similar to a list, except that the elements of a tuple cannot be modified. Tuples written in parentheses
, the elements are separated by commas.
Although the elements of a tuple cannot be changed, it can contain mutable objects such as list lists.
the collection (set) is a set of unordered, non-repeating elements. You can create a set set using curly braces or set () functions, note: Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary.
The dictionary is a mapping type (mapping type), which is an unordered key: a collection of value pairs.
The keyword must use an immutable type, that is, the list and the tuple containing the mutable type cannot do the keyword.
In the same dictionary, the keywords must also differ from each other.
Python3 List
Write in square brackets, separated by commas. Do not have to be the same type. Nesting support
can be indexed and sliced. The slice returns a copy.
Support + stitching operation; assignment [] is empty
. Append () Add new item at the end, modify specified interval value
Use Len () to count the length
To create a list using the sequence expression for in
When traversing in a sequence, the index position and the corresponding value can be obtained simultaneously using the enumerate () function
Python3 tuples
Written in parentheses, elements cannot be modified
Tuples contain only one element, you need to add commas (50,)
Tuples can also be connected and copied, using Del to delete the entire tuple;
Tuple () Converts a list to a tuple
Python3 Dictionary
Write in curly braces to store any type of object
D = {key1:value1, key2:value2}
The key value cannot be modified, can be used as a number, string or tuple, and the list does not; Access value by key value
Constructor Dict () constructs a dictionary directly from a key-value pair of tuple lists
When traversing in a dictionary, the keywords and corresponding values can be read at the same time using the items () method.
21-piece Control
If condition: Need to add a colon after conditional judgment
Distinguish blocks of code using the same indentation
No switch-case statement
22 Cycles
While loop: Colon indent
For loop:
For <variable> in <sequence>:
<statements>
Else
<statements>
Range (A,B,C) produces a to b-1 number, in C as the step size. Default A=0,c=1
Else statement in loop: executed when the loop terminates, but not when terminated by break
Pass Statement
23 iterators
ITER () Next ()
Iterators can only move forward without backing back. Can be used for looping
Generator
The generator is a function that uses yield to return an iterator.
In the process of calling the generator to run, the function pauses and saves all current run information each time the yield is encountered, returning the value of yield. and continue running from the current location the next time the next () method is executed.
24 functions
Defining functions using the DEF keyword
def function name (parameter list):
function body
Scope of the variable
Default parameter: Must point to immutable object
Add an * number to the parameter to change the parameter to a variable parameter. This is actually a tuple. You can pass in any parameter when passing in a parameter.
The function can also be called by using the Kwarg=value keyword Parameter form to add the * * number before the argument. These keyword parameters are automatically assembled inside the function as a dict. Use the special delimiter *, followed by the parameter to become the named keyword parameter. The named keyword argument must pass in the parameter name.
The function can return more than one value (return x, y) actually returns a tuple
The function name is also a variable. A function can receive another function as an argument for a higher order function.
25 format input and output
Python uses print for output. Separate multiple strings with commas, and the comma output as spaces.
STR (output converted to string) repr (). Rjust\ljust\center () The string is right, left, middle, and filled with spaces.
Replace the parentheses and the characters inside the string with the parameters of Str.format ().
Read the input using input (). Note The data type that is returned is Str. Use the data conversion function to convert it to the desired type.
26 File read/write
27 Decorators
Takes a function as an argument and returns a function.
With the @ syntax, you can place the adorner at the function definition
Place the @log at the definition of the now () function, which is equivalent to executing the statement. Today = log (now)
28 Partial function
When a function has too many arguments and needs to be simplified, use functools.partial to create a new function that can fix some of the parameters of the original function, making it easier to invoke.
Python Basic grammar Learning record