Python之 for in 循环遍历 列表、元组、集合、字典类型的案列
"""
本篇文章是Python语言之for in 循环遍历 列表、元组、集合、字典类型的案列 希望对大家有所帮助
"""
var = [1, 3, 5, 7, 9]
print(type(var)) # <class 'list'>
for a in var:
print(str(a) + "\t", end="") # 1 3 5 7 9 -------------------------------------------
print("-------------------------------------------")
var = (1, 3, 5, 7, 9)
print(type(var)) # <class 'tuple'>
for a in var:
print(str(a) + "\t", end="") # 1 3 5 7 9 -------------------------------------------
print("-------------------------------------------")
var = {1, 3, 5, 7, 9}
print(type(var)) # <class 'set'>
for a in var:
print(str(a) + "\t", end="") # 1 3 5 7 9 -------------------------------------------
print("-------------------------------------------")
var = {'one': 1, 'two': 2, 'three': 3}
print(type(var)) # <class 'dict'>
for key, value in var.items():
print(str(key) + ":" + str(value) + "\t", end="") # one:1 two:2 three:3 -------------------------------------------
print("-------------------------------------------")
具体使用方法请看代码,下面是运行结果: