美文网首页
第八章 函数

第八章 函数

作者: Shinichi新一君 | 来源:发表于2017-01-26 15:07 被阅读32次

本章学习:

1) 向函数传递信息的方式
2) 如何编写主要任务是显示信息的函数
3) 用于处理数据并返回一个或一组值的函数
4) 如何将函数存储在模块的独立文件中

8.1 定义函数

def display_message(course_name): #注意冒号,这个username是形参

    print('I study ' + course_name.title() + ' in this lesson.') #到这一步定义函数过程已经完成

display_message('function') #注意引号 这里'function'是实参,这个值被存储在实参中

8.2 传递实参

1) 位置实参:实参的顺序与形参相同
2) 关键词实参: 每个实参都由变量名和值组成
3) 使用列表和字典

8.2.1 位置实参

可多次调用,顺序很重要.

def phone_summary(phone_name, phone_system):

    print('\nMy phone is ' + phone_name.title() + ' and its system is '+ phone_system.title() + '.')

phone_summary('samsung', 'andriod')

phone_summary('iphone', 'IOS')

8.2.2 关键字实参

def phone_summary(phone_name, phone_system):

    print('\nMy phone is ' + phone_name.title() + ' and its system is '+ phone_system.title() + '.')

phone_summary(phone_name = 'samsung', phone_system = 'andriod') #这里是关键字实参,无需考虑顺序,务必要准确指定函数定义的形参名

phone_summary('iphone', 'IOS')

8.2.3 默认值

def phone_summary(phone_name, phone_system = 'andriod'): #这里默认值是安卓系统

    print('\nMy phone is ' + phone_name.title() + ' and its system is '+ phone_system.title() + '.')

phone_summary(phone_name = 'samsung')

phone_summary(phone_name = 'xiaomi')

phone_summary(phone_name = 'iphone', phone_system = 'ios') #这里是不是默认值的写法

注意:使用默认值时,在形参列表中必须先列出没有默认值的形参,再列出有默认值的形参,让python依然能正确解读位置实参

8.3 返回值

使用return语句将值返回到调用函数的代码行

8.3.1 返回简单值

def city_country(city_name, country_name):

    city_countrys = city_name.title() + ' ' + country_name.title()

    return city_countrys

city_data = city_country('santiago', 'chile')

print(city_data)

8.3.2 让实参变成可选的(结合while循环)

def make_album(singer_name, album_name, songs_number = ''):

    if songs_number:

        album = singer_name + ', ' + album_name + ', ' + str(songs_number)

    else:

        album = singer_name + ', ' + album_name

    return album.title()

while True:

    print('\nPlease enter album information.')

    print('Or you can enter "q" to quit at anytime.')

    singer_n = input('singer name is:')

    if singer_n == 'q':

        break

    album_n = input('album name is:')

    if album_n == 'q':

        break

    songs_n = input('songs number is:')

    if songs_n == 'q':

        break

    new_album = make_album(singer_n, album_n, songs_n)

    print(new_album)

8.4 传递列表

def fvrt_phone(names): #定义函数

    for name in names: #遍历列表

        msg = name.title() + "'s phone is Huawei."

        print(msg)

usernames = ['damon', 'claire', 'gigi'] 定义列表

fvrt_phone(usernames)

8.4.1 在函数中修改列表

每个函数都应只负责一项具体的工作,这优于使用一个函数来完成两项工作

8.4.2 禁止函数修改列表

切片表示法[:]创建列表的副本

list_name[:]

8.5 传递任意数量的实参

形参名*tuple_name中的星号让python创建一个名为tuple_name的空元组

两个*即是创建空的字典
字典键值对的格式是 键1 = '值1', ...

def make_car(first, second, **third):

    car_info = {}

    car_info['brand'] = first #注意这里都是方括号,否则报错can't assign to function call

    car_info['type'] = second

    for key, value in third.items():

        car_info[key] = value

    return car_info

car = make_car('saburu', 'outback', color = 'blue', two_package = True) #注意这里字典的表达方式

print(car)

8.6 将函数存储在模块中

使用函数可将代码块与主程序分离.
可以更进一步,将函数存储在被称为模块的独立文件中

8.6.1 导入整个模块

import xxx #xxx为xxx.py的文件名

xxx.yyy() #这里yyy是函数名

8.6.2 导入特定的函数

from xxx import yyy1, yyy2, yyy3

8.6.3 使用as'给函数指定别名

导入的函数名称可能与程序中现有名称冲突;或者函数名太长

from xxx import yyy as zzz #zzz就是别名

8.6.4 使用as给模块指定别名

import xxx as zzz

zzz.yyy()

8.6.5 导入模块中的所有函数

使用 * 让python导入模块中的所有函数

from xxx import *

yyy() #区别于xxx.yyy()

8.7 函数编写指南

相关文章

网友评论

      本文标题:第八章 函数

      本文链接:https://www.haomeiwen.com/subject/mdtxittx.html