美文网首页
Python零基础5:for循环、while循环

Python零基础5:for循环、while循环

作者: Iphone60Plus | 来源:发表于2020-03-15 21:23 被阅读0次

for……in……循环

# for循环三要点:1空房间(元素=变量:唯一性)2排除办业务的人3业务流程
for i in [1,2,3,4,5]:
    print(i*3)
3
6
9
12
15

# 列表、字典、字符串都可以被遍历
for i in '孙悟空':
    print(i)
孙
悟
空

#整数、浮点数不可以被遍历

range()函数

# 取头不取尾,步长为3,range(默认为0,10,默认为1)
for i in range(0,10,3):
    print(i)
0
3
6
9

# range() 第3参数默认为1
for i in range(1,4):
    print('我可以的')

# range() 第1参数默认为0,第3参数默认为1
for i in range(5):
    print('我可以的')

for循环:办事流程

#字典中键进办公室后,单独出来。
d = {'小明':'醋','小红':'油','小白':'盐','小张':'米'}
for i in d:
    print(d[i])
醋
油
盐
米

while循环

#while循环2个要点:1放行条件2办事流程
a = 0
while a <5:
    a = a + 1
    print(a)
1
2
3
4
5

#print(a)未缩进,没在while办事流程内
a = 0
while a <5:
    a = a + 1
print(a)
5

两种循环对比

# 确定数量用for循环
for i in range(3):
    print('我很棒')

# 满足条件用while循环
a = ''
while a != '558':
    a = input('请输入密码:')
print('恭喜进入')

pop()函数

# pop()函数为提取和删除元素的合集,默认提取删除最后元素
tudents = ['小明','小红','小刚']
for i in range(3):
    students.append(students.pop(0))
    print(students)
['小红', '小刚', '小明']
['小刚', '小明', '小红']
['小明', '小红', '小刚']

相关文章

  • Python零基础5:for循环、while循环

    for……in……循环 range()函数 for循环:办事流程 while循环 两种循环对比 pop()函数

  • Python 学习笔记 - 循环 while

    Python 循环 - while Python 中有 for 循环 while 循环 如果条件符合,while...

  • python循环执行

    python有两种循环,while循环和for循环。 python循环的流程图如下: while循环 python...

  • 我的python学习笔记-第十天

    循环语句 Python中的循环语句有 for 和 while。 while 循环 Python中while语句的一...

  • 3 Python基础

    Python基础 1.循环语句 while循环 for循环 1.1 循环语句的基本使用 1.2 综合小案例 1.3...

  • Python学习-循环

    查看所有Python相关学习笔记 while循环,for循环,break,continue 循环 while循环 ...

  • 2018-10-30

    Python3 while循环 0基础的我最近在学python,看到while循环这个章节时,初看很简单,但自己在...

  • 第五周python学习

    Python提供了for循环和while循环(在Python中没有do..while循环): 循环类型描述 whi...

  • 2019实战第二期-控制读书打卡

    -----学习《Python基础教程第3版》读书笔记----- 条件 循环 while for 跳出循环

  • Python3 & 循环语句

    Python 提供了 for 循环和 while 循环(在 Python 中没有 do..while 循环)。 W...

网友评论

      本文标题:Python零基础5:for循环、while循环

      本文链接:https://www.haomeiwen.com/subject/xiwvehtx.html