• 技术文章 >后端开发 >Python教程

    Python之条件判断和循环

    高洛峰高洛峰2017-02-17 11:17:09原创743

    Python之条件判断和循环

    1. Python之if语句

    score = 75
    if score >= 60:
        print 'passed'

    2. Python之if-else

    score = 55
    if score >= 60:
        print 'passed'
    else:
        print 'failed'

    3. Python之if-elif-else

    score = 85
    if score >= 90:
        print 'excellent'
    elif score >= 80:
        print 'good'
    elif score >= 60:
        print 'passed'
    else:
        print 'failed'

    4. Python之for循环

    L = [75, 92, 59, 68]
    sum = 0.0
    for x in L:
        sum = sum + x
    print sum / 4

    5. Python之while循环

    sum = 0
    x = 1
    while x < 100:
        sum = sum + x
        x = x + 2
    print sum

    6. Python之break退出循环

    sum = 0
    x = 1
    n = 1
    while True:
        if n > 20:
            break
        sum = sum + x
        x = x * 2
        n = n + 1
    print sum

    7. Python之continue继续循环

    sum = 0
    x = 0
    while True:
        x = x + 1
        if x > 100:
            break
        if x % 2 == 0:
            continue
        sum = sum + x
    print sum

    8. Python之continue继续循环

    for x in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
        for y in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
            if x < y:
                print x * 10 + y

    更多Python之条件判断和循环 相关文章请关注PHP中文网!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:Python
    上一篇:Python之Dict和Set类型 下一篇:Python之List和Tuple类型
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• Python图像处理之PIL库• python数据可视化之饼状图的绘制• 实例讲解Python批量修改文件名• Python实例详解pdfplumber读取PDF写入Excel• 归纳总结Python中的装饰器知识点
    1/1

    PHP中文网