Python Basics 10_Functions

Directly posted notes:

#!/usr/bin/env python
# coding:utf-8

#When defining a function, it is a good commentary habit to use three single quotes 
def test(x):
     '''
    Calculate a y=2*x+1
    :param x: integer
    :return: integer
    ''' 
    y = 2 * x + 1
     return y

# print(test)
print(test(4))

# ## This lesson reference http://www.cnblogs.com/linhaifeng/articles/6113086.html#label1

def test(): # # The function defined after will override the previous function 
    '''
    test function
    :return:
    ''' 
    y = 33*2
     return y

print(test())

"""
Function definition method in python:
 
def test(x):
    "The function definitions"
    x+=1
    return x
     
def: keyword that defines a function
test: function name
(): Internally definable formal parameters
"": Documentation description (not necessary, but strongly recommended to add description information to your function)
x+=1: generally refers to code block or program processing logic
return: define the return value


Call run: with or without parameters
Function name()

"""

A procedure is really a function with no return value:

#!/usr/bin/env python
# coding:utf-8

#Reference 2 Why use functionshttp://www.cnblogs.com/linhaifeng/articles/6113086.html#label1

'''
def send mail(content)
    # send email reminder
    Connect to Mailbox Server
    send email
    close the connection


while True:

if cpu utilization > 90%:
    send mail('CPU alarm')

if hard disk usage > 90 %:
    send mail ('hard disk alarm')

if memory usage > 80%:
    send mail('memory alarm')
'''


#Summary of the benefits of using functions:
#
# 1. Code reuse
#
# 2. Maintain consistency and easy maintenance
#
# 3. Scalability


# ########## A process is a function with no return value and no return

def test01():
    msg = 'test 01'
    print(msg)


def test02():
    msg = 'hello test02'
    print(msg)
    return msg


def test03():
    msg = 'test 03'
    print(msg)
    return 0,10,'hello',['alex','lb'],{'WuDaLang':'lb'}


def test04():
    msg = 'test 04'
    print(msg)
    return {'WuDaLang':'ssb'}


t1 = test01()
t2 = test02()
t3 = test03()
t4 = test04()

print(t1)
print(t2)
print(t3)
print(t4, type(t4))

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324680635&siteId=291194637