python小白日常之循环

一 程序执行的三大流程
1.定义
一共有三种流程方式
顺序–从上到下,顺序执行代码

步骤一
步骤二
步骤三

分支–根据条件判断,决定执行代码的分支

Created with Raphaël 2.2.0 开始 确认? 条件成立时,我的操作 结束 条件不成立时,我的操作 yes no

循环–谈特定的代码重复执行

Created with Raphaël 2.2.0 开始 条件? 循环代码 处理条件 条件不成立时,我的操作 yes no

拓展:流程图的处理及优化
https://blog.csdn.net/baidu_38172402/article/details/88757379

二 while循环
1.定义
指定的代码 重复执行
2.基本语法

Created with Raphaël 2.2.0 开始 While? Code block Continue Program yes no

案例:

#  定义计数器
i = 1

while i <= 5:
    print("hello world")
    i += 1

print("循环结束时i的值为:{}".format(i))

运行结果:

hello world
hello world
hello world
hello world
hello world
循环结束时i的值为:6
# 1定义一个最终结果的变量
result = 0
#  2定义计数器
i = 1
# 定义循环条件
while i <= 100:
    result += i
    i += 1

print("0-100求和结果为:{}".format(result))
# 1定义一个最终结果的变量
result = 0
#  2定义计数器
i = 1
# 定义循环条件
while i <= 100:
    if i%2 == 0:
        result += i
    i += 1

print("0-100的偶数求和结果为:{}".format(result))

运行结果:

0-100求和结果为:5050
0-100的偶数求和结果为:2550

补充:
在pycharm中运行当前的文件可以右键运行或者是Alt+Shift+F10快捷键

三 break和continue
1.定义:

  • break和continue是专门在循环中使用关键字
  • break某一个条件满足时,退出循环,不再执行后续重复的代码
  • continue 某一个条件满足时,不执行后续重复的代码,开始下一次循环
  • break 和continue 只针对当前所有循环有效
    在这里插入图片描述
    在这里插入图片描述
    案例:
i = 0
while i < 10:
    if i == 3:
        break
    print(i)
    i += 1

print("继续往下执行")

print("*"*20)

j = 0
while j < 10:
    if j == 3:
        j += 1
        continue
    print(j)
    j += 1

print("继续往下执行")

运行结果:

0
1
2
继续往下执行
********************
0
1
2
4
5
6
7
8
9
继续往下执行

使用while和for循环编写99乘法表

while i <= 9:
    j = 1
    while j <= i:
        print("{} * {} = {} ".format(i,j,i*j),end="\t")
        j += 1
    print()
    i += 1

for i in range(1,10):
    for j in range(1,i+1):
        print("{} * {} = {} ".format(j, i, i * j),end="\t")
    print()

四 字符串中的转义字符
\t 在控制台输出一个 制表符,协助咋输出文本时 垂直方向 保持对齐
\n 在控制台输出一个 换行符
制表符 的功能是在不使用表格的情况下在垂方向按列对齐文本

转义描述
\\反斜杠符号
\’单引号
\"双引号
\n换行
\t横向制表符
\r回车

案例:

# 换行
print("*"*10)
print()
print("*"*10)
print("\n")
print("*"*10)
# 转义
print('add"')
print('add\"')

运行结果:

**********

**********


**********
add"
add"

五 for循环
1.定义

  • 可以遍历任何序列类型
    • (包括元组,字符串,列表,字典)—字典遍历的是key值
  • 通过遍历对象的长度来控制循环次数
  • 遍历完毕,循环即结束
  • 格式:
    在这里插入图片描述

2.遍历的方式

  • 方式一
    案例:
    可以用过索引取值(下标来遍历)
sequence_1 = "abc"
one_index = (0,1,2)
for key in one_index:
print(sequence_1[key])

运行结果:

a
b
c
  • 方式二 直接遍历序列
one_list = ["苹果","橘子","柠檬","香蕉","西瓜","哈密瓜"]

for item in one_list:
    print(item)  #默认\n换行

运行结果:

苹果
橘子
柠檬
香蕉
西瓜
哈密瓜

遍历的次数由值的个数决定—当值为0个时不会执行下面的代码

demo = ""  # 值为0个
for i in demo:
    print(i) # 不会执行,运行结果为空

3.遍历字典

  • 方式一:只对键的遍历
user_info = {"name":"苹果","age":18,"heifht":1.75,"weight":45}
for key in user_info:
	print(key)

运行结果:

name
age
heifht
weight
  • 方式二:对键和值都进行遍历
user_info = {"name":"苹果","age":18,"heifht":1.75,"weight":45}
for item in user_info.items():
	print(item)

运行结果:

('name', '苹果')
('age', 18)
('heifht', 1.75)
('weight', 45)
user_info = {"name":"苹果","age":18,"heifht":1.75,"weight":45}
for item in user_info.items():
	print(item)

运行结果:

('name', '苹果')
('age', 18)
('heifht', 1.75)
('weight', 45)
user_info = {"name":"苹果","age":18,"heifht":1.75,"weight":45}
for key,value in user_info.items():
	print("key = {}, value = {}".format(key,value))

运行结果:

key = name, value = 苹果
key = age, value = 18
key = heifht, value = 1.75
key = weight, value = 45

拓展元组拆包:

name,age = ("苹果",18)
print(name,age)
name,age = ("苹果",18,None)
#  当数量不匹配时会报错
print(name,age)

运行结果:

苹果 18
Traceback (most recent call last):
  File "F:/py37/py16/demo_031801.py", line 287, in <module>
    name,age = ("苹果",18,None)
ValueError: too many values to unpack (expected 2)

演练:

# 打印1到10之和
i = 0
result = 0
for i in range(1,11):
    result += i
print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值