• Python for循环使用 else 语句


    Python for循环使用 else 语句:

    else:当 for 所有的语句代码块正常运行完,才会运行 else 语句。

    示例:

    '''
    for 迭代对象 in 序列:
        代码块(一行语句或多行代码)
    else:
        代码块(一行语句或多行代码)
    '''

    程序:

    # 字符串
    strs = "Hello World."
    for i in strs:
        print(i,end=" ")
    #     H e l l o   W o r l d . 运行OK
    else:
        print("运行OK")
    print()
    
    # 列表
    lst = [1,2.3,8+9j,'abc',(4,5),{7,8,'a'},{'a':4}]
    for i in lst:
        print(i,end=" ")
    #     1 2.3 (8+9j) abc (4, 5) {8, 'a', 7} {'a': 4} 运行OK
    else:
        print("运行OK")
    print()
    
    # 元组
    tup = (1,2,3,4,5,6)
    for i in tup:
        print(i,end=" ")
    #     1 2 3 4 5 6 运行OK
    else:
        print("运行OK")
    print()
    
    # 字典
    dic = {'a':{'b':123},(4,5):'str',123:[4,5,6]}
    # 键为不可变类型 字符串、元组、数字
    for i in dic:
        print(i,end=" ")
    #     a (4, 5) 123 运行OK
    else:
        print("运行OK")
    print()
    
    # 集合
    set_1 = {1,2.5,'a',(7,8,9)}
    for i in set_1:
        print(i,end=" ")
    #     1 2.5 a (7, 8, 9) 运行OK
    else:
        print("运行OK")
    print()

    2020-02-06

  • 相关阅读:
    JVM笔记
    数论之GCD
    a+b problem
    table 随着内容自动适应宽度
    jquery 去除 css 的 background-image 样式
    jquery.table2excel,将HTML的table标签数据导出成excel
    C# MVC 视图 计算某一个列的总和
    template.js 求和 问题
    JavaScript 数组去重
    c# 获取api 数据
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12268076.html
Copyright © 2020-2023  润新知