01 for循环

01 for循环

一、循环取值

while循环,循环取值

  • 缺点:1、需要统计你要取值的多值类型的总长度。2、代码不简洁
# 需求:打印列表中的每个值
students = ['egon', 'lxx', 'alex']
i = 0
while i < 3:
    print(students[i], end=" ")
    i += 1

for循环,循环取值

  • 循环取值,while并不擅长,python提供了一个专门循环取值的功能:for循环

  • 注意:没有任何外加条件的字典的循环取值,默认取出的值是字典的key。其它可取值类型的取值,都是取到里面每一个值。

# 1、需求:打印列表中的每个值
students = ['egon', 'wangmao', 'alex']
for student in students:
    print(student, end=" ")  # egon wangmao alex

# 2、需求:打印字典中的每个值
dic = {'name': 'egon', 'age': 18, 'sex': 'male'}
for key in dic:
    print(key, end=" ")  # name age sex

# 3、需求:打印字符串中的每个值
for char in 'wangmao':
    print(char, end=" ")  # w a n g m a o

二、range(start,stop,step)

while循环,重复执行某段代码

# 需求:重复打印'Hello World!'3次
count = 0
while count < 3:
    print('Hello World!')
    count += 1

利用for循环取值原理,重复执行某段代码

  • for循环取值,能取几次for循环就能执行几次
# 需求:重复打印'Hello World!'3次
for i in ['a', 'b', 'c']:
    print('Hello World!')

for+range,重复执行某段代码

range介绍

  • range中3个参数(顾头不顾尾),默认用逗号隔开,默认从0开始,结束位置不包含在内,默认步长1
  • 总结:rang(5)中数字是多少,循环的次数就是几次
# 需求:重复打印'Hello World!'3次
# 1、range()指定结束位置
for a in range(3):  # 范围0~2,循环3次
    print('Hello World!')

# 2、range()指定起始位置 + 结束位置
for b in range(1, 4):  # 范围1~3,循环3次
    print('Hello World!')

# 3、range()指定起始位置 + 结束位置 + 步长
for c in range(1, 6, 2):  # 范围1 3 5,循环3次
    print('Hello World!')

拓展

  • py2的range打印是列表,py3不是
# py2中
>>> range(3)
[0, 1, 2]

# py3中
>>> range(3)
range(0, 3)

三、len()

len()用法

  • len(多个值的数据类型):统计该数据类型的个数(不包含数字类型)
  • 注意:len()统计字典,统计的是字典key的个数
  • 注意:len()统计嵌套类型(例如:列表),统计的是以第一层为单位,里面虽然有嵌套列表,单也只看作一个整体(只以最外面的列表为单位,里面虽然有嵌套类型,但是也是看作一个整体)
# 统计数字类型
print(len(1000))  # 错误
print(len(1000.1000))  # 错误

# 统计字符串
print(len('wangmao'))  # 7

# 统计列表
print(len([1, 2, 3, ['a', 'b', 'c']]))  # 4

# 统计字典
print(len({'name': 'wangmao', 'sex': 'male', 'age': 18}))  # 3

range(),循环取值

  • 缺点:当前你能看到列表中的值的个数,但是许多情况下你并不知道列表有多少个值。
# 需求:循环取出students中的值
students = ['egon', 'lxx', 'alex']
for i in range(3):
    print(students[i])

len()+range(),循环取值

# 需求:循环取出students中的值
students = ['egon', 'lxx', 'alex']
for i in range(len(students)):  # len(stduents)=3,范围0~3,刚好它可以充当列表的索引
    print(students[i])
    
# 需求:取出li中索引为0 2 4的值
li = ['a', 'b', 'c', 'd', 'e', 'f']
for j in range(len(li), 2): # len(li)=6,范围0 2 4
    print(li[j])

四、for+break

# 需求:用户登陆验证,错误打印提示信息,共计错误3次退出(达到同while循环一样)
# 运用for+range,达到for循环循环的条件取决去in后面数据类型的个数
for i in range(3):
    username = input('please input your username>>:')
    password = input('please input your password>>:')
    if username == 'wangmao' and password == '123':
        print('login successfully!!')
        break
    else:
        print('login failure。。。。')

五、for+continue

# 需求:数字1~5,2 3不打印
for i in range(5):
    if i == 2 or i == 3:
        continue
    print(i)       

六、for+else

  • 优点:能为3次错误正常退出,做总结错误提示信息
# 需求:用户登陆验证,错误打印提示信息,共计错误3次退出
for i in range(3):
    username = input('please input your username>>:')
    password = input('please input your password>>:')
    if username == 'wangmao' and password == '123':
        print('login successfully!!')
        break
    else:
        print('login failure...')
else:
    print('Account password error reached the maximun limit, exit the program')

七、总结

while循环与for循环区别

相同之处

  • 都是循环,都是用来做重复的事情

不同之处

  • while循环通常循环执行某段代码,for循环通常用来进行循环取值
  • while循环的循环次数取决于条件什么时候为假,for循环循环的条件取决于in后的数据类型所包含的值的个数

for+range

  • for+range用来产生一个数字序列
  • 用途1:用来重复n次,执行某段代码
  • 用途2:range可以用来产生数字序列,数字对应的是列表等索引类型的索引值,所以说,for+range是可以按照索引去遍历列表这种索引类型的数据的。

for+range+len

  • 用途:len统计可遍历数据类型,作为rang的结束值,for循环遍历,可以取到不知道该数据类型中值的个数的情况。

猜你喜欢

转载自www.cnblogs.com/yang1333/p/12364169.html
01
#01
今日推荐