''' 函数是带名字的代码块,用于完成具体的工作。 要执行函数定义的特定任务,可调用该函数。 需要在程序中多次执行同一项任务时,你无需反复编写完成该任务的代码,而只需调用执行该任务的函数,让Python运行 其中的代码。 你将发现,通过使用函数,程序的编写、阅读、测试和修复都将更容易。 ''' #一个简单的函数 def greet_user(): print('Hello !') #函数的调用,函数不调用,是不会执行的。 greet_user() #输出:Hello ! #向函数传递参数 def greet_user1(username): print('Hello ' + username + ' !') #带参函数的调用 user_name = input('what is your name :')#输入XiaoMing greet_user1(user_name) ''' what is your name :XiaoMing Hello XiaoMing ! ''' #函数需要多个参数时,在调用的时候传参位置很重要 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('hamster', 'harry') describe_pet(animal_type='hamster', pet_name='harry2')#实参 关键字实参 是传递给函数的名称—值对 ''' I have a hamster. My hamster's name is Harry. I have a hamster. My hamster2's name is Harry2. ''' #定义函数时设置参数的默认值如下:type = 'dog' def describe_pet(name,type = 'dog'): print('I have a ' + type) print('It is '+ name) #调用有默认值的函数时,可以不传有默认值的参数 describe_pet('DaHuang') describe_pet('harry','hamster') ''' I have a dog It is DaHuang I have a hamster It is harry ''' #注意:使用默认值时,在形参列表中必须先列出没有默认值的形参,再列出有默认值的实参。这让Python依然能够正确地解读位置实参。 #返回值 def age(): temp = input('How old are you ?') #输入 12 age = int(temp) return age your_age = age() print(your_age) # 输出: 12 #设置可选参数,如下 : middle_name='' 即为可选参数 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 ''' #返回字典 def build_person(first_name, last_name): """返回一个字典,其中包含有关一个人的信息""" person = {'first': first_name, 'last': last_name} return person musician = build_person('jimi', 'hendrix') print(musician) #{'first': 'jimi', 'last': 'hendrix'} #列表形参 def my_dog(mydog): print('I have 4 dogs , respectively are:') for dog in mydog: print(dog.title()) #传参时传入列表 mydog = ['dahuang','wangcai','haly','ahuang'] my_dog(mydog) ''' I have 4 dogs , respectively are: Dahuang Wangcai Haly Ahuang ''' #传递任意数量的实参 def make_pizza(*toppings): print("\nMaking a pizza with the following toppings:") for topping in toppings: print("- " + topping) #传入任意个实参 make_pizza('pepperoni') make_pizza('mushrooms', 'green peppers', 'extra cheese') ''' Making a pizza with the following toppings: - pepperoni Making a pizza with the following toppings: - mushrooms - green peppers - extra cheese ''' #结合使用位置实参和任意数量实参 def make_pizza(size, *toppings): """概述要制作的比萨""" print("\nMaking a " + str(size) + "-inch pizza with the following toppings:") for topping in toppings: print("- " + topping) make_pizza(16, 'pepperoni') make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese') ''' Making a 16-inch pizza with the following toppings: - pepperoni Making a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese '''
Python 函数相关
最新推荐文章于 2022-12-07 23:55:13 发布