Python: 函数详解

一、函数的名称、参数和返回值

def bmi(height, weight):
    return round(weight/(height*height),1)
print(bmi(1.6,44))        #17.2

round函数返回一个数值,该数值是按照指定的小数位数进行四舍五入运算的结果。

二、Docstring

def bmi(height, weight):
    '''
    计算BMI的值:
    :param height: 1.6m
    :param weight: 44kg
    :return: weight/(height*height),保留一位小数
    '''
    return round(weight/(height*height),1)
print(bmi.__doc__)
print(round.__doc__)
print(bmi(1.6,44))        #17.2

Python: 函数详解_第1张图片

print(__doc__) 输出文件开头注释的内容

三、参数(见上一节函数的参数)

def gongxi():
    print('恭喜发财!')
gongxi()           #恭喜发财!
def hello(name,type):
    print(f"{name}是一个{type}")
hello('狗','大黄')

四、返回值

函数可以有返回值也可以没有,None;遇到return,函数执行结束。

def send_flower(flower_num):
    if (flower_num < 10):
        return 'NO'
    if (flower_num % 14 == 0):
        return 'YES'
    else:
        return 'THANKS'
print(send_flower(18))      #THANKS

如果返回多个值,多个值被包在一个元组里面

def bmi(height,weight):
    bmi_value = round(weight/(height*height),1)
    if(bmi_value < 18.5):
        return bmi_value,'多吃点'
    elif(bmi_value <= 24):
        return bmi_value,'你真棒'
    else:
        return '谢谢你'
# print(bmi(1.8,60))   #(18.5, '你真棒')
result = bmi(1.8,60)
print(result[1])      #你真棒

五、传值与传引用

(1)基本类型是传值,简单复制一份,不影响原来的变量的饿值

height = 1.58

def predict_height(height):
    '''预测将来的身高'''
    height = height + 0.3
    return height
print(predict_height(height))
print(height)
#1.8800000000000001
#1.58

(2)复杂类型是传引用,传过去是变量的内存地址,要修改就都修改了

heights = [1.58,1.56,1.87,1.98]
def predict(heights):
    for index,h in enumerate(heights):
        heights[index] = h +0.3
    return heights
future_heights = predict(heights)
print(future_heights)
print(heights)
#[1.8800000000000001, 1.86, 2.17, 2.28]
#[1.8800000000000001, 1.86, 2.17, 2.28]

(3)复杂类型如何传值,若仍想保留原来的值,只需要再复制一份

heights = [1.58,1.56,1.87,1.98]
def predict(heights):
    for index,h in enumerate(heights):
        heights[index] = h +0.3
    return heights
future_heights = predict(heights[:])
print(future_heights)
print(heights)
# [1.8800000000000001, 1.86, 2.17, 2.28]
# [1.58, 1.56, 1.87, 1.98]

六、函数有自己的属性,可以被赋值,也可以被传递

def bmi(height,weight):
    bmi_value = round(weight/(height*height),1)
    if(bmi_value < 18.5):
        return bmi_value,'多吃点'
    elif(bmi_value <= 24):
        return bmi_value,'你真棒'
    else:
        return '谢谢你'

def main(check):
    height = float(input('输入身高'))
    weight = float(input('输入体重'))
    print(check(height,weight))
main(bmi)
# 输入身高1.8
# 输入体重60
# (18.5, '你真棒')

七、匿名函数(lambda)

#传统方法
numbers = [2,4,5,6,8,3,6]
def process(numbers):
    for n in numbers:
        print(n*n,end = ' ')
    print()
process(numbers)    #4 16 25 36 64 9 36
#匿名函数 lambda
numbers = [2,4,5,6,8,3,6]
def process2(numbers,calc):
    for n in numbers:
        print(calc(n),end = ' ')
    print()
process2(numbers, lambda x:x+5)   #7 9 10 11 13 8 11
process2(numbers, lambda x:x if x%3 !=0 else 11)  #2 4 5 11 8 11 11

八、变量的作用域:全局和局部

局部变量:def 和 class 内定义的变量

全局变量:在脚本内(def 和 class外)定义的变量,可以在global和函数内访问

局部变量和全局变量重名,局部变量优先使用

全局是指在当前文件(模块)内全局

count = 8
def work():
    a = 8
    count = 10
    print(count)
work()            #10
print(count)      #8

九、用global在函数中改写全局变量

count = 8
def work():
    global count #这个count就是外面的count
    count = 10
    print(count)
work()            #10
print(count)      #10

十、import(5种语法)

import method
method.f1()

import method as m2
m2.f1()

from method import f1
f1()

from method import f1 as func1
func1()

from method import *
f1()
f2()
print(a)

你可能感兴趣的:(Python学习,python,开发语言,后端)