Python Basic data type
The self-band method of sequence type
1. List of common methods
2. Common methods of the Yuan Zu
3. Common methods of strings
1. List of common methods
L.append (obj) #在列表末尾添加新的对象L. Clear () #清空列表L. Copy () #复制列表, not the same object, with the same content, with a return value. Different IDs (different addresses in memory) l.count (obj) #统计某个元素在列表中出现的次数L. Extend (obj) #用obj扩展原来的列表L. Index (obj) #默认值返回这个元素最先出现的索引位置; Need to appear next, add optional parameter start,stop. Linsert (index,obj) #插入元素, you can specify a position by specifying the location L.pop (index) #出栈. Index, default is L[-1], by index delete l.remove (obj) #移除指定元素从左边开始第一个L. Reverse () #反向列表元素L. Sort () #对列表进行排序 (default acsii). The elements in the list are of the same type (Key=len) built-in functions: sorted () and reversed () >>> li = [1,2,3]>>> dir (li) #查看li列表的属性方法, Underlined for magic methods and private methods, without tube. Learn the rest. [' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' _ _ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __gt__ ', ' __hash__ ', ' __iadd__ ', ' __imul__ ', ' __init__ ', ' __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 (LI) #列出所有方法 >>> help (Li.append) #查看li的append的用法Help on built-in function append:append (.. .) Method of Builtins.list instance L.append (object), None--Append object to end #添加对象返回空值, add >>> L at the end of the original list I.append (4) >>> li[1, 2, 3, 4]>>> li.clear () >>> li[]>>> li = [1,2,3]>>> ID ( LI) 54036680>>> li2 = li.copy () >>> li2[1, 2, 3]>>> ID (li2) 54636232>>> Li.count (2) 1 >>> li.append (2) >>> Li.count (2) 2>>> li[1, 2, 3, 2]>>> li.extend ("456") >> > Li[1, 2, 3, 2, ' 4 ', ' 5 ', ' 6 ']>>> li = [1,2,3]>>> li.extend ([4,5,6]) >>> li[1, 2, 3, 4, 5, 6 ]>>> Li.extend ((7,8)) >>> li[1, 2, 3, 4, 5, 6, 7, 8]>>> li[1, 2, 3, 4, 5, 6, 7, 8]>>> l I.index (7) 6>>> li = [' A ', ' B ',' C ', ' d ']>>> li.index (' C ') 2>>> Help (Li.index) Help on built-in function Index:index (...) Method of Builtins.list instance L.index (value, [Start, [stop]]), integer-return first index of value. Raises valueerror if the value is not present.>>> li = [' A ', ' B ', ' C ', ' d ', ' C ']>>> li.index (' C ') 2>>& Gt Li.index (' C ', 3) 4>>> li.index (' C ', 3,5) 4>>> li.index (' C ', 3,6) 4>>> Help (Li.insert) Built-in function Insert:insert (...) method of Builtins.list instance L.insert (Index, object)--Insert object before I Ndex>>> li[' A ', ' B ', ' C ', ' d ', ' C ']>>> li.insert (1, ' lucky ') >>> li[' A ', ' lucky ', ' B ', ' C ', ' d ', ' C ']>>> li.insert ( -1, ' QQ ') >>> li[' A ', ' lucky ', ' B ', ' C ', ' d ', ' QQ ', ' C ']>>> help (Li.pop) help On built-in function Pop:pop (...) method of Builtins.list instance L.pop ([index]), item--Remove and return item At index (default last). Raises indexerror if List is empty or index is out of range.>>> li.pop (1) ' Lucky ' >>> li[' A ', ' B ', ' C ', ' d ', ' QQ ', ' C ']>>> Li.pop () ' C ' >>> li[' A ', ' B ', ' C ', ' d ', ' QQ ']>>> help (Li.remove)-On built-in function remove:remove ( ...) Method of Builtins.list instance L.remove (value) and None--remove first occurrence of value. Raises valueerror if the value is not present.>>> li[' a ', ' B ', ' C ', ' d ', ' QQ ']>>> li.remove (' C ') >> > li[' A ', ' B ', ' d ', ' QQ ']>>> help (Li.reverse)-On built-in function reverse:reverse (...) Method of Builtins . List instance L.reverse ()--Reverse *in place*>>> li.reverse () >>> li[' C ', ' d ', ' B ', ' a ']>>> ; Help (Li.sort)-On built-in function Sort:sort (...) method of Builtins.list instance L.sort (Key=none, Reverse=false) None--Stable sort *in place*>>> li.sort () >>> li[' A ', ' B ', ' C ', ' d ']>>> li = [' A ', ' C ', ' a B ']>>> li.sort () >>> Li[' a ', ' ab ', ' C ']>>> li.sort (key=len) #按照长度相等的排序然后再按不等的排序 >>> li[' A ', ' C ', ' ab ']>>> li. Sort (key=len,reverse=true) >>> li[' ab ', ' a ', ' C '] functions reversed and sorted:>>> reversed (LI) #返回一个迭代器 < List_reverseiterator Object at 0x00000000033c2978>>>> list (reversed (LI)) [' C ', ' a ', ' AB ']>>> Sorted (LI) [' A ', ' ab ', ' C '] variable features:>>> li = [2,3]>>> id (li) 53901000>>> li.append (4) >> > ID (LI) 53901000>>> li.pop () 4>>> ID (LI) 53901000
2. Common methods of the Yuan Zu
count(obj) #统计某个元素在元祖中出现的次数index(obj) #从列表中找到某个值第一个匹配项的索引位置注意:生命只有一个元素的元组时要加逗号特点:不可变举例:>>> tu = 1,2>>> dir(tu)[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘count‘, ‘index‘]>>> tu.count(1)1>>> tu.count(3)0>>> help(tu.index)Help on built-in function index:index(...) method of builtins.tuple instance T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.>>> tu.index(1)0
3. Common methods of strings
S.count (x) #返回字符串x在s中出现的次数, with parameter S.endswith (x) #如果字符串s以x结尾, return Trues.startswith (x) #如果字符串s以x开头, return to Trues.find (x) #返回字符串中出现x的最左端字符的索引值, return -1s.index (x) #返回字符串中出现x的最左端的索引值 if absent, or throw ValueError exception S.isalpha () if not #测 All letters are true, otherwise falses.isdigit () #测试是否全是数字, are true; otherwise falses.islower () #测试全是小写s. Isupper () #测试全是大写 S.lower () #将字符串转为小写s. Upper () #将字符串转为大写s. replace (x, y) #字串替换, substituting Y for any position where the string x appears in the string S S.split () #返回一系列用空格分割的字符串列表s. Split (b) #a, B is an optional parameter, a is the string that will be split, and B is a description of the maximum number of splits. Examples of:>>> s= ' >>> dir (s) [' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __getnewargs__ ', ' __gt__ ', ' __hash__ ', ' __init__ ' , ' __iter__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __mod__ ', ' __mul__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' _ _repr__ ', ' __rmod__ ', ' __rmul__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __sub 'Classhook__ ', ' capitalize ', ' casefold ', ' center ', ' count ', ' encode ', ' endswith ', ' expandtabs ', ' find ', ' format ', ' format ' _map ', ' index ', ' isalnum ', ' isalpha ', ' isdecimal ', ' isdigit ', ' isidentifier ', ' islower ', ' isnumeric ', ' isprintable ', ' Isspace ', ' istitle ', ' isupper ', ' join ', ' ljust ', ' lower ', ' lstrip ', ' Maketrans ', ' partition ', ' replace ', ' rfind ', ' Rindex ', ' rjust ', ' rpartition ', ' rsplit ', ' Rstrip ', ' Split ', ' splitlines ', ' startswith ', ' strip ', ' swapcase ', ' title ', ' Transla Te ', ' Upper ', ' Zfill ']>>> s.count (' 3 ') 0>>> Help (S.endswith)-On built-in function EndsWith: EndsWith (...) method of Builtins.str instance S.endswith (suffix[, start[, end]), bool Return True if S ends WI Th the specified suffix, False otherwise. With optional start, test S beginning on that position. With optional end, stop comparing S on that position. Suffix can also is a tuple of strings to try.>>> s= ' ABCDE ' >>> s.endswith (' a ') false>>> S.endswit H ('E ') true>>> Help (S.startswith)-On built-in function Startswith:startswith (...) Method of Builtins.str Instance S.startswith (prefix[, start[, end]), bool Return True if S starts with the specified prefix, False ot Herwise. With optional start, test S beginning on that position. With optional end, stop comparing S on that position. Prefix can also be a tuple of the strings to try.>>> s.startswith (' a ') true>>> help (S.find) help on built-in F Unction Find:find (...) method of Builtins.str instance S.find (sub[, start[, end]), int Return the lowest index In S where substring sub is found, such this sub is contained within S[start:end]. Optional arguments start and end is interpreted as in slice notation. Return-1 on failure.>>> s.find (' V ') #不存在的值 -1>>> s.find (' B '), 1>>> Help (S.index) help on B uilt-in function Index:index (...) method of Builtins.str instance S.index (sub[, start[, end])NT like S.find () but raise valueerror when the substring was not found.>>> s.index (' e ') #默认返回最先出现的位置4 >&G t;> s= ' ABCDE ' >>> s.isalpha () #测试全是字母True >>> s.isdigit () #测试全是数字, only positive integers can be tested, negative numbers returned -1FALSE>>&G T S.islower () #测试全是小写True >>> s.isupper () #测试全是大写False >>> s1 = ' 12AA ' >>> s1.isupper () true& Gt;>> s1.upper () #全部改成大写 ' 12AA ' >>> s1.lower () #全部改成小写 ' 12aa>>> Help (S.replace)-On Buil t-in function Replace:replace (...) method of Builtins.str instance S.replace (old, new[, Count]) str Return a C Opy of S with all occurrences of substring-replaced by new. If The optional argument count is given and only the first count occurrences be replaced.>>> s1.replace (' A ', ' BB ') #将旧元素替换成新的元素. The parameters of the replacement number are optional ' Bbaa ' >>> s1.replace (' A ', ' B ', 1) ' 12bA ' >>> help (S.split) and help on built-in function split: Split (...) method of Builtins.str instance S.split (sep=none, maxsplit=-1), List of strings Return a list of the words in S, using Sep as the delimiter string. If Maxsplit is given, at most maxsplit splits be done. If Sep is no specified or is None, any whitespace string is a separator and empty strings be removed from the ResU Lt.>>> s.split (sep= ' o ') [' i l ', ' ve Pyth ', ' n ']>>> s.split (maxsplit=1) [' I ', ' Love Python ']>> > S.split (' 1 ') [' I love Python ']>>> s.split (sep= ' o ', maxsplit=1) [' I l ', ' ve python ']>>> s= ' 123 ' > >> s.split (sep= ' 1 ') [', ' 23 ']
Python basic data Type (ii)-PYTHON3.0 learning notes