day08-循环之for循环

for循环-前言
'''
1、什么是for循环 循环就是重复做某件事,for循环是python提供第二种循环机制 2、为何要有for循环 理论上for循环能做的事情,while循环都可以做 之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简洁 3、如何用for循环 语法: for 变量名 in 可迭代对象:# 可迭代对象可以是:列表、字典、字符串、元组、集合 代码1 代码2 代码3 ... '''

一:for基本使用之循环取值
案例1:列表循环取值
简单版

l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
for x in l:      # x='lxx_dsb'
    print(x)
结果如下:
alex_dsb
lxx_dsb
egon_nb

复杂版:

l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
i = 0
while i < 3:
    print(l[i])
    i += 1
结果如下:
alex_dsb
lxx_dsb
egon_nb

案例2:字典循环取值
简单版

dic={'k1':111,'k2':2222,'k3':333}
for k in dic:   # 字典形式默认循环的是key
    print(k,dic[k])  #  利用key可以打印对应的value
结果如下:
k1 111
k2 2222
k3 333

复杂版:while循环可以遍历字典,太麻烦了 就不写了

案例3:字符串循环取值

简单版

msg="you can you up,no can no bb"
for x in msg:
    print(x)   # 字符串的话,中间空格也算字符,会依次打印每个字符串
结果如下:
y
o
u

c
a
n

y
o
u

u
p
,
n
o

c
a
n

n
o

b
b

复杂版:while循环可以遍历字典,太麻烦了


二:总结for循环与while循环的异同
1、相同之处:都是循环,for循环可以干的事,while循环也可以干
2、不同之处:
  while循环称之为条件循环,循环次数取决于条件何时变为假
  for循环称之为"取值循环",循环次数取决in后包含的值的个数

for x in [1,2,3]:
    print('===>')
    print('8888')   #  这里虽然在遍历列表,但是没有使用列表中的数据,仅循环三次
结果如下:
===>
8888
===>
8888
===>
8888

三:for循环控制循环次数:range()
3.1、in后直接放一个数据类型来控制循环次数有局限性:
  当循环次数过多时,数据类型包含值的格式需要伴随着增加

for x in 'a c': # 这样就可以实现循环三次的功能了
    inp_name=input('please input your name:   ')
    inp_pwd=input('please input your password:   ')

3.2、range功能介绍  

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 
>>> range(1,9) # 1...8
[1, 2, 3, 4, 5, 6, 7, 8]
>>> 
>>> range(1,9,1) # 1 2 3 4 5 6 7 8 
[1, 2, 3, 4, 5, 6, 7, 8]
>>> range(1,9,2) # 1 3 5 7 
[1, 3, 5, 7]

for i in range(30):  # 循环遍历30次,顾头不顾尾,(0,1,2,3...28,29)
print('===>')  # 打印30次,也就是30行


for+break: 同while循环一样
for+else:同while循环一样

username='egon'
password='123'
for i in range(3):
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')

    if inp_name == username and inp_pwd == password:
        print('登录成功')
        break
else:
    print('输错账号密码次数过多')

四:range补充知识(了解)
1、for搭配range,可以按照索引取值,但是麻烦,所以不推荐

l=['aaa','bbb','ccc'] # len(l)
for i in range(len(l)):
    print(i,l[i])
结果如下:
0 aaa
1 bbb
2 ccc

for x in l:
  print(l)
2、range()在python3里得到的是一只"会下蛋的老母鸡"

  在python2中,有range中有多少他会打印多少,这样数量过多会占用过多资源,是属于浪费的,所以python3中改变了这种形式,

只取头尾显示

C:UsersAdministrator>python3
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> range(5)
range(0, 5)
>>> exit()

C:UsersAdministrator>python2
Python 2.7.17 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> range(5)
[0, 1, 2, 3, 4]
>>>

五:for+continue

for i in range(6):  # 0 1 2 3 4 5
    if i == 4:
        continue
    print(i)
结果如下:
0
1
2
3
5

六:for循环嵌套:外层循环循环一次,内层循环需要完整的循环完毕

for i in range(2):
    print('外层循环-->', i)
    for j in range(3):
        print('内层-->', j)
结果如下:
外层循环--> 0
内层--> 0
内层--> 1
内层--> 2
外层循环--> 1
内层--> 0
内层--> 1
内层--> 2

补充:终止for循环只有break一种方案


补充知识点:

1、print之逗号的使用

print('hello %s' % 'egon') # 这个是格式化输出
# hello egon
print('hello','world','egon') # 逗号代表了以空格为分隔符
# hello world egon
2、换行符
print('hello ')
print('world')
hello
# 到这里是第一行的 换行,加上默认的print的结尾换行符
world # 这里是正常的print换行
3、print值end参数的使用
# print('hello ',end='') # 这个引号里是每行的结束符,默认是换行符,可以修改
# print('word')
print('hello',end='*')
print('world',end='*')
hello*world*
原文地址:https://www.cnblogs.com/xiao-zang/p/12456264.html