Python BASICS (functions, file operations)
I. built-in functions 1. abs (x) for mathematical operations # Calculate the absolute value divmod (a, B) # Calculate the quotient and the remainder float ([x]) respectively. # convert a string or number to a floating point number. If no parameter is specified, 0.0int ([x [, base]) is returned. # convert a character to the int type. base indicates the hexadecimal long ([x [, base]). # convert a character to a long type range ([start], stop [, step]) # generate a sequence, which starts from 0 by default. oct (x) # convert a number to an octal hex (x) # convert integer x to a hexadecimal string chr (I) # Return the ASCII character 2 corresponding to integer I, set operation class format (value [, format_spec]) # format the output string, such as "I am {0 }, I like {1} "iter (o [, sentinel]) # generates an object iterator. The second parameter represents the delimiter max (iterable [, args...] [key]) # returns the maximum value min (iterable [, args...] in the set. [key]) # Return the minimum value dict ([arg]) in the set # create a data dictionary list ([ite Rable]) # convert a collection class to another collection class str ([object]) # convert to string type sorted (iterable [, cmp [, key [, reverse]) # sorting xrange ([start], stop [, step]) in the team set # returns an xrange object. 3. Logically determining all (iterable) # True when all elements in the set are true, if it is an empty string, return Trueany (iterable) # If one of the elements in the set is true, return Falsecmp (x, y) # If x <y, return a negative number; x = y, return 0; x> y, return positive number 4, reflection hash (object) # If the object is of the hash table type, return the hash value id (object) of the object) # Return object Unique Identifier len (s) # Return set length eload (module) # reload module type (object) # Return this Object Type 5. input ([prompt]) for I/O operations # obtain user input. Using raw_input, this function does not capture users' incorrect input file (filename [, mode [, bufsize]) # open (name [, mode [, buffering]) open the file. openraw_input ([prompt]) is recommended. # Set the input, all inputs are processed as strings. 2. File Operations. 1. Open Mode list. # w. Open in write mode. # a. Open in append mode. (create a file when necessary, starting from EOF) # r + open in read/write mode # w + in read/write mode (see w) # a + open in read/write mode (see) # rb is enabled in binary read mode # wb is enabled in binary write mode (see w) # AB is enabled in binary append mode (see) # rb + in binary read/write mode (see r +) # wb + in binary read/write mode (see w +) # AB + in binary mode In the read/write mode (see a +) # "U, \ r \ n can be automatically converted to \ n (used in the same mode as r or r +) rU, r + U2, and file object method file. close () # close the file. fileno () # returns the file descriptor file. flush () # refresh the file's internal buffer file. isatty () # determine whether the file is a tty-like device file. next () # return the next row of the file, or raise the StopIteration exception file when no other rows exist. read (size =-1) # read size bytes from the file. When no size or negative value is given, read all the remaining bytes and return the file as a string. readline (size =-1) # Read from the file and return a row (including the row Terminator), or return a maximum size character file. readlines (sizhint = 0) # Read all rows of the object as a list Back to file. xreadlines () # used for iteration. You can replace a more efficient method file of readlines. seek (off, whence = 0) # Move the file pointer in the file, and offset the off byte file from whence (0 indicates the start of the file, 1 indicates the current position, and 2 indicates the end of the file. tell () # Return the current position file in the file. truncate (size = file. tell () # truncate the file to the maximum size byte. The default value is the current file location. write (str) # write a string file to the file. writelines (seq) # Write the string sequence seq to the file; seq should be an iterative object that returns the string. 3. File Reading f = open('tpm.txt ') # read mode to read s = f. read () print (s, '\ n \ n') print (f. tell () # after reading the above, move the pointer to the end and use seek to move the file pointer to Assembly header f. seek (0) # Read a row each time using readline while (True): line = f. readline () print (line) if (len (line) = 0): breakf. close () # To avoid forgetting to close a file after it is opened, you can manage the context, that is, with open ('log', 'R') as f: with open ('log1 ') as obj1, open ('log2') as obj2: pass 2, function 1, define and use function def function name (parameter): function body 3, parameter parameters: define the variable while defining the function. For example, a and B. Real parameter: the value used by the input parameter is named real parameter. Def printMax (a, B): if a> B: print a, 'is maximum' else: rint B, 'is maximum' printMax (3, 4) 4. Local variables # When you declare variables in the function definition, they have no relationship with other variables with the same name outside the function, that is, the variable name is local for the function. This is called the scope of a variable. You can use global to define a local variable as a global variable. For example, def func (): global xprint 'x is ', xx = 2 print 'changed local x ', xx = 50 func () print 'value of x is ', x result: x is 50 Changed global x to 2 Value of x is 25, key parameter # If a function has many parameters, but you only want to specify a part of them, you can assign values to these parameters by name. This is called a key parameter. We use a name (keyword) instead of a location to specify a real parameter for the function. This has two advantages: 1. Since we do not have to worry about the order of parameters, it is easier to use functions. 2. If other parameters have default values, we can only assign values to those parameters we want. B = 5 and c = 10 are the default values. Example: def func (a, B = 5, c = 10): print 'A', a, 'and B is', B, 'and c is ', cfunc (3, 7) func (25, c = 24) func (c = 50, a = 100) output: a is 3 and B is 7 and c is 10is 25 and B is 5 and c is 24a is 100 and B is 5 and c is 50 dynamic parameters: Example 1: def func (* args): print args execution 1: func (11,33, 4454, 5) execution 2: li = [11,2, 54] func (* li) example 2: def func (** kwargs): print args run 1: func (name = 'lxb ', age = 21) Run 2: dic = {'name ': 'lxb ', age: 21, 'gender': 'ma Le '} func (** dic) executes 3: def func (* args, ** kwargs): print argsprint kwargs6. the return value # return Statement is used to jump out of a function after a function is returned. We can also return a value from the function. Def maximum (x, y): if x> y: return xelse: return yprint maximum (2, 3)