{"id":1046,"date":"2022-05-31T02:31:32","date_gmt":"2022-05-31T02:31:32","guid":{"rendered":"https:\/\/262235.xyz\/?p=1046"},"modified":"2022-05-31T02:31:32","modified_gmt":"2022-05-31T02:31:32","slug":"1046","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2022\/05\/31\/1046\/","title":{"rendered":"\u4e2d\u6587python_base_\u5343\u884c\u4ee3\u7801\u5165\u95e8Python.py"},"content":{"rendered":"<h2>\u4e2d\u6587python_base_\u5343\u884c\u4ee3\u7801\u5165\u95e8Python.py   \u6e90\u7801<\/h2>\n<p><a href=\"https:\/\/raw.githubusercontent.com\/hongwenjun\/LearnPython\/master\/python_base.py\">https:\/\/raw.githubusercontent.com\/hongwenjun\/LearnPython\/master\/python_base.py<\/a><\/p>\n<h1>-- \u5bfb\u6c42\u5e2e\u52a9:<\/h1>\n<pre><code>dir(obj)            # \u7b80\u5355\u7684\u5217\u51fa\u5bf9\u8c61obj\u6240\u5305\u542b\u7684\u65b9\u6cd5\u540d\u79f0\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\nhelp(obj.func)      # \u67e5\u8be2obj.func\u7684\u5177\u4f53\u4ecb\u7ecd\u548c\u7528\u6cd5\n<\/code><\/pre>\n<h1>-- \u6d4b\u8bd5\u7c7b\u578b\u7684\u4e09\u79cd\u65b9\u6cd5\uff0c\u63a8\u8350\u7b2c\u4e09\u79cd<\/h1>\n<pre><code>if type(L) == type([]): print(\"L is list\")\nif type(L) == list: print(\"L is list\")\nif isinstance(L, list): print(\"L is list\")\n    <\/code><\/pre>\n<h1>-- Python\u6570\u636e\u7c7b\u578b\uff1a\u54c8\u5e0c\u7c7b\u578b\u3001\u4e0d\u53ef\u54c8\u5e0c\u7c7b\u578b<\/h1>\n<pre><code># \u54c8\u5e0c\u7c7b\u578b\uff0c\u5373\u5728\u539f\u5730\u4e0d\u80fd\u6539\u53d8\u7684\u53d8\u91cf\u7c7b\u578b\uff0c\u4e0d\u53ef\u53d8\u7c7b\u578b\u3002\u53ef\u5229\u7528hash\u51fd\u6570\u67e5\u770b\u5176hash\u503c\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u5b57\u5178\u7684key\n\"\u6570\u5b57\u7c7b\u578b\uff1aint, float, decimal.Decimal, fractions.Fraction, complex\"\n\"\u5b57\u7b26\u4e32\u7c7b\u578b\uff1astr, bytes\"\n\"\u5143\u7ec4\uff1atuple\"\n\"\u51bb\u7ed3\u96c6\u5408\uff1afrozenset\"\n\"\u5e03\u5c14\u7c7b\u578b\uff1aTrue, False\"\n\"None\"\n# \u4e0d\u53efhash\u7c7b\u578b\uff1a\u539f\u5730\u53ef\u53d8\u7c7b\u578b\uff1alist\u3001dict\u548cset\u3002\u5b83\u4eec\u4e0d\u53ef\u4ee5\u4f5c\u4e3a\u5b57\u5178\u7684key\u3002\n<\/code><\/pre>\n<h1>-- \u6570\u5b57\u5e38\u91cf<\/h1>\n<pre><code>1234, -1234, 0, 999999999                    # \u6574\u6570\n1.23, 1., 3.14e-10, 4E210, 4.0e+210          # \u6d6e\u70b9\u6570\n0o177, 0x9ff, 0X9FF, 0b101010                # \u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u6570\u5b57\n3+4j, 3.0+4.0j, 3J                           # \u590d\u6570\u5e38\u91cf\uff0c\u4e5f\u53ef\u4ee5\u7528complex(real, image)\u6765\u521b\u5efa\nhex(I), oct(I), bin(I)                       # \u5c06\u5341\u8fdb\u5236\u6570\u8f6c\u5316\u4e3a\u5341\u516d\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u201c\u5b57\u7b26\u4e32\u201d\nint(string, base)                            # \u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u6574\u6570\uff0cbase\u4e3a\u8fdb\u5236\u6570\n# 2.x\u4e2d\uff0c\u6709\u4e24\u79cd\u6574\u6570\u7c7b\u578b\uff1a\u4e00\u822c\u6574\u6570\uff0832\u4f4d\uff09\u548c\u957f\u6574\u6570\uff08\u65e0\u7a77\u7cbe\u5ea6\uff09\u3002\u53ef\u4ee5\u7528l\u6216L\u7ed3\u5c3e\uff0c\u8feb\u4f7f\u4e00\u822c\u6574\u6570\u6210\u4e3a\u957f\u6574\u6570\nfloat('inf'), float('-inf'), float('nan')    # \u65e0\u7a77\u5927, \u65e0\u7a77\u5c0f, \u975e\u6570\n<\/code><\/pre>\n<h1>-- \u6570\u5b57\u7684\u8868\u8fbe\u5f0f\u64cd\u4f5c\u7b26<\/h1>\n<pre><code>yield x                                      # \u751f\u6210\u5668\u51fd\u6570\u53d1\u9001\u534f\u8bae\nlambda args: expression                      # \u751f\u6210\u533f\u540d\u51fd\u6570\nx if y else z                                # \u4e09\u5143\u9009\u62e9\u8868\u8fbe\u5f0f\nx and y, x or y, not x                       # \u903b\u8f91\u4e0e\u3001\u903b\u8f91\u6216\u3001\u903b\u8f91\u975e\nx in y, x not in y                           # \u6210\u5458\u5bf9\u8c61\u6d4b\u8bd5\nx is y, x is not y                           # \u5bf9\u8c61\u5b9e\u4f53\u6d4b\u8bd5\nx&lt;y, x&lt;=y, x&gt;y, x&gt;=y, x==y, x!=y             # \u5927\u5c0f\u6bd4\u8f83\uff0c\u96c6\u5408\u5b50\u96c6\u6216\u8d85\u96c6\u503c\u76f8\u7b49\u6027\u64cd\u4f5c\u7b26\n1 &lt; a &lt; 3                                    # Python\u4e2d\u5141\u8bb8\u8fde\u7eed\u6bd4\u8f83\nx|y, x&amp;y, x^y                                # \u4f4d\u6216\u3001\u4f4d\u4e0e\u3001\u4f4d\u5f02\u6216\nx&lt;&lt;y, x&gt;&gt;y                                   # \u4f4d\u64cd\u4f5c\uff1ax\u5de6\u79fb\u3001\u53f3\u79fby\u4f4d\n+, -, *, \/, \/\/, %, **                        # \u771f\u9664\u6cd5\u3001floor\u9664\u6cd5\uff1a\u8fd4\u56de\u4e0d\u5927\u4e8e\u771f\u9664\u6cd5\u7ed3\u679c\u7684\u6574\u6570\u503c\u3001\u53d6\u4f59\u3001\u5e42\u8fd0\u7b97\n-x, +x, ~x                                   # \u4e00\u5143\u51cf\u6cd5\u3001\u8bc6\u522b\u3001\u6309\u4f4d\u6c42\u8865\uff08\u53d6\u53cd\uff09\nx[i], x[i:j:k]                               # \u7d22\u5f15\u3001\u5206\u7247\u3001\u8c03\u7528\nint(3.14), float(3)                          # \u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\n<\/code><\/pre>\n<h1>-- \u6574\u6570\u53ef\u4ee5\u5229\u7528bit_length\u51fd\u6570\u6d4b\u8bd5\u6240\u5360\u7684\u4f4d\u6570<\/h1>\n<pre><code>a = 1;       a.bit_length()    # 1\na = 1024;    a.bit_length()    # 11\n<\/code><\/pre>\n<h1>-- repr\u548cstr\u663e\u793a\u683c\u5f0f\u7684\u533a\u522b<\/h1>\n<pre><code>repr\u683c\u5f0f\uff1a\u9ed8\u8ba4\u7684\u4ea4\u4e92\u6a21\u5f0f\u56de\u663e\uff0c\u4ea7\u751f\u7684\u7ed3\u679c\u770b\u8d77\u6765\u5b83\u4eec\u5c31\u50cf\u662f\u4ee3\u7801\u3002\nstr\u683c\u5f0f\uff1a\u6253\u5370\u8bed\u53e5\uff0c\u8f6c\u5316\u6210\u4e00\u79cd\u5bf9\u7528\u6237\u66f4\u52a0\u53cb\u597d\u7684\u683c\u5f0f\u3002<\/code><\/pre>\n<h1>-- \u6570\u5b57\u76f8\u5173\u7684\u6a21\u5757<\/h1>\n<pre><code># math\u6a21\u5757\n# Decimal\u6a21\u5757\uff1a\u5c0f\u6570\u6a21\u5757\n    import decimal\n    from decimal import Decimal\n    Decimal(\"0.01\") + Decimal(\"0.02\")        # \u8fd4\u56deDecimal(\"0.03\")\n    decimal.getcontext().prec = 4            # \u8bbe\u7f6e\u5168\u5c40\u7cbe\u5ea6\u4e3a4 \u5373\u5c0f\u6570\u70b9\u540e\u8fb94\u4f4d\n# Fraction\u6a21\u5757\uff1a\u5206\u6570\u6a21\u5757\n    from fractions import Fraction\n    x = Fraction(4, 6)                       # \u5206\u6570\u7c7b\u578b 4\/6\n    x = Fraction(\"0.25\")                     # \u5206\u6570\u7c7b\u578b 1\/4 \u63a5\u6536\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u53c2\u6570\n<\/code><\/pre>\n<h1>-- \u96c6\u5408set<\/h1>\n<pre><code>set\u662f\u4e00\u4e2a\u65e0\u5e8f\u4e0d\u91cd\u590d\u5143\u7d20\u96c6, \u57fa\u672c\u529f\u80fd\u5305\u62ec\u5173\u7cfb\u6d4b\u8bd5\u548c\u6d88\u9664\u91cd\u590d\u5143\u7d20\u3002\nset\u652f\u6301union(\u8054\u5408), intersection(\u4ea4), difference(\u5dee)\u548csysmmetric difference(\u5bf9\u79f0\u5dee\u96c6)\u7b49\u6570\u5b66\u8fd0\u7b97\u3002\nset\u652f\u6301x in set, len(set), for x in set\u3002\nset\u4e0d\u8bb0\u5f55\u5143\u7d20\u4f4d\u7f6e\u6216\u8005\u63d2\u5165\u70b9, \u56e0\u6b64\u4e0d\u652f\u6301indexing, slicing, \u6216\u5176\u5b83\u7c7b\u5e8f\u5217\u7684\u64cd\u4f5c<\/code><\/pre>\n<pre><code>s = set([3,5,9,10])                          # \u521b\u5efa\u4e00\u4e2a\u6570\u503c\u96c6\u5408\uff0c\u8fd4\u56de{3, 5, 9, 10}\nt = set(\"Hello\")                             # \u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u5b57\u7b26\u7684\u96c6\u5408\u8fd4\u56de{}\na = t | s;    t.union(s)                     # t \u548c s\u7684\u5e76\u96c6\nb = t &amp; s;    t.intersection(s)              # t \u548c s\u7684\u4ea4\u96c6\nc = t \u2013 s;    t.difference(s)                # \u6c42\u5dee\u96c6\uff08\u9879\u5728t\u4e2d, \u4f46\u4e0d\u5728s\u4e2d\uff09\nd = t ^ s;    t.symmetric_difference(s)      # \u5bf9\u79f0\u5dee\u96c6\uff08\u9879\u5728t\u6216s\u4e2d, \u4f46\u4e0d\u4f1a\u540c\u65f6\u51fa\u73b0\u5728\u4e8c\u8005\u4e2d\uff09\nt.add('x');   t.remove('H')                  # \u589e\u52a0\/\u5220\u9664\u4e00\u4e2aitem\nt.update([10,37,42])                         # \u5229\u7528[......]\u66f4\u65b0s\u96c6\u5408\nx in s,  x not in s                          # \u96c6\u5408\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u503c\ns.issubset(t); s.issuperset(t); s.copy(); s.discard(x); s.clear()\n{x**2 for x in [1, 2, 3, 4]}                 # \u96c6\u5408\u89e3\u6790\uff0c\u7ed3\u679c\uff1a{16, 1, 4, 9}\n{x for x in 'spam'}                          # \u96c6\u5408\u89e3\u6790\uff0c\u7ed3\u679c\uff1a{'a', 'p', 's', 'm'}\n<\/code><\/pre>\n<h1>-- \u5e03\u5c14\u7c7b\u578bbool<\/h1>\n<pre><code>type(True)                   # \u8fd4\u56de&lt;class 'bool'&gt;\nisinstance(False, int)       # bool\u7c7b\u578b\u5c5e\u4e8e\u6574\u5f62\uff0c\u6240\u4ee5\u8fd4\u56deTrue\nTrue == 1; True is 1         # \u8f93\u51fa(True, False)\n<\/code><\/pre>\n<h1>-- \u5e38\u89c1\u5b57\u7b26\u4e32\u5e38\u91cf\u548c\u8868\u8fbe\u5f0f<\/h1>\n<pre><code>S = ''                                  # \u7a7a\u5b57\u7b26\u4e32\nS = \"spam\u2019s\"                            # \u53cc\u5f15\u53f7\u548c\u5355\u5f15\u53f7\u76f8\u540c\nS = \"s<\/code><\/pre>\n<p>p    ax00m\"                      # \u8f6c\u4e49\u5b57\u7b26<\/p>\n<pre><code>S = \"\"\"spam\"\"\"                          # \u4e09\u91cd\u5f15\u53f7\u5b57\u7b26\u4e32\uff0c\u4e00\u822c\u7528\u4e8e\u51fd\u6570\u8bf4\u660e\nS = r'    emp'                            # Raw\u5b57\u7b26\u4e32\uff0c\u4e0d\u4f1a\u8fdb\u884c\u8f6c\u4e49\uff0c\u6291\u5236\u8f6c\u4e49\nS = b'Spam'                             # Python3\u4e2d\u7684\u5b57\u8282\u5b57\u7b26\u4e32\nS = u'spam'                             # Python2.6\u4e2d\u7684Unicode\u5b57\u7b26\u4e32\ns1+s2, s1*3, s[i], s[i:j], len(s)       # \u5b57\u7b26\u4e32\u64cd\u4f5c\n'a %s parrot' % 'kind'                  # \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u8868\u8fbe\u5f0f\n'a {0} parrot'.format('kind')           # \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\nfor x in s: print(x)                    # \u5b57\u7b26\u4e32\u8fed\u4ee3\uff0c\u6210\u5458\u5173\u7cfb\n[x*2 for x in s]                        # \u5b57\u7b26\u4e32\u5217\u8868\u89e3\u6790\n','.join(['a', 'b', 'c'])               # \u5b57\u7b26\u4e32\u8f93\u51fa\uff0c\u7ed3\u679c\uff1aa,b,c\n<\/code><\/pre>\n<h1>-- \u5185\u7f6estr\u5904\u7406\u51fd\u6570\uff1a<\/h1>\n<pre><code>str1 = \"stringobject\"\nstr1.upper(); str1.lower(); str1.swapcase(); str1.capitalize(); str1.title()        # \u5168\u90e8\u5927\u5199\uff0c\u5168\u90e8\u5c0f\u5199\u3001\u5927\u5c0f\u5199\u8f6c\u6362\uff0c\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u6bcf\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\u90fd\u5927\u5199\nstr1.ljust(width)                       # \u83b7\u53d6\u56fa\u5b9a\u957f\u5ea6\uff0c\u5de6\u5bf9\u9f50\uff0c\u53f3\u8fb9\u4e0d\u591f\u7528\u7a7a\u683c\u8865\u9f50\nstr1.rjust(width)                       # \u83b7\u53d6\u56fa\u5b9a\u957f\u5ea6\uff0c\u53f3\u5bf9\u9f50\uff0c\u5de6\u8fb9\u4e0d\u591f\u7528\u7a7a\u683c\u8865\u9f50\nstr1.center(width)                      # \u83b7\u53d6\u56fa\u5b9a\u957f\u5ea6\uff0c\u4e2d\u95f4\u5bf9\u9f50\uff0c\u4e24\u8fb9\u4e0d\u591f\u7528\u7a7a\u683c\u8865\u9f50\nstr1.zfill(width)                       # \u83b7\u53d6\u56fa\u5b9a\u957f\u5ea6\uff0c\u53f3\u5bf9\u9f50\uff0c\u5de6\u8fb9\u4e0d\u8db3\u75280\u8865\u9f50\nstr1.find('t',start,end)                # \u67e5\u627e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6307\u5b9a\u8d77\u59cb\u53ca\u7ed3\u675f\u4f4d\u7f6e\u641c\u7d22\nstr1.rfind('t')                         # \u4ece\u53f3\u8fb9\u5f00\u59cb\u67e5\u627e\u5b57\u7b26\u4e32\nstr1.count('t')                         # \u67e5\u627e\u5b57\u7b26\u4e32\u51fa\u73b0\u7684\u6b21\u6570\n#\u4e0a\u9762\u6240\u6709\u65b9\u6cd5\u90fd\u53ef\u7528index\u4ee3\u66ff\uff0c\u4e0d\u540c\u7684\u662f\u4f7f\u7528index\u67e5\u627e\u4e0d\u5230\u4f1a\u629b\u5f02\u5e38\uff0c\u800cfind\u8fd4\u56de-1\nstr1.replace('old','new')               # \u66ff\u6362\u51fd\u6570\uff0c\u66ff\u6362old\u4e3anew\uff0c\u53c2\u6570\u4e2d\u53ef\u4ee5\u6307\u5b9amaxReplaceTimes\uff0c\u5373\u66ff\u6362\u6307\u5b9a\u6b21\u6570\u7684old\u4e3anew\nstr1.strip(); str1.lstrip(); str1.rstrip(); str1.strip('d'); str1.lstrip('d'); str1.rstrip('d')\nstr1.startswith('start')                # \u662f\u5426\u4ee5start\u5f00\u5934\nstr1.endswith('end')                    # \u662f\u5426\u4ee5end\u7ed3\u5c3e\nstr1.isalnum(); str1.isalpha(); str1.isdigit(); str1.islower(); str1.isupper()      # \u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5168\u4e3a\u5b57\u7b26\u3001\u6570\u5b57\u3001\u5927\u5199\u3001\u5c0f\u5199\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e2d\u6587python_base_\u5343\u884c\u4ee3\u7801\u5165\u95e8Python.py \u6e90\u7801 https:\/\/raw.githu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-1046","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1046"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1046\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}