2、Python的基本数据类型

本节内容

  1. 列表
  2. 元组
  3. 字符串操作
  4. 字典操作
  5. 集合操作
  6. 文件操作
  7. 字符编码与转码

1. 列表list

列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

定义列表

1
names = ['Alex',"Tenglan",'Eric']

通过下标访问列表中的元素,下标从0开始计数

1
2
3
4
5
6
7
8
>>> names[0]
'Alex'
>>> names[2]
'Eric'
>>> names[-1]
'Eric'
>>> names[-2#还可以倒着取
'Tenglan'

 list的切片操作

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

 

class list(object):
    """
    list() -> new empty list,创建一个新的list
    list(iterable) -> new list initialized from iterable's items从一个可迭代创建一个list
    """
    def append(self, p_object): # real signature unknown; restored from __doc__
        """ L.append(object) -> None -- append object to end 在list的末尾加上新元素"""
        pass

    def clear(self): # real signature unknown; restored from __doc__
        """ L.clear() -> None -- remove all items from L 删除L的所有元素"""
        pass

    def copy(self): # real signature unknown; restored from __doc__
        """ L.copy() -> list -- a shallow copy of L 一个L的浅拷贝"""
        return []

    def count(self, value): # real signature unknown; restored from __doc__
        """ L.count(value) -> integer -- return number of occurrences of value 返回L中某个value值的个数"""
        return 0

    def extend(self, iterable): # real signature unknown; restored from __doc__
        """ L.extend(iterable) -> None -- extend list by appending elements from the iterable 将可迭代对象追加到list里"""
        pass

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        L.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        在L中第一次索引到某个值的的位置
        """
        return 0

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
        """ L.insert(index, object) -- insert object before index 在索引位置之前插入对象"""
        pass

    def pop(self, index=None): # real signature unknown; restored from __doc__
        """
        L.pop([index]) -> item -- remove and return item at index (default last).
        Raises IndexError if list is empty or index is out of range.
        删除L中某个元素,不指定,默认删除最后一个元素
        """
        pass

    def remove(self, value): # real signature unknown; restored from __doc__
        """
        L.remove(value) -> None -- remove first occurrence of value.
        Raises ValueError if the value is not present.
        删除L中的某个值
        """
        pass

    def reverse(self): # real signature unknown; restored from __doc__
        """ L.reverse() -- reverse *IN PLACE* """
        将L中的元素反转
        pass

    def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
        """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
        对L中的元素进行排序
        pass
Python的list官方说明

 

2. 元组

 

 1 class tuple(object):
 2     """
 3     tuple() -> empty tuple
 4     tuple(iterable) -> tuple initialized from iterable's items
 5     
 6     If the argument is a tuple, the return value is the same object.
 7     """
 8     def count(self, value): # real signature unknown; restored from __doc__
 9         """ T.count(value) -> integer -- return number of occurrences of value返回某个值出现的次数 """
10         return 0
11 
12     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
13         """
14         T.index(value, [start, [stop]]) -> integer -- return first index of value返回索引到某个值的第一个位置.
15         Raises ValueError if the value is not present.
16         """
17         return 0
18 
19     def __add__(self, *args, **kwargs): # real signature unknown
20         """ Return self+value. 追加内容,注意只能追加tuple"""
21         pass
22 
23     def __contains__(self, *args, **kwargs): # real signature unknown
24         """ Return key in self. """
25         pass
26 
27     def __eq__(self, *args, **kwargs): # real signature unknown
28         """ Return self==value. """
29         pass
30 
31     def __getattribute__(self, *args, **kwargs): # real signature unknown
32         """ Return getattr(self, name). """
33         pass
34 
35     def __getitem__(self, *args, **kwargs): # real signature unknown
36         """ Return self[key]. """
37         pass
38 
39     def __getnewargs__(self, *args, **kwargs): # real signature unknown
40         pass
41 
42     def __ge__(self, *args, **kwargs): # real signature unknown
43         """ Return self>=value. """
44         pass
45 
46     def __gt__(self, *args, **kwargs): # real signature unknown
47         """ Return self>value. """
48         pass
49 
50     def __hash__(self, *args, **kwargs): # real signature unknown
51         """ Return hash(self). """
52         pass
53 
54     def __init__(self, seq=()): # known special case of tuple.__init__
55         """
56         tuple() -> empty tuple
57         tuple(iterable) -> tuple initialized from iterable's items
58         
59         If the argument is a tuple, the return value is the same object.
60         # (copied from class doc)
61         """
62         pass
63 
64     def __iter__(self, *args, **kwargs): # real signature unknown
65         """ Implement iter(self). """
66         pass
67 
68     def __len__(self, *args, **kwargs): # real signature unknown
69         """ Return len(self). """
70         pass
71 
72     def __le__(self, *args, **kwargs): # real signature unknown
73         """ Return self<=value. """
74         pass
75 
76     def __lt__(self, *args, **kwargs): # real signature unknown
77         """ Return self<value. """
78         pass
79 
80     def __mul__(self, *args, **kwargs): # real signature unknown
81         """ Return self*value.n """
82         pass
83 
84     @staticmethod # known case of __new__
85     def __new__(*args, **kwargs): # real signature unknown
86         """ Create and return a new object.  See help(type) for accurate signature. """
87         pass
88 
89     def __ne__(self, *args, **kwargs): # real signature unknown
90         """ Return self!=value. """
91         pass
92 
93     def __repr__(self, *args, **kwargs): # real signature unknown
94         """ Return repr(self). """
95         pass
96 
97     def __rmul__(self, *args, **kwargs): # real signature unknown
98         """ Return self*value. """
99         pass
tuple官方文档

 

元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表

语法

1
names = ("alex","jack","eric")

它只有2个方法,一个是count,一个是index,完毕。

 3. 字符串

特性:不可修改 

 

posted @ 2017-01-22 20:05  skiler  阅读(139)  评论(0)    收藏  举报