• Python学习笔记(2)


    Python编程:从入门到实践》学习笔记

    1、用户输入和while循环

    1.1用户输入

    1.1.1函数input()的工作原理

    函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便使用。

    示例如下:

    1. message=input("請輸入姓名:")
    2. print(message)

    函数input()接受一个参数:即要向用户显示的提示或说明,让用户知道该如何做。在这个
    示例中,Python运行第1行代码时,用户将看到提示請輸入姓名:。程序等待用户输入,并在用户按回车键后继续运行。输入存储在变量message中,接下来的print(message)将输入呈现给用户

    运行结果:

    請輸入姓名:zhangsha
    zhangsha

    说明:绿色字体为人为手动输入的值,下同。

    1.1.2编写清晰的程序

    有时候,提示可能超过一行,例如,你可能需要指出获取特定输入的原因。在这种情况下,
    可将提示存储在一个变量中,再将该变量传递给函数input()。这样,即便提示超过一行,input()
    语句也非常清晰。

    1. prompt = "If you tell us who you are, we can personalize the messages you see."
    2. prompt += "\nWhat is your first name? "
    3. name = input(prompt)
    4. print("\nHello, " + name + "!")

    这个示例演示了一种创建多行字符串的方式。第1行将消息的前半部分存储在变量prompt中;
    在第2行中,运算符+=在存储在prompt中的字符串末尾附加一个字符串。
    运行结果:

    If you tell us who you are, we can personalize the messages you see.
    What is your first name? kangkang

    Hello, kangkang!

    1.1.3使用int()来获取数值输入

    使用函数input()时,Python将用户输入解读为字符串。函数int()将数字的字符串表示转换为数值表示,如下所示:

    1. height = input("How tall are you, in inches? ")
    2. height = int(height)
    3. if height >= 36:
    4. print("\nYou're tall enough to ride!")
    5. else:
    6. print("\nYou'll be able to ride when you're a little older.")

    在这个程序中,为何可以将height同36进行比较呢?因为在比较前,height = int(height)

    将输入转换成了数值表示。如果输入的数字大于或等于36,我们就告诉用户他满足身高条件:

    运行结果:

    How tall are you, in inches? 70

    You're tall enough to ride!

    将数值输入用于计算和比较前,务必将其转换为数值表示。

    1.1.4求模运算符

    处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数。

    求模运算符不会指出一个数是另一个数的多少倍,而只指出余数是多少。

    如果一个数可被另一个数整除,余数就为0,因此求模运算符将返回0。你可利用这一点来判
    断一个数是奇数还是偶数:

    1. number = input("Enter a number, and I'll tell you if it's even or odd: ")
    2. number = int(number)
    3. if number % 2 == 0:
    4. print("\nThe number " + str(number) + " is even.")
    5. else:
    6. print("\nThe number " + str(number) + " is odd.")

    偶数都能被2整除,因此对一个数(number)和2执行求模运算的结果为零,即number % 2 ==
    0,那么这个数就是偶数;否则就是奇数。

    输出结果:

    Enter a number, and I'll tell you if it's even or odd: 12

    The number 12 is even.

    1.2while循环

    for循环用于针对集合中的每个元素都一个代码块,而while循环不断地运行,直到指定的条
    件不满足为止。

    1. prompt = "\nTell me something, and I will repeat it back to you:"
    2. prompt += "\nEnter 'quit' to end the program. "
    3. active = True
    4. while active:
    5. message = input(prompt)
    6. if message == 'quit':
    7. active = False
    8. else:
    9. print(message)

    运行结果:

    Tell me something, and I will repeat it back to you:
    Enter 'quit' to end the program. quit

    在这个程序中,输入quit程式结束;输入其他,程序会进入死循环状态。

    1.2.1使用break 退出循环

    要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句。break语句用于控制程序流程,可使用它来控制哪些代码行将执行,哪些代码行不执行,从而让程序按你的要求执行你要执行的代码。

    1. prompt = "\nTell me something, and I will repeat it back to you:"
    2. prompt += "\nEnter 'quit' to end the program. "
    3. active = True
    4. while active:
    5. message = input(prompt)
    6. if message == 'quit':
    7. active = False
    8. else:
    9. print(message)
    10. break

    在这个程序中,输入quit程式结束;输入其他,程序也将结束。


    注意:在任何Python循环中都可使用break语句。例如,可使用break语句来退出遍历列表或字典
    的for循环。


    1.2.2在循环中使用continue

    要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,
    不像break语句那样不再执行余下的代码并退出整个循环。例如,来看一个从1数到10,但只打印
    其中奇数的循环:

    1. current_number = 0
    2. while current_number < 10:
    3. current_number += 1
    4. if current_number % 2 == 0:
    5. continue
    6. print(current_number)

    运行结果:

    1
    3
    5
    7
    9

    1.3使用while 循环来处理列表和字典

    for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则将导致Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环。通过将while循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示

    1.3.1在列表之间移动元素

    假设有一个列表,其中包含新注册但还未验证的网站用户;验证这些用户后,如何将他们移到另一个已验证用户列表中呢?一种办法是使用一个while循环,在验证用户的同时将其从未验证用户列表中提取出来,再将其加入到另一个已验证用户列表中。代码可能类似于下面这样:

    1. # 首先,创建一个待验证用户列表
    2. # 和一个用于存储已验证用户的空列表
    3. unconfirmed_users = ['alice', 'brian', 'candace']
    4. confirmed_users = []
    5. # 验证每个用户,直到没有未验证用户为止
    6. # 将每个经过验证的列表都移到已验证用户列表中
    7. while unconfirmed_users:
    8. current_user = unconfirmed_users.pop()
    9. print("Verifying user: " + current_user.title())
    10. confirmed_users.append(current_user)
    11. # 显示所有已验证的用户
    12. print("\nThe following users have been confirmed:")
    13. for confirmed_user in confirmed_users:
    14. print(confirmed_user.title())

    运行结果:

    第一次:Verifying user: Candace

    The following users have been confirmed:
    Candace
    第二次:Verifying user: Brian

    The following users have been confirmed:
    Candace
    Brian
    第三次:Verifying user: Alice

    The following users have been confirmed:
    Candace
    Brian
    Alice

    1.3.2删除包含特定值的所有列表元素

    假设你有一个宠物列表,其中包含多个值为'cat'的元素。要删除所有这些元素,可不断运行一个while循环,直到列表中不再包含值'cat',如下所示:

    1. # 删除包含特定值的所有列表元素
    2. pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
    3. print(pets)
    4. while 'cat' in pets:
    5. pets.remove('cat')
    6. print(pets)

    运行结果:

    ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
    ['dog', 'dog', 'goldfish', 'rabbit']

    1.3.3使用用户输入来填充字典

    可使用while循环提示用户输入任意数量的信息。下面来创建一个调查程序,其中的循环每次执行时都提示输入被调查者的名字和回答。我们将收集的数据存储在一个字典中,以便将回答同被调查者关联起来:

    1. #定义空字典
    2. responses = {}
    3. # 设置一个标志,指出调查是否继续
    4. polling_active = True
    5. while polling_active:
    6. # 提示输入被调查者的名字和回答
    7. name = input("\nWhat is your name? ")
    8. response = input("Which mountain would you like to climb someday? ")
    9. # 将答卷存储在字典中
    10. responses[name] = response
    11. # 看看是否还有人要参与调查
    12. repeat = input("Would you like to let another person respond? (yes/ no) ")
    13. if repeat == 'no':
    14. polling_active = False
    15. # 调查结束,显示结果
    16. print("\n--- Poll Results ---")
    17. for name, response in responses.items():
    18. print(name + " would like to climb " + response + ".")

    运行结果:

    What is your name? yu
    Which mountain would you like to climb someday? rr
    Would you like to let another person respond? (yes/ no) yes

    What is your name? yy
    Which mountain would you like to climb someday? ww
    Would you like to let another person respond? (yes/ no) no

    --- Poll Results ---
    yu would like to climb rr.
    yy would like to climb ww.

    2.函 数

    2.1函数定义

    使用关键字def来告诉Python你要定义一个函数。

    1. #自定义函数
    2. def greet_hello():
    3. print("你好")
    4. greet_hello()

    运行结果:

    你好

    2.1.1向函数传递信息

    通过在这里添加username,就可让函数接受你给username指定的任何值。现在,这个函数要求你调用它时给username指定一个值。

    1. #向函数传递信息
    2. def greet_hello(username):
    3. print("你好,"+username.title()+"!")
    4. greet_hello("小李")

    运行结果:

    你好,小李!

    2.1.2实参和形参

    在函数greet_hello()的定义中,变量username是一个形参——函数完成其工作所需的一项信息。在代码greet_hello('小李')中,值'小李'是一个实参。实参是调用函数时传递给函数的信息。我们调用函数时,将要让函数使用的信息放在括号内。在greet_hello('小李')中,将实参'jesse'传递给了函数greet_hello(),这个值被存储在形参username中。

    2.2传递实参

    鉴于函数定义中可能包含多个形参,因此函数调用中也可能包含多个实参。向函数传递实参的方式很多,可使用位置实参,这要求实参的顺序与形参的顺序相同;也可使用关键字实参,其中每个实参都由变量名和值组成;还可使用列表和字典。

    2.2.1 位置实参

    你调用函数时,Python必须将函数调用中的每个实参都关联到函数定义中的一个形参。为此,最简单的关联方式是基于实参的顺序。这种关联方式被称为位置实参

    1. def describe_pet(animal_type, pet_name):
    2. """显示宠物的信息"""
    3. print("\nI have a " + animal_type + ".")
    4. print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    5. describe_pet('hamster', 'harry')
    6. describe_pet('dog', 'willie')

    这个函数的定义表明,它需要一种动物类型和一个名字。调用describe_pet()时,需要按顺序提供一种动物类型和一个名字。例如,在前面的函数调用中,实参'hamster'存储在形参animal_type中,而实参'harry'存储在形参pet_name中。在函数体内,使用了这两个形参来显示宠物的信息。你可以根据需要调用函数任意次。

    运行结果:

    I have a hamster.
    My hamster's name is Harry.

    I have a dog.
    My dog's name is Willie.

    2.2.2 关键字实参

          关键字实参是传递给函数的名称—值对。你直接在实参中将名称和值关联起来了,因此向函数传递实参时不会混淆(不会得到名为Hamster的harry这样的结果)。关键字实参让你无需考虑函数调用中的实参顺序,还清楚地指出了函数调用中各个值的用途

    1. #关键字实参
    2. def describe_pet(animal_type, pet_name):
    3. """显示宠物的信息"""
    4. print("\nI have a " + animal_type + ".")
    5. print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    6. describe_pet( pet_name='harry',animal_type='hamster')

    运行结果:

    I have a hamster.
    My hamster's name is Harry.


    注意 使用关键字实参时,务必准确地指定函数定义中的形参名。


    2.2.3 默认值

    编写函数时,可给每个形参指定默认值。在调用函数中给形参提供了实参时,Python将使用指定的实参值;否则,将使用形参的默认值。因此,给形参指定默认值后,可在函数调用中省略相应的实参。使用默认值可简化函数调用,还可清楚地指出函数的典型用法。

    1. def describe_pet(pet_name, animal_type='dog'):
    2. """显示宠物的信息"""
    3. print("\nI have a " + animal_type + ".")
    4. print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    5. describe_pet(pet_name='willie')
    6. describe_pet('willie')
    7. describe_pet(pet_name='harry', animal_type='hamster')

    三种函数调用方式都可行。

    运行结果:

    I have a dog.
    My dog's name is Willie.

    I have a dog.
    My dog's name is Willie.

    I have a hamster.


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


    2.3 返回值

    函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值。函数返回的值被称为返回值。在函数中,可使用return语句将值返回到调用函数的代码行。返回值让你能够将程序的大部分繁重工作移到函数中去完成,从而简化主程序。

    2.3.1 返回简单值

    下面来看一个函数,它接受名和姓并返回整洁的姓名:

    1. #函數返回值
    2. def get_formatted_name(first_name, last_name):
    3. """返回整洁的姓名"""
    4. full_name = first_name + ' ' + last_name
    5. return full_name.title()
    6. musician = get_formatted_name('jimi', 'hendrix')
    7. print(musician)

    运行结果:

    Jimi Hendrix

    2.3.2让实参变成可选的

    有时候,需要让实参变成可选的,这样使用函数的人就只需在必要时才提供额外的信息。可使用默认值来让实参变成可选的。

    1. def get_formatted_name(first_name, last_name, middle_name=''):
    2. """返回整洁的姓名"""
    3. if middle_name:
    4. full_name = first_name + ' ' + middle_name + ' ' + last_name
    5. else:
    6. full_name = first_name + ' ' + last_name
    7. return full_name.title()
    8. musician = get_formatted_name('jimi', 'hendrix')
    9. print(musician)
    10. musician = get_formatted_name('john', 'hooker', 'lee')
    11. print(musician)

    运行结果:

    Jimi Hendrix
    John Lee Hooker

    2.3.3 返回字典

    函数可返回任何类型的值,包括列表和字典等较复杂的数据结构

    1. def build_person(first_name, last_name, age=''):
    2. """返回一个字典,其中包含有关一个人的信息"""
    3. person = {'first': first_name, 'last': last_name}
    4. if age:
    5. person['age'] = age
    6. return person
    7. musician = build_person('jimi', 'hendrix')
    8. print(musician)
    9. musician = build_person('jimi', 'hendrix', age=27)
    10. print(musician)

    在函数定义中,我们新增了一个可选形参age,并将其默认值设置为空字符串。如果函数调用中包含这个形参的值,这个值将存储到字典中。在任何情况下,这个函数都会存储人的姓名,但可对其进行修改,使其也存储有关人的其他信息。

    运行结果:

    {'first': 'jimi', 'last': 'hendrix'}
    {'first': 'jimi', 'last': 'hendrix', 'age': 27}

    2.3.4结合使用函数和while 循环

    例如,下面将结合使用函数get_formatted_name()和while循环,以更正规的方式问候用户。下面尝试使用名和姓跟用户打招呼:

    1. def get_formatted_name(first_name, last_name):
    2. """返回整洁的姓名"""
    3. full_name = first_name + ' ' + last_name
    4. return full_name.title()
    5. while True:
    6. print("\nPlease tell me your name:")
    7. print("(enter 'q' at any time to quit)")
    8. f_name = input("First name: ")
    9. if f_name == 'q':
    10. break
    11. l_name = input("Last name: ")
    12. if l_name == 'q':
    13. break
    14. formatted_name = get_formatted_name(f_name, l_name)
    15. print("\nHello, " + formatted_name + "!")

    运行结果:

    Please tell me your name:
    (enter 'q' at any time to quit)
    First name:
    Last name:

    Hello, 裡 裡!

    Please tell me your name:
    (enter 'q' at any time to quit)
    First name: q


    2.4传递列表

    你经常会发现,向函数传递列表很有用,这种列表包含的可能是名字、数字或更复杂的对象(如字典)。将列表传递给函数后,函数就能直接访问其内容。下面使用函数来提高处理列表的效率。
    假设有一个用户列表,我们要问候其中的每位用户。下面的示例将一个名字列表传递给一个名为greet_users()的函数,这个函数问候列表中的每个人:

    1. def greet_users(names):
    2. """向列表中的每位用户都发出简单的问候"""
    3. for name in names:
    4. msg = "Hello, " + name.title() + "!"
    5. print(msg)
    6. usernames = ['hannah', 'ty', 'margot']
    7. greet_users(usernames)

    运行结果:

    Hello, Hannah!
    Hello, Ty!
    Hello, Margot!

    2.4.1 在函数中修改列表

    将列表传递给函数后,函数就可对其进行修改。在函数中对这个列表所做的任何修改都是永久性的,这让你能够高效地处理大量的数据。

    1. def print_models(unprinted_designs, completed_models):
    2. """
    3. 模拟打印每个设计,直到没有未打印的设计为止
    4. 打印每个设计后,都将其移到列表completed_models中
    5. """
    6. while unprinted_designs:
    7. current_design = unprinted_designs.pop()
    8. # 模拟根据设计制作3D打印模型的过程
    9. print("Printing model: " + current_design)
    10. completed_models.append(current_design)
    11. def show_completed_models(completed_models):
    12. """显示打印好的所有模型"""
    13. print("\nThe following models have been printed:")
    14. for completed_model in completed_models:
    15. print(completed_model)
    16. unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
    17. completed_models = []
    18. print_models(unprinted_designs, completed_models)
    19. show_completed_models(completed_models)

    运行结果:

    Printing model: dodecahedron
    Printing model: robot pendant
    Printing model: iphone case

    The following models have been printed:
    dodecahedron
    robot pendant
    iphone case


    2.4.2禁止函数修改列表

    有时候,需要禁止函数修改列表。例如,假设像前一个示例那样,你有一个未打印的设计列表,并编写了一个将这些设计移到打印好的模型列表中的函数。你可能会做出这样的决定:即便打印所有设计后,也要保留原来的未打印的设计列表,以供备案。但由于你将所有的设计都移出了unprinted_designs,这个列表变成了空的,原来的列表没有了。为解决这个问题,可向函数传递列表的副本而不是原件;这样函数所做的任何修改都只影响副本,而丝毫不影响原件。要将列表的副本传递给函数,可以像下面这样做:

    调用:print_models(unprinted_designs[:], completed_models)

    1. def print_models(unprinted_designs, completed_models):
    2. """
    3. 模拟打印每个设计,直到没有未打印的设计为止
    4. 打印每个设计后,都将其移到列表completed_models中
    5. """
    6. while unprinted_designs:
    7. current_design = unprinted_designs.pop()
    8. # 模拟根据设计制作3D打印模型的过程
    9. print("Printing model: " + current_design)
    10. completed_models.append(current_design)
    11. def show_completed_models(completed_models):
    12. """显示打印好的所有模型"""
    13. print("\nThe following models have been printed:")
    14. for completed_model in completed_models:
    15. print(completed_model)
    16. unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
    17. completed_models = []
    18. print_models(unprinted_designs[:], completed_models)
    19. show_completed_models(completed_models)

    虽然向函数传递列表的副本可保留原始列表的内容,但除非有充分的理由需要传递副本,否则还是应该将原始列表传递给函数,因为让函数使用现成列表可避免花时间和内存创建副本,从而提高效率,在处理大型列表时尤其如此。

    2.5传递任意数量的实参

    有时候,你预先不知道函数需要接受多少个实参,好在Python允许函数从调用语句中收集任意数量的实参。

    例如,来看一个制作比萨的函数,它需要接受很多配料,但你无法预先确定顾客要多少种配料。下面的函数只有一个形参*toppings,但不管调用语句提供了多少实参,这个形参都将它们统统收入囊中:

    1. def make_pizza(*toppings):
    2. """概述要制作的比萨"""
    3. print("\nMaking a pizza with the following toppings:")
    4. for topping in toppings:
    5. print("- " + topping)
    6. make_pizza('pepperoni')
    7. make_pizza('mushrooms', 'green peppers', 'extra cheese')

    形参名*toppings中的星号让Python创建一个名为toppings的空元组,并将收到的所有值都封装到这个元组中。

    运行结果:

    Making a pizza with the following toppings:
    - pepperoni

    Making a pizza with the following toppings:
    - mushrooms
    - green peppers
    - extra cheese

    2.5.1结合使用位置实参和任意数量实参

    如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最
    后。

    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)
    7. make_pizza(16, 'pepperoni')
    8. make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

    基于上述函数定义,Python将收到的第一个值存储在形参size中,并将其他的所有值都存储
    在元组toppings中。在函数调用中,首先指定表示比萨尺寸的实参,然后根据需要指定任意数量
    的配料。

    运行结果:

    Making a 16-inch pizza with the following toppings:
    - pepperoni

    Making a 12-inch pizza with the following toppings:
    - mushrooms
    - green peppers
    - extra cheese

    2.5.2使用任意数量的关键字实参

    有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种
    情况下,可将函数编写成能够接受任意数量的键—值对——调用语句提供了多少就接受多少。
    个这样的示例是创建用户简介:你知道你将收到有关用户的信息,但不确定会是什么样的信息。
    在下面的示例中,函数build_profile()接受名和姓,同时还接受任意数量的关键字实参:

    1. def build_profile(first, last, **user_info):
    2. """创建一个字典,其中包含我们知道的有关用户的一切"""
    3. profile = {}
    4. profile['first_name'] = first
    5. profile['last_name'] = last
    6. for key, value in user_info.items():
    7. profile[key] = value
    8. return profile
    9. user_profile = build_profile('albert', 'einstein',location='princeton',field='physics')
    10. print(user_profile)

    运行结果:

    {'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}

    2.6 将函数存储在模块中

          函数的优点之一是,使用它们可将代码块与主程序分离。通过给函数指定描述性名称,可让
    主程序容易理解得多。你还可以更进一步,将函数存储在被称为模块的独立文件中,再将模块导
    入到主程序中。import语句允许在当前运行的程序文件中使用模块中的代码
           通过将函数存储在独立的文件中,可隐藏程序代码的细节,将重点放在程序的高层逻辑上。
    这还能让你在众多不同的程序中重用函数。将函数存储在独立文件中后,可与其他程序员共享这
    些文件而不是整个程序。知道如何导入函数还能让你使用其他程序员编写的函数库
          导入模块的方法有多种,下面对每种都作简要的介绍。

    2.6.1 导入整个模块

          要让函数是可导入的,得先创建模块。模块是扩展名为.py的文件,包含要导入到程序中的
    代码。下面来创建一个包含函数make_pizza()的模块。为此,我们将文件pizza.py中除函数
    make_pizza()之外的其他代码都删除:

    pizza.py

    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)

          接下来,我们在pizza.py所在的目录中创建另一个名为making_pizzas.py的文件,这个文件导
    入刚创建的模块,再调用make_pizza()两次:

    1. import pizza
    2. pizza.make_pizza(16, 'pepperoni')
    3. pizza.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

    2.6.2 使用as 给函数指定别名

          如果要导入的函数的名称可能与程序中现有的名称冲突,或者函数的名称太长,可指定简短
    而独一无二的别名——函数的另一个名称,类似于外号。要给函数指定这种特殊外号,需要在导
    入它时这样做。

    1. from pizza import make_pizza as mp
    2. mp(16, 'pepperoni')
    3. mp(12, 'mushrooms', 'green peppers', 'extra cheese')
    4. import pizza as p
    5. p.make_pizza(16, 'pepperoni')
    6. p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

    使用星号(*)运算符可让Python导入模块中的所有函数:

    1. from pizza import *
    2. make_pizza(16, 'pepperoni')
    3. make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

    注意:

    1.给形参指定默认值时,等号两边不要有空格:

    def function_name(parameter_0, parameter_1='default value')

    2.对于函数调用中的关键字实参,也应遵循这种约定:

    function_name(value_0, parameter_1='value')

  • 相关阅读:
    【AGC】【认证服务】认证服务集成第三方登录后返回third provider is disabled,错误码203817988
    mysql的主从复制与读写分离
    windows中MySQL主从配置【第一篇】
    SpringMVC中的拦截器
    Java文件输入输出(简单易懂版)
    react+canvas实现刮刮乐效果
    “构建高效的前端表单验证与增删改功能实现“
    《恋上数据结构与算法》第1季:算法概述
    机器学习强基计划3-2:详细推导支持向量机SVM原理+Python实现
    高博基于stereo-imu的VO运行尝鲜
  • 原文地址:https://blog.csdn.net/qq_42711010/article/details/133201235