我的Python升级打怪之路【二】:Python的基本数据类型及操作

基本数据类型

1.数字

int(整型)

  在32位机器上,整数的位数是32位,取值范围是-2**31~2--31-1

  在64位系统上,整数的位数是64位,取值范围是-2**63~2**63-1

  1 class int(object):
  2     """
  3     int(x=0) -> integer
  4     int(x, base=10) -> integer
  5     
  6     Convert a number or string to an integer, or return 0 if no arguments
  7     are given.  If x is a number, return x.__int__().  For floating point
  8     numbers, this truncates towards zero.
  9     
 10     If x is not a number or if base is given, then x must be a string,
 11     bytes, or bytearray instance representing an integer literal in the
 12     given base.  The literal can be preceded by '+' or '-' and be surrounded
 13     by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
 14     Base 0 means to interpret the base from the string as an integer literal.
 15     >>> int('0b100', base=0)
 16     4
 17     """
 18     def bit_length(self):
 19         """
 20         返回表示该数字的时候占用的最小位数
 21         """
 22         return 0
 23 
 24     def conjugate(self, *args, **kwargs):
 25         """ 返回该复数的共轭复数 """
 26         pass
 27 
 28     @classmethod # known case
 29     def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
 30         pass
 31 
 32     def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
 33         pass
 34 
 35     def __abs__(self, *args, **kwargs): # real signature unknown
 36         """ 返回绝对值 """
 37         pass
 38 
 39     def __add__(self, *args, **kwargs): # real signature unknown
 40         """ Return self+value. """
 41         pass
 42 
 43     def __and__(self, *args, **kwargs): # real signature unknown
 44         """ Return self&value. """
 45         pass
 46 
 47     def __bool__(self, *args, **kwargs): # real signature unknown
 48         """ self != 0 """
 49         pass
 50 
 51     def __ceil__(self, *args, **kwargs): # real signature unknown
 52         """ Ceiling of an Integral returns itself. """
 53         pass
 54 
 55     def __divmod__(self, *args, **kwargs): # real signature unknown
 56         """ 相除,的得到商和余数组成的元组 """
 57         pass
 58 
 59     def __eq__(self, *args, **kwargs): # real signature unknown
 60         """ Return self==value. """
 61         pass
 62 
 63     def __float__(self, *args, **kwargs): # real signature unknown
 64         """ 转换位浮点数 """
 65         pass
 66 
 67     def __floordiv__(self, *args, **kwargs): # real signature unknown
 68         """ Return self//value. """
 69         pass
 70 
 71     def __floor__(self, *args, **kwargs): # real signature unknown
 72         """ Flooring an Integral returns itself. """
 73         pass
 74 
 75     def __format__(self, *args, **kwargs): # real signature unknown
 76         pass
 77 
 78     def __getattribute__(self, *args, **kwargs): # real signature unknown
 79         """ Return getattr(self, name). """
 80         pass
 81 
 82     def __getnewargs__(self, *args, **kwargs): # real signature unknown
 83         ''' 内部调用__new__方法或创建对象时传入参数使用 '''
 84         pass
 85 
 86     def __ge__(self, *args, **kwargs): # real signature unknown
 87         """ Return self>=value. """
 88         pass
 89 
 90     def __gt__(self, *args, **kwargs): # real signature unknown
 91         """ Return self>value. """
 92         pass
 93 
 94     def __hash__(self, *args, **kwargs): # real signature unknown
 95         """ 如果对象object位哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,
 96             哈希值用于快速比较字典的键。两个数值相等,则哈希值也相等
 97         """
 98         pass
 99 
100     def __index__(self, *args, **kwargs): # real signature unknown
101         """ 用于切片 """
102         pass
103 
104     def __init__(self, x, base=10): # known special case of int.__init__
105         """
106         int(x=0) -> integer
107         int(x, base=10) -> integer
108         
109         Convert a number or string to an integer, or return 0 if no arguments
110         are given.  If x is a number, return x.__int__().  For floating point
111         numbers, this truncates towards zero.
112         
113         If x is not a number or if base is given, then x must be a string,
114         bytes, or bytearray instance representing an integer literal in the
115         given base.  The literal can be preceded by '+' or '-' and be surrounded
116         by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
117         Base 0 means to interpret the base from the string as an integer literal.
118         >>> int('0b100', base=0)
119         4
120         # (copied from class doc)
121         """
122         pass
123 
124     def __int__(self, *args, **kwargs): # real signature unknown
125         """ 转化为整数 """
126         pass
127 
128     def __invert__(self, *args, **kwargs): # real signature unknown
129         """ ~self """
130         pass
131 
132     def __le__(self, *args, **kwargs): # real signature unknown
133         """ Return self<=value. """
134         pass
135 
136     def __lshift__(self, *args, **kwargs): # real signature unknown
137         """ Return self<<value. """
138         pass
139 
140     def __lt__(self, *args, **kwargs): # real signature unknown
141         """ Return self<value. """
142         pass
143 
144     def __mod__(self, *args, **kwargs): # real signature unknown
145         """ Return self%value. """
146         pass
147 
148     def __mul__(self, *args, **kwargs): # real signature unknown
149         """ Return self*value. """
150         pass
151 
152     def __neg__(self, *args, **kwargs): # real signature unknown
153         """ -self """
154         pass
155 
156     @staticmethod # known case of __new__
157     def __new__(*args, **kwargs): # real signature unknown
158         """ Create and return a new object.  See help(type) for accurate signature. """
159         pass
160 
161     def __ne__(self, *args, **kwargs): # real signature unknown
162         """ Return self!=value. """
163         pass
164 
165     def __or__(self, *args, **kwargs): # real signature unknown
166         """ Return self|value. """
167         pass
168 
169     def __pos__(self, *args, **kwargs): # real signature unknown
170         """ +self """
171         pass
172 
173     def __pow__(self, *args, **kwargs): # real signature unknown
174         """ 幂,次方 """
175         pass
176 
177     def __radd__(self, *args, **kwargs): # real signature unknown
178         """ Return value+self. """
179         pass
180 
181     def __rand__(self, *args, **kwargs): # real signature unknown
182         """ Return value&self. """
183         pass
184 
185     def __rdivmod__(self, *args, **kwargs): # real signature unknown
186         """ Return divmod(value, self). """
187         pass
188 
189     def __repr__(self, *args, **kwargs): # real signature unknown
190         """ 转化为解释器可读取的形式 """
191         pass
192 
193     def __rfloordiv__(self, *args, **kwargs): # real signature unknown
194         """ Return value//self. """
195         pass
196 
197     def __rlshift__(self, *args, **kwargs): # real signature unknown
198         """ Return value<<self. """
199         pass
200 
201     def __rmod__(self, *args, **kwargs): # real signature unknown
202         """ Return value%self. """
203         pass
204 
205     def __rmul__(self, *args, **kwargs): # real signature unknown
206         """ Return value*self. """
207         pass
208 
209     def __ror__(self, *args, **kwargs): # real signature unknown
210         """ Return value|self. """
211         pass
212 
213     def __round__(self, *args, **kwargs): # real signature unknown
214         """
215         Rounding an Integral returns itself.
216         Rounding with an ndigits argument also returns an integer.
217         """
218         pass
219 
220     def __rpow__(self, *args, **kwargs): # real signature unknown
221         """ Return pow(value, self, mod). """
222         pass
223 
224     def __rrshift__(self, *args, **kwargs): # real signature unknown
225         """ Return value>>self. """
226         pass
227 
228     def __rshift__(self, *args, **kwargs): # real signature unknown
229         """ Return self>>value. """
230         pass
231 
232     def __rsub__(self, *args, **kwargs): # real signature unknown
233         """ Return value-self. """
234         pass
235 
236     def __rtruediv__(self, *args, **kwargs): # real signature unknown
237         """ Return value/self. """
238         pass
239 
240     def __rxor__(self, *args, **kwargs): # real signature unknown
241         """ Return value^self. """
242         pass
243 
244     def __sizeof__(self, *args, **kwargs): # real signature unknown
245         """ Returns size in memory, in bytes """
246         pass
247 
248     def __str__(self, *args, **kwargs): # real signature unknown
249         """ 转化为阅读的形式,如果没有适合于人阅读的形式则返回解释器阅读的形式 """
250         pass
251 
252     def __sub__(self, *args, **kwargs): # real signature unknown
253         """ Return self-value. """
254         pass
255 
256     def __truediv__(self, *args, **kwargs): # real signature unknown
257         """ Return self/value. """
258         pass
259 
260     def __trunc__(self, *args, **kwargs): # real signature unknown
261         """ 返回数值被截取为整形的值,在整数中无意义 """
262         pass
263 
264     def __xor__(self, *args, **kwargs): # real signature unknown
265         """ Return self^value. """
266         pass
267 
268     denominator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
269     """分母 = 1"""
270 
271     imag = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
272     """虚数,无意义"""
273 
274     numerator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
275     """分子 = 数字大小"""
276 
277     real = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
278     """实数,无意义"""
int源代码剖析

数值的类型:

2.布尔值

  真或假

  1  或  0

3.字符串

"hello python"  一个标准的字符串

字符串的常用操作:

  • 移除空白
  • 分割
  • 长度
  • 索引
  • 切片
  1 class str(object):
  2     """
  3     str(object='') -> str
  4     str(bytes_or_buffer[, encoding[, errors]]) -> str
  5     
  6     Create a new string object from the given object. If encoding or
  7     errors is specified, then the object must expose a data buffer
  8     that will be decoded using the given encoding and error handler.
  9     Otherwise, returns the result of object.__str__() (if defined)
 10     or repr(object).
 11     encoding defaults to sys.getdefaultencoding().
 12     errors defaults to 'strict'.
 13     """
 14     def capitalize(self):
 15         """
 16         首字母大写
 17         """
 18         return ""
 19 
 20     def casefold(self): 
 21         """
 22         S.casefold() -> str
 23         
 24         Return a version of S suitable for caseless comparisons.
 25         """
 26         return ""
 27 
 28     def center(self, width, fillchar=None): 
 29         """
 30         内容居中,总长度;fillchar:空白处填充内容,默认为''
 31         """
 32         return ""
 33 
 34     def count(self, sub, start=None, end=None): 
 35         """
 36         子序列个数
 37         """
 38         return 0
 39 
 40     def encode(self, encoding='utf-8', errors='strict'): 
 41         """
 42         编码,针对unicode
 43         """
 44         return b""
 45 
 46     def endswith(self, suffix, start=None, end=None): 
 47         """
 48         判断是否是以xxx结束
 49         """
 50         return False
 51 
 52     def expandtabs(self, tabsize=8): 
 53         """
 54         将tab转换为空格,默认一个tab转换为8个空格
 55         """
 56         return ""
 57 
 58     def find(self, sub, start=None, end=None):
 59         """
 60         寻找子序列位置,如果没找到,返回-1
 61         """
 62         return 0
 63 
 64     def format(self, *args, **kwargs): 
 65         """
 66         字符串格式化,动态参数
 67         """
 68         pass
 69 
 70     def format_map(self, mapping): 
 71         """
 72         S.format_map(mapping) -> str
 73         
 74         Return a formatted version of S, using substitutions from mapping.
 75         The substitutions are identified by braces ('{' and '}').
 76         """
 77         return ""
 78 
 79     def index(self, sub, start=None, end=None):
 80         """
 81         子序列的位置,如果没有 报错
 82         """
 83         return 0
 84 
 85     def isalnum(self): 
 86         """
 87         是否是字母和数字
 88         """
 89         return False
 90 
 91     def isalpha(self): 
 92         """
 93         是否是字母
 94         """
 95         return False
 96 
 97     def isdecimal(self): 
 98         """
 99         S.isdecimal() -> bool
100         
101         Return True if there are only decimal characters in S,
102         False otherwise.
103         """
104         return False
105 
106     def isdigit(self): 
107         """
108         是否是数字
109         """
110         return False
111 
112     def isidentifier(self): 
113         """
114         S.isidentifier() -> bool
115         
116         Return True if S is a valid identifier according
117         to the language definition.
118         
119         Use keyword.iskeyword() to test for reserved identifiers
120         such as "def" and "class".
121         """
122         return False
123 
124     def islower(self): 
125         """
126         是否小写
127         """
128         return False
129 
130     def isnumeric(self): # real signature unknown; restored from __doc__
131         """
132         S.isnumeric() -> bool
133         
134         Return True if there are only numeric characters in S,
135         False otherwise.
136         """
137         return False
138 
139     def isprintable(self): # real signature unknown; restored from __doc__
140         """
141         S.isprintable() -> bool
142         
143         Return True if all characters in S are considered
144         printable in repr() or S is empty, False otherwise.
145         """
146         return False
147 
148     def isspace(self): # real signature unknown; restored from __doc__
149         """
150         S.isspace() -> bool
151         
152         Return True if all characters in S are whitespace
153         and there is at least one character in S, False otherwise.
154         """
155         return False
156 
157     def istitle(self): # real signature unknown; restored from __doc__
158         """
159         S.istitle() -> bool
160         
161         Return True if S is a titlecased string and there is at least one
162         character in S, i.e. upper- and titlecase characters may only
163         follow uncased characters and lowercase characters only cased ones.
164         Return False otherwise.
165         """
166         return False
167 
168     def isupper(self): # real signature unknown; restored from __doc__
169         """
170         S.isupper() -> bool
171         
172         Return True if all cased characters in S are uppercase and there is
173         at least one cased character in S, False otherwise.
174         """
175         return False
176 
177     def join(self, iterable): 
178         """
179         可用于拼接字符串
180         """
181         return ""
182 
183     def ljust(self, width, fillchar=None): 
184         """
185         内容左对齐,右侧填充
186         """
187         return ""
188 
189     def lower(self): 
190         """
191         变小写
192         """
193         return ""
194 
195     def lstrip(self, chars=None): 
196         """
197         移除左侧空白
198         """
199         return ""
200 
201     def maketrans(self, *args, **kwargs): # real signature unknown
202         """
203         Return a translation table usable for str.translate().
204         
205         If there is only one argument, it must be a dictionary mapping Unicode
206         ordinals (integers) or characters to Unicode ordinals, strings or None.
207         Character keys will be then converted to ordinals.
208         If there are two arguments, they must be strings of equal length, and
209         in the resulting dictionary, each character in x will be mapped to the
210         character at the same position in y. If there is a third argument, it
211         must be a string, whose characters will be mapped to None in the result.
212         """
213         pass
214 
215     def partition(self, sep): # real signature unknown; restored from __doc__
216         """
217         S.partition(sep) -> (head, sep, tail)
218         
219         Search for the separator sep in S, and return the part before it,
220         the separator itself, and the part after it.  If the separator is not
221         found, return S and two empty strings.
222         """
223         pass
224 
225     def replace(self, old, new, count=None):
226         """
227         替换
228         """
229         return ""
230 
231     def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
232         """
233         S.rfind(sub[, start[, end]]) -> int
234         
235         Return the highest index in S where substring sub is found,
236         such that sub is contained within S[start:end].  Optional
237         arguments start and end are interpreted as in slice notation.
238         
239         Return -1 on failure.
240         """
241         return 0
242 
243     def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
244         """
245         S.rindex(sub[, start[, end]]) -> int
246         
247         Return the highest index in S where substring sub is found,
248         such that sub is contained within S[start:end].  Optional
249         arguments start and end are interpreted as in slice notation.
250         
251         Raises ValueError when the substring is not found.
252         """
253         return 0
254 
255     def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
256         """
257         S.rjust(width[, fillchar]) -> str
258         
259         Return S right-justified in a string of length width. Padding is
260         done using the specified fill character (default is a space).
261         """
262         return ""
263 
264     def rpartition(self, sep): # real signature unknown; restored from __doc__
265         """
266         S.rpartition(sep) -> (head, sep, tail)
267         
268         Search for the separator sep in S, starting at the end of S, and return
269         the part before it, the separator itself, and the part after it.  If the
270         separator is not found, return two empty strings and S.
271         """
272         pass
273 
274     def rsplit(self, sep=None, maxsplit=-1): 
275         return []
276 
277     def rstrip(self, chars=None): # real signature unknown; restored from __doc__
278         """
279         S.rstrip([chars]) -> str
280         
281         Return a copy of the string S with trailing whitespace removed.
282         If chars is given and not None, remove characters in chars instead.
283         """
284         return ""
285 
286     def split(self, sep=None, maxsplit=-1):
287         """
288         分割,maxsplit分割多少次
289         """
290         return []
291 
292     def splitlines(self, keepends=None):
293         """
294         根据换行进行分割
295         """
296         return []
297 
298     def startswith(self, prefix, start=None, end=None): 
299         """
300         是否起始
301         """
302         return False
303 
304     def strip(self, chars=None): 
305         """
306         移除两端的空白
307         """
308         return ""
309 
310     def swapcase(self): 
311         """
312         大小写之间的转换
313         """
314         return ""
315 
316     def title(self): # real signature unknown; restored from __doc__
317         """
318         S.title() -> str
319         
320         Return a titlecased version of S, i.e. words start with title case
321         characters, all remaining cased characters have lower case.
322         """
323         return ""
324 
325     def translate(self, table): 
326         """
327         转换,需要先做一个对应表,最后一个表示删除字符集合
328         """
329         return ""
330 
331     def upper(self): # real signature unknown; restored from __doc__
332         """
333         S.upper() -> str
334         
335         Return a copy of S converted to uppercase.
336         """
337         return ""
338 
339     def zfill(self, width): 
340         """
341         返回固定长度的字符串,原字符串右对齐,前面填充0
342         """
343         return ""
344 
345     def __add__(self, *args, **kwargs): # real signature unknown
346         """ Return self+value. """
347         pass
348 
349     def __contains__(self, *args, **kwargs): # real signature unknown
350         """ Return key in self. """
351         pass
352 
353     def __eq__(self, *args, **kwargs): # real signature unknown
354         """ Return self==value. """
355         pass
356 
357     def __format__(self, format_spec): # real signature unknown; restored from __doc__
358         """
359         S.__format__(format_spec) -> str
360         
361         Return a formatted version of S as described by format_spec.
362         """
363         return ""
364 
365     def __getattribute__(self, *args, **kwargs): # real signature unknown
366         """ Return getattr(self, name). """
367         pass
368 
369     def __getitem__(self, *args, **kwargs): # real signature unknown
370         """ Return self[key]. """
371         pass
372 
373     def __getnewargs__(self, *args, **kwargs): # real signature unknown
374         pass
375 
376     def __ge__(self, *args, **kwargs): # real signature unknown
377         """ Return self>=value. """
378         pass
379 
380     def __gt__(self, *args, **kwargs): # real signature unknown
381         """ Return self>value. """
382         pass
383 
384     def __hash__(self, *args, **kwargs): # real signature unknown
385         """ Return hash(self). """
386         pass
387 
388     def __init__(self, value='', encoding=None, errors='strict'): # known special case of str.__init__
389         """
390         str(object='') -> str
391         str(bytes_or_buffer[, encoding[, errors]]) -> str
392         
393         Create a new string object from the given object. If encoding or
394         errors is specified, then the object must expose a data buffer
395         that will be decoded using the given encoding and error handler.
396         Otherwise, returns the result of object.__str__() (if defined)
397         or repr(object).
398         encoding defaults to sys.getdefaultencoding().
399         errors defaults to 'strict'.
400         # (copied from class doc)
401         """
402         pass
403 
404     def __iter__(self, *args, **kwargs): # real signature unknown
405         """ Implement iter(self). """
406         pass
407 
408     def __len__(self, *args, **kwargs): # real signature unknown
409         """ Return len(self). """
410         pass
411 
412     def __le__(self, *args, **kwargs): # real signature unknown
413         """ Return self<=value. """
414         pass
415 
416     def __lt__(self, *args, **kwargs): # real signature unknown
417         """ Return self<value. """
418         pass
419 
420     def __mod__(self, *args, **kwargs): # real signature unknown
421         """ Return self%value. """
422         pass
423 
424     def __mul__(self, *args, **kwargs): # real signature unknown
425         """ Return self*value.n """
426         pass
427 
428     @staticmethod # known case of __new__
429     def __new__(*args, **kwargs): # real signature unknown
430         """ Create and return a new object.  See help(type) for accurate signature. """
431         pass
432 
433     def __ne__(self, *args, **kwargs): # real signature unknown
434         """ Return self!=value. """
435         pass
436 
437     def __repr__(self, *args, **kwargs): # real signature unknown
438         """ Return repr(self). """
439         pass
440 
441     def __rmod__(self, *args, **kwargs): # real signature unknown
442         """ Return value%self. """
443         pass
444 
445     def __rmul__(self, *args, **kwargs): # real signature unknown
446         """ Return self*value. """
447         pass
448 
449     def __sizeof__(self): # real signature unknown; restored from __doc__
450         """ S.__sizeof__() -> size of S in memory, in bytes """
451         pass
452 
453     def __str__(self, *args, **kwargs): # real signature unknown
454         """ Return str(self). """
455         pass
Str源码剖析

4.列表

创建列表:

1 list1 = ['1','2',3]
2 
3 list2 = list( ['1','2',3] )

基本操作:

  • 索引
  • 切片
  • 追加
  • 删除
  • 长度
  • 切片
  • 循环
  • 包含
  1 class list(object):
  2     """
  3     list() -> new empty list
  4     list(iterable) -> new list initialized from iterable's items
  5     """
  6     def append(self, p_object):
  7         """ list添加数据的常用方法 """
  8         pass
  9 
 10     def clear(self): # real signature unknown; restored from __doc__
 11         """ L.clear() -> None -- remove all items from L """
 12         pass
 13 
 14     def copy(self): # real signature unknown; restored from __doc__
 15         """ L.copy() -> list -- a shallow copy of L """
 16         return []
 17 
 18     def count(self, value): # real signature unknown; restored from __doc__
 19         """ 计数 """
 20         return 0
 21 
 22     def extend(self, iterable): # real signature unknown; restored from __doc__
 23         """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """
 24         pass
 25 
 26     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
 27         """
 28         L.index(value, [start, [stop]]) -> integer -- return first index of value.
 29         Raises ValueError if the value is not present.
 30         """
 31         return 0
 32 
 33     def insert(self, index, p_object): # real signature unknown; restored from __doc__
 34         """ L.insert(index, object) -- insert object before index """
 35         pass
 36 
 37     def pop(self, index=None): # real signature unknown; restored from __doc__
 38         """
 39         弹出
 40         """
 41         pass
 42 
 43     def remove(self, value): # real signature unknown; restored from __doc__
 44         """
 45         移除
 46         """
 47         pass
 48 
 49     def reverse(self): # real signature unknown; restored from __doc__
 50         """ L.reverse() -- reverse *IN PLACE* """
 51         pass
 52 
 53     def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
 54         """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
 55         pass
 56 
 57     def __add__(self, *args, **kwargs): # real signature unknown
 58         """ Return self+value. """
 59         pass
 60 
 61     def __contains__(self, *args, **kwargs): # real signature unknown
 62         """ Return key in self. """
 63         pass
 64 
 65     def __delitem__(self, *args, **kwargs): # real signature unknown
 66         """ Delete self[key]. """
 67         pass
 68 
 69     def __eq__(self, *args, **kwargs): # real signature unknown
 70         """ Return self==value. """
 71         pass
 72 
 73     def __getattribute__(self, *args, **kwargs): # real signature unknown
 74         """ Return getattr(self, name). """
 75         pass
 76 
 77     def __getitem__(self, y): # real signature unknown; restored from __doc__
 78         """ x.__getitem__(y) <==> x[y] """
 79         pass
 80 
 81     def __ge__(self, *args, **kwargs): # real signature unknown
 82         """ Return self>=value. """
 83         pass
 84 
 85     def __gt__(self, *args, **kwargs): # real signature unknown
 86         """ Return self>value. """
 87         pass
 88 
 89     def __iadd__(self, *args, **kwargs): # real signature unknown
 90         """ Implement self+=value. """
 91         pass
 92 
 93     def __imul__(self, *args, **kwargs): # real signature unknown
 94         """ Implement self*=value. """
 95         pass
 96 
 97     def __init__(self, seq=()): # known special case of list.__init__
 98         """
 99         list() -> new empty list
100         list(iterable) -> new list initialized from iterable's items
101         # (copied from class doc)
102         """
103         pass
104 
105     def __iter__(self, *args, **kwargs): # real signature unknown
106         """ Implement iter(self). """
107         pass
108 
109     def __len__(self, *args, **kwargs): # real signature unknown
110         """ Return len(self). """
111         pass
112 
113     def __le__(self, *args, **kwargs): # real signature unknown
114         """ Return self<=value. """
115         pass
116 
117     def __lt__(self, *args, **kwargs): # real signature unknown
118         """ Return self<value. """
119         pass
120 
121     def __mul__(self, *args, **kwargs): # real signature unknown
122         """ Return self*value.n """
123         pass
124 
125     @staticmethod # known case of __new__
126     def __new__(*args, **kwargs): # real signature unknown
127         """ Create and return a new object.  See help(type) for accurate signature. """
128         pass
129 
130     def __ne__(self, *args, **kwargs): # real signature unknown
131         """ Return self!=value. """
132         pass
133 
134     def __repr__(self, *args, **kwargs): # real signature unknown
135         """ Return repr(self). """
136         pass
137 
138     def __reversed__(self): # real signature unknown; restored from __doc__
139         """ L.__reversed__() -- return a reverse iterator over the list """
140         pass
141 
142     def __rmul__(self, *args, **kwargs): # real signature unknown
143         """ Return self*value. """
144         pass
145 
146     def __setitem__(self, *args, **kwargs): # real signature unknown
147         """ Set self[key] to value. """
148         pass
149 
150     def __sizeof__(self): # real signature unknown; restored from __doc__
151         """ L.__sizeof__() -- size of L in memory, in bytes """
152         pass
153 
154     __hash__ = None
List源码剖析

5.元组

创建元组:

1 ages1 = (1,2,3,)
2 
3 ages2 = tuple((1,2,3,))

基本操作:

  • 索引
  • 切片
  • 循环
  • 长度
  • 包含
  1 lass 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, y): # real signature unknown; restored from __doc__
 20         """ x.__add__(y) <==> x+y """
 21         pass
 22 
 23     def __contains__(self, y): # real signature unknown; restored from __doc__
 24         """ x.__contains__(y) <==> y in x """
 25         pass
 26 
 27     def __eq__(self, y): # real signature unknown; restored from __doc__
 28         """ x.__eq__(y) <==> x==y """
 29         pass
 30 
 31     def __getattribute__(self, name): # real signature unknown; restored from __doc__
 32         """ x.__getattribute__('name') <==> x.name """
 33         pass
 34 
 35     def __getitem__(self, y): # real signature unknown; restored from __doc__
 36         """ x.__getitem__(y) <==> x[y] """
 37         pass
 38 
 39     def __getnewargs__(self, *args, **kwargs): # real signature unknown
 40         pass
 41 
 42     def __getslice__(self, i, j): # real signature unknown; restored from __doc__
 43         """
 44         x.__getslice__(i, j) <==> x[i:j]
 45                    
 46                    Use of negative indices is not supported.
 47         """
 48         pass
 49 
 50     def __ge__(self, y): # real signature unknown; restored from __doc__
 51         """ x.__ge__(y) <==> x>=y """
 52         pass
 53 
 54     def __gt__(self, y): # real signature unknown; restored from __doc__
 55         """ x.__gt__(y) <==> x>y """
 56         pass
 57 
 58     def __hash__(self): # real signature unknown; restored from __doc__
 59         """ x.__hash__() <==> hash(x) """
 60         pass
 61 
 62     def __init__(self, seq=()): # known special case of tuple.__init__
 63         """
 64         tuple() -> empty tuple
 65         tuple(iterable) -> tuple initialized from iterable's items
 66         
 67         If the argument is a tuple, the return value is the same object.
 68         # (copied from class doc)
 69         """
 70         pass
 71 
 72     def __iter__(self): # real signature unknown; restored from __doc__
 73         """ x.__iter__() <==> iter(x) """
 74         pass
 75 
 76     def __len__(self): # real signature unknown; restored from __doc__
 77         """ x.__len__() <==> len(x) """
 78         pass
 79 
 80     def __le__(self, y): # real signature unknown; restored from __doc__
 81         """ x.__le__(y) <==> x<=y """
 82         pass
 83 
 84     def __lt__(self, y): # real signature unknown; restored from __doc__
 85         """ x.__lt__(y) <==> x<y """
 86         pass
 87 
 88     def __mul__(self, n): # real signature unknown; restored from __doc__
 89         """ x.__mul__(n) <==> x*n """
 90         pass
 91 
 92     @staticmethod # known case of __new__
 93     def __new__(S, *more): # real signature unknown; restored from __doc__
 94         """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
 95         pass
 96 
 97     def __ne__(self, y): # real signature unknown; restored from __doc__
 98         """ x.__ne__(y) <==> x!=y """
 99         pass
100 
101     def __repr__(self): # real signature unknown; restored from __doc__
102         """ x.__repr__() <==> repr(x) """
103         pass
104 
105     def __rmul__(self, n): # real signature unknown; restored from __doc__
106         """ x.__rmul__(n) <==> n*x """
107         pass
108 
109     def __sizeof__(self): # real signature unknown; restored from __doc__
110         """ T.__sizeof__() -- size of T in memory, in bytes """
111         pass
元组源码剖析

6.字典

创建字典:

1 dict1 = {'k1':'v1','k2':'v2'}
2 
3 dict2  =dict({'k1':'v1','k2':'v2'})

常用操作:

  • 索引
  • 新增
  • 删除
  • 键、值、键值对的操作
  • 循环
  • 长度
  1 class dict(object):
  2     """
  3     dict() -> new empty dictionary
  4     dict(mapping) -> new dictionary initialized from a mapping object's
  5         (key, value) pairs
  6     dict(iterable) -> new dictionary initialized as if via:
  7         d = {}
  8         for k, v in iterable:
  9             d[k] = v
 10     dict(**kwargs) -> new dictionary initialized with the name=value pairs
 11         in the keyword argument list.  For example:  dict(one=1, two=2)
 12     """
 13 
 14     def clear(self): # real signature unknown; restored from __doc__
 15         """ 清除内容 """
 16         """ D.clear() -> None.  Remove all items from D. """
 17         pass
 18 
 19     def copy(self): # real signature unknown; restored from __doc__
 20         """ 浅拷贝 """
 21         """ D.copy() -> a shallow copy of D """
 22         pass
 23 
 24     @staticmethod # known case
 25     def fromkeys(S, v=None): # real signature unknown; restored from __doc__
 26         """
 27         dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
 28         v defaults to None.
 29         """
 30         pass
 31 
 32     def get(self, k, d=None): # real signature unknown; restored from __doc__
 33         """ 根据key获取值,d是默认值 """
 34         """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
 35         pass
 36 
 37     def has_key(self, k): # real signature unknown; restored from __doc__
 38         """ 是否有key """
 39         """ D.has_key(k) -> True if D has a key k, else False """
 40         return False
 41 
 42     def items(self): # real signature unknown; restored from __doc__
 43         """ 所有项的列表形式 """
 44         """ D.items() -> list of D's (key, value) pairs, as 2-tuples """
 45         return []
 46 
 47     def iteritems(self): # real signature unknown; restored from __doc__
 48         """ 项可迭代 """
 49         """ D.iteritems() -> an iterator over the (key, value) items of D """
 50         pass
 51 
 52     def iterkeys(self): # real signature unknown; restored from __doc__
 53         """ key可迭代 """
 54         """ D.iterkeys() -> an iterator over the keys of D """
 55         pass
 56 
 57     def itervalues(self): # real signature unknown; restored from __doc__
 58         """ value可迭代 """
 59         """ D.itervalues() -> an iterator over the values of D """
 60         pass
 61 
 62     def keys(self): # real signature unknown; restored from __doc__
 63         """ 所有的key列表 """
 64         """ D.keys() -> list of D's keys """
 65         return []
 66 
 67     def pop(self, k, d=None): # real signature unknown; restored from __doc__
 68         """ 获取并在字典中移除 """
 69         """
 70         D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
 71         If key is not found, d is returned if given, otherwise KeyError is raised
 72         """
 73         pass
 74 
 75     def popitem(self): # real signature unknown; restored from __doc__
 76         """ 获取并在字典中移除 """
 77         """
 78         D.popitem() -> (k, v), remove and return some (key, value) pair as a
 79         2-tuple; but raise KeyError if D is empty.
 80         """
 81         pass
 82 
 83     def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
 84         """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """
 85         """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
 86         pass
 87 
 88     def update(self, E=None, **F): # known special case of dict.update
 89         """ 更新
 90             {'name':'alex', 'age': 18000}
 91             [('name','sbsbsb'),]
 92         """
 93         """
 94         D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
 95         If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
 96         If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
 97         In either case, this is followed by: for k in F: D[k] = F[k]
 98         """
 99         pass
100 
101     def values(self): # real signature unknown; restored from __doc__
102         """ 所有的值 """
103         """ D.values() -> list of D's values """
104         return []
105 
106     def viewitems(self): # real signature unknown; restored from __doc__
107         """ 所有项,只是将内容保存至view对象中 """
108         """ D.viewitems() -> a set-like object providing a view on D's items """
109         pass
110 
111     def viewkeys(self): # real signature unknown; restored from __doc__
112         """ D.viewkeys() -> a set-like object providing a view on D's keys """
113         pass
114 
115     def viewvalues(self): # real signature unknown; restored from __doc__
116         """ D.viewvalues() -> an object providing a view on D's values """
117         pass
118 
119     def __cmp__(self, y): # real signature unknown; restored from __doc__
120         """ x.__cmp__(y) <==> cmp(x,y) """
121         pass
122 
123     def __contains__(self, k): # real signature unknown; restored from __doc__
124         """ D.__contains__(k) -> True if D has a key k, else False """
125         return False
126 
127     def __delitem__(self, y): # real signature unknown; restored from __doc__
128         """ x.__delitem__(y) <==> del x[y] """
129         pass
130 
131     def __eq__(self, y): # real signature unknown; restored from __doc__
132         """ x.__eq__(y) <==> x==y """
133         pass
134 
135     def __getattribute__(self, name): # real signature unknown; restored from __doc__
136         """ x.__getattribute__('name') <==> x.name """
137         pass
138 
139     def __getitem__(self, y): # real signature unknown; restored from __doc__
140         """ x.__getitem__(y) <==> x[y] """
141         pass
142 
143     def __ge__(self, y): # real signature unknown; restored from __doc__
144         """ x.__ge__(y) <==> x>=y """
145         pass
146 
147     def __gt__(self, y): # real signature unknown; restored from __doc__
148         """ x.__gt__(y) <==> x>y """
149         pass
150 
151     def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
152         """
153         dict() -> new empty dictionary
154         dict(mapping) -> new dictionary initialized from a mapping object's
155             (key, value) pairs
156         dict(iterable) -> new dictionary initialized as if via:
157             d = {}
158             for k, v in iterable:
159                 d[k] = v
160         dict(**kwargs) -> new dictionary initialized with the name=value pairs
161             in the keyword argument list.  For example:  dict(one=1, two=2)
162         # (copied from class doc)
163         """
164         pass
165 
166     def __iter__(self): # real signature unknown; restored from __doc__
167         """ x.__iter__() <==> iter(x) """
168         pass
169 
170     def __len__(self): # real signature unknown; restored from __doc__
171         """ x.__len__() <==> len(x) """
172         pass
173 
174     def __le__(self, y): # real signature unknown; restored from __doc__
175         """ x.__le__(y) <==> x<=y """
176         pass
177 
178     def __lt__(self, y): # real signature unknown; restored from __doc__
179         """ x.__lt__(y) <==> x<y """
180         pass
181 
182     @staticmethod # known case of __new__
183     def __new__(S, *more): # real signature unknown; restored from __doc__
184         """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
185         pass
186 
187     def __ne__(self, y): # real signature unknown; restored from __doc__
188         """ x.__ne__(y) <==> x!=y """
189         pass
190 
191     def __repr__(self): # real signature unknown; restored from __doc__
192         """ x.__repr__() <==> repr(x) """
193         pass
194 
195     def __setitem__(self, i, y): # real signature unknown; restored from __doc__
196         """ x.__setitem__(i, y) <==> x[i]=y """
197         pass
198 
199     def __sizeof__(self): # real signature unknown; restored from __doc__
200         """ D.__sizeof__() -> size of D in memory, in bytes """
201         pass
202 
203     __hash__ = None
字典代码剖析

7.集合 

set集合,一个无序而且不重复的元素集合

常用操作:

  • 追加
  • 删除
  • 并集
  • 交集
  • 差集
  1 class set(object):
  2     """
  3     set() -> new empty set object
  4     set(iterable) -> new set object
  5      
  6     Build an unordered collection of unique elements.
  7     """
  8     def add(self, *args, **kwargs): # real signature unknown
  9         """
 10         Add an element to a set,添加元素
 11          
 12         This has no effect if the element is already present.
 13         """
 14         pass
 15  
 16     def clear(self, *args, **kwargs): # real signature unknown
 17         """ Remove all elements from this set. 清除内容"""
 18         pass
 19  
 20     def copy(self, *args, **kwargs): # real signature unknown
 21         """ Return a shallow copy of a set. 浅拷贝  """
 22         pass
 23  
 24     def difference(self, *args, **kwargs): # real signature unknown
 25         """
 26         Return the difference of two or more sets as a new set. A中存在,B中不存在
 27          
 28         (i.e. all elements that are in this set but not the others.)
 29         """
 30         pass
 31  
 32     def difference_update(self, *args, **kwargs): # real signature unknown
 33         """ Remove all elements of another set from this set.  从当前集合中删除和B中相同的元素"""
 34         pass
 35  
 36     def discard(self, *args, **kwargs): # real signature unknown
 37         """
 38         Remove an element from a set if it is a member.
 39          
 40         If the element is not a member, do nothing. 移除指定元素,不存在不保错
 41         """
 42         pass
 43  
 44     def intersection(self, *args, **kwargs): # real signature unknown
 45         """
 46         Return the intersection of two sets as a new set. 交集
 47          
 48         (i.e. all elements that are in both sets.)
 49         """
 50         pass
 51  
 52     def intersection_update(self, *args, **kwargs): # real signature unknown
 53         """ Update a set with the intersection of itself and another.  取交集并更更新到A中 """
 54         pass
 55  
 56     def isdisjoint(self, *args, **kwargs): # real signature unknown
 57         """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False"""
 58         pass
 59  
 60     def issubset(self, *args, **kwargs): # real signature unknown
 61         """ Report whether another set contains this set.  是否是子序列"""
 62         pass
 63  
 64     def issuperset(self, *args, **kwargs): # real signature unknown
 65         """ Report whether this set contains another set. 是否是父序列"""
 66         pass
 67  
 68     def pop(self, *args, **kwargs): # real signature unknown
 69         """
 70         Remove and return an arbitrary set element.
 71         Raises KeyError if the set is empty. 移除元素
 72         """
 73         pass
 74  
 75     def remove(self, *args, **kwargs): # real signature unknown
 76         """
 77         Remove an element from a set; it must be a member.
 78          
 79         If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
 80         """
 81         pass
 82  
 83     def symmetric_difference(self, *args, **kwargs): # real signature unknown
 84         """
 85         Return the symmetric difference of two sets as a new set.  对称差集
 86          
 87         (i.e. all elements that are in exactly one of the sets.)
 88         """
 89         pass
 90  
 91     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
 92         """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
 93         pass
 94  
 95     def union(self, *args, **kwargs): # real signature unknown
 96         """
 97         Return the union of sets as a new set.  并集
 98          
 99         (i.e. all elements that are in either set.)
100         """
101         pass
102  
103     def update(self, *args, **kwargs): # real signature unknown
104         """ Update a set with the union of itself and others. 更新 """
105         pass
Set集合代码剖析

这里补充几点

1.for循环

用户按照顺序循环可迭代对象中的内容

1 li = [1,2,3]
2 
3 for item in li:
4     print(item)

2.enumrate与for循环连用

为可迭代对象添加序号

1 li = [1,2,3]
2 
3 for item_index,item_value in enumerate(li,1):    #表示序号从1开始
4     print(item_index,item_value)

3.range和xrange

python2.x:

生成一个范围

1 print(range(1,10))
2 # 结果:[1,2,3,4,5,6,7,8,9]
3 
4 print(range(1,10,2))
5 # 结果:[1,3,5,7,9]
6 
7 print(range(10,0,-2))
8 # 结果:[10,8,6,4,2]

这里额外说一句:

xrange会返回一个生成器对象

python3.x:

Python3中取消了xrange,但是python3中的range返回的就是生成器对象

1 print(rage(0,10))
2 # 结果:range(0,10)

这里再补充几点:运算符

1.算数运算

2.比较运算

3.赋值运算

4.逻辑运算

5.成员运算

原文地址:https://www.cnblogs.com/smiling-crying/p/9201986.html