Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1bedf6f

Browse files
author
VincentJ
committed
[C]增加了关于函数的一些使用--定义、传参、返回值、引用等.
Signed-off-by: VincentJ <rogue1990@163.com>
1 parent 40fa31b commit 1bedf6f

File tree

7 files changed

+216
-0
lines changed

7 files changed

+216
-0
lines changed

‎chapter7/function.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 函数
2+
def greet_user():
3+
# 文档字符串 ( docstring) 的注释, 描述了函数是做什么的。 文档字符串用三引号括
4+
# 起, Python使用它们来生成有关程序中函数的文档
5+
"""显示简单的问候语"""
6+
print("Hello Python")
7+
8+
9+
greet_user()
10+
11+
12+
# 带形参的函数
13+
def say_hello_2_someone(user_name):
14+
"""传入名字"""
15+
print('Hello ' + user_name)
16+
17+
18+
say_hello_2_someone("John")
19+
20+
21+
# 最简单的关联方式是基于实参的顺序。 这种关联方式被称为位置实参
22+
def describe_pet(animal_type, pet_name):
23+
"""显示宠物的信息"""
24+
print("\nI have a " + animal_type + ".")
25+
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
26+
27+
28+
describe_pet('hamster', 'harry')
29+
# 可以多次调用
30+
describe_pet('dog', 'willie')
31+
32+
# 关键字实参 --是传递给函数的名称—值对
33+
print("关键字实参")
34+
describe_pet(animal_type='hamster', pet_name='harry')
35+
describe_pet(pet_name='harry', animal_type='hamster')
36+
37+
38+
# 编写函数时, 可给每个形参指定默认值
39+
# 使用默认值时, 在形参列表中必须先列出没有默认值的形参, 再列出有默认值的实参。 这让Python依然能够正确地解读位置实参。
40+
def describe_pet(pet_name, animal_type='dog'):
41+
"""显示宠物的信息"""
42+
print("\nI have a " + animal_type + ".")
43+
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
44+
45+
46+
describe_pet(pet_name='willie')

‎chapter7/function_return.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 函数返回值
2+
# 返回简单值
3+
def get_formatted_name(first_name, last_name):
4+
full_name = first_name + ' ' + last_name
5+
return full_name.title()
6+
7+
8+
musician = get_formatted_name('jimi', 'hendrix')
9+
print(musician)
10+
11+
12+
# 让实参变成可选的---中间使用''默认实现
13+
def get_formatted_name(first_name, last_name, middle_name=''):
14+
"""返回整洁的姓名"""
15+
if middle_name:
16+
full_name = first_name + ' ' + middle_name + ' ' + last_name
17+
else:
18+
full_name = first_name + ' ' + last_name
19+
return full_name.title()
20+
21+
22+
musician = get_formatted_name('jimi', 'hendrix')
23+
print(musician)
24+
musician = get_formatted_name('john', 'hooker', 'lee')
25+
print(musician)
26+
27+
28+
# 返回字典
29+
def build_person(first_name, last_name):
30+
"""返回一个字典, 其中包含有关一个人的信息"""
31+
person = {'first': first_name, 'last': last_name}
32+
return person
33+
34+
35+
musician = build_person('jimi', 'hendrix')
36+
print(musician)
37+
38+
39+
# 结合使用函数和while 循环
40+
41+
def greet(first_name, last_name):
42+
full_name = first_name + " " + last_name
43+
return full_name.title()
44+
45+
46+
while True:
47+
print("\n Please tell your name:")
48+
fir_name = input("Input your first name:")
49+
las_name = input("Input your last name:")
50+
greetStr = greet(fir_name, las_name)
51+
print("Hello," + greetStr)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def make_pizza(size, *toppings):
2+
"""概述要制作的比萨"""
3+
print("\nMaking a " + str(size) +
4+
"-inch pizza with the following toppings:")
5+
for topping in toppings:
6+
print("- " + topping)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 保存函数到模块中
2+
3+
# 1.导入整个模块
4+
5+
# import pizza
6+
# pizza.make_pizza(16, 'pepperoni')
7+
# pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
8+
9+
# 导入特定的函数
10+
# from pizza import make_pizza
11+
# make_pizza(16,'aa')
12+
13+
14+
# 使用as 给函数指定别名,同样的也有:使用as 给模块指定别名
15+
16+
# from pizza import make_pizza as do_pizza
17+
18+
# do_pizza(12, 'banana')
19+
20+
# 导入模块中的所有函数
21+
# from pizza import *

‎chapter7/tips‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
函数编写指南
2+
3+
1.给函数指定描述性名称, 且只在其中使用小写字母和下划线;
4+
2.每个函数都应包含简要地阐述其功能的注释, 该注释应紧跟在函数定义后面, 并采用文档字符串格式;
5+
3.给形参指定默认值时, 等号两边不要有空格,对于函数调用中的关键字实参,也一样适用;
6+
4.PEP 8( https://www.python.org/dev/peps/pep-0008/ ) 建议代码行的长度不要超过79字符;
7+
5.如果程序或模块包含多个函数, 可使用两个空行将相邻的函数分开, 这样将更容易知道前一个函数在什么地方结束, 下一个函数从什么地方开始。
8+
6.所有的import 语句都应放在文件开头, 唯一例外的情形是, 在文件开头使用了注释来描述整个程序。

‎chapter7/trans_any_obj.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 传递任意数量的实参--类比JDK1.5引入的不定参数
2+
# 空元组--toppings
3+
4+
5+
def make_pizza(*toppings):
6+
"""打印Pizza"""
7+
print(toppings)
8+
for topping in toppings:
9+
print('--' + topping)
10+
11+
12+
make_pizza('pepperoni')
13+
make_pizza('mushrooms', 'green peppers', 'extra cheese')
14+
15+
16+
# 结合使用位置实参和任意数量实参
17+
# 如果要让函数接受不同类型的实参, 必须在函数定义中将接纳任意数量实参的形参放在最后。
18+
19+
def make_pizza(size, *toppings):
20+
"""概述要制作的比萨"""
21+
print("\nMaking a " + str(size) +
22+
"-inch pizza with the following toppings:")
23+
for topping in toppings:
24+
print("- " + topping)
25+
26+
27+
make_pizza(16, 'pepperoni')
28+
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
29+
30+
31+
# 使用任意数量的关键字实参
32+
# **info--空字典
33+
def build_profile(first, last, **info):
34+
"""创建一个字典来保存客户信息"""
35+
profile = {'first_name': first, 'last_name': last}
36+
for key, value in info.items():
37+
profile[key] = value
38+
39+
return profile
40+
41+
42+
user_profile = build_profile('albert', 'einstein',
43+
location='princeton', field='physics')
44+
45+
print(user_profile)

‎chapter7/trans_array.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 传递列表
2+
3+
def greet_users(names):
4+
for name in names:
5+
print('Hello,' + name)
6+
7+
8+
users = ['Lily', 'Tom', 'Jerry']
9+
greet_users(users)
10+
11+
# 在函数中修改列表
12+
old_data = ["a", 'b', 'c']
13+
new_data = []
14+
15+
16+
def change(values):
17+
while values:
18+
new_data.append(values.pop())
19+
20+
21+
change(old_data)
22+
23+
print(new_data)
24+
print(old_data)
25+
26+
# 有时需要禁止函数修改列表--永久性的
27+
old = ['1', '2', '3']
28+
new = []
29+
30+
31+
def stay_same(nums):
32+
while nums:
33+
new.append(nums.pop())
34+
35+
36+
stay_same(old[:])
37+
38+
print(new)
39+
print(old)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /