• Python中的流程控制(if、while、for)


    流程控制之if判断

    age = 18
    if age < 20:
        print("青年!")
    elif 20 < age < 35:
        print("中年!")
    else:
        print("老年!")
    

    流程控制之while循环

    一、语法

    循环就是一个不停重复的过程。

    # 当条件为正的时候,执行code1、code2,code3
    while 条件:
    	code 1
        code 2
        code 3
        ···
    while True:
        print('1')
        print('2')
    

    二、while + break

    break的意思是跳出当前循环。

    while True:
        print('1')
        print('2')
        break
        print('3')
    print('我出来啦')
    
    # 打印结果
    # 1
    # 2
    # 我出来啦
    

    三、while + continue

    continue的意思是跳出本次循环,进入下一次循环

    n = 1
    while n < 5:
        if n == 3:
            n += 1
            continue
        print(n)
        n += 1
    
    # 打印结果
    # 1
    # 2
    # 4
    

    流程控制之for循环

    一、语法

    for循环是用来循环容器的。

    list_1 =[1, 2, 3,]
    for i in list_1:
        print(i)
        
    # 打印结果
    # 1
    # 2
    # 3
    

    二、range()

    for i in range(1,4):
        print(i)
    
    # 打印结果
    # 1
    # 2
    # 3
    
  • 相关阅读:
    ThreadPoolExecutor使用介绍
    apache和tomcat区别(转)
    ThreadFactory的理解
    Tomcat报45秒无法启动错误修改方法
    快速排序
    冒泡排序
    矩阵快速幂
    CF#524-C
    CF#524-B
    hdu3308—LCIS
  • 原文地址:https://www.cnblogs.com/17vv/p/11283770.html
Copyright © 2020-2023  润新知