Python Beginner function Basics (function definition call value passing, etc.)

Source: Internet
Author: User
Tags function definition

1, the main mode of programming Focus:

Process-oriented "class" keyword class   

2. The Python function is a logical and structured collection. Functions are defined and called:

#Author:xiajinqi#functiondef_print ():Print("Hello World")    return0#function Callx=_print ()#function returns the resultPrint(x)def_print ():Print("Hello world1")    return0#process essence is a function that does not return resultsdef_print ():Print("Hello world2")#function Callx=_print ()#function returns the resultPrint(x) E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/users/xiajinqi/pycharmprojects/twoday/Function.pyhello World0hello world2noneprocess finished with exit code 0


3, the function of the mutual invocation, and the use of a bit of function, reduce duplication of code, strong extensibility. Logic is clearer and more reasonable.

#Author:xiajinqi#functionImportSys,timedeflogger (): Time_format='%y-%m-%d'time_current=Time.strftime (Time_format) with open ("Test100.txt","A +", encoding="Utf-8") as F:f.write ("log end%s \ n"%time_current)return0deftest2 (): Logger ()Print("first-time write")    return0deftest1 (): Logger ()Print("Second Write")    return0deftest3 (): Logger ()Print("third-time write")    return0deftest4 (): Logger ()Print("fourth time write")    return0test1 () test2 () test3 () test4 ( )


4, the value of the function is passed. The return value. The code after return does not execute. and return returns any value. Tuples, arrays, and any number of values, returning multiple values is a tuple

#function value passing, simple deliverydef_print_name (x):Print("your name is%s."%x)return0_print_name ("Xiajinqi")#Author:xiajinqi#function value passing, defining return valuedef_print_name (x):Print("your name is%s."%x)returnx,['test1','test2','Text3'],'Test'    Print("The statement after return does not execute") _print_name ("Xiajinqi")Print(_print_name ('Xiajinqi'))

E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe e:/users/xiajinqi/pycharmprojects/twoday/ function.py your name is Xiajinqi your name is Xiajinqi (' Xiajinqi ', [' test1 ', ' test2 ', ' Text3 '], ' test ')

Process finished with exit code 0

5. Several ways to pass the function value. Value Delivery,

# Author:xiajinqi # function value passing, defining return value def _print_name (x, y   ): Print (x, y) def _print_name1 (x, y   , z): Print = _print_name(100,y=300) # positional parameters and keyword parameters _        Print_name1 (## error, keyword parameter can no longer precede position parameter

6, the use of indefinite parameters

#Author:xiajinqi#function value passing, defining return valuedef_print_name (x, y):Print(x, y)def_print_name1 (x, Y, z):Print(x, Y, z)#default parameter _print_name2 (x,y=11,z) error, position parameter can no longer form parameter frontdef_print_name2 (x,y=11,z=222):   Print(x, Y, z)#Indeterminate Parametersdef_print_name4 (x,*args):Print(x)Print(args)Print(args[1])#Pass a dictionary and turn the keyword into a dictionarydef_PRINT_NAME5 (* *Kwargs):Print(Kwargs) x= 100y= 200Test= ['test1','test2','test3']test1= {'name':'Xiajinqi',' Age':'222'}_print_name (100,Y=300)#positional parameters and formal parameters_print_name1 (100,200,Z=100)##_print_name1 (100,z=100,200) #报错, positional parameters cannot be preceded by formal parameters_print_name2 (100,2222,1000) _print_name4 (100,*[" -"," $",' -']) _print_name4 (*test) _print_name5 (**test1) _print_name5 (**{'name':'Xiajinqi'})


7. Global variables and local variables:

#string, shaping, etc. cannot be modified in local variables, lists, dictionaries, etc. can be directly modified in local variables, if you do not want to change, you can set as a tupleName ="Xiajinqi" Age=88#Global VariablesNAME1 = ['Test','Xiajinqi','Wangwu']defchange_name (): Age=100#Scoped to function, only changes have no effect on global variables    GlobalName#declared as a global variableName ="Test"    return0defchange_name2 (): name1[0]='test2'    return0change_name ()Print(age) change_name2 ()Print(name1) E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/users/xiajinqi/pycharmprojects/twoday/function.py88['test2','Xiajinqi','Wangwu']process finished with exit code 0

Python Beginner function Basics (function definition call value passing, etc.)

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.