zoukankan      html  css  js  c++  java
  • 5数据类型2

    数据类型2

    基本数据类型-列表

    基本数据类型-元组

    1:基本数据类型-列表

    定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素。

    列表的创建:

    list_test=['张三','李四','alex']

    #或

    list_test=list('alex')

    #或

    list_test=list(['张三','李四','alex'])

    列表的特点和常用操作:

    特性:

    1.可存放多个值

    2.按照从左到右的顺序定义列表元素,下标从0开始顺序访问,有序

    3.可修改指定索引位置对应的值,可变

    常用操作:

    索引

    >>> a = ['tom' , 'bob' , 'bsb']

    >>> a

    ['tom', 'bob', 'bsb']

    >>> a[0]

    'tom'

    >>> a[2]

    'bsb'

    切片

    >>> a[0:2]

    ['tom', 'bob']

    >>> a[2:4]

    ['bsb']

    >>> a[:2]

    ['tom', 'bob']

    >>> a[:]

    ['tom', 'bob', 'bsb']

    切片—步长

    >>> a[::2]

    ['tom', 'bsb']

    >>> a[::-1]

    ['bsb', 'bob', 'tom']

    追加

    >>> a.append('elx')

    >>> a

    ['tom', 'bob', 'bsb', 'elx']

    删除

    >>> a.remove('elx')

    >>> a

    ['tom', 'bob', 'bsb']

    >>> a.pop()

    'bsb'

    >>> a

    ['tom', 'bob']

    长度

    >>> len(a)

    2

    包含

    >>> 'tom' in a

    True

    >>> 'bsb' in a

    False

    循环列表

    >>> for i in a:

    ... print(i) 这里必须要有缩进,否则报错

    File "<stdin>", line 2

    print(i)

    ^

    IndentationError: expected an indented block

    >>> for i in a:

    ... print(i)

    ...

    tom

    bob

    列表和字符串的转换:

    字符串转换为列表---split()

    >>> s = 'hello world tom !'

    >>> s

    'hello world tom !'

    >>> s.split(' ') # 以''为分割的字符

    ['hello', 'world', 'tom', '!']

    >>> s2 = 'hello,world' 以,为分割的字符

    >>> s2.split(',')

    ['hello', 'world']

    >>>

    列表转换为字符串---join()

    >>> a

    ['tom', 'bob']

    >>> '!'.join(a)

    'tom!bob'

    2:基本数据类型元祖

    定义:与列表类似,只不过[]改成()

    特性:

      1.可存放多个值

      2.不可变

    3.按照从左到右的顺序定义元组元素,下标从0开始顺序访问,有序

    元组的创建和常用操作:

    创建:

    >>> ages = (11,22,33,33)

    >>> ages

    (11, 22, 33, 33)

    >>> type(ages)

    <class 'tuple'>

    >>> age1 = tuple((11,22,33,44,55))

    >>> age1

    (11, 22, 33, 44, 55)

    >>> age2 = (11,) 创建一个元素的元组的时候,需要加上逗号

    >>> type(age1) , type(age2)

    (<class 'tuple'>, <class 'tuple'>)

    >>> age3 = (1)

    >>> type(age3) , age3

    (<class 'int'>, 1)

    常用操作:

    索引:

    >>> ages

    (11, 22, 33, 33)

    >>> ages[0]

    11

    >>> ages[1]

    22

    >>> ages[-1]

    33

    切片:同list操作一样

    循环:

    >>> for age in ages:

    ... print(age)

    ...

    11

    22

    33

    33

    长度:

    >>> len(ages)

    4

    包含:

    >>> 11 in ages

    True

    >>> 66 in ages

    False

    >>> 11 not in ages

    False

    元组的特性详解:

    1:可以存放多个值:元组中不仅可以存放数字、字符串,还可以存放更加复杂的数据类型

    2:不可变:元组本身不可变,如果元组中还包含其他可变元素,这些可变元素可以改变

    >>> ages

    (11, 22, 33, 33)

    >>> ages[0] = 12 索引0位置上的为数字类型,不可变

    Traceback (most recent call last):

    File "<stdin>", line 1, in <module>

    TypeError: 'tuple' object does not support item assignment

    >>> ages = (11,[22,33],44) 索引为1的值为可变的数据类型-列表,修改里面的值不会发生改变

    >>> ages

    (11, [22, 33], 44)

    >>> ages[1][0]=12

    >>> ages

    (11, [12, 33], 44)

    菜鸟的自白
  • 相关阅读:
    将博客搬至CSDN
    一种全新的屏幕适配方法 自动百分比适配 一切px说了算
    一些精品开源代码
    Android View 事件分发机制 源码解析 (上)
    Android 高清加载巨图方案 拒绝压缩图片
    Android EventBus实战 没听过你就out了
    Markdown 11种基本语法
    JavaScript 语言基础知识点总结(思维导图)
    隐藏滚动条 支持Chrome, IE (6+), Firefox, Opera, and Safari
    (function(){})(this)
  • 原文地址:https://www.cnblogs.com/lzjloveit/p/10584615.html
Copyright © 2011-2022 走看看