python基础第七课_函数1

本文深入探讨Python函数的使用,包括如何定义函数、接收返回值、函数的封装概念以及位置参数和关键字参数的差异。通过实例解析,帮助读者更好地理解和应用Python函数。

函数

一、自动化测试应用场景

cases = [
    {"username": "yuz", "expected": "success"},
    {"username": "simple", "expected": "success"},
    {"username": "xiaopingguo", "expected": "success"},
]

for case in cases:#外部循环得到三个字典,赋值给case
    # case = {"username": "yuz", "expected": "success"}
    # print(case["username"])
    for k, v in case.items():
        print(f"键值对{k}: {v}")

 

1.开发一个测试运行工具,先录入自动化测试的用例,然后依次执行所有的用例。

#录入 3 组
times = 0
while times < 3:
    # 录入代码
    pass

例如:
users = list()#空列表
while len(users) < 3:#while (条件)  直到条件不满足为止,才跳出循环
    username = input("请输入用户名:")
    password = input("请输入密码:")
    age = input("请输入年龄:")

    user = dict()#空字典
    # user.update(username=username,
    #             password=password,
    #             age=age)
    user["username"] = username #字典的添加,得到结果:"username" =username 例如:info["age"] = 19- 'age': 19
    user["password"] = password #字典的添加
    user["age"] = age #字典的添加
    users.append(user)#增加元素:append, 在最后追加一个元素,只能增加一个元素,例如:user.append('艳丽')
    print(users)

for case in users:#依次获取列表users中的每个元素(字典),保存到case变量中
    print(f"运行用例-用户名{case['username']}")#此时case是字典,case['username']-一个一个获取字典中的key
二、已经学过的函数

1.input()

2.type()

3.len()

4.range()

5.max()

6.特殊的函数, 类和对象(方法)

  a = ''

7.a.upper()

8.a.format()

9.a.replace()
三、函数的定义

1.函数的定义:

def 函数名称():

    函数要执行的代码程序

2.函数的定义:就是把要存储的程序放到函数名称下面

例子一:
def add():
    c = 3 + 2
    d = c * 3
    return d

print(add())

 

例子二:

def add():
    c = 3 + 2
    d = c * 3
    print(d)

add()

3.函数的使用,和变量差不多,变量可以多次重复使用, 函数也一样,可以多次重复使用

# 函数的调用
add()

4.函数可以重读调用
def add():
    c = 3 + 2
    d = c * 3
    print(d)


for i in range(10):#可以表示循环的次数
    add()

 

四、函数的返回值

1. return 表示函数的返回值,返回给函数外面使用,返回值是函数执行完成以后,得到的计算结果,数据,既然是数据,就可以存在变量当中。

def add():
    c = 3 + 2
    d = c * 3
    print("函数内", d)
    return 1


print(add())#add()的结果是”函数内 15“,print(add())的结果是1


2.用变量接收函数的返回值,和print(add())结果一样

# 用变量接收函数的返回值
result = add()
print(result)

3.return 还有一个作用,函数的执行遇到 return 就终止,当 return 执行后, return 后面的代码不会再执行了。函数有 return 外面得到的数据就是 return 值 ,没有 return, 就是 None。

a = "abc"
print(len(a))
result = len(a)
print(result)
a = [1,2]
result = a.append(3)
print(a)
# 由函数定义时 return 决定的。
print(result)#None

# result = a.remove(1)
# print(result)
def add_elem():
    a = ['a']
    a.append("4")
    return "aasd"

print(add_elem())

 

五、函数的封装(函数就是个黑盒)

1.函数定义提供的参数形式参数:变量名称

def get_discount_price(price):
    # price = 110
    discount = 1
    if 50 <= price <= 100:
        discount = 0.9
    elif price > 100:
        discount = 0.8
    print(f'购买折扣:{discount * 10 :.0f} 折')
    print(f'优惠价格:{discount * price :.2f} 元')
    # return discount * price

get_discount_price(100)

2.函数调用的提供的参数实际参数:变量的值

get_discount_price(100)
get_discount_price(50)
get_discount_price(40)
get_discount_price(200)

3.多个函数的参数

def get_max(a, b, c):
    max_num = a
    if a < b:
        max_num = b
    if max_num < c:
        max_num = c
    return max_num

print(get_max(1,2,3))

4.位置参数:多个形式参数对应多个实际参数:形式参数和实际参数必须一一对应,这种一一对应的状态叫 位置参数( 没有 =), 位置要匹配

print(get_max(5,3,10))

5.关键字参数:(调用的时候有 a=14)在函数调用的时候提供一个关键字,标记是哪个变量,调换调用的参数的顺序:因为当参数多了之后,你记不住对应的位置,方便阅读和记忆

print(get_max(5, c=3, b=10))

6.默认参数:(定义的时候有 a=14)在函数定义的时候给变量提供一个默认值,defult 参数:可以让我们在调用函数的时候少传参数,调用函数更简单。
def get_max(a, b=8, c=9):
    max_num = a
    if a < b:
        max_num = b
    if max_num < c:
        max_num = c
    return max_num


print(get_max(12))

7.注意事项:不管是关键参数,还是默认参数,都要写到位置参数的后面。
def get_max( a, b=8, c=9):
    """需求文档
    获取最大值
    """
    max_num = a
    if a < b:
        max_num = b
    if max_num < c:
        max_num = c
    return max_num

print(get_max(12, c=23, b=55 ))

#
first_param = 5
b_param = 6
c_param= 13
expected = 5
print(expected == get_max(first_param, b_param, c_param))

 

 

六、总结

def run():
    a = 1 + 6
    # 1、你自己定义的函数:写 return
    return "hello world"


if __name__ == '__main__':
    b = run()
    print(b)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值