学习python的day3之if、while、for

一、循环

在python中,循环分为while循环和for循环两种,两种循环的最终实现效果相同

循环的作用是为了让代码更高效的重复执行

1.1while循环语句

语法格式:

while 条件语句:

  循环体1

  循环体2

  ......

#需求:输出五次hello python!
i = 0
while i < 5 :
    print('hello python!')
    i += 1
#需求:1-100累加和
'''
1.准备做加法运算的数据1-100,增量为1
2.准备一个变量保存运算的结果
3.循环做加法运算
    1)做累加和的变量自增,增量为1
5.输出结果
'''
i = 1
result = 0
while i<=100:
    result += i
    i += 1
print(f'1-100累加和的结果是{result}')

注意:在写while循环时,有计数器变量一定要有累加,不然程序就会进入死循环

1.2 break和continue

break和continue是循环中满足一定条件退出循环的两种不同方式

break:

例如:一共有5个苹果,吃到第三个就吃饱了,第四个第五个就不用再吃了,这就是break控制循环流程,即终止此循环。

#需求:一共5个苹果,吃三个苹果就吃饱了
'''
1.准备一个保存吃了几个苹果的变量
2.准备循环
    1)是否吃饱了
        吃饱了退出循环
    2)输出这是吃的第几个苹果
    3)变量自增  增量为1
3.输出总共吃了几个苹果
'''
i = 0
while i <= 5:
    if i == 4:
        print('我吃饱了')
        break
    print(f'这是我吃的第{i}个苹果')
    i += 1
print(f'我总共吃了{i-1}个苹果')

continue:

例如:一共有5个苹果,吃到第三个发现这个苹果有虫子,这个苹果就不会再吃了,直接开始吃第四个和第五个苹果,这就是continue控制循环流程,即退出当前循环体的此次循环,开始当前循环体的下一次循环。

#需求:一共有5个苹果,迟到第三个苹果时发现这个苹果有虫子,就不吃第三个苹果了,直接开始吃第四个苹果
'''
1.准备一个保存吃了几个苹果的变量   并用该变量做while循环的条件
2.进行循环
    1)是否吃到有虫子的苹果
        输出该苹果有虫子
        跳过该次循环,进行下一次循环
    2)变量自增,增量为1
'''
i = 0
while i <= 5:
    if i == 3:
        print(f'第{i}个苹果有虫子')
        i += 1              #此处的变量自增才能继续进行循环
        continue
    i += 1

注意:在用continue控制循环流程时,continue所在的地方还需计数器进行一次变量自增,才不会使程序进入死循环

1.3 while循环的嵌套

所谓while循环嵌套,就是在一个while循环里再写一个或多个while循环,与之前的基础语法是相同的

语法格式:

while 条件语句1:

  条件语句1成立执行的代码

  while 条件语句2:

    条件语句2成立执行的代码

#需求:输出一次hello python 就输出两次i love you;一共输出三次
i = 0
while i < 3:
    print('hell0 python')
    j=0
    while j < 2:
        print('i love you')
        j += 1
    i += 1

 2.for循环

语法:

for 临时变量 in 序列:

  重复执行的代码1

  重复执行的代码2

  ...

#循环打印字符串中的字符
str1 = 'as_scheduled'
for i in str1:
    print(i)

break控制for循环

str1 = 'as_schedued'
for i in str1:
    if i == '_':
        print('跳过该次循环,继续下一次循环')
        continue
    print(i)

continue控制for循环

str1 = 'as_schedued'
for i in str1:
    if i == '_':
        print('跳过该次循环,继续下一次循环')
        continue
    print(i)

3.while...else

语法:

while 条件语句:

  重复执行的代码1

  重复执行的代码2

  ....

else:

  while循环正常结束执行的代码

i = 0
while i < 5:
    print('as_schedueld!')
    i += 1
else:
    print('welcome you!as_scheduled!')

while...else之break

i = 0
while i < 5:
    if i == 3:
        print('hello python')
        break
    print('as_schedueld!')
    i += 1
else:
    print('welcome you!as_scheduled!')

while...else之continue

i = 0
while i < 5:
    if i == 3:
        print('hello python')
        i += 1
        continue
    print('as_schedueld!')
    i += 1
else:
    print('welcome you!as_scheduled!')

4.for...esle

语法:

for 临时变量 in 序列:

  重复执行的代码1

  重复执行的代码2

  ....

else:

  for循环正常结束执行的代码

str1 = 'as_schedueld'
for i in str1:
    print(i,end='.')
else:
    print()
    print('hello')

for...else之break

str1 = 'as_schedueld'
for i in str1:
    if i == '_':
        print(end='~')
        break
    print(i,end='//')
else:
    print()
    print('hello')

for...else之continue

str1 = 'as_schedueld'
for i in str1:
    if i == '_':
        print(end='~')
        continue
    print(i,end='//')
else:
    print()
    print('hello')
原文地址:https://www.cnblogs.com/scheduled/p/13603875.html