Python Learning Sample source code

Source: Internet
Author: User
Tags function definition

Functional and functional programming
    • function definition;

    • function call;

    • Process definition;

    • Procedure Call;

    • Process-oriented programming method;

Example: function definition and Procedure definition example: using functions to reduce duplicate code

Why use a function:

1. Reduce duplication of code

Example: Using a function to make a program extensible

2. Make the program extensible

Example: function return value

Why do you write a function with a return value: you want the result of the function's execution.

The function body can be a line of code, or it can be 10,000 lines of code. The result of this function execution is what the program behind me needs to return the results of this program.

Example: a function with parameters

Learn the following methods: Positional parameters, default parameters, parameter group parameters, dictionary parameter

Example: example of a function definition with a parameter group: an example of a function definition with a dictionary: parameter-extensible function definition mode

If in doubt, can join QQ Learning Group: 484601288

? Local variables and with global variables

? Is it possible to reference another function in one function?

Example: Local variables
#Local variables: Variables inside a function are local variables, only valid in functions                defchange_name (name):Print("befor Change", name) name='San Zhang'  #This function is the scope of this variable    Print(" after change", name) name='Zhangsan'change_name (name)Print(name)
Example: Global variables
#Global variables: Variables that are in effect throughout the program, variables defined at the top level of the entire code are global variables Company='Ali'              defchange_name (name): Company='Tengxu'    Print("befor Change", name, company) name='Zhangsan'  #This function is the scope of this variable    Print(" after change", name)Print(company) name='Lisi'change_name (name)Print(Name, company)
Example: Changing a local variable to a global variable 1

Inside the function, by default "string" and "constant", these two types of local variables cannot change the global variables, if you want to change the local variable in the function to the global variable method: Before declaring global, it is not recommended to use this method.

#inside a function, local variables cannot change global variables by default, and if you want to change the local variable to a global variable in a function: Declare global before you change it, it is not recommended to use this method Company='Ali'  #Global Variables              defchange_name (name):Global Company Company='Tengxu'    #Local Variables    Print("befor Change", name, company) name='Zhangsan'  #This function is the scope of this variable    Print(" after change", name)Print('Company :', company)#perform a view of the print before the function callName ='Lisi'change_name (name)Print(name)Print('Company :', company)#to perform a view of a function call after printing
Example: Changing a local variable to a global variable 2

Unlike the < local variable changed to global variable 1> declares global and changes the local variable to a global variable. So the default cannot be changed only for "string and integer" outside. Complex data types: Lists, classes, dictionaries, and collections are all variables that can be changed to a global variable.

#Local variables change global variables Company='Ali'  #Global Variablesnames = ['Zhangsan','Sili','Wangwu']        defchange_name (): Names[0]='Zhang San'    Print("Inside_func:", names)#run to view execution resultsChange_name ()Print('Ouside_func:', names)#run to view execution results
Example: a dead global variable definition

Maybe you'll see this way somewhere else, forget it, and you can't use this method, which is not good for debugging in logically complex code, even if it's your own code.

# It is not recommended to use this method def chang_name ():     Global name     ' Zhangsan '      chang_name ()print(name)
Example: using recursion to implement, pass a value, each time divided by 2 until cannot be removed

Recursive characteristics: 1, must have a definite end condition; 2, each time entering a deeper level of recursion, the problem size should be reduced compared to the previous recursion, 3, the recursive efficiency is not high, may cause memory resource exhaustion

To do the best way to run recursion, using breakpoints to debug.

A simple recursive implementation passes a value each time it is divided by 2 until it cannot be removed:

# pass a value each time divided by 2 until it cannot be removed.  def Calc (n):     Print (n)     if int (N/2) >0:        return calc (int (n/2))    print( ' --->> ' , N)  Calc (10)

All sample programs are executed successfully under python3.5 under the Ubuntu16.04 system.

If in doubt, can join QQ Learning Group discussion: 484601288

Python Learning Sample source code

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.