Python Basics 03-Functions in a detailed

Source: Internet
Author: User

Python functions in a detailed

This content includes:

    1. Function
    2. Module
    3. File operations
    4. Exception handling

Function:

    • Cognitive functions
    • Local variables and global variables
    • Explanation of function parameter usage
    • function Use Combat

The essence of function is the function of the sub-assembly. For example, we encapsulate a function c=a*a that asks for a 2-time square. We can use this function if we ask for a 2-time change (just for example).

The POW (x, y) in Python is the function that asks for X's Y-square.

Function: Provides the programming efficiency and the scale of the program

Local variables and global variables

A variable has an effective scope, which is scoped.

Global variables: Scope from the beginning of a variable to the last variable on the program to make a global variable

Local variables: Scope only local variables are called local variables "

" " Scope " " a=10def  ABC ():      Global J#用global定义局部变量 changed to globally variable    J=1   Print  (a) ABC ()print(j)

How to use the function:

Function call

# The function definition calls a=1def  ABC ():  #def定义函数 followed by the  function name  and:    print("  ABC") #具体方法, let the function do what, for example: Print out ABC call method: ABC ()
Output ABC ()

Use of function parameters

Argument: The parameter used for the definition is an argument, and the parameter used when invoked is called an argument

Formal parameters: Just the form of this parameter, no specific value, no value is a formal parameter.

Invoked as an argument.

# formal parameters and arguments def AB (A, B):# The parameters are separated by commas, AB is a formal parameter, there is no actual value    if a>B:        Print  (a)    elif a==B:        Print(A +"=  "+B)    Else:        print(b)

Call AB (+)

Output is 2

Python Basics 03-Functions in a detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.