python basic functions _

Function basis

aims

  • Experience fast function
  • Use basic functions
  • Function parameters
  • Function's return value
  • Nested function calls
  • Defined functions in the module

01. Experience fast function

  • The so-called function , that is, the code block having individual functions organized into a small module, when needed to call
  • Using the function comprises two steps:
    1. Define the function - the package independent function
    2. Function calls - Enjoy package results
  • Role function , in the development of the program, written using the function can improve the efficiency and code reuse

02. Use basic functions

2.1 Definition of functions

Format function is as follows:

def 函数名():

    函数封装的代码
    ……
  • def is the abbreviation of the English define
  • Function name should be capable of expressing a function package code function to facilitate the subsequent calls
  • Function name names should follow the naming rules for identifiers
    • It may consist of letters, underscores and digital composition
    • You can not begin with a number
    • Must be unique keyword

2.2 function calls

Very simple function call by the function name () to complete the call to the function

2.3 first function walkthrough

demand

  • 1. Write a function Poke, Poke package three lines of code say_hello
  • 2. Call greet the code below function
name = "hello"

# 解释器知道这里定义了一个函数
def say_hello():
    print("hello 1")
    print("hello 2")
    print("hello 3")

print(name)
# 只有在调用函数时,之前定义的函数才会被执行
# 函数执行完成之后,会重新回到之前的程序中,继续执行后续的代码
say_hello()

print(name)
  • After defined the function, the function only that it encapsulates a piece of code
  • If you do not take the initiative to call a function, the function is not actively performed

Think

  • Can function calls placed above the function definition?

    • Can not!
    • Because the function name before calling the function, we must ensure that Python is already known to exist functions
    • Otherwise, the console will prompt NameError: name 'say_hello' IS not defined (name wrong: say_hello name is not defined)

2.4 PyCharm debugging tools

  • F8 Step Over can step through the code, seen as a direct function call will execute one line of code
  • F7 Step Into can step through the code, if a function, the function will enter the interior

2.5 documentation comments function

  • In development, if you want to add comments to the function should be below the defined functions using three consecutive quotes
  • In three consecutive quotation marks to write captions of function between
  • In calling the function position, shortcut keys CTRL + Q information see the description of the function
    Note: Because the function thereof is relatively independent , and should be defined above the other function code (including comments) retains two blank lines

03. Function parameters

Walkthrough demand

  1. Development of function of a sum_2_num
  2. Function can be achieved in two digital summation function
    exercise code is as follows:
def sum_2_num():

    num1 = 10
    num2 = 20
    result = num1 + num2

    print("%d + %d = %d" % (num1, num2, result))

sum_2_num()

Think about what problems exist

函数只能处理 **固定数值** 的相加

How to solve?

  • If you need to be able to calculate the numbers, when you call the function passed to an internal function just fine!

3.1 function parameters

  • Fill inside parentheses after the function name parameter
  • A plurality of parameters used between partition
def sum_2_num(num1, num2):

    result = num1 + num2

    print("%d + %d = %d" % (num1, num2, result))

sum_2_num(50, 20)

3.2 Role of the parameters

  • Function , the code blocks having individual functions organized into a small module, when needed to call
  • Function parameters , function of increasing versatility , for the same data processing logic can accommodate more data
    1. Inside the function, the argument as the variable used for data processing required for
    2. Function call, defined as a function of the parameter order , the data processing inside the function desired, parameter passing

And parameter arguments 3.3

  • Parameter: the definition of a function, the argument in parentheses, is used to receive the parameters used, within the function as a variable
  • Arguments: calling a function, parameter parentheses, is used to pass data to the function of the number of internal use

04. The return value of the function

  • Program development, sometimes, will want a function is executed after the end, tell the caller a result , for the caller to do follow-up treatment for concrete results
  • The return value is a function of the completion of the work , the last to the caller of a result
  • Use the return keyword to return results in the function
  • Function call party may use the variable to the received function returns

    Note: return means return, the subsequent code will not be executed

def sum_2_num(num1, num2):
    """对两个数字的求和"""

    return num1 + num2

# 调用函数,并使用 result 变量接收计算结果
result = sum_2_num(10, 20)

print("计算结果是 %d" % result)

05. nested function calls

  • A function which in turn calls up another function , which is nested function calls
  • If the function test2, the test1 call another function
    is executed to the calling function test1, test1 will first function of executing the task will return to the position of calling a function test1 test2, the continued implementation of the follow-up code
def test1():

    print("*" * 50)
    print("test 1")
    print("*" * 50)

def test2():

    print("-" * 50)
    print("test 2")

    test1()

    print("-" * 50)

test2()

06. Use of a function module

Python module is a core concept of the program architecture

  • Module is like a kit , in order to use this tool kit, it is necessary to import import this module
  • Each ending with py to extend the Python source code files are a module
  • As defined in the module global variables, functions are provided to the tool module can be used directly outside

6.1 Use Module

  • You may be in a Python file in the defined variables or functions
  • Then another document using the import introducing this module
  • Once imported, you can use the module name. Variable / module name. Way function, using the variable or function defined in module

    Module allows the code ever written is easy to be multiplexed!

6.2 module name is an identifier

  • Identifier may consist of letters, underline , and digital composition
  • You can not begin with a number
  • Must be unique keyword

Note: If at the given Python file named, begin with a number can not be imported by this module in the PyCharm

6.3 Pyc file (understand)

C is compiled compiled meaning

Steps

  • Browse the program directory will find a pycache directory
  • Directory will have a file_name.cpython-XX.pyc file, cpython-XX indicates the version of the Python interpreter
  • This document is pyc Python interpreter source module is converted into bytecode
    Python save this byte code is used as a startup speed optimization

Bytecode

  • Python is in the interpretation of the source program is divided into two steps of the
    first source code, the compiler generates a binary byte code
    and then the bytecode processing, can be recognized by CPU will be generated machine code
  • After the byte code files with the module, when run under the program, if the last save bytecode no modified source code, Python will load .pyc files compiled skip this step and
  • When recompile Python, it automatically checks the source file and the time stamp byte code file
    if you go on to modify the source code, the next time the program is run, the bytecode will automatically re-created

Python module is a core concept of the program architecture

Guess you like

Origin blog.51cto.com/tobeys/2439965