zoukankan      html  css  js  c++  java
  • Python学习笔记(三)

    Python学习笔记(三):

    1. 元组
    2. 购物车作业讲解
    3. 字典
    4. 字符串
    5. 作业-三级目录(省份菜单)

    1. 元组

    1. 元组不能修改
    2. 元组的增删查与列表相似

    2. 购物车作业讲解

    • 知识点

      1. 嵌套-列表的元素可以为任意类型的
    • 程序改进

      1. 列表嵌套实现商品储存
      2. ...等等

    3. 字典

    1. 语法:dic={'name':'han'}
    2. 字典是无需存储的
    3. 字典的键是不可修改且唯一的
    4. 操作
    
    # 增
    
    dic = {'name','han'}
    dic['age'] = 18				#通过赋值增添
    dic.setdefault('age',34)	#如果键存在则不变动,如果不存在则添加(返回age真实的值)
    
    # 查
    
    print(dic['name'])			#通过键查找值
    print(dic.keys())			#查找字典下的keys
    print(dic.values())			#查找字典下的values
    print(dic.items())			#查找字典下的键值对
    
    # 改
    
    dic['age'] = 22				#通过赋值修改
    dic2 = {'1':111,'2':222,'3':333}
    dic3 = {'1':111,'2':333,'4':444}
    dic2.update(dic3)			#键一样的进行修改,键不同的进行添加
    print(dic2)					#结果:{'1': 111, '2': 333, '3': 333, '4': 444}
    
    # 删
    
    del dic['name']				#根据键删除键值对
    dic.clear()					#清空字典
    dic.pop('age')				#根据键删除键值对,返回删除的值
    dic.popitem()				#随机删除某组键值对,并返回值
    del dic						#删除字典
    
    # 其他操作
    
    # 字典初始化(涉及到深浅拷贝)
    dic4 = dict.fromkeys(['name1','name2','name3'],'test')
    print(dic4)					#结果:{'name1': 'test', 'name2': 'test', 'name3': 'test'}
    
    # 字典的遍历
    
    #推荐
    for i in dic4:
    	print(i,dic4[i])
    #不推荐
    for i,v in dic4.items():
    	print(i,v)
    
    

    4. 字符串

    1. 字符串包括单引号时用双引号反之亦然,若都有则用三引号
    2. 操作
    
    print("hello" * 2)			#输出两遍hello
    print("hello" [2:])			#输出切片后的字符串
    print("el" in "hello")		#判断后者是否包括前者
    name = DaMing
    print("Hi , %s ." %name)	#格式化字符串
    a = '123'
    b = 'abc'
    c = a+b
    print(c)					#用‘+’字符串拼接
    c = '---'.join([a,b])		#用join进行拼接
    print(c)					#结果:123---abc
    
    1. 内置方法
    st = 'hello world'
    
    print(st.count('l'))		#★统计元素个数
    print(st.capitalize())		#字符串首字母大写
    print(st.center(50,'-'))	#★指定字符串所占位数且字符串居中,空余用指定符号补充
    print(st.encode)			#★编码解码(之后统一讲)
    print(st.endswith('ld'))	#判断是否以……为结尾
    print(st.startswith('h'))	#★判断是否以……为开头(文字处理)
    print(st.expandtabs(tabsize=10))#设置\t转义后的tab大小
    print(st.find('o'))			#★查找元素第一次出现的位置,返回索引值
    st2 = '{name} is {age}'
    print(st2.format(name='han',age=22))#★格式化输出
    print(st2.format_map({'name':'han','age':22}))#格式化输出
    print(st.index('d'))		#查找元素第一次出现的位置,返回索引值
    print(st.isalnum())         #是否包含字母(汉子)、数字
    print(st.isdigit())			#是否是整型
    print(st.islower())			#是否全部是小写的
    print(st.isupper())			#是否全部是大写的
    print(st.isspace())			#是否全是空格
    print(st.istitle())			#是否是标题(每个单词的首字母是大写)
    print(st.lower())			#★大写变小写
    print(st.upper())			#★小写变大写
    print(st.swapcase())		#大小写反转
    print(st.ljust(50,'*'))		#占位左对齐,空位补充
    print(st.rjust(50,'*'))		#占位右对齐,空位补充
    print(st.strip())			#★去除字符串左右两遍的空格和换行符
    print(st.replace('l','q'))	#★q替换l
    print(st.rfing('l'))		#从右边开始查找元素第一次出现的位置,返回索引值
    print(st.split(' '))		#★通过指定符号分割成元素储存在列表中(对应join)
    print(st.title())			#以标题的行使输出
    

    5. 作业-三级目录(省份菜单)

    • 作业要求

    要求

    
    # 三层目录
    
    # 目录字典
    
    # 山东省,河北省,山西省
    
    nav = {'省略'}
    
    # 记录当前位置
    
    s_flag = []		# 省份坐标
    c_flag = []		# 城市坐标
    is_quit = True	# 是否不退出
    
    # 输出最外层:省份目录
    while is_quit:
        print("这是一份省份菜单,您可以根据提示数字进入对应省份:")
        for i in enumerate(nav,1):
            s_flag.append(i[1])
            print("%s---%s" %(i[0],i[1]))
        id = input("请输入您要进入的省份编号:(退出请输入0)")
        if id.isdigit():
            id = int(id)
            if id > 0 and id <= len(nav):
                s_coor = s_flag[id-1]
    
                # 第二层循环
                while is_quit:
                    print("您已经进入%s" %s_coor)
                    print("这是%s的城市菜单:" %s_coor)
                    for i in enumerate(nav[s_coor],1):
                        c_flag.append(i[1])
                        print("%s---%s" %(i[0],i[1]))
                    print("返回上一层请按:9 | 退出请按:0")
                    id = input("请输入您要进入的城市编号:")
                    if id.isdigit():
                        id = int(id)
                        if id == 0:
                            is_quit = False
                            break
                        elif id == 9:
                            c_flag.clear()
                            break
                        elif id > 0 and id <= len(nav[s_coor]):
                            c_coor = c_flag[id-1]
    
    
                            #第三层循环
                            while is_quit:
                                print("您已进入%s---%s" %(s_coor,c_coor))
                                print("这是%s的城市菜单:" % c_coor)
                                for i in enumerate(nav[s_coor][c_coor], 1):
                                    print("%s---%s" % (i[0], i[1]))
                                id = input("返回上一层请按:9 | 退出请按:0")
                                if id.isdigit():
                                    id = int(id)
                                    if id == 0:
                                        is_quit = False
                                        break
                                    elif id == 9:
                                        c_flag.clear()
                                        break
                                    else:
                                        print("请输入正确数字编号!")
                                else:
                                    print("请输入正确数字编号!")
    
                        else:
                            print("请输入正确数字编号!")
                    else:
                        print("请输入正确数字编号!")
    
            elif id == 0:
                is_quit = False
            else:
                print("请输入正确数字编号!")
        else:
            print("请输入正确数字编号!")
    
    以此为趣,乐在其中。
  • 相关阅读:
    【26.09%】【codeforces 579C】A Problem about Polyline
    【水水水】【洛谷 U4566】赛车比赛
    【24.58%】【BZOJ 1001】狼抓兔子
    【心情】2016ICPC青岛站打铁记
    【record】11.7..11.13
    Identifying a distributed denial of service (DDOS) attack within a network and defending against such an attack
    在页面所有元素加载完成之后执行某个js函数
    day38 01-Spring框架的概
    day37 10-SH整合的案例练习
    day37 09-Struts2和Hibernate整合环境搭建
  • 原文地址:https://www.cnblogs.com/ryomahan/p/7506545.html
Copyright © 2011-2022 走看看