variablePython has a slightly different approach to most other computer languages, assigning values directly to variables without defining which data type the variable belongs to, and it looks more like putting the name on top of the value before you use the variable, you need to assign a value to the first. Variable names can include letters, numbers, underscores, but variable names cannot begin with numbers, and are case-sensitive for letters
comments and Code indentation
A single-line comment in Python begins with a # and a multiline comment encloses the comment in three single quotes (' ") or three double quotes (" ") without semicolons in Python; As a code terminator, the start and end of a block of code need not be marked with curly braces, just separated by a colon (:), using strict code indentation in Python to represent code blocks
the conditional branch of Python ([] within the optional)
If condition: ... [Elif conditions: ...] Else: ...
the while loop of Python
While condition: loop body
Python for Loop and range ()
Li = [' A ', ' B ', ' C '] for s in Li:print (s)
For I in range (5): Print (i)
python common operators and their precedence
Power Operation (* *) > PLUS sign (+x,-x) > Arithmetic operator (*,/,//, +,-) > comparison operator (<,<=,>,>=,==,!=) > Logical operator (not,and,or)
assert (Assert)
Assert this keyword we call "assertion", when the condition behind this keyword is false, the program automatically crashes and throws a Assertionerror exception.
string
Create a string: You can enclose characters in quotes, either single or double quotes, but must be pairs, and escape symbols (\) can be used to escape the quotation marks in a string if a single or double quotation mark is required in the string. Use three quotes to get a long string of strings that spans multiple lines in the original string: we can use the backslash to escape the backslash itself: str = ' C:\\now ', but if there are many backslashes in a string, the original string is available, just add an English letter R to the front of the string: str = r ' C:\now\test\demo ' String related actions: String method and annotation string format symbolic meaning and escape character meaning
lists: Variable sequences
Create a list: Enclose a bunch of data in brackets, the data is separated by commas to create the list, and the empty list is directly used [], and the list () function is used to convert a sequence type to a list assignment: element assignment, fragment assignment (replaceable or deleted list) adding elements to the list: Append () method is used to append a new object to the end of the list. The Extend () method can append more than one value from another sequence at the end of the list, and the Insert () method is used to insert the object into the list to get the element from the list: The element's index value (starting from 0) gets a single element from the list, Use fragmentation to get multiple elements from a list to remove elements from a list: The Remove () method removes the first occurrence of a value in the list, and the Del,pop () method removes one element from the list (the default is the last) and returns some common operators of the value list for that element: the comparison operator, Logical operators, connection operators, repeat operators, member relations operators, reverse () method to store the elements in the list backwards, the sort () method is used to sort the list in the original location, the index () method is used to find the index position of the first occurrence of a value from the list, count () method to count the number of times an element appears in a list
tuples: Immutable sequences
Create a tuple: enclose a bunch of data in parentheses, separating the data with commas to create tuples; or you can also create tuples automatically by separating values directly with commas; empty tuples are used directly (), and only a tuple of a value is followed by a comma; the tuple () function takes a sequence as an argument and converts it to a tuple Stitching tuples: element values in tuples are not allowed to be modified, but can be combined to delete tuples: element values in tuples are not allowed to be deleted, but some common operators that can use the DEL statement to delete an entire tuple are similar to lists. Reference: python--list, tuples
sequence
Lists, tuples, and strings have a lot in common, collectively called sequences: 1. Each element can be indexed by 2. The default index value always starts at 0 (the negative index counts from right to left) 3. You can get a set of elements within a range (without step size: [Start position (included): End position (not included)] and have step length: [Start position (inclusive): End position (not included): Step]) 4. There are a number of common operators (repeat operators: Multiply "*" by a positive integer to indicate the number of repetitions, concatenation operators: The same type of sequence with "+" stitching, Member relations operators: "in" to check whether a value is in the sequence) here are some common bif (built-in methods) for sequences: list ( Iterable) Converts an iterative object to a list
Tuple ([iterable]) converts an iterative object to a tuple
STR (obj) converts an Obj object to a string
Len (sub) Returns the length of a sub Max () returns the maximum value in a sequence or a set of parameters
MIN () returns the minimum value in a sequence or a set of parameters
Sum (iterable[, start=0]) returns the sum of the sequence iterable and optional parameter start
Sorted (iterable, Key=none, Reverse=false) returns a sorted list, using the method consistent with the built-in function of the list (List.sort ()) reversed (sequence) to return the value of the reverse iteration sequence, Consistent with the built-in function of the list (List.reverse ())
Enumerate (iterable) generates tuples of index and item values for each element
Zip (iter1 [, Iter2 [...]]) returns a tuple of the sequence of individual arguments
function
Category: Systems with built-in functions, third-party functions, custom function definition functions: def function name (parameter): code block function to have return value returns, parameters are passed by reference function parameters: Prerequisites: The function must be passed in the correct order. The number of calls must be the same as when it is declared. Named parameter: The caller determines the passed-in parameter value with the name of the parameter. You can skip parameters that are not passed or random arguments, because the Python interpreter can match the parameter values with the name of the argument. Default parameter: When the function is invoked, the value of the default parameter, if not passed in, is considered to be the default variable length parameter: A function can handle more than the original declaration of parameters, these parameters are called indefinite parameters, the addition of the asterisk "*" variable name will hold all the unnamed variables parameters, but also select not multiple parameters Global keyword: The scope of a globally variable is the entire module (that is, the entire code snippet), but be aware that it is just a matter of accessing the global variable within the function, and do not attempt to modify the global variable, because once the function inside attempts to modify the global variable, Python automatically creates a local variable with exactly the same name inside the function, so that the modified result is modified only to local variables without affecting the global variables. inline function: Python's function definition can be nested, which is to allow the creation of another function inside the function, which is called an inline function or internal function. Closures: The closure in Python is defined in terms of representation: If you are referencing a variable in an external scope (but not in the global scope) in an intrinsic function, then the intrinsic function is considered a closure (closure) anonymous function: Python uses a lambda to create an anonymous function. Syntax: lambda [arg1 [, Arg2,..... argn]]:expression.
Dictionaries
The dictionary is the only mapping type in Python, and the dictionary is made up of keys and corresponding values. Dictionaries are also referred to as associative arrays or hash tables. The dictionary also has a key symbol is the curly braces {}. As with a sequence, you can create a dictionary with Dict (); Unlike a sequence, if you try to assign a value to a nonexistent location in a sequence, the error occurs, but if the dictionary, TA automatically creates the corresponding key and adds the corresponding value. Modify Dictionary: dict[' age ' = 8; If the age key exists, modify the value of the age key, and if not, add the deletion of the dictionary element: Del dict[' name ' delete key is ' name ' entry, dict.clear () empty dictionary all entries, Del Dict Delete Dictionary Dictionary key features: Do not allow the same key to appear two times; the key must be immutable, so the number is available, the string or tuple acts, and the list doesn't work. Dictionary built-in Methods: Dict.pop () Dict.popitem () dict.clear (): Delete all elements in the dictionary Dict.copy (): Returns a shallow copy of a dictionary Dict.fromkeys (): Create a new dictionary, the key in the sequence seq to do the dictionary, Val for the dictionary all keys corresponding to the initial value dict.get (key, Default=none): Return the value of the specified key , if the value does not return the default value Dict.has_key (key) in the dictionary: Returns False Dict.items () if the key returns true in the Dictionary Dict (): Returns a traversal (key, value) tuple array in the list Dict.keys () : Returns a dictionary with a list all the key Dict.setdefault (key, Default=none): is similar to get (), but if the key does not already exist in the dictionary, the key is added and the value is set to default Dict.update (DICT2) : Update the dictionary dict2 key/value pairs into Dict dict.values (): Returns all values in the dictionary as a list
python file operations related
Open mode and file object methods for files
Methods of using functions commonly used in files and directories in OS and Os.path modules
Exception Handling
1, try...except statement: To catch and handle errors
2, raise: for throwing an exception
3, try...finally statement: Finally within the statement regardless of whether or not the previous exception will be executed
4, with statements, simplifying try/except/finally-related details
5, Python Standard Exception Summary
python Else Statement
1. Match with If statement
2. Match with circular statement
3. Match with exception handling
Classes and Objects
The class in the Learning Notes--python"python (classes)
Magic Method
A detailed explanation of the Python magic method
Module
Regular Expressions
Python3 Regular expression special symbols and usage (detailed list)
The python Introductory tutorial
Python Basics Tutorial 0 Basics Learning Python Python official documentation