python从入门到实践-8章函数
#!/user/bin/env python
# -*- coding:utf-8 -*-
# 给形参指定默认值时,等号两边不要有空格 def function_name("parameter_0",parameter_1='default value')
# 函数形参的位置很重要 传递参数使用关键字实参(一一对应的传递,可以不用理会顺序)
# 默认值传递时候要指定传递(可以对应位置传递)
# 返回值return 默认函数已经结束了
def get_formatted_name(frist_name,last_name,middle_name=''):
if middle_name:
full_name = frist_name + ' ' + middle_name + ' ' + last_name
else:
full_name = frist_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi','hendrix')
print(musician)
musician = get_formatted_name('jimi','li','men')
print(musician)
# 返回字典
def build_person(frist_name, last_name):
person = {'frist': frist_name, 'last': last_name}
return person
musician = build_person('jimi','hendrix')
print(musician)
# 结合while写函数
# 向函数传递列表 for循环提取
def greet_user(names):
for name in names:
msg = 'hello ' + name.title()
print(msg)
user_names = ['hannah','ty','margot']
greet_user(user_names)
# 函数中修改列表就是调用列表方法修改
'''【遇到禁止修改源文件的列表,就要用[:]创建一个副本进行修改】'''
# 传递任意数量的实参用: *
def make_pizza(size, *topings):
print("\nMaking a " + str(size) + "-inch pizza with following toppings")
for toping in topings:
print("- " + toping)
make_pizza(16, 'pepperoni')
make_pizza(12,'mushrooms', 'green peppers')
# 传递任意数量的关键字参数
def build_proflie(frist, last, **user_info):
profile = {}
profile['frist_name'] = frist
profile['last_name'] = last
for key,value in user_info.items():
profile[key] = value
return profile
user_profile = build_proflie('albert','einstein',
location='princeton',
field='physics')
print(user_profile)
# 导入模块 每个py文件都可以是模块
# import 模块
# from 模块 import 函数
# from 模块 import 函数 as 另一个名字
# import 模块 as 另一个名字
# from 模块 import * 导入模块中所有函数
# 所有import都要放在开头,除非在文件开头使用了注释性语言来描述整个程序
python从入门到实践-8章函数的更多相关文章
- Python:从入门到实践--第八章-函数-练习
#.消息:编写一个名为display_message()的函数,它打印一个句子,指出你在本章学的是什么. #调用这个函数,确认显示的消息无误 def display_message(name): pr ...
- python从入门到实践-9章类
#!/user/bin/env python# -*- coding:utf-8 -*- # 类名采用的是驼峰命名法,即将类名中每个单词的首字母大写,而不使用下划线.# 对于每个类,都应紧跟在类定义后 ...
- Python:从入门到实践--第九章-类--练习
#.餐馆:创建一个名为Restaurant的类,其方法_init_()设置两个属性:restaurant_name和cuisine_type. #创建一个名为describe_restaurant的方 ...
- python从入门到实践 第二章
python变量赋值: python的变量赋值 可以是单引号 也可以是双引号python 变量赋值的时候不能加()的 比如 name = "My Name is GF"变量赋值的时 ...
- python从入门到实践-11章测试模块(测试函数出问题)
#!/user/bin/env python# -*- coding:utf-8 -*- # 用python中unittes中工具来测试代码 # 1.测试函数import unittestfrom n ...
- python从入门到实践-10章文件和异常(括号问题)
#!/user/bin/env python# -*- coding:utf-8 -*- # 1.从文件中读取数据with open('pi_digits.txt') as file_object: ...
- python从入门到实践-7章用户输入和while循环
#!/user/bin/env python# -*- coding:utf-8 -*- # input() 可以让程序暂停工作# int(input('please input something: ...
- python从入门到实践-6章字典
#!/user/bin/env python# -*- coding:utf-8 -*- # 前面不用空格,后面空格# 访问只能通过keyalien_0 = {'color': 'green', 'p ...
- python从入门到实践-5章if语句
#!/user/bin/env python cars = ['audi','bmw','subaru','toyota']for car in cars: if car == 'bmw': prin ...
随机推荐
- jmeter测试报告分析
转载:http://www.cnblogs.com/miaomiaokaixin/p/6118081.html 在cmd中用命令行执行jmeter脚本: jmeter地址 -n -t 脚本地址 ...
- Python爬虫基础之正则表达式
一.Python正则表达式的基本使用 Python 3 使用re模块可以实现大部分的正则表达式情况. 1.re.compile(pattern, flags=0) re.compile构建匹配规则并返 ...
- h1-h3使用
一个页面也就只允许出现一个h1标签.内容页文章的标题,是seo中使用最多的地方,基本的文章页面标题都是使用h1标签.一.<h1>用来修饰网页的主标题,一般是网页的标题 ,文章标题,< ...
- 公设基础Generic
1# 与泛型相关的一些术语 1.类型参数(type parameter) : EX: List<E> 这里的E就属于List接口的单个类型参数E 2.参数化的类型(parameterize ...
- [原创]Zynq SDIO WIFI SotfAP调试
编译好kernel和driver 加载firmware后,运行下述命令. mkdir /var/run/ mkdir /var/run/hostapd ifconfig -a ...
- RabbitMQ 消息确认机制以及lazy queue+ disk消息持久化
一:Basic的一些属性,一些方法 1. 消费端的确认 自动确认: message出队列的时候就自动确认[broke] basicget... 手工确认: message出队列之后,要应用程序自己去确 ...
- Gradle 下载的依赖包在什么位置?
Mac系统默认下载到:/Users/(用户名)/.gradle/caches/modules-2/files-2.1Windows系统默认下载到:C:\Users\(用户名)\.gradle\cach ...
- Github 上怎样把新 commits 使用在自己的 fork 上
作者:黄晓佳 链接:https://www.zhihu.com/question/20393785/answer/105370502 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...
- haproxy5-ssl
配置实例: https://andyleonard.com/2011/02/01/haproxy-and-keepalived-example-configuration 配置haproxy支持htt ...
- jquery.string.js
/** * jquery.string - Prototype string functions for jQuery * version: 1.1.0 * (c) 2008-2011 David E ...