- Characters are enclosed in double quotation marks, and numbers are not enclosed;
- Delimiter is a comma (,) and cannot be a space
- Assign a value when the variable is defined
- With Utf-8 code:#-*-coding:utf-8-*-or #coding:utf-8
- String definition: Single/double quotes
- The% symbol is used to format the string, "%s" means replacing it with a string, "%d" represents an integer substitution, and "%r" means that whatever is printed out
- Triple quotation marks can indicate a multiline string with the freedom to use single and double quotes
- The escape character is a backslash (\), note: The end of the line backslash indicates that the string continues uninterrupted on the next line
- String prefixed with R or R indicates that the string is a natural string, where the character does not require special handling, and the regular expression is processed using natural strings
- If...elif...elif...else; thewhile loop first defines a true value, while a loop is executed when the truth is false, and halfway through ends the truth is set to the False,while loop can use an ELSE clause; In loop, the iteration parameter in the For loop (the "I" is often used by the individual) is not defined; break termination statement: Break is not executed; The continue statement skips the remaining statement of the current block and executes the next loop;
- Raw_input (): standard input function; Range () returns the number of a sequence, two parameters when the third parameter represents the step from the first to the second (contains the first number does not contain the second), three arguments
- function definition: def function name (parameter ...): Use Global to declare a parameter in a global variable function to define a default value, a parameter with a default value must be at the back of the parameter without a default value
- The return statement can jump out of a function or return a value
- Pass statement does not do anything, generally used as a placeholder statement;
- Document string:"", beginning of the first line of capital letters, period end, the second line is blank, the third row is described in detail; you can use the __doc__ call (double underline on each side)
- Module: Name the __name__ property with a. py suffix : By judging whether the __name__ property is __main__, the module executor module calls the import module name; Module Object invocation: module name. Object Name Each of the. Py Python programs is also a module that can be called by other programs in the same directory
- Dir () function: Lists the identifier of the module definition, including: functions, classes, and variables when you provide a module name for Dir (), it returns a list of names defined by the module, and if no arguments are supplied, it returns a list of the names defined in the current module.
- Data structure (Sequence): list, tuple, data
List: Content variable: x=[' apple ', ' banana ']sort () method for array sorting "Sort by list x: x . Sort () " , Sorted gets the sorted copy "Y:y=sorted (x)" x.append (' rice ')" del method: Delete "If delete x list first: del x[0]" pop () method: Removes from the list and returns the element object insert () method: Inserts the specified object into the list " If you start adding an item to the X list A:x.insert (0, ' a ') list prints the default one-line wrap, adding a comma (,) to the end of the print statement to replace the line break
Tuple (tuple): content is immutable and is typically used to print statements: zoo= (' Wolf ', ' Elephant ') contains only one item the tuple needs to be followed by a comma at the end of the project. Used to differentiate a tuple from an object with parentheses can be used in conjunction with the% number for printing;
Dictionary (dict): Key <---> value, key immutable, variable value: dict={key1: Value1,key2:value2} item method can be used to crawl key values in a dictionary
- Sequence: index operator (grab a specific item, also called subscript operation) and the slice operator (grab part of a sequence, that is, a slice) assignment statements do not create a copy, just create a reference (Mylist=shoplist), equivalent to a shortcut, a change; To implement a copy list or a sequence or complex object, you must use the slice operation to obtain a copy (mylist=shoplist[:]), which is not affected by the modification;
- String method: str.startswith (' xx ');' xx ' in str;str.find (' xx ');
fengefu= '---' Fengefu.join (str)
- Classes and objects: Methods for classes/objects: There is only one difference from normal functions: they must have an additional first parameter name , self, which refers to the object itself. the __init__ method can be used to initialize a variable of an object class: Shared by all objects of the class, variables that change the object: owned by each object/instance of the class, not affected
- Inheritance: Superclass---inheritance----subclasses; class inheritance: Subclasses are defined when the superclass is followed by a tuple; function inheritance in a class: the superclass name. Function name (self, variable 1, variable x) Reference: Class name. Name of the variable (from large to small)
- File classes indicate open files and modes:f=file (' file.txt ', ' W '), Mode has write (' W '), read (' R ') <----as default open mode, append (' a ') and other modes; Write () Method:f.write (' xxx '), write; Close () Method:f.close (), close after use, ReadLine () method to read an entire line including line end newline character, so if the read result is empty, Description to the end of the file; print ' with comma ', ' End: Eliminate Wrap
- Memory:Pickle Module (cpickle), which can be used to store and remove any Python object cpickle.dump (list,f): Stores the contents of the list in file F; stored= Cpickle.load (f): Remove content from f to stored
- Exception: The try...except statement is used to handle exceptions, try-the normal statements in blocks, except-block ERROR-handling statements raise statements can be used to throw exceptions, placed in try-blocks; The finally statement is used to handle statements in a finally block whether or not an exception occurs, such as closing an operation (Close ())
- SYS.ARGV: Returns a command-line argument,sys.argv[0] represents the code itself file path, so the parameter is argv[1][2 from the beginning :] to take the first command-line argument, but to remove two bytes, such as the command line argument is "--help", Get rid of----Execute the Help parameter
- OS module: os.name: the system platform used;os.getcwd (): Get the current working directory;Os.getenv () and os.putenv (): Read and set environment variables;Os.listdir () : Returns all files and directory names under the specified directory;os.remove (): Deletes a file;Os.system (): Runs the shell command;os.linesep: String gives the line terminator of the current platform; Os.path.split (): Returns the directory name and file name of a path,os.path.isfile () and Os.path.isdir (): Verify whether the given path is a file or a directory;Os.path.existe () : Verify that the given path exists;
- Use the * and * * prefixes to receive tuples and lists, which store extra function arguments as a tuple after the * parameter, in which the extra arguments are considered to be a dictionary key/value pair.
- Lambda form: Lambda statements (return lambda s: expressions) are used to create new function objects and return them at run time (lambda requires a parameter, followed only by a single expression as the body of the function, and the value of the expression is returned by this new function, note!) : Lambda can only use expressions, even if the print statement is not in lambda form
- EXEC statement: Used to execute Python statements stored in a string or file, such as:exec ' print ' Hello world! ' ---execution output Hello world! statement;
- Eval statement: Used to calculate a valid python expression stored in a string, such as:eval (' 2*3 ')---Calculate the value of 2*3;
- Assert statement: Used to declare a conditional statement is true, such as:assert conditional Statement---to determine whether the condition statement is true, truth does not return information, the condition statement is false when the Assert statement fails, will throw a assertionerror error.
- repr function: Used to get the canonical string representation of an object, such as:repr (i)---returns the canonical string representation of the list I (the inverse quotation mark, also known as the "translator" can do the same function, such as:' i ')
A concise tutorial for python---learning notes