zoukankan      html  css  js  c++  java
  • day02 python基础之列表,元祖,字典

    今日内容:
    1. 常用数据类型及内置方法
    2.文件处理
    3.函数
      列表:定义:在[]内,可以存放多个任意类型的值,并以逗号隔开。
     一般用于存放学生的爱好,课堂的周期等等。
    students=['1','3','孙']
    student_info=['sal',44,['泡吧','喝酒']]

    1.按索引存取值(正向存取+反向存取):即可存也可取

    print(student_info[2])#输出泡吧,喝酒

    print(student_info[2][1]输出喝酒

    student_info.append('安徽')#添加到student_info最后一个位置

    2.切片(顾头不顾尾,步长)

    print(student_info[0:3:2]#1,孙

    3.长度

     print(len(student_info))  #4

    4.成员运算in和not in

    print('孙'in student_info) #true

    5.追加

    student_info=['sal',44,['泡吧','喝酒']]

    #在student_info列表末尾追加一个值

    student_info.append('安徽最牛的学院')

    6.删除

    #删除列表中索引为2的值

    del student_info[2]

    print(student_info)

    需要掌握的:

    student_info=['尹浩卿',95,'female',['尬舞','喊麦“],95]

    1.index获取列表中某个值的索引

    print(student_info.index(95))  #1

    2.count获取列表中某个值的数量

    print(student_info.index(95))  #2

    3.取值,默认取列表中最后一个值,类似删除

    若pop()括号中写了索引,则取索引对应的值

    student_info.pop()

    print(student_info)

    取出列表中索引为2的值,并赋值给sex变量名

    sex=student_info.pop(2)

    print(sex)

    4.移除,把列表中的某个值的第一个值移除

    student_info.remove(95)

    print(student_info) #['尹浩卿','female',['尬舞','喊麦“],95]

    name=student_info.remove('尹浩卿')

    print(name)    #None

    print(student_info)     #['female',['尬舞‘,’喊麦'],95]

    5.插入值

    在student_info中,索引为2的位置插入“合肥学院”

    6.extend合并列表

    student_info1=['尹浩卿', 95, ['尬舞', '喊麦'], 95]

    student_info2=['尹浩卿', ['尬舞', '喊麦'], 95]

    #把student_info2所有的值插入student_info1内

    student_info1.extend(student_info2)

    元组:

    定义:在()内,可以存放多个任意类型的值,并以逗号隔开。

    注意:元组与列表不一样的是,只能在定义时初始化值,不能对其进行修改

    优点:在内存中占用的资源比列表少

    优先掌握的操作:

    1.按索引取值(正向取=反向取):只能取

    print(tuple1[2])    #3

    2.切片(顾头不顾尾,步长)

    #从0开始切片到5-1,步长为3

    print(tuple1[0:5:3])       #(1,'五‘)

    3.长度

    len(tuple1)

    4.成员运算in和not in

    5.循环

    for line in tuple1:

          print(line)

          #print默认end参数是

           print(line,end='_')    #把end由换行改为下划线

    不可变类型;
    变量的值修改后,内存地址一定不一样
    数字类型:
           int      float
    字符串类型
             str
    元组类型
             tuple

    可变类型:
     列表类型
               list
    字典类型:
              dict 

    不可变类型

    #int

    number=100

    print(id(number))   #140716317794432

    number=111

    print(id(number))    #140716317794784

    #float

    sal=1.0

    print(id(sal))            #2241865168024

    sal=2.0

    print(id(sal))             #2241865170088

    #字符串

    str1='hello python!'

    print(id(str1))             #2241905529520

    str2=str1.replace('hello','like')

    print(id(str2))              #2241905088368

    不可变类型

    list1=[1,2,3]

    list2=list1

    list1.append(4)

    print(id(list1))            #2241905588232

    print(id(list2))            #2241905588232

    print(list1)                   [1, 2, 3, 4]

    print(list2)                      [1, 2, 3, 4]

    字典类型

    作用:在{}内,以逗号隔开可存放多个值,

    以key-value存取,取值速度快。

    定义:key必须是不可变类型,value可以是任意类型

    dict1={'age':18,'name':'tank'}

    print(dict1)

    #取值,字典名+[],括号内写值对应的key

    print(dict1['age'])

    1.按key存取值

    存一个level:9的值到dect1字典中

    dict1['level']=9

    2, 长度len

    3.成员运算in和not in   只判断字典中的key

    print('name' in dict1)       #true

    4.删除

    del dict1['level']

    5.键keys90,值values(),键值对items()

    #得到字典中所有的key

    print(dict1.keys())
    #得到字典中所有的values

    print(dict1.values())

    #得到字典中所有的Items

    print(dict1.items())

    6.循环

    for key in dict1:

                print(key)

                print(dict1[key])

    #get

    dict1={’age':18,'name':'tank'}

  • 相关阅读:
    Web开发常用知识点
    我的PHP之旅:开篇,走入开源的世界
    WPF Knowledge Points ContentControl和ContentPresenter的区别
    WPF Knowledge Points 控件状态利器:VisualStateManager详解
    WPF Control Hints ComboBox : 如何去掉ComboBox的DropDownButton
    WPF Control Hints ContextMenu : 怎么通过MenuItem的Click事件取得ContextMenuItem绑定的类实例?
    WPF Knowledge Points Binding.StringFormat不起作用的原理和解决
    AJAX请求 $.ajaxSetup方法的使用
    Html标签输出到前台并导出到Excel
    XML序列化和反序列化
  • 原文地址:https://www.cnblogs.com/chmusk/p/12465880.html
Copyright © 2011-2022 走看看