# 定义列表
list1 = [100, 'hello', True, 12.5, (10, 20) ]
for value in list1:
print(value)
# 定义元组
tuple1 = (10, 20, 30, 40, 50)
for i in tuple1:
print(i)
# 定义字符串
str1 = 'hello'
for i in str1:
print(i)
# 定义字典
dict1 = {"name": "s李白", "age": 30, "high": 178.5}
for k in dict1:
print(k, dict1[k])
# 注意:for循环只能遍历像列表,元组,字符串,字典这样的可以遍历的容器,
# 不能遍历数字,浮点数,布尔类型这样不可遍历的数据
while循环基本使用
示例:使用while循环实现5次你好
#定义变量记录循环次数
#while后面添加条件判断
#while条件满足时,执行缩进部分代码
#改变循环变量便于下次循环ß
count = 1
while( count <= 5)
print('你好 ,count = %d' %count)
count += 1
while循环遍历容器
list1 = [100, 'hello', True, 12.3, (10, 20)]
i = 0
while i < len(list1):
print('list1[%d] = %s' % (i, list1[i]))
i += 1
留言与评论(共有 0 条评论) “” |