python编程:从入门到实践 第八章知识汇总 + 习题8-1~8-17
- 如何编写函数,以及如何传递实参,让函数能够访问完成其工作所需的信息;
- 如何使用位置实参和关键字实参,以及如何接受任意数量的实参;
- 显示输出的函数和返回值的函数;
- 如何将函数同列表、字典、if 语句和while 循环结合起来使用。
- 如何将函数存储在被称为模块模 的独立文件中,让程序文件更简单、更易于理解;
- 学习了函数编写指南。
def display_message():print(1+1)
display_message()
2Process finished with exit code 0
def favorite_book(title):print("One of my favorite books is " + title.title() + ".")
favorite_book("TOM and JEnny")
One of my favorite books is Tom And Jenny.Process finished with exit code 0
def make_shirt(size, words):print("\nI would like to have a " + size + " T-shirt, which has words like " + words + '.')make_shirt('XL', 'Hello World')
make_shirt(words='I LOVE YOU', size='L')
I would like to have a XL T-shirt, which has words like Hello World.I would like to have a L T-shirt, which has words like I LOVE YOU.Process finished with exit code 0
def make_shirt(size='L', words='I love Python'):print("\nI would like to have a " + size + " T-shirt, which has words like " + words + '.')make_shirt()
make_shirt('M')
make_shirt(words='ABCDEFG')
I would like to have a L T-shirt, which has words like I love Python.I would like to have a M T-shirt, which has words like I love Python.I would like to have a L T-shirt, which has words like ABCDEFG.Process finished with exit code 0
def describe_city(CityName, Contry='China'):print(CityName + " is in " + Contry + ".")describe_city('Shanghai')
describe_city('London', 'England')
describe_city('Guangzhou')
Shanghai is in China.
London is in England.
Guangzhou is in China.Process finished with exit code 0
"Santiago, Chile"
def city_country(city, contry):full_infor = city + ', ' + contryreturn full_infor.title()infor = city_country('santiago', 'chile')
print(infor)
infor = city_country('shanghai', 'china')
print(infor)
infor = city_country('chognqing', 'china')
print(infor)
Santiago, Chile
Shanghai, China
Chognqing, ChinaProcess finished with exit code 0
def make_album(singer, album):dict_album = {'signer': singer,'album': album}return dict_albuminformation = make_album('zhuli', 'FULL world')
print(information)
information = make_album('sisi', 'a little bird')
print(information)
information = make_album('rose', 'Fly away')
print(information)
{'signer': 'zhuli', 'album': 'FULL world'}
{'signer': 'sisi', 'album': 'a little bird'}
{'signer': 'rose', 'album': 'Fly away'}Process finished with exit code 0
def make_album(singer, album, song_number=''):dict_album = {'signer': singer,'album': album}if song_number:dict_album['song_number'] = song_numberreturn dict_albuminformation = make_album('zhuli', 'FULL world')
print(information)
information = make_album('sisi', 'a little bird', 5)
print(information)
information = make_album('rose', 'Fly away',song_number=7)
print(information)
{'signer': 'zhuli', 'album': 'FULL world'}
{'signer': 'sisi', 'album': 'a little bird', 'song_number': 5}
{'signer': 'rose', 'album': 'Fly away', 'song_number': 7}Process finished with exit code 0
def make_album(singer, album, song_number=''):dict_album = {'signer': singer,'album': album}if song_number:dict_album['song_number'] = song_numberreturn dict_albumwhile True:print("Welcome to join in the game~")print("(enter 'q' at any time to quit)")name = input("\nPlease enter a singer name: ")if name == 'q':breakalbum = input("Please enter " + name.title() + "`s album name: ")if album == 'q':breakoutput = make_album(name, album)print(output)
Welcome to join in the game~
(enter 'q' at any time to quit)Please enter a singer name: Tom
Please enter Tom`s album name: July
{'signer': 'Tom', 'album': 'July'}
Welcome to join in the game~
(enter 'q' at any time to quit)Please enter a singer name: qProcess finished with exit code 0
def show_magicians(names):for name in names:print(name.title())magicians = ['Tom', 'JHON', 'ROSE', 'TONY', 'Alice']
show_magicians(magicians)
Tom
Jhon
Rose
Tony
AliceProcess finished with exit code 0
def show_magicians(names):for name in names:print(name.title())def make_great(repeat_name):great_magicians = []while magicians:magician = magicians.pop()great_magician = magician + ' the Great'great_magicians.append(great_magician)for great_magician in great_magicians:magicians.append(great_magician)magicians = ['Tom', 'JHON', 'ROSE', 'TONY', 'Alice']
show_magicians(magicians)print('\n')
make_great(magicians)
show_magicians(magicians)
Tom
Jhon
Rose
Tony
AliceAlice The Great
Tony The Great
Rose The Great
Jhon The Great
Tom The GreatProcess finished with exit code 0
# 创建show_magicians 函数,并打印所有 魔术师的名字。
def show_magicians(magicians):for magician in magicians:print(magician.title())# 创建make_great函数def make_magician(magicians):# 将the great 加入每个魔术师的名字后面# 创建一个新的函数,放 zzz+ the greatgreat_magicians = []# 让每个名字变成 magician+ the greatwhile magicians:magician = magicians.pop()great_magician = magician + ' the Great'great_magicians.append(great_magician)# 将great_magicians 放入 magicians中for great_magician in great_magicians:magicians.append(great_magician)return magiciansmagicians = ['Tom', 'JHON', 'ROSE', 'TONY', 'Alice']
show_magicians(magicians)print("\nGreat magicians: ")
great_magicians = make_magician(magicians[:])
show_magicians(great_magicians)print("\nOriginal magicians: ")
show_magicians(magicians)
Tom
Jhon
Rose
Tony
AliceGreat magicians:
Alice The Great
Tony The Great
Rose The Great
Jhon The Great
Tom The GreatOriginal magicians:
Tom
Jhon
Rose
Tony
AliceProcess finished with exit code 0
# 方法一:
def add_food(foods):"""打印一条消息"""print("\nYou would like to order " + foods + " sandwich.")add_food('orange')
add_food('apple')
add_food('banana')# 方法二:
def add_food(*toppings):"""打印一条消息"""print("\nMaking a sandwich with the following toppings: ")for topping in toppings:print("-" + topping)add_food('orange', 'banana', 'apple')
You would like to order orange sandwich.You would like to order apple sandwich.You would like to order banana sandwich.Making a sandwich with the following toppings:
-orange
-banana
-appleProcess finished with exit code 0
def build_profile(first, last, **user_info):profile = {'first_name': first, 'last_name': last}for k, v in user_info.items():profile[k] = vreturnuser_profile = build_profile('Jin', 'Ho', age='18', weight='45kg')
print(user_profile)
{'first_name': 'Jin', 'last name': 'Ho', 'age': '18', 'weight': '45kg'}Process finished with exit code 0
car = make_car('subaru', 'outback', color='blue', tow_package=True)
打印返回的字典,确认正确地处理了所有的信息。
def make_car(company, size, **cars_info):car_profile = {'car_company': company, 'car_size': size}for k, v in cars_info.items():car_profile[k] = vreturn car_profilecar = make_car('subaru', 'outback', color='blue', tow_package='pink')
print(car)
{'car_company': 'subaru', 'car_size': 'outback', 'color': 'blue', 'tow_package': 'pink'}Process finished with exit code 0
import module_name
from module_name import fn
function_name
from module_name
import function_name as fn
import module_name as mn
from module_name import *
python编程:从入门到实践 第八章知识汇总 + 习题8-1~8-17相关推荐
- Python编程从入门到实践第五章部分习题
Python编程从入门到实践第五章部分习题 5-8 5-9` names = ['admin','zhang','li','zhao','song'] for name in names:if nam ...
- python编程从入门到实践第八章_Python编程从入门到实践的第三天
#-*- coding = utf-8 -*- #今天是12月24号了,天气阴,不是太好,这是我看Python编程从入门到实践的第三天,现在是上午,我是皮卡丘,这是我敲的第八章的代码 #第八章练习题1 ...
- Python编程:从入门到实践(基础知识)
第一章 起步 计算机执行源程序的两种方式: 编译:一次性执行源代码,生成目标代码 解释:随时需要执行源代码 源代码:采用某种编程语言编写的计算机程序 目标代码:计算机可执行,101010 编程语言分为 ...
- Python编程-从入门到实践第15章课后习题详解
第15章 使用Plotly模拟掷骰子--课后习题答案 练习15-6 #掷两个D8 from plotly.graph_objs import Bar,Layout from plotly import ...
- Python编程从入门到实践(第三、四章的列表和元祖)
1.Python中列表用[]来表示,并用逗号分隔其中元素 2.访问列表元素,给出元素的索引值即可(索引从0开始) 3.修改,添加和删除元素 3.1修改时给出列表名和修改元素的索引,然后赋新值 3.2在 ...
- python编程从入门到实践第九章——类
相关文章链接: python编程从入门到实践第二章--变量和简单数据类型 python编程从入门到实践第三章--列表简介 python编程从入门到实践第四章--操作列表 python编程从入门到实践第 ...
- Python编程从入门到实践(第二版)课后习题自写代码
Python编程从入门到实践(第二版)课后习题自写代码 第八章 函数 最近自学的python,动手做了一下课后习题,错误也许会有,和大家一起探讨.多多指教! 8.3 返回值 动手试一试代码片 &quo ...
- python编程 从入门到实践怎么样-python编程从入门到实践这本书怎么样
<Python编程-从入门到实践>作者: Eric Matthes,已翻译为中文,人民邮电出版社出版. python编程从入门到实践怎么样? 我们一起看看已经学习的同学对这本书的口碑和评价 ...
- python编程入门指南怎么样-python编程从入门到实践这本书怎么样
<Python编程-从入门到实践>作者: Eric Matthes,已翻译为中文,人民邮电出版社出版. python编程从入门到实践怎么样? 我们一起看看已经学习的同学对这本书的口碑和评价 ...
最新文章
- ESP32检测调制激光信号程序优化
- 糍粑大叔的独游之旅-开篇语
- This function or variable may be unsafe
- nodejs写html文件路径,Nodejs读取文件时相对路径的正确写法(使用fs模块)
- P4201-[NOI2008]设计路线【结论,树形dp】
- 屌丝笔记本玩Windows Phone 8开发(在Windows Server 2012中安装WP8 SDK)
- win下修改anaconda的jupyter notebook默认打开路径
- BigDecimal.divide方法
- 一个可以免费下载数据集的网站
- Studio3T安装
- 最基础内网ip地址配置
- 苹果手机数据线正确鉴定方法
- (3)Pairing Functions Element Functions
- WebDriverPool浏览器驱动池 减少驱动频繁打开和关闭引起的资源损耗
- 论文研究范围从什么角度怎么写?
- 嵌入式工资为什么比纯软工资低那么多?
- C++库常用函数一览表
- vue 3.0 即将发布,敬请期待
- Python|为什么列表推导式会更快
- linux技术咨询,Linux技术咨询委员会已完成对UMN内核漏洞植入事件的调查
热门文章
- Eclipse配置SSM框架(非maven模式)
- c#线程中的属性isbackground
- 轻量级的PHP在线考试系统源码+实测可用
- 无理取闹,我Release要你pdb文件做啥
- 龙芯服务器稳定性,真实现状!国产龙芯水平究竟如何(续)
- Qt——绘制瀑布图/热度图
- Seata-AT如何保证分布式事务一致性
- Academic Phrasebank 2021《学术短语库 2021在线版 英译汉》
- c语言错误的常量表达式,C struct object Stack – 在常量表达式中不允许函数调用(错误)...
- C语言学习入门篇(1.1)