Python Basics----Functions + modules

Source: Internet
Author: User

Function

function definition
functions are methods, functions, To put it bluntly, a function is to combine a bunch of code together into a whole.

Function: Improve the reusability of code

Note: Functions not called will not be executed

Exercise: What is the result of running the following code?

def Test ():     Global a     def test1 ():     = a+5    return= test1 ()print(res)

Parsing: Many people think that is 10, because the test () function, set A as a global variable, and a value of 5, so the Test1 () function refers to the global variable a=5, so c=10, it seems reasonable, however ....

Run error, A is not defined, because the test () wood has been called, the wood has been called, the wood has been called, ah hahaha, know the importance of the function call is executed


Global variables, local variables

The variables inside the function are local variables, only for this function, variables outside the function are global variables, all functions are used------can declare local variables as global variables through global

c=10# global variable def  Test ():    Global a# via Global, Declare local variable A as global variable    a = 5     b=2# local variable, only valid in test () function    d=a*b*C
Return d def test1 (): = +c return m res1 == test1 ()print(res1,res2) #100 15

Parameters

Parameters, arguments, positional parameters, default parameter-- positional parameter, required parameter,

def Campare (a,b=5):# formal parameter    if a>=B:        return  A     Else :         return bres=campare (100,1000)# argument, a positional parameter, required, is the first parameter, the reference to the location corresponding to Res1=campare (4)# B is the default value parameter, not required, if not filled, take the default value, if the parameter, the value of the passed argument Print #    5

Variable parameters

defTest (A,b,*args):#variable Parameters    Print('a', a)Print('b', B)Print(args[2]) Test (2, 3,'123','456','789','SFSDF')#Location Call#a--2 b--3 args[2]---' 789 '#print (args) returns a tuple that contains all values except the value of a/b that can be accessed by subscript

Keyword parameters

def test2 (A,**kwargs):#**kwargs accepted value k-v key value pair    print(a)      Print(Kwargs) #返回一个字典test2 (a=1,name='lsas', age=18,addr='  Beijing')# must be called in the keyword this way, otherwise the error is not required #1 # {' addr ': ' Beijing ', ' age ': ' Name ': ' LSAs '}

return value

Return---If the function does not write return, the return value is None,return not must be written, it needs to get the return result of the function to write again.

Note: Return immediate End Function

Recursive---call yourself

Recursive can be achieved by the use of loops can be achieved, the final recursion, not high efficiency, up to 999 times recursive

def test1 (num):    num=int (input (' Enter a number:'))    if num%2== 0:         return True     Print (' not even, please re-enter!  ')    return test1 ()# recursive call to print(test1 ( ))

Module

A python file is a module that includes three different modules
1, standard module-- python comes with, do not need you to install
2, third-party modules- need to install, others provide.
Automatic installation--pip install XXX automatic installation
Manual Installation--1. Download the installation package 2. Unzip 3. In the command line into the directory after the decompression (shift+ right--here to open the command line) 4. Execute Python setup.py install
3. Write your own python file
Import Module
Import xx imports a file, what is the essence of the import file, run this python once, the file name cannot be the same as the file name
Import when importing the file, first find the file from the current directory, and then from the Python environment variables (environment variable is to let a command, regardless of which directory can be executed)
View environment variables
Import SYS Print (Sys.path) # View the environment variables for the current system you can import your own files into any directory in the displayed environment variable



Python Basics----Functions + modules

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.