【Day 6】
- 函数关键字
(1)def
def greet_user(username):#username为形参
'''显示简单的问候语'''
print('hello,' + username.title()+'!')
greet_user('penny')#penny为实参
#输出结果
hello,Penny!
def display_message(message):
'''预告学习内容'''
print("你在本章将学到 " + message + '.')
display_message('function')
#输出结果
你在本章将学到 function.
(2)as
如果要导入的函数的名称可能与程序中现有的名称冲突,或者函数的名称太长,可指定简短而独一无二的 别名 —— 函数的另一个名称,类似于外号。要给函数指定这种特殊外号,需要在导入它时这样做。
#通用语法
from module_name import function_name as fn
-
函数的定义
函数 :带名字的代码块,用于完成具体的工作。(定义的方法见上) -
函数参数与作用域
(1)函数的参数
形参/默认值
def describe_pet(pet_name , animal_type='dog'):
#必须将不具有默认值的形参放参数开头,让 Python 能够正确地解读位置实参
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('harry')
#输出结果
I have a dog.
My dog's name is Harry.
实参/关键字实参
特点:具体的
好处:与位置无关
def describe_pet(animal_type, pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(animal_type='hamster', pet_name='harry')
#输出结果
I have a hamster.
My hamster's name is Harry.
(2)等效调用
def describe_pet(pet_name , animal_type='dog'):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('harry')#等效调用
describe_pet('coke')
describe_pet('lily','cat')#
#输出结果
I have a dog.
My dog's name is Harry.
I have a dog.
My dog's name is Coke.
I have a cat.
My cat's name is Lily.
- 函数返回值
(1)返回简单值
def get_formatted_name(first_name, last_name, middle_name=''):
""" 返回整洁的姓名 """
if middle_name:
full_name = first_name + ' ' + middle_name + ' ' + last_name
else:
full_name = first_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)
#输出结果
Jimi Hendrix
John Lee Hooker
(2)返回字典
def build_dictionary(word_Chinese,word_English):
'''返回一个英汉字典'''
dictionary={'Chinese':word_Chinese,'English':word_English,}
return dictionary
word=build_dictionary('蟒蛇','python')
print(word)
#输出结果
{'Chinese': '蟒蛇', 'English': 'python'}
(3)结合函数与while循环
def get_formatted_name(first_name, last_name):
""" 返回整洁的姓名 """
full_name = first_name + ' ' + last_name
return full_name.title()
while True:
print("\nPlease tell me your name:")
print("(enter 'q' at any time to quit)")
f_name = input("First name: ")
if f_name == 'q':
break
l_name = input("Last name: ")
if l_name == 'q':
break
formatted_name = get_formatted_name(f_name, l_name)
print("\nHello, " + formatted_name + "!")
#输出结果
Please tell me your name:
(enter 'q' at any time to quit)
First name: r
Last name: bull
Hello, R Bull!
Please tell me your name:
(enter 'q' at any time to quit)
First name: q
【作业构想】
6. 实现random.sample方法
#方法一:randint
from random import randint
x = randint(1, 6)
print(x)
#输出结果
2
#方法二:从数列中随机取任意个数数字
import random
y=list(range(1,100))
slice = random.sample(y, 5)
print (slice)
- 实现Max方法
x=max(1,2,5,7,23,3)
print(x)
#输出结果
23
- 实现判断两个字符串是否相等的方法
a=input('请输入你的第一个字符串:')
b=input('请输入你的第二个字符串:')
if a==b:
print('两字符串相等')
else:
print('两字符串不相等')
#输出结果
请输入你的第一个字符串:nihao
请输入你的第二个字符串:nihao
两字符串相等