第八章 函数

8.1 定义函数

def 函数名():
   函数体

函数名()#调用函数名

8.1.1 向函数传递信息

  • 可以多次调用函数实现信息传递
def user(username):
    print("Hello "+username.title())
user("lili")
user("xixi")

8.1.2 实参和形参

  • username是形参
  • lili、xixi表示实参

8.2 传递实参

8.2.1 位置实参

  • 每个实参都关联到函数定义的一个形参,关联方式基于实参的顺序
  • 调用函数多次(很高效的方式)
  • 位置实参的顺序
def user_pet(username,petname):
    print("Hello "+username.title()+",you have a "+petname)
user_pet("lili","dog")

image

8.2.2 关键字实参

  • 传递给函数的名称-值对,使得名称和值关联起来
  • 名称需要准确
def user_pet(username,petname):
    print("Hello "+username.title()+",you have a "+petname)
user_pet(username="lili",petname="dog")

8.2.3 默认值

  • 可给每个形参指定默认值
  • 在形参列表中必须先列出没有默认值的形参,再列出有默认值的实参
def user_pet(username,petname="dog"):
    print("Hello "+username.title()+",you have a "+petname)
user_pet(username="lili")

8.2.4 等效的函数调用

  • 混合使用位置实参、关键字实参和默认值

8.2.5 避免实参错误

  • 你提供的实参多余或少于函数工作所需的信息时,将出现实参不匹配错误。

8.3 返回值

  • 使用return语句将值返回到调用函数的代码行

8.3.1 返回简单值

def user_pet(username,petname):
    u_p=username+' '+petname
    return u_p.title()
res=user_pet("lili","dog")
print(res)

image

8.3.2 让实参变成可选的

  • 这个值可赋值,可不赋值
  • 给实参指定一个默认值,为空字符串,并将其移到形参的末尾
def user_pet(username,petname1,petname2=""):
    if petname2:
        u_p=username+' '+petname1+' '+petname2
    else:
        u_p=username+' '+petname1
    return u_p.title()
res=user_pet("lili","dog")
print(res)
res1=user_pet("lili","dog","cat")
print(res1)

image

8.3.3 返回字典

  • 函数的返回值为字典类型
def user_pet(username,petname1,petname2=""):
    u_p={'username':username,'petname1':petname1}
    if petname2: #如果u_p存在,那么将其添加到字典中
        u_p['petname2']=petname2
    return u_p
res=user_pet("lili","dog")
print(res)
res1=user_pet("lili","dog","cat")
print(res1)

8.3.4 结合使用函数和while循环

  • 主要用来判断输入是否是退出值,如果是,就退出循环
#核心代码
while True:
  if name='q':
    break

8.4 传递列表

  • 将列表作为参数传递给函数,函数能直接访问其内容
def user(names):
    for name in names:
        print(name)
names=["lili","xixi","dada"]
user(names)

image

8.4.1 在函数中修改列表

  • 将列表传递给函数之后,函数就可以对其进行修改。
  • 函数中对列表所做的任何修改都是永久的
  • 每个函数都应只负责一项具体工作

8.4.1 禁止函数修改列表

  • 本质就是将列表的副本传递给函数
function_name(list_name[:])
  • 输出也可以输出副本
print_models(a[:],b)

8.5 传递任意数量的实参

  • 利用 *names 创建一个名names的空元组
  • 可以包含任意个数值
def user(*names):
    print(names)
user("lili","xixi","dada")

image

8.5.1 结合使用位置实参和任意数量实参

def make_pizza(size,*toppings)

8.5.2 使用任意数量的关键字实参

  • 用**name 创建一个名为name的空字典,并将收到的所有名称-值都封装到字典中
def build(first,last,**user_info):
    message={}
    message['f_name']=first
    message['l_name']=last
    for key,value in user_info.items():
        message[key]=value
    return message

res=build("li","ni",location="beijing",field="machine learning")

print(res)

image

8.6 将函数存储在模块中

8.6.1 导入整个模块

  • module_name.function_name()
  • module:文件名.py 文件名=module
  • function_name:方法名

8.6.2 导入特定的韩素华

  • from module_name import function_name
  • from module_name import function_0,function_1,function_2

8.6.3 使用as给函数指定别名

from pizza import make_pizza as mp

8.6.4 使用as给模块指定别名

import pizza as p 

8.6.5 导入模块中的所有函数

from pizza import *

最佳的做法是,要么只导入你需要使用的函数,要么导入整个模块并使用句号表示法

牛客刷题(76-93)

1.列表的最值运算(76)

image

a=list(map(int,input().split())) #要转为整形
print(max(a))
print(min(a))

2.常见的函数

  • 求列表最大值:max(a)
  • 求列表最小值:min(a)
  • 求绝对值:abs(a)
  • 字母转为ASCII码:ord(a)
  • 十进制转为十六进制:hex(a)
  • 十进制转为二进制形式:bin(a)
  • 幂运算:pow(x,y)
  • 计算列表中的0的个数:a.count(0)
  • 输出某个元素的位置:a.index('NiuNiu')
  • 判断字符是否是字母:a.isalpha()
  • 判断字符是否是数字:a.isdigit()
  • 判断字符是否是空格:a.isspace()
  • 输出字符第一次出现的位置,没有则输出-1:a.find(元素)
  • 替换字符中的某段字符串:a.replace('a*','ab')
  • 小数位修正:round(a,位数)
  • 以字符串的形式输入公式,包括加减乘幂四种运算,数字都是整数,有正有负有零,就能输出结果:eval(a)

3. 字符子串的查找(86)

image

a=input()
print(a.find('NiuNiu'))

4. 单词造句(89)

image

b=[]
while True:
   a=input()
   if a=='0': #0是字符串
       break
   else:
       b.append(a)
for i in range(len(b)):
    print(b[i],end=" ")
posted @ 2022-11-15 12:03  Trouvaille_fighting  阅读(23)  评论(0)    收藏  举报