while/For 循环相关

 
概要
 
  1. while 循环相关
  2. for 循环相关
 
详细
 
1 while 循环相关
 
 
1.1 例

 

 

1.2 while+continue
# 1.使用while循环打印出0-10
# count = 0
# while count < 11:
#     print(count)
#     count += 1
# 2.使用while循环打印出0-10但是不打印4
# 1.定义一个起始变量
count = 0
# 2.循环
while count < 11:
    # 5.判断 如果count为4则不打印
    if count == 4:
        count += 1
        # 跳过本次循环 开始下一次循环
        continue
    # 3.打印变量的值
    print(count)
    # 4.变量值自增1
    count += 1
"""
continue会让循环体代码直接回到条件判断处重新判断
"""

 

1.3 while+else
count = 0
while count < 5:
     print(count)
     count += 1
else:
     print('嘿嘿嘿')  # 会执行else子代码
 
 
count = 0
while count < 5:
    if count == 3:
        break
    print(count)
    count += 1
else:
    print('嘿嘿嘿')  # 不会执行else子代码
"""
当while循环没有被人为中断(break)的情况下才会走else
"""

 

2 for 循环
 
 
2.1 典型案例
For 循环字典 案例 

 

 

 
详细内容
# for循环能做到的事情
# while循环都可以做到
# 但是for循环语法更加简洁
# 并且在循环取值问题上更加方便
#
# name_list = ['jason', 'tony', 'kevin', 'jack', 'xxx']
# # 循环取出列表的每一个元素并打印
# # while实现
# # count = 0
# # while count < 5:
# #     print(name_list[count])
# #     count += 1
# # for循环
# for name in name_list:
#     print(name)
#
# """
# for 变量名 in 可迭代对象:  # 字符串、列表、字典、元组、集合
#     for循环体代码
#
# ps:变量名如果没有合适的名称 那么可以使用i,j,k,v,item等
# """
#
# # name_list = ['jason', 'tony', 'kevin', 'jack', 'xxx']
# # 循环取出列表的每一个元素并打印
# # while实现
# # count = 0
# # while count < 5:
# #     print(name_list[count])
# #     count += 1
# # for循环
# # for name in name_list:
# #     print(name)
#
# # for循环字符串
# # for i in 'hello world':
# #     print(i)
#
#
# # for循环字典:默认只能拿到k
# d = {'username': 'jason', 'pwd': 123, 'hobby': 'read'}
# for k in d:
#     print(k, d[k])

 

2.2 range
2.2.1 range 一个参数 
2.2.2 range两个参数
含义:从4依次取数到10-1
2.2.3 range 三个参数
含义:从4开始取,然后每2个取一下
 
详细内容
# 关键字range
# 第一种:一个参数  从0开始 顾头不顾尾
# for i in range(10):
#     print(i)
# 第二种:两个参数  自定义起始位置 顾头不顾尾
# for i in range(4, 10):
#     print(i)
# 第三种:三个参数  第三个数字用来控制等差值
# for i in range(2, 100, 10):
#     print(i)
 
 
"""
扩展知识
    https://movie.douban.com/top250  第一页
    https://movie.douban.com/top250?start=25&filter=  第二页
    https://movie.douban.com/top250?start=50&filter=  第三页
    https://movie.douban.com/top250?start=75&filter=  第四页
 
 
    https://movie.douban.com/top250?start=0&filter=  推测第一页
"""
# base_url = "https://movie.douban.com/top250?start=%s&filter="
# for i in range(0, 250, 25):
#     print(base_url % i)
 
 
    # range在不同版本的解释器中 本质不同
#     在python2.X中range会直接生成一个列表
#     在python2.X中有一个xrange也是迭代器(老母猪)
# 在python3.X中range是一个迭代器(老母猪)
# 节省内存空间
'''python2.X中xrange就是python3.x里面的range'''

 

2.3 for+break
# 跟while+break 一样
# # break功能也是用于结束本层循环
# for i in range(10):
#     if i == 4:
#         break
#     print(i)

 

2.4  for + continue:
      continue是结束本次循环,即后边的不会再执行,而是回到条件开始处判断执行.
2.5 for+else
# 与 while+else 一样
# # else也是在for循环正常结束的情况下才会执行
# for i in range(10):
#     if i == 4:
#         break
#     print(i)
# else:
#     print('你追我!!!')
 
2.6 For循环嵌套
 
2.6.1 理解程序运行或者有问题找出来
不执行程序,理解程序是如何一步一执行的(借助画流程图)
 
 
2.6.2 案例:部分乘法口诀表

 

 

 
 
posted @ 2021-12-03 14:57  tslam  阅读(35)  评论(0编辑  收藏  举报