1.while 是个死循环,结束有2个条件:1.不满足条件,跳出 执行else,2.break 强制结束。
count = 0
while count < 5:
print("%d is less than 5" % count)
count = 6
break
else:
print("%d is not less than 5" % count)
可实际运用:满足条件1,运行程序1,然后跳出判断,再运行程序2
2。print默认是换行的,加参数end=’ ‘,不换行输出
for i in ‘ILoveLSGO’:
print(i, end=’ ') # 不换行输出
3.字典{} 转换列 [],for key, value in D.items():,一一对应逗号前的数据。
D = {‘Google’: ‘www.google.com’, ‘Runoob’: ‘www.runoob.com’, ‘taobao’: ‘www.taobao.com’}
print(“字典值 : %s” % D.items())
print(“转换为列表 : %s” % list(D.items()))
遍历字典列表
‘’’
for key, value in D.items():
print(key, value)
aaa = key + value
print(aaa)
‘’’
for wokao,wokao2 in D.items():
print(wokao,wokao2)
输出:
C:\Users\bn1982\PycharmProjects\untitled2\venv\Scripts\python.exe C:/python/aly1.py
字典值 : dict_items([(‘Google’, ‘www.google.com’), (‘Runoob’, ‘www.runoob.com’), (‘taobao’, ‘www.taobao.com’)])
转换为列表 : [(‘Google’, ‘www.google.com’), (‘Runoob’, ‘www.runoob.com’), (‘taobao’, ‘www.taobao.com’)]
Google www.google.com
Runoob www.runoob.com
taobao www.taobao.com
进程已结束,退出代码 0
3补充,key,value ,是一一对应.items,也可单独处理,见下例2-3、
dic = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
for key, value in dic.items():
print(key, value, sep=’:’, end=’ ')
a:1 b:2 c:3 d:4
dic = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
for key in dic.keys():
print(key, end=’ ')
a b c d
dic = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}
for value in dic.values():
print(value, end=’ ')