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 ...
随机推荐
- Tomcat使用shutdown.bat关闭会将其他Tomcat关掉的问题
Tomcat使用shutdown.bat关闭会将其他Tomcat关掉的问题 shutdown.bat文件有一句if not "%CATALINA_HOME%" == "& ...
- Eclipse 配置Tomcat 服务器
第一部分:eclipse环境下如何配置tomcat 1.下载并成功安装Eclipse和Tomcat 2.打开Eclipse,单击“window”菜单,选择下方的“Preferences” . 选择好自 ...
- sqlserver 获取汉字拼音的首字母(大写)函数
1:创建函数: USE [test] GO /****** 对象: UserDefinedFunction [dbo].[GetFirstChar] 脚本日期: 02/22/2019 16:39:06 ...
- ubuntu系统的teamviewer的安装及使用
参考链接: 安装: https://blog.csdn.net/weixin_34613450/article/details/80541799 使用: https://jingyan.baidu.c ...
- mui 记录
1.轮播添加无限循环 需要在 .mui-slider-group节点上增加.mui-slider-loop类 2.web移动端侧滑与滑动同时存在 参考https://segmentfault.com/ ...
- 基于物品的协同过滤item-CF 之电影推荐 python
推荐算法有基于协同的Collaboration Filtering:包括 user Based和item Based:基于内容 : Content Based 协同过滤包括基于物品的协同过滤和基于用户 ...
- neutron--ml2 plugin
ml2 plugin 对 plugin 的功能进行抽象和封装,有 ml2 plugin ,各种 network 无需开发自己的 plugin,只需开发 ml2 plugin 相对应的 driver , ...
- Bootstrap-datepicker3官方文档中文翻译---Keyboard support/键盘支持(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
本日期控件包含了键盘导航. “focused date” 在键盘导航期间一直会被保持追踪并且高亮显示(就想鼠标悬停的时候一样),当一个日期被切换(译者注:选中状态的切换)时或者控件隐藏时清除. up ...
- Bootstrap-datepicker3官方文档中文翻译---Event/事件(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
Events/事件 DatePicker在某些情况下触发一些事件.所有事件都拥有 传递给任何事件处理程序的 事件对象的 附加数据.(译者注:这里英语拗口,汉语也拗口,我用空格给大家断断句) ...
- SpringBoot动态配置加载
1.SpringBoot对配置文件集中化进行管理,方便进行管理,也可以使用HttpClient进行对远程的配置文件进行获取. 创建一个类实现EnvironmentPostProcessor 接口,然后 ...