Python functions, python function manual
This part of functions is very important in python learning. It makes your code look neat and clear, so it is convenient to call
#! /Usr/bin/env python # coding: UTF-8 # function concept: functions are well organized and reusable for single implementation, or a code segment associated with a function # rule for defining a function: the function code block starts with the def keyword, followed by the Function Identifier name and parentheses () # Any input parameter and independent variable must be placed in the middle of the parentheses, the center of the parentheses can be used to define the parameter # the content of the function starts with a colon and is indented. # Return (expression) ends the function and returns a value to the caller selectively. # Chestnut def hello (name): print "hello % s" % (name) hello (name = "fujinzhou") hello (name = "reboot ") # variable length parameter # variable names with asterisks will store all unnamed variable parameters def prininfo (arg1, * vartuple): print "output:" print arg1 print vartuple returnprininfo, 30,40) # The anonymous function lambda is an expression # lambda [arg1 [, arg2 ,..... argn]: Statement # function description sum1 = lambda arg1, arg2: arg1 * arg2 # Call the sum1 function print "after the multiplication value is:", sum1)
# Return Statement [expression] exit the function and selectively return an expression to the caller. The return statement without the Parameter Value Returns None. # Function description def count (arg1, arg2): total = arg1 + arg2 print "function:", total return total # Call the count function total = count) print "outside function:", total
# Variable scope all variables of a program are not accessible anywhere. The access permission depends on where the variable is assigned. # global variables and local variables # variables defined in the function have a local scope, global scope defined outside the function # local variables can only be accessed within the declared function, and global variables can be accessed throughout the Program # This is a global variable total = 0def sum (arg1, arg2): total = arg1 + arg2 print "the function contains local variables:", total return totalsum () print "the function is a global variable:", total