Python self-study notes (seven)

1. defined functions and function calls

    1-1. Defined Functions

     Syntax writing function is defined: def function name (parameter name) (Note: in brackets can be empty, or may be a plurality of parameters, can be separated by commas between a plurality of parameters)

      

 

 

     As can be seen from the above, the function returns the default None

2. The function of important concepts

  2-1. The default parameters

  After the default parameters must be placed in the position parameter. If no arguments are passed to the function when calling the default value, but if the call passes the data to the default parameters, the measured parameters are replaced by the new default parameters.

  

 

 

   2-2, variable length parameter

    An asterisk * plus parameter name. When using a number of parameters passed here is uncertain. Data type tuples (tuples (tuple)): the written is the data in parentheses (), its use and a list of similar use, the main difference is that the elements of the list at any time changes, but the tuple element can not change. Like lists, tuples are iterable objects, which means we can use a for loop to traverse it.

  When the default parameter in a variable length parameter when the back, if you want to change the default parameters, need to specify parameter = 'parameter', e.g.

  

 

 

   Note: When there are arguments following variable length parameter, and there is no default values, when calling the function parameters must be written in the form of "specific data", a =, or error. E.g

  

 

 

   2-3, return statement

    In fact, calculating the length of sentence len () is also a built-in functions, his form is probably as follows:    

    DEF len ( 'content'):
      (Calculation 'content' length)
      return length value
    A = 'content'
    print ( referred to as ( a))
    

3, variable scope

  3-1, local variables

   Variables in the function definition, using only (local scope) within the function

  

  Variables can only be used inside a function

  3-2, Global Variables

  All variables other than the function replication can be used anywhere and then the program (global scope)

  

   3-3, global statement

  Local variables can be converted to global variables, generally written in the first line of the function body

  

 

  Note: Local and global variables try not to duplicate names, the system will be confused, so an error.

4 Expansion

  4-1, list () function

  You may convert the data into a list

  

 

   4-2, reversed () function  

  the reversed () function data may be reversed from the forward iteration.

  After reversed (), for the second cycle, List (), tuple () and the results of join () obtained are empty, the reason is not reversed b list itself, but rather a list of reverse iterator, so a direct output after the function return value is similar to the distortion, and the reversed (), returns the value only when the first pass.

  

Guess you like

Origin www.cnblogs.com/bpjj/p/11580813.html