Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python3 function


May 10, 2021 Python3


Table of contents


In this section, we'll introduce you to the application of functions in Python.

This section can be found in the Python Function Application Details.

The Python definition function uses the def keyword, which is generally in the following format:

def  函数名(参数列表):
    函数体

Let's use the function to output "Hello World! ":

>>> def hello() :
  print("Hello World!")

 
>>> hello()
Hello World!
>>> 

Application of more complex points with parameter variables in functions:

def area(width, height):
    return width * height
 
def print_welcome(name):
    print("Welcome", name)

print_welcome("Fred")
w = 4
h = 5
print("width =", w, " height =", h, " area =", area(w, h))

The above example output results:

Welcome Fred
width = 4  height = 5  area = 20

The scope of the function variable

A variable that defines inside a function has a local scope, and a variable that defines a global scope outside the function.

You can get a clear understanding of the scope of the Python function variable by following the following examples:

#!/usr/bin/env python3
a = 4  # 全局变量
 
def print_func1():
    a = 17 # 局部变量
    print("in print_func a = ", a)
def print_func2():   
    print("in print_func a = ", a)
print_func1()
print_func2()
print("a = ", a) 

The above examples run as follows:

in print_func a =  17
in print_func a =  4
a =  4

Keyword parameters

The function can also be called in the form of a keyword argument for kwarg s value. F or example, the following function:

def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
    print("-- This parrot wouldn't", action, end=' ')
    print("if you put", voltage, "volts through it.")
    print("-- Lovely plumage, the", type)
    print("-- It's", state, "!")

You can be called in several ways:

parrot(1000)                                          # 1 positional argument
parrot(voltage=1000)                                  # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM')             # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000)             # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump')         # 3 positional arguments
parrot('a thousand', state='pushing up the daisies')  # 1 positional, 1 keyword

Here's the wrong call method:

parrot()                     # required argument missing
parrot(voltage=5.0, 'dead')  # non-keyword argument after a keyword argument
parrot(110, voltage=220)     # duplicate value for the same argument
parrot(actor='John Cleese')  # unknown keyword argument

Returns a value

The Python function returns a function value using the return statement, which can be assigned as a value to a specified variable:

def return_sum(x,y):
    c = x + y
    return c

res = return_sum(4,5)
print(res)

You can also have the function return an empty value:

def empty_return(x,y):
    c = x + y
    return

res = empty_return(4,5)
print(res)

A list of variable parameters

Finally, a less common feature is the ability for functions to call variable numbers of parameters.

These parameters are wrapped into a group (see the yuan and sequence).

Before these variable parameters, you can have zero to more than one normal parameter:

def arithmetic_mean(*args):
    if len(args) == 0:
        return 0
    else:
        sum = 0
        for x in args:
            sum += x
        return sum/len(args)

print(arithmetic_mean(45,32,89,78))
print(arithmetic_mean(8989.8,78787.78,3453,78778.73))
print(arithmetic_mean(45,32))
print(arithmetic_mean(45))
print(arithmetic_mean())

The output of the above examples is:

61.0
42502.3275
38.5
45.0
0

For more detailed tutorials, see Python Function Application Details.