Monday, November 4, 2013Linux comes with Python, as long as the input Python can enter the editor; Number=23;guess=int (Raw_input (' Enter: ')) #int转换成整型 #raw_input是一个自带函数, keyboard input if number==guess: #if ... else statement (Python does not have a switch statement)The if, else, for, and other statements must be followed by a colon, print (' right ');the indentation after the colon is what you want to do, no indentation is performedelse print (' wrong '); forI in range (1,5): #for ... in loop, print (i); #The range (1,5) function returns the number of a sequence, from 1 to 4,range (1,5,2), when the default step is 1 is modified to 2, and the input 1,3else print (' For loop was over '); forLoop supportContinueAnd Break @[email protected]: Python has strict restrictions on indentation, for the following indentation is for the run content, no indentation is not def Define functions such as: Def Printm (A, B): if (a>b): Print (A, ' is Max '); Else:print (b, ' is Max '); PRINTM (3,4); --->output: 4 is MaxGlobal defines variables as globalsparameter notation: The arguments following the function can have default values, such as Func (A, B = 5), not func (a = 5, b)Key parameters:The assignment of parameters can be specified, for func (A, B, c), assignable func (+ c = 5), func (c = 5, B = 6) Monday, December 23, 2013Return statement:each function implies a return none statementDocstrings: A document string that describes a function or a class, such as a format (recommended): "This is a document string. You ' d better use it. " To print the docstrings, use the print function body. __doc__ (note that double underlines before and after) Wednesday, December 25, 2013module:Each module has its own __name__, if it is "__main__", it means that the module is run by the user alone. Import: Import Modulesuch as import example.py, you can write the import example at the beginning, using example method can write Example.init ()From...inport: As with import, the format is different from the beginning of the example import init, version, when used can be directly written to Init (), or call the variable versiondir (): Lists the list of properties for the current module, including methods and variables, and lists the modules that can be written as Dir (module name)list:Cases. Shoplist = [' Apple ', ' mango ', ' carrot ', ' banana '] usage: len (shoplist) length shoplist. Append (' rice ') adds element shoplist. Sort () Sorting del shoplist[0] Delete first element2. When a variable is used as a string connection, it should be written as "abc" + STR (ARG) Wednesday, July 2, 2014modules that handle command-line arguments:getopt (handles simple commands), Optparse (powerful, can be used for unix/posix)For optparse import optionparser parser = Optionparser () parser.add_option ("-F",Short Parameters"--file",Long Parameters type= "string", parameter type default= "file", default valuedest= "FileName",the stored variable, essentially the variable that executes the options behind it, Options.filenamehelp= "Write report to FILE",Help InformationMetavar= "FILE")alert user, command line expected parametersExecution: (options, args) = Parser.parse_args () Reference: http://blog.csdn.net/lwnylslwnyls/article/details/8199454Thread sleep:Time.sleep (0.1) The number of seconds in parentheses, which can be decimalabout self in the class method:a class MyClass and its instance MyObject, the class method isDef method (self)When calling this object's method Myobject.method (Arg1, arg2), Python automatically transitions toMyobject.method (MyObject, arg1, arg2) Thursday, August 28, 2014Python's unlocalerror:local variable reference not defined beforeIs that Python does not have a variable declaration, so it is a simple rule to find out the scope of the variable, if there is a function of the internal variable assignment, the variable is considered local, so the value of the modified variable is considered a local variable,? Workaround: At local variables, add global
Python Learning Record