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 ...
随机推荐
- 云栖社区用机器人爬CSDN的文章?
这个云栖社区的文章https://yq.aliyun.com/ziliao/539322 这篇文章是我13年写的,不知道咋插入图片,见谅. 下面是我的文件记录 分享XAML图标的网站 原创 2013年 ...
- cocos2dx模拟器修改窗口大小
修改模拟器窗口大小SimulatorWin.cpp搜索 frameSize修改frameSize = Size(1920*0.9, 1080*0.9);
- 搭建jenkins实现自动化部署
搭建jenkins实现自动化部署 一.安装jenkins 1.添加yum repos,然后安装 sudo wget -O /etc/yum.repos.d/jenkins.repo https://p ...
- git 服务器 LINUX端的使用
首先,需要建立一个git服务器----- 这里介绍如何使用git这个服务器 我们在github上下载一份代码,里面有如下内容 我们使用git服务器的时候不能有.git 文件,所以在此将其删除 ys-l ...
- selenium+python自动化测试,上传文件怎样实现
其实上传图片操作与输入框的输入是一样的,一种是在输入框中输入字符信息,一种是在输入文件格式的信息,下面是用代码实现上传文件, from selenium import webdriverfrom ti ...
- JpaManytoMany
package com.allqj.calculator.entity; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; i ...
- ef core自动映射
原回答:https://stackoverflow.com/questions/26957519/ef-core-mapping-entitytypeconfiguration 一.反射 protec ...
- [转] 初探webpack4
一.前言 2018/2/25,webpack4正式发布,距离现在已经过去三个多月了,也逐渐趋于稳定,而且现在的最新版本都到了4.12.0(版本迭代快得真是让人害怕). 很多人都说webpack复杂,难 ...
- mysql 和 sqlserver sql差异比较
mysql:select * from table_name limit 100,200;--取出从100到200的数据 获取时间:mysql:now() mysql tinyint(0,1) → b ...
- RabbitMQ 消息确认机制以及lazy queue+ disk消息持久化
一:Basic的一些属性,一些方法 1. 消费端的确认 自动确认: message出队列的时候就自动确认[broke] basicget... 手工确认: message出队列之后,要应用程序自己去确 ...