Python学习日志-7


今天分享的是第八章的要点和部分课后习题的参考代码。

要点:

1、函数的定义与使用.

2、位置实参和关键字实参.

3、为参数提供默认值.

4、有返回值的函数.

5、可选实参的用法.

6、用切片表示法[ : ]防止列表作为参数时被修改.

7、传递任意数量的实参.

8、模块与模块特点函数的导入.


参考代码:

8-1 

def display_message():
	print("We will learn how to use function this chapter")

display_message()

运行结果:

def display_message():
	print("We will learn how to use function this chapter")

display_message()

8-2

def favorite_book(title):
	print("One of my favorite boks is" + title.title() +".")

favorite_book("Alice in Wonderland")

运行结果:

One of my favorite boks isAlice In Wonderland.
[Finished in 0.0s]

8-3略(8-4的简单版)

8-4

def make_shirt(logo = "I love Python" , size = "L"):
	print("The logo is " + logo + ".")
	print("The size is " + size + ".")

make_shirt()

运行结果:

The logo is I love Python.
The size is L.
[Finished in 0.0s]

8-5略

8-6

def city_country(city, country):
	return city + ', ' + country

print(city_country("Guangzhou", "China"))
print(city_country("New York", "America"))
print(city_country("Tokyo", "Japan"))

运行结果:

Guangzhou, China
New York, America
Tokyo, Japan
[Finished in 0.0s]

8-7略

8-8略

8-9

def show_magicians(magicians):
	for magician in magicians:
		print(magician)

show_magicians(["liu Qian", "Jack", "Tom"])

运行结果:

liu Qian
Jack
Tom
[Finished in 0.0s]

8-10略

8-11

def show_magicians(magicians):
	for magician in magicians:
		print(magician)

def make_great(magicians):
	ms = []
	for magician in magicians:
		magician = "the Great " + magician
		ms.append(magician)
	return  ms

old_magicians = ["liu Qian", "Jack", "Tom"]
new_magicians = make_great(old_magicians[:])

show_magicians(old_magicians)
print()
show_magicians(new_magicians)

运行结果:

liu Qian
Jack
Tom

the Great liu Qian
the Great Jack
the Great Tom
[Finished in 0.0s]

8-12

def add_toppings(*toppings):
	print("You add these thing in you sandwich:" )
	for topping in toppings:
		print(topping)
	print()

add_toppings("egg")
add_toppings("apple","fish")
add_toppings("beef","chicken","pork")

运行结果:

You add these thing in you sandwich:
egg

You add these thing in you sandwich:
apple
fish

You add these thing in you sandwich:
beef
chicken
pork

[Finished in 0.0s]

8-13略

8-14

def make_car(grand, model, **other):
	car = {}
	car['grand'] = grand
	car['model'] = model
	for key, value in other.items():
		car[key] = value
	return car

print(make_car('subaru', 'outback', color='blue', tow_package=True))
print(make_car('BMW', 'R', color='white', tow_package=True))
print(make_car('Benz', 's', color='red', tow_package=True))

运行结果:

{'grand': 'subaru', 'model': 'outback', 'color': 'blue', 'tow_package': True}
{'grand': 'BMW', 'model': 'R', 'color': 'white', 'tow_package': True}
{'grand': 'Benz', 'model': 's', 'color': 'red', 'tow_package': True}
[Finished in 0.0s]

8-15

#test1.py
def print1():
	print("This is 8-15")
#test1.py
def print1():
	print("This is 8-15")

运行结果:

This is 8-15
[Finished in 0.0s]

8-16略

8-17略


智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告