运维八一 运维八一
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)

运维八一

运维,运维!
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)
  • Go

  • Python

    • 前言

    • python基础

      • 变量
      • 格式化输出和运算符
      • 数据类型
      • 流程控制
        • 1. if判断
          • 1.1 单分支
          • 1.2 双分支
          • 1.3 多分支
        • 2. while循环
          • 2.1 while语法
          • 2.2 while ...else
          • 2.3 循环控制
          • 2.4 死循环
      • 文件处理
      • 字符编码原理
      • 函数概念
      • 函数嵌套和作用域
      • 装饰器-迭代器-生成器
      • 递归函数和匿名函数
      • 内置函数
  • Shell

  • Java

  • Vue

  • 前端

  • 编程浅尝
  • Python
  • python基础
lyndon
2023-06-07
目录

流程控制

# 1. if判断

# 1.1 单分支

if 条件

​ 满足条件后要执行的代码

age_of_oldboy=50
if age_of_oldboy > 40:
    print('too old,time to end')
1
2
3

# 1.2 双分支

if 条件

​ 满足条件执行代码

else:

​ if条件不满足就走这段

age_of_oldboy=50

if age_of_oldboy > 100:
    print('too old,time to end')
else:
    print('impossible')
1
2
3
4
5
6

# 1.3 多分支

if 条件:

满足条件执行代码

elif 条件:

上面的条件不满足就走这个

elif 条件:

上面的条件不满足就走这个

elif 条件:

上面的条件不满足就走这个

else:

上面所有的条件不满足就走这段

age_of_oldboy=91

if age_of_oldboy > 100:
    print('too old,time to end')
elif age_of_oldboy > 90:
    print('age is :90')
elif age_of_oldboy > 80:
    print('age is 80')
else:
    print('impossible')
1
2
3
4
5
6
7
8
9
10

# 2. while循环

# 2.1 while语法

while 条件: #只有当while后面的条件成立时才会执行下面的代码

执行代码...

count=1
while count <= 3:    
    print(count)    
    count+=1
1
2
3
4

练习:打印10内的偶数

count=0
while count <= 10:    
    if count % 2 == 0:        
        print(count)    
    count+=1
1
2
3
4
5

# 2.2 while ...else

当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

count=1
while count <= 3:
    if count == 4:
        break
    print(count)
    count+=1
else: #while没有被break打断的时候才执行else的子代码
    print('=========>')
1
2
3
4
5
6
7
8

# 2.3 循环控制

  • break 用于完全结束一个循环,跳出循环体执行循环后面的语句
  • continue 终止本次循环,接着还执行后面的循环,break则完全终止循环

例:break

count=1
while count <= 100:
    if count == 10:        #当count=10时,就跳出本层循环
        break          #跳出本层循环
    print(count)
    count+=1
1
2
3
4
5
6

例:continue

count=0
while count < 5:
    if count == 3:
        count+=1        #当count=3时,就跳出本次循环,不打印3,进入下一次循环
        continue        #跳出本次循环
    print(count)
    count+=1
1
2
3
4
5
6
7

练习:使用continue实现打印10以内的偶数

count=0
while count <= 10:
    if count % 2 != 0:
        count+=1
        continue
    print(count)
    count+=1
1
2
3
4
5
6
7

# 2.4 死循环

while 是只要后边条件成立(也就是条件结果为真)就一直执行,一般写死循环可以:

while True:

执行代码。。。

还有一种:(好处是可以使用一个变量来控制整个循环)

tag=True

while tag:

执行代码。。。

while tag:

​ 执行代码。。。

count=0
tag=True
while tag:
    if count > 2:
        print('too many tries')
        break
    user=input('user: ')
    password=input('password: ')
    if user == 'egon' and password == '123':
        print('login successful')
        while tag:
            cmd=input('>>: ')
            if cmd == 'q':
                tag=False
                continue
            print('exec %s' %cmd)

    else:
        print('login err')
        count+=1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 2023/07/04, 15:10:38
数据类型
文件处理

← 数据类型 文件处理→

最近更新
01
ctr和crictl显示镜像不一致
03-13
02
alpine镜像集成常用数据库客户端
03-13
03
create-cluster
02-26
更多文章>
Theme by Vdoing | Copyright © 2015-2024 op81.com
苏ICP备18041258号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式