python入门2

来源:互联网 发布:对人工智能社会的畅想 编辑:程序博客网 时间:2024/06/05 21:57

数字模块:math random

>>> s = 'spam'>>> len(s)4>>> s[0]'s'>>> s[-1]'m'>>> s[-2]'a'>>> s[1:]'pam'>>> s[0:3]'spa'>>> s+'meto''spammeto'>>> s'spam'>>> s*5'spamspamspamspamspam'>>> #字符串在Python具有不可变性>>> s[0]='z'Traceback (most recent call last):  File "<pyshell#41>", line 1, in <module>    s[0]='z'TypeError: 'str' object does not support item assignment>>> s = 'z' + s[1:]>>> s'zpam'#find和replace方法>>> s.find('pa')1>>> s.replace('pa','me')'zmem'>>> s'zpam'>>> s.find('pa')1>>> s.replace('pa','me')'zmem'>>> s'zpam'>>> line = 'aa,bb,cc,dd'>>> line.split(',')['aa', 'bb', 'cc', 'dd']>>> line.upper()'AA,BB,CC,DD'>>> line'aa,bb,cc,dd'>>> line.isalpha()False#字符串模块里的属性和方法查看>>> 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__', '__subclasshook__', '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', 'translate', 'upper', 'zfill']>>> help(s.rfind)Help on built-in function rfind:rfind(...) method of builtins.str instance    S.rfind(sub[, start[, end]]) -> int    Return the highest index in S where substring sub is found,    such that sub is contained within S[start:end].  Optional    arguments start and end are interpreted as in slice notation.    Return -1 on failure.>>> #模式匹配>>> import re>>> match = re.match('hello[\t]*(.*)world','hello python world')>>> match.group(1)' python '>>> dir(re)['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_alphanum_bytes', '_alphanum_str', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_locale', '_pattern_type', '_pickle', '_subx', 'compile', 'copyreg', 'error', 'escape', 'findall', 'finditer', 'fullmatch', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']>>> dir(match)['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']>>> #列表>>> L = [123, 'spam', 1.23]>>> len(L)3>>> L+[23, 'me', 'to'][123, 'spam', 1.23, 23, 'me', 'to']>>> L[123, 'spam', 1.23]>>> L = L+[23, 'me', 'to']>>> L[123, 'spam', 1.23, 23, 'me', 'to']>>> >>> >>> #在列表中增加元素>>> L.append(45)>>> L[123, 'spam', 1.23, 23, 'me', 'to', 45]>>> #在list delete元素>>> L.pop(3)23>>> L[123, 'spam', 1.23, 'me', 'to', 45]>>> >>> #列表的排序和翻转>>> M = ['bb', 'aa', 'cc']>>> M.sort()>>> M['aa', 'bb', 'cc']>>> M.reverse()>>> M['cc', 'bb', 'aa']>>> >>> #嵌套列表>>> M = [    [1,2,3],    [4,5,6],    [7,8,9]    ]>>> M[[1, 2, 3], [4, 5, 6], [7, 8, 9]]>>> M[1][4, 5, 6]>>> M[2][1]8>>> #列表的解析>>> >>> col2 = [row[1] for row in M]>>> col2[2, 5, 8]>>> >>> [row[1] for row in M if row[1]%2==0][2, 8]>>> >>> >>> diag = [M[i][i] for i in [0,1,2]]>>> diag[1, 5, 9]>>> >>> >>> doubles = [c * 2 for c in 'spam']>>> doubles['ss', 'pp', 'aa', 'mm']>>> #用解析来创建列表、集合、字典>>> [ord(x) for x in 'spam'][115, 112, 97, 109]>>> {ord(x) for x in 'spam'}{112, 97, 115, 109}>>> {x: ord(x) for x in 'spam'}{'s': 115, 'p': 112, 'm': 109, 'a': 97}>>> #字典>>> #映射操作>>> D = {'food':'spam', 'quantity':4, 'color':'pink'}>>> D{'food': 'spam', 'quantity': 4, 'color': 'pink'}>>> d['food']Traceback (most recent call last):  File "<pyshell#131>", line 1, in <module>    d['food']NameError: name 'd' is not defined>>> D['food']'spam'>>> >>> #另一种创建方法>>> D = {}>>> D['name']='me'>>> D['job']='dev'>>> D['sex']='falme'>>> D{'name': 'me', 'job': 'dev', 'sex': 'falme'}>>> print(D['job'])dev>>> >>> >>> >>> #重访嵌套>>> rec = {'name':{'first':'Bob','last':'Smith'}, 'job':['dev','mgr'],'age':40.5}>>> rec{'name': {'last': 'Smith', 'first': 'Bob'}, 'age': 40.5, 'job': ['dev', 'mgr']}>>> >>> rec['name']{'last': 'Smith', 'first': 'Bob'}>>> rec['name']['last']'Smith'>>> >>> >>> rec['job']['dev', 'mgr']>>> rec['job'][1]'mgr'

0 0
原创粉丝点击