自学python

                                       第三天

1.学习了字符串的常用操作
name = "alex   \t name  a a  is {name} and i am {age}"


print(name.capitalize())#首字母大写
print(name.count("a"))#输出字母a的出现次数
print(name.center(50,"-"))#把name放中间,总共50个字符
print(name.encode())#转成二进制
print(name.endswith("ex"))#判断字符串最后的字母是不是一样的,一样输出true
print(name.expandtabs(tabsize=30))#中间插入多少个空格
print(name.find("a"))#取出字符开始的数字
print(name[name.find("name"):])#切片
print(name.format(name = 'alex',age = 25))
print(name.format_map({'name':'alex','age':100}))
print(name.isalnum())#只有当字符里为英文字母和数字时显示True
print(name.isalpha())#只有纯英文字符
print(name.isdecimal())#只有为数的时候才为True
print(name.isdigit())#只有为整数的时候才为True
print(name.isidentifier())#判断是不是一个合法的标识符,即合法变量名
print(name.islower())#判断是否小写
print(name.isnumeric())#判断是不是全为数字
print(name.isspace())#判断是不是一个空格
print(name.istitle())#判断是不是标题,即首个字符大写
print(name.isprintable())#tty file,drive file
print(name.isupper())#判断是不是大写
print(','.join(['1','2','3','4']))
print(name.ljust(50,'*'))#长度50,不够用*补上
print(name.rjust(50,'-'))#长度50前面补上-
print(name.lower())#把大写变为小写
print(name.upper())#把小写变为大写
print(name.lstrip())#去掉左边空格和回车
print(name.rstrip())#去掉右边的空格和回车
print(name.strip())#去掉空格和回车

p = str.maketrans("alexif",'123456')
print("alex li".translate(p))#把后面的数字和前面的字母对应起来(加密密码)

print(name.replace('l','L',1))#替换字母,后面的数字是只要替换几个字母
print(name.rfind('a'))#找到最后面的字母的下标
print(name.split('a'))#按照空格分成列表,括号里面的当成分隔符
print(name.splitlines())#
print(name.swapcase())#将字母变为大写或者小写,大写->小写,小写->大写
print(name.title())#将首字母变成大写,当做标题
print(name.zfill(50))#不够50个的话,前面补0
2.学习了字典的基本用法
info = {
    'stu1101': "tenlanwu",
    'stu1102': "djhasdka",
    'stu1103':  "nishi1zhu1",
    'stu1104': "ahsdaiad"


}


print(info)

b = {
    'stu1101':"alex",
    1:3,
    2:5
}
info.update(b)#两个字典交叉,相同的覆盖更新,没有的加入进去
print(info)
c = dict.fromkeys([6,7,8],[1,{"name":"alex"},444])#初始化一个新的字典
print(c)
c[7][1]['name'] = "jack"
print(c)
print(info.items())#把字典转成了列表

info['stu1103']#查找(确定字典里有这个)
print(info.get('stu1102'))#查找(安全方法)

print('stu1106' in info)#查找

info["stu1105"] = "xiaoazhe"#增加

info["stu1104"] = "xiaozhi"#修改

info.pop("stu1101")#标准删除
del info['stu1102']#其他删除类型
info = {'stu1104': "ahsdaiad",'stu1103':  "nishi1zhu1"}#随机删除
print(info)
info.values()#打印所有的值,不包括key
info.keys()#打印所有的key,不包括值

av_category = {
    "欧美":{
        "www.1232":["很多免费","质量一般"],
        "www.asdfhajk":["免费的","质量还行"],
        "www.asdasd":["收费的","质量高"]
    },
    "日韩":{
        "towhjda":["质量不太懂","忘了"]
    },
    "大陆":{
        "hdjkas":["网速快","质量不好"]
    }

}
av_category["大陆"]["hdjkas"][1] = "质量还可以"#修改
av_category.setdefault("台湾",{"www.baidu.com":[1,2]})#可以新建一个
print(av_category)

for i in info:
    print(i,info[i])#输出字典

for k,v in info.items():
    print(k,v)#不是特别好
3.做了一个代码异常low的三级菜单,输入q结束,输入b回到上一级
data = {
    '北京':{
        "昌平":{
            "沙河":["oldboy","test"]
        },
        "朝阳":{},
        "海淀":{},
    },
    '上海':{
        "徐汇区":{"dasdas","jygjh"},
        "宝山区":{"asdas","dasdas"}
    },
    '广东':{
        "东莞":{},
        "广州":{},
        "佛山":{},
    }


}

exit = False

while not exit:
    for i in data:
        print(i)

    choice = input(">>:")
    if choice in data:
        while not exit:
            for i2 in data[choice]:
                print("\t",i2)
            choice2 = input("按b返回>>:")
            if choice2 in data[choice]:
                while not exit:
                    for i3 in data[choice][choice2]:
                        print("\t\t",i3)
                    choice3 = input("按b返回>>:")
                    if choice3 in data[choice][choice2]:
                        for i4 in data[choice][choice2][choice3]:
                            print("\t\t\t",i4)
                        choice4 = input("最后一层,按b返回>>:")
                        if choice4 == "b" :
                            break
                        elif choice4 == "q":
                            exit = True
                    if choice3 == "b":
                        break
                    elif choice3== "q":
                        exit = True
            if choice2 == "b":
                break
            elif choice2 == "q":
                exit = True


猜你喜欢

转载自blog.csdn.net/weixin_42147992/article/details/80289987