言成说 - 变量
type: channel
link: https://www.elecdiy.com/tag/%E5%8F%98%E9%87%8F/
items: ex18 命令、变量、代码和函数
items: ex4 变量和命名的使用

ex18 命令、变量、代码和函数
type: item
date: 2021-03-16T21:48:00+08:00
description: 学习要点def 用于函数创建，函数代码块以 def 关键词开头，后接函数标识符名称和圆括号 ()*args意思是告诉python把函数中所有参数都接收进来，放进args列表中去。程序代码 # this one is like your scripts with argv def print_two(*args): arg1, arg2 = args print(f"arg1: {arg1}, arg2: {arg2}") # ok, that *args is actually pointless, we can just do this def print_two_again(arg1, arg2): print(f"arg1: {arg1}, arg2: {arg2}") # this just takes one argument def print_one(arg1): print(f"arg1: {arg1}") # this one takes no arguments def print_none(): print("I got nothin'.") print_two("Zed","Shaw") print_two_again("Zed","Shaw") print_one("First!") print_none() ```` ### 输出结果 arg1: Zed, arg2: Shawarg1: Zed, arg2: Shawarg1: First!I got nothin'.

ex4 变量和命名的使用
type: item
date: 2021-03-01T22:40:00+08:00
description: 程序代码： cars = 100 space_in_a_car = 4.0 drivers = 30 passengers = 90 cars_not_drivers = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * space_in_a_car #拼车的承载量 average_passengers_per_car = passengers / cars_driven print("There are", cars, "cars available.") print("There are only", drivers, "drivers available.") print("There will be", cars_not_drivers, "empty cars today.") print("We can transport", carpool_capacity, "people today.") print("We have", passengers, "to carpool today.") print("We need to put about",average_passengers_per_car, "in each car.") 输出结果： There are 100 cars available. There are only 30 drivers available. There will be 70 empty cars today. We can transport 120.0 people today. We have 90 to carpool today. We need to put about 3.0 in each car.