标签:col abc class python hello strong family else 列表
Python for循环使用 else 语句:
else:当 for 所有的语句代码块正常运行完,才会运行 else 语句。
示例:
‘‘‘
for 迭代对象 in 序列:
代码块(一行语句或多行代码)
else:
代码块(一行语句或多行代码)
‘‘‘
程序:
# 字符串
strs = "Hello World."
for i in strs:
print(i,end=" ")
# H e l l o W o r l d . 运行OK
else:
print("运行OK")
print()
# 列表
lst = [1,2.3,8+9j,‘abc‘,(4,5),{7,8,‘a‘},{‘a‘:4}]
for i in lst:
print(i,end=" ")
# 1 2.3 (8+9j) abc (4, 5) {8, ‘a‘, 7} {‘a‘: 4} 运行OK
else:
print("运行OK")
print()
# 元组
tup = (1,2,3,4,5,6)
for i in tup:
print(i,end=" ")
# 1 2 3 4 5 6 运行OK
else:
print("运行OK")
print()
# 字典
dic = {‘a‘:{‘b‘:123},(4,5):‘str‘,123:[4,5,6]}
# 键为不可变类型 字符串、元组、数字
for i in dic:
print(i,end=" ")
# a (4, 5) 123 运行OK
else:
print("运行OK")
print()
# 集合
set_1 = {1,2.5,‘a‘,(7,8,9)}
for i in set_1:
print(i,end=" ")
# 1 2.5 a (7, 8, 9) 运行OK
else:
print("运行OK")
print()
2020-02-06
标签:col abc class python hello strong family else 列表
原文地址:https://www.cnblogs.com/hany-postq473111315/p/12268076.html