python函数篇
1. for
- python中的for可以直接从序列中取出元素
例如:
weekdays = ['Monday','Tuesday','Wednesday','Thursday','Friday']
i = 0
while i < len(weekdays):
print(weekdays[i])
i +=1
上述代码等价于
for day in weekdays:
print(day)
- 用for取字典中的值,取出来的只有键值
for item in pizza:
print(item)
输出:
直接取values
for item in pizza.values():
print(item)
分别取出keys和values
for k,v in pizza.items():
print("key is {},value is {}".format(k,v))
2. print()
2.1 输出固定参数
print()可同时输出多个参数
print("hello","Jane","!
结果为:hello Jane !
2.2 任意数量参数的输入
1. 一个*
- 输入参数时,加个*,表示参数可以无限扩展,数据类型为元组
def print_args(*args):
print("Positional args:",args)
print_args(1,2,3,"hello","hi")
- 而且还可以用下标访问
def print_args(*args):
print(type(args))
print("Positional args:",args[3])
print_args(1,2,3,'quantity','hi')
结果
- 至少输入两个参数
def print_args_required(req1,req2,