Loading

Python之数据类型

数据类型

一:简介

有int float bin chr bool等,还有还有容器类list tuple set dict,以及更多的namedtuple,ordereddict等等。

这么我们可以常用的数据类型帮我们更好的处理数据,他们全部都是类,是内置类,里面的有许多方法,节省了我们的时间不用再写直接拿过来用。

没有你适合的?那么就自定义数据类型,就是自定义类喽,创建的对象就是自定义对象。

int() dict()等等都是创建了一个对象。

二:list

l = []

l=list()都是创建空列表

my_list = [10,'hello',True,None,[1,2,3],print]可以存放任意数据类型,因为数据外置,但是索引位置都是固定4bytes地址空间。不能超过索引否则报异常
import pprint
pprint.pprint(dir(list))

 显示list所有方法,本质背后调用的list.__dict__拿的list的名称空间里面的所有属性和方法的key。

['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append'
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']

双下的方法是为了高度定制类,看后面没有下划线的方法。直接help(list)

/是python3.8新特性,/左边是位置参数,必须传递。

append(self, object, /)
| Append object to the end of the list.
|
| clear(self, /)
| Remove all items from list.
|
| copy(self, /)
| Return a shallow copy of the list.
|
| count(self, value, /)
| Return number of occurrences of value.
|
| extend(self, iterable, /)
| Extend list by appending elements from the iterable.不是必须是列表,只要是可迭代的即可。
|
| index(self, value, start=0, stop=2147483647, /)
| Return first index of value.
|
| Raises ValueError if the value is not present.
|
| insert(self, index, object, /)
| Insert object before index.
|
| pop(self, index=-1, /)#默认弹出的是最后一个,可以指定索引来弹出
| Remove and return item at index (default last).
|
| Raises IndexError if list is empty or index is out of range.
|
| remove(self, value, /)#移除指定元素,只有第一个
| Remove first occurrence of value.
|
| Raises ValueError if the value is not present.
|
| reverse(self, /)
| Reverse *IN PLACE*.
|
| sort(self, /, *, key=None, reverse=False)#默认升序,默认不反转,需要反转就改为True
| Sort the list in ascending order and return None.
|
| The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
| order of two equal elements is maintained).
|
| If a key function is given, apply it once to each list item and sort them,
| ascending or descending, according to their function values.
|
| The reverse flag can be set to sort in descending order.
|

其他就不写了,help()就可以自查,这是需要记忆的,但是这么多总会遗忘,多看看敲敲。

posted @ 2019-11-07 09:47  浅忆尘  阅读(129)  评论(0编辑  收藏  举报