循坏结构
1.计算机基础2.编程语言和Python语言介绍3.八大基本数据类型4.python语法入门变量和常量5.python和pycharm相关补充6.计算机五大部件补充7.文件操作8.编码和解码9.字符编码10.基本数据类型的内置方法
11.循坏结构
12.流程控制13.基本运算符14.程序与用户交互15.迭代器16.闭包函数17.装饰器18.多层语法糖嵌套19.函数的参数20.函数的可变长参数21.命名关键字参数22.名称空间与作用域23.异常处理24.深浅拷贝问题25.函数的定义和调用26.控制文件读写内容的模式27.主动控制文件内指针移动28.文件的操作方法29.垃圾回收机制GC30.可变与不可变类型31.元类32.内置方法33.pickle模块34.单例模式35.方法补充issubclass和isinstance36.多态37.继承38.面向对象的三大特性39.封装40.内置函数41.面向对象42.logging模块43.subprocess模块44.正则45.datetime模块46.time模块47.绝对路径和相对路径48.random模块49.os模块50.hashlib模块51.模块与包52.json模块53.生成器54.三元运算符(一)循环结构
(1)什么是循环结构
- 循环结构是一种程序控制结构,用于反复执行一组语句,直到满足某个条件为止。
- 循环结构使得程序可以更有效地重复执行某段代码,节省了编写重复代码的工作。
(2)循环结构的作用
- 循环结构的主要作用是重复执行一组语句,直到满足某个条件。
- 这种重复执行的过程可以是固定次数的,也可以是根据条件动态确定的。
- 循环结构使得程序可以更灵活、高效地处理需要重复执行的任务。
(3)while循环
(1)语法
- while是循环的关键字
- 条件时循环的条件,条件为真,循坏体就一直执行
- 循环体时需要重复执行的代码块
while condition:
#循坏体
(2)使用
count=1
while count<5:
print(count)
count+=1
#执行结果:
#1
#2
#3
#4
(3)案例:
(4)for循环
(1)语法
- for是循环关键字
variable
是循环变量,它会在每次循环中取sequence
中的一个值。sequence
是一个序列,可以是列表、元组、字符串等。
for variable in sequence:
#循环体
(2)使用
list = [1, 2, 3, 4, 5]
for i in list:
print(i)
#执行结果:
# 1
# 2
# 3
# 4
# 5
(5)退出循环break
(1)语法
while condition:
# 循环体
if some_condition:
break # 退出循环
(2)使用
count=1
while count<5:
print(count)
count+=1
if count==3:
print(count)#3
break#退出循环
#执行结果:
#1
#2
#3
(1)登录案例(while循环----break结束循环)
-
用户登录有三次机会,如果输入正确就直接结束循环
count=0 name='syh' pwd='123' while count<3: username=input('请输入你的用户名:') password=input('请输入你的密码:') if username==name and password==pwd: print('登录成功') break#输入正确的用户名密码后。直接结束循环 else: print('登录失败') count+=1
(6)退出循环continue
(1)语法
while condition:
# 循环体
if some_condition:
continue # 跳过当前循环,继续下一次循环
(2)使用
count=0
while count<5:
count += 1
if count==3:#当count=3时,跳出本次循环,继续循环
continue
print(count)
#执行结果:跳过count=3
#1
#2
#4
#5
(7)无限循环(死循环)
- 有时候,我们需要程序在满足某个条件时一直执行,这就需要用到无限循环。
- 最简单的无限循环可以通过
while
语句实现,条件永远为真。
while True:
print("hello world")
- 这段代码会一直输出 "hello world",因为
while True:
的条件永远为真,所以循环不会停止。 - 在实际编程中,我们可能会在无限循环中加入某个条件来实现根据需要退出循环的逻辑。
(8)标志位
(1)语法
- flag = True # 标志位
flag = True
while flag:
#循坏体
if some_condition:
flag = False # 修改标志位,退出循环
(2)使用
flag=True
while flag:
username_input=input('请输入hello:')
if username_input == 'hello':
print('hello!!!!')
flag=False#修改标志位,退出循环
else:
print('no hello ')
#执行结果:
#请输入hello:hello
#hello!!!!
(9)循环分支 while +else
- while循环后面可以跟上else
- 当while循环执行完毕后没有停止,就会继续执行else
count=0
while count<5:
print('hello')
count+=1
else:
print('stop')
#执行结果
#hello
#hello
#hello
#hello
#hello
#stop
补充 range关键字
(1)遍历数字序列
- 语法
- range(stop)从0开始到stop结束
- range(start, stop)从start开始到stop结束
- range(start, stop, step)从start开始到stop结束step是步长
for i in range(stop): #从0开始到stop结束
# 循环体
for i in range(start, stop):#从start开始到stop结束
# 循环体
for i in range(start, stop, step):#从start开始到stop结束step是步长
# 循环体
- 使用
#遍历数字序列
for i in range(3):
print(i)
# 0
# 1
# 2
#指定区间
for i in range(1, 5):
print(i)
# 1
# 2
# 3
#指定步长
for i in range(1, 10, 3):
print(i)
# 1
# 4
# 7
(2)range+len遍历序列
(1)原理
list = [1, 2, 3, 4, 5, 6]
for i in range(len(list)):
#循环体
(2)使用
list = [1, 2, 3, 4, 5, 6]
for i in range(len(list)):
print(f'{i},{list[i]}')
#执行结果:
# 0,1
# 1,2
# 2,3
# 3,4
# 4,5
# 5,6
(3)range创建
(1)原理
numbers = list(range(start, stop, step))
(2)使用
numbers = list(range(0, 10))
print(numbers)
# 输出:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(二)循环分支嵌套
- 循环结构和分支结构可以嵌套在一起,形成复杂的程序逻辑。
while True:
score = input("请输入您的成绩:")
if score.isdigit():
score = int(score)
if score > 100 or score < 0:
print("您的输入有误!")
elif score > 90:
print("成绩优秀")
elif score > 70:
print("成绩良好")
elif score > 60:
print("成绩及格")
else:
print("成绩不及格")
else:
print("请输入一个数字")
#执行结果
#请输入您的成绩:66
#成绩及格
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)