• for循环和字典预习


    print("*" *8)
    for a in range(1,9):
    print(a,end="")
    #1-9的奇数
    print()
    for a in range(1,10,2):
    print(a,end="")
    #1-100的和
    print()
    sum=0
    for a in range(1,101):
    sum+=a
    print(sum)
    print()
    #字典及字典的常用功能
    D={
    'k1':'v1',
    'k2':'v2',
    'k3':'v3',

    }
    print("D的类型是:",type(D)) # <class 'dict'>
    print(len(D))# 3
    print(D.keys())#dict_keys(['k1', 'k2', 'k3'])
    print(D.values())#dict_values(['v1', 'v2', 'v3'])
    print(D.items())# dict_items([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])

    print("-------------------")
    F={
    'm1':[1,2,3,4],
    1:2,
    'm2':'v2',
    'm3':(1,2,3),
    'm4':{
    'name':'vae',
    'age':18,
    }
    }
    print("F的类型是:",type(F))#<class 'dict'>
    print(len(F))# 5
    print(F.keys())#dict_keys(['m1', 1, 'm2', 'm3', 'm4'])
    print(F.values())#dict_values([[1, 2, 3, 4], 2, 'v2', (1, 2, 3), {'name': 'vae', 'age': 18}])
    print(F.items())#dict_items([('m1', [1, 2, 3, 4]), (1, 2), ('m2', 'v2'), ('m3', (1, 2, 3)), ('m4', {'name': 'vae', 'age': 18})])
    print('#获取值的方式1')

    print(F[1])# 2
    print(F["m1"])#[1, 2, 3, 4]
    print(F['m2'])#v2
    print(F['m3'])#(1, 2, 3)
    print(F["m4"])#{'name': 'vae', 'age': 18}
    # print(F["m5"]) KeyError: 'm5'
    print('#获取值的方式2')
    print(F.get('m1'))#[1, 2, 3, 4]
    print(F.get('m2'))#v2
    print(F.get("m3"))# (1, 2, 3)
    print(F.get("m4"))#{'name': 'vae', 'age': 18}
    print(F.get(1))# 2
    print(D.get('m6'))#无值也不报错 None
    print(D.get('m6',"True"))#True
    print('#获取值的方式3')#此方法会修改字典
    print(F.setdefault('m1'))#[1, 2, 3, 4]
    print(F.setdefault('m7'))#None
    print(F)#{'m1': [1, 2, 3, 4], 1: 2, 'm2': 'v2', 'm3': (1, 2, 3), 'm4': {'name': 'vae', 'age': 18}, 'm7': None}


  • 相关阅读:
    (3)C++复合类型
    (2)C++基本类型
    (7)js调试
    Oracle语句优先级
    oracle排序问题
    jsp四大对象
    postgresql时间加减计算
    全角空格,跟汉字一样宽
    bzoj1433 [ZJOI2009]假期的宿舍 最大流
    BZOJ 1264 AHOI2006 基因匹配Match 动态规划+树状数组
  • 原文地址:https://www.cnblogs.com/startl/p/11731558.html
Copyright © 2020-2023  润新知