• Python学习笔记(二)


    Python基础进阶之数据类型

    一、数据类型

      Python3 中有六个标准的数据类型:

    • Number(数字)
    • String(字符串)
    • List(列表)
    • Tuple(元组)
    • Sets(集合)
    • Dictionary(字典)

        Python3 支持 int(整形)、float(浮点型)、bool(布尔型)、complex(复数)。在Python 3里,只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。布尔值:真或假,1 或 0。

    二、列表

    1.列表创建

    >>> list = [1,2,3,4,5]
    >>> print(list)
    [1, 2, 3, 4, 5]

     2.列表切片

    list = [1,2,3,4,5]
    >>> print(list)
    [1, 2, 3, 4, 5]
    >>> list[1:4] #取下标1至下标4之间的数字,包括下标1,不包括下标4
    [2, 3, 4]
    >>> list[1:-1] #取下标1至-1的值,不包括-1
    [2, 3, 4]
    >>> list[0:3]
    [1, 2, 3]
    >>> list[:3] #如果是从头开始取,0可以忽略,跟上句效果一样
    [1, 2, 3]
    >>> list[3:] #如果想取最后一个,必须不能写-1,只能这么写
    [4, 5]
    >>> list[:-1] #这样-1就不会被包含了
    [1, 2, 3, 4]
    >>> list[0::2] #后面的2是代表,每隔一个元素,就取一个
    [1, 3, 5]
    >>> list[::2] #和上句效果一样
    [1, 3, 5]

    3.列表追加


    >>> list.append(6) >>> list [1, 2, 3, 4, 5, 6] #默认追加到最后面,要想往前面看下一个

    4.列表插入

    复制代码
    >>> list
    [1, 2, 3, 4, 5, 6]
    >>> list.insert(1,"插入到2前面") #默认插入到下标前面,元素2的下标是1
    >>> list
    [1, '插入到2前面', 2, 3, 4, 5, 6]
    >>> list.insert(3,"插入到2后面") #2的下标变成了2,插入2的后面也就是往下标3的前面插入
    >>> list
    [1, '插入到2前面', 2, '插入到2后面', 3, 4, 5, 6]
    复制代码

    5.列表修改

    >>> list
    [1, '插入到2前面', 2, '插入到2后面', 3, 4, 5, 6]
    >>> list[1]="下标一"
    >>> list[3]="下标三"
    >>> list
    [1, '下标一', 2, '下标三', 3, 4, 5, 6]

    6.列表删除

    >>> list
    [1, '下标一', 2, '下标三', 3, 4, 5, 6]
    >>> del list[1] #删除下标是1的元素
    >>> list
    [1, 2, '下标三', 3, 4, 5, 6]
    >>> list.remove("下标三") #删除叫下标三的元素
    >>> list
    [1, 2, 3, 4, 5, 6]
    >>> list.pop(5) #删除下标是5的元素
    >>> list
    [1, 2, 3, 4, 5]
    >>> list.pop() #默认删除最后一个元素
    >>> list
    [1, 2, 3, 4]

    7.列表扩展

    复制代码
    >>> list
    [1, 2, 3, 4]
    >>> list + [5,6,7,8,9]
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list
    [1, 2, 3, 4]
    >>> list=list + [5,6,7,8,9]
    >>> list
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list2=[10]
    >>> list.extend(list2)
    >>> list
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    复制代码

    8.列表copy(浅copy和深copy)

    >>> list
    [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list2=list.copy()
    >>> list2
    [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    复制代码
    >>> list
    [[0, 1], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list2=list.copy()
    >>> list2
    [[0, 1], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list[0][1]="test"
    >>> list
    [[0, 'test'], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list2
    [[0, 'test'], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    #当list改变,list2也同时发生了改变,copy只能copy第一层,深copy需要copy模块
    >>> import copy
    >>> list3=copy.deepcopy(list) #深copy
    >>> list3
    [[0, 0], '一', 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list[0][0]=666
    >>> list
    [[666, 0], '一', 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list3
    [[0, 0], '一', 2, 3, 4, 5, 6, 7, 8, 9, 10] #修改list后,未发生变化
    复制代码

    9.列表统计

    >>> list
    [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list.count(1) #统计1元素有几个
    2

    10.翻转和排序

    复制代码
    >>> list
    [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list.reverse() #列表元素翻转
    >>> list
    [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
    >>> list.sort() #按照ASCII表进行排序
    >>> list
    [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    复制代码

    11.获取下标

    >>> list
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> list.index(1) #元素1的下标
    0

     三、元组

        组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表。它只有2个方法,一个是index,一个是count。

    >>> list = (0,1)
    >>> list.index(0)
    0
    >>> list.count(0)
    1

    四、字典

        字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。

        字典的特性:

    • dict是无序的
    • key必须是唯一的

    1.字典创建

    >>> dict={'key1':'value1','key2':'value2'} #字典用的是大括号,列表是中括号,元组是小括号
    >>> dict
    {'key1': 'value1', 'key2': 'value2'}

    2.字典增加

    >>> dict['key3']='value3'
    >>> dict
    {'key3': 'value3', 'key1': 'value1', 'key2': 'value2'}

    3.字典修改

    >>> dict['key3']='value33333'
    >>> dict
    {'key3': 'value33333', 'key1': 'value1', 'key2': 'value2'}

    4.字典删除(三种姿势)

    复制代码
    >>> dict.pop("key3") #第一种姿势
    'value33333'
    >>> dict
    {'key1': 'value1', 'key2': 'value2'}
    >>> dict.popitem() #随机删除姿势
    ('key1', 'value1')
    >>> dict
    {'key2': 'value2'}
    >>> del dict('key2')  #注意,符号打错了,和上面的不一样
    SyntaxError: can't delete function call
    >>> del dict['key2'] #删除key2项
    >>> dict
    {}
    复制代码

    5.字典查找

    复制代码
    dict={'key1':'value1','key2':'value2'}
    >>> 'key1' in dict #标准用法
    True
    >>> dict.get("key1") #获取
    'value1'
    >>> dict.get('key10') #如果没有这个key
    
    >>> dict['key1'] #获取
    'value1'
    >>> dict['key10'] #如果没有这个key,报错
    Traceback (most recent call last):
      File "<pyshell#125>", line 1, in <module>
        dict['key10']
    KeyError: 'key10'
    复制代码

    City_list = {
    "北京":{
    "海淀":['上地','清河'],
    "昌平":['回龙观','沙河'],
    },
    "河北":{
    "保定":['易县','定州'],
    "石家庄":['新华区','桥西区'],
    },
    "辽宁":{
    "沈阳":['皇姑区','铁西区'],
    "大连":['瓦房店','普兰店'],
    },
    }
    one=True
    two=True
    while one:
    print("欢迎使用多级菜单,现在显示一级菜单:")
    for i in City_list.keys():
    print(" ",i)
    first_input=input("请输入省级菜单名称打开市级菜单,按q退出,按b返回主菜单:")
    if first_input == "q":
    one=False
    break
    elif first_input == 'b':
    break
    else:
    for k in (City_list[first_input].keys()):
    print(" ",k)
    while two:
    second_input=input("请输入市级菜单名称打开县级菜单,按q退出,按b返回主菜单:")
    if second_input == "q":
    one=two=False
    break
    elif second_input == "b":
    break
    else:
    for l in (City_list[first_input][second_input]):
    print(" ",l)
    third_input=input("执行完毕,按q退出,按b返回上一层,任意键返回主菜单:")
    if third_input == "q":
    exit()
    elif third_input == "b":
    continue
    else:
    for n in City_list.keys():
    print(" ",n)
    break

    多级菜单程序


    复制代码
     1 City_list = {
     2     "北京":{
     3         "海淀":['上地','清河'],
     4         "昌平":['回龙观','沙河'],
     5     },
     6     "河北":{
     7         "保定":['易县','定州'],
     8         "石家庄":['新华区','桥西区'],
     9     },
    10     "辽宁":{
    11         "沈阳":['皇姑区','铁西区'],
    12         "大连":['瓦房店','普兰店'],
    13     },
    14 }
    15 one=True
    16 two=True
    17 while one:
    18     print("欢迎使用多级菜单,现在显示一级菜单:")
    19     for i in City_list.keys():
    20         print(" ",i)
    21     first_input=input("请输入省级菜单名称打开市级菜单,按q退出,按b返回主菜单:")
    22     if first_input == "q":
    23         one=False
    24         break
    25     elif first_input == 'b':
    26         break
    27     else:
    28         for k in (City_list[first_input].keys()):
    29             print(" ",k)
    30         while two:
    31             second_input=input("请输入市级菜单名称打开县级菜单,按q退出,按b返回主菜单:")
    32             if second_input == "q":
    33                 one=two=False
    34                 break
    35             elif second_input == "b":
    36                 break
    37             else:
    38                 for l in (City_list[first_input][second_input]):
    39                     print(" ",l)
    40                 third_input=input("执行完毕,按q退出,按b返回上一层,任意键返回主菜单:")
    41                 if third_input == "q":
    42                     exit()
    43                 elif third_input == "b":
    44                     continue
    45                 else:
    46                     for n in City_list.keys():
    47                          print(" ",n)
    48                     break
    复制代码

    多级菜单流程图

  • 相关阅读:
    如何构造分片报文
    Python趣味入门7:循环与条件的爱恨情仇
    如何使用Javascript开发sliding-nav带滑动条效果的导航插件?
    java获取微信用户信息(含源码,直接改下appid就可以使用了)
    数据库设计工具-PowerDesigner中table视图显示name与code
    typora增加到鼠标右键
    基于springboot整合spring-retry
    idea中提交项目到github及invalid authentication data 404 not found not found问题
    git秘钥问题解析及gitlab配置(Please make sure you have the correct access rights and the repository exists)
    idea中打包跳过test
  • 原文地址:https://www.cnblogs.com/gzliuc/p/5866007.html
Copyright © 2020-2023  润新知