Python编程:从入门到实践第八章函数(function)笔记

函数

  • 命名-语义化
  • 形参和实参
  • 函数实参
    • 位置实参,顺序严格
    • 关键字实参,键值对一一对应,顺序无关
    • 默认参数,传参则覆盖
    • 多种结合
    • 数量匹配
  • 返回值
    • 让实参变成可选的,默认传一个空字符串,默认参数再非默认参数之后
    • 返回字典
  • 传递列表
    • 修改列表,传入列表本身
    • 切片法传入副本,不对原列表修改
  • 传入任意数量的实参
    • 使用任何数量的位置实参,定义空元组*parameters将收集的数据封装到元组中
    • 使用任意数量的关键字实参,定义空字典**parameters将收集到的参数封装到字典中
  • 将函数存储到模块中
    • 导入模块 import
    • 导入函数 from import
    • 别名 as
    • 导入所有函数 from import *

8.1.0 函数定义

def print_message():
    print("我最牛逼")
print_message()

8.1.1 函数传参

def printName(name):
    print(name)
printName('libinglin')
# libinglin

8.2.1 位置实参,按位序传值

def display_info(name,age,weight):
    print(name+'的年纪是'+str(age)+',体重是'+str(weight)+"千克!")
display_info('libinglin',22,74)
#libinglin的年纪是22,体重是74千克!

8.2.2 关键字形参,位置无关

def display_info(name,age,weight):
    print(name+'的年纪是'+str(age)+',体重是'+str(weight)+"千克!")
display_info(weight=74,age=22,name='libinglin')
#libinglin的年纪是22,体重是74千克!

8.2.2 默认参数,不传值也有输出

#先列出没有默认值的weight,再列出有默认值的age和name
def display_info(weight,age=22,name='libinglin'):
    print(name+'的年纪是'+str(age)+',体重是'+str(weight)+"千克!")

#调用函数,传没有默认值的参数
display_info(75)

# libinglin的年纪是22,体重是74千克!
```python
#调用函数传参数,覆盖默认参数
display_info(100,40,'詹姆斯')
# 詹姆斯的年纪是40,体重是100千克!

8.2.3 动手做-T恤

def make_shirt(size,word):
    print('the size of T-shirt is '+str(size).upper()+',the word demo is '+word)
#位置实参
make_shirt('xxl','peace and love')
# the size of T-shirt is XXL,the word demo is peace and love
#关键字实参
make_shirt(word='peace and love',size = 'xl')
# the size of T-shirt is XL,the word demo is peace and love

8.2.4 动手做-大号T恤

def make_shirt(size='xxl',word='I love python'):
    print('the size of T-shirt is '+str(size).upper()+',the word demo is '+word.title())

#默认
make_shirt()
# the size of T-shirt is XXL,the word demo is I Love Python

#默认字样,中号
make_shirt('xl')
# the size of T-shirt is XL,the word demo is I Love Python

#非默认
make_shirt('xl','peace and love')
# the size of T-shirt is XL,the word demo is Peace And Love

8.3.1 返回值-简单值

def concat(word1,word2):
    return word1.title()+word2.title()
concat('li','binglin')
#'LiBinglin'

8.3.2 可选实参

def concat(word1,word2,word3=""):
    if word3:#如果word不为空
        return (word1+word2+word3).title()
    else:
        return word1.title()+word2.title()
concat('li','bing','lin')#'Libinglin'
concat('luo','lei')#'LuoLei'

8.3.3 返回字典

person = {}
def redict(name,gender):
    person[name] = gender#对字典的操作是永久的
redict('libinglin','male')
redict('zhoujielun','male')
redict('shiwaxingge','male')
for name,gender in person.items():
    print(name+" is "+gender)
# libinglin is male
# zhoujielun is male
# shiwaxingge is male

8.3.4 构建专辑字典函数

def make_album(name,album,count=1):
    albums = {}
    albums['name'] = name
    albums[album] = album
    albums['count'] = count
    return albums
album1 = make_album('泰勒斯威夫特','1989',11)
print(album1)
album2 = make_album('林俊杰','江南',2)
print(album2)
album3 = make_album('周杰伦','mojito')
print(album3)
# {'name': '泰勒斯威夫特', '1989': '1989', 'count': 11}
# {'name': '林俊杰', '江南': '江南', 'count': 2}
# {'name': '周杰伦', 'mojito': 'mojito', 'count': 1}

8.4.1 音乐人

songers = ['周杰伦','林俊杰','郭富城','刘德华']
def printSonger(s):
    for a in s:
        print(a)
printSonger(songers)
# 周杰伦
# 林俊杰
# 郭富城
# 刘德华
def make_great(s):
    for i in range(0,len(s)):
        s[i] = 'The great ' + s[i]

# 传入副本
make_great(songers[:])
printSonger(songers)
# 周杰伦
# 林俊杰
# 郭富城
# 刘德华

#传入本身
make_great(songers)
printSonger(songers)
# The great 周杰伦
# The great 林俊杰
# The great 郭富城
# The great 刘德华

8.5.1 传递任意数量的实参

#*con代表传入的是元组,用圆括号定义,访参考列表
def make_pizza(*con):
    # 遍历参数
    for a in con:
        print(a)
make_pizza('apple')
# apple

8.5.2结合位置实参和任意实参

def get_name(first_name,*last_name):
    full_name = first_name
    for name in last_name:
        full_name = full_name + name
    print(full_name)
get_name('周','杰','伦')
# 周杰伦

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

# **user_info是字典
def build_profile(first,last,**user_info):
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key,value in user_info.items():
        profile[key] = value
    return profile

profile_zhou = build_profile('周','杰伦',weight='80kg',
    height=180,
    address='taiwan')
for key,value in profile_zhou.items():
    print(key+":"+str(value))
# first_name:周
# last_name:杰伦
# weight:80kg
# height:180
# address:taiwan

8.5.4 顾客三明治添加食材

#调用三次,每次添加不同数量的食材
def add_material(*adds):
    print("顾客添加了:"+str(adds))
add_material('beef','egg','fruit')
add_material('apple')
# 顾客添加了:('beef', 'egg', 'fruit')
# 顾客添加了:('apple',)

8.5.5 cars

def make_car(manufacturer,type,**other_info):
    cars = {}
    cars['manufacturer'] = manufacturer
    cars['type'] = type
    for key,value in other_info.items():
        cars[key] = value
    return cars
car = make_car('subaru','outback',color='blue',max_speed='180',weight='18000kg')
for key,value in car.items():
    print(key+":"+str(value))
# manufacturer:subaru
# type:outback
# color:blue
# max_speed:180
# weight:18000kg

8.6 导入模块

* 导入整个模块
* - import module_name
# 导入特定函数
* - from module_name import function_name
* - from module_name import function_1,function_2,...
# 使用as指定函数别名
* - from module_name import function_name as funN
# 使用as指定模块别名
* - import module_name as moln
# 导入模块所有函数
* - from module_name import *

8.7 函数编写指南

  • 命名-语义化
  • 给形参指定默认值时,等号两边不要有空格
    • def function_name(parameter_0,parameter_1=‘defaule_value’)
  • -对于调用的关键字形参也同样遵守这种规则
  • 缩进
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值