{"id":383391,"date":"2021-03-11T05:23:47","date_gmt":"2021-03-10T21:23:47","guid":{"rendered":"http:\/\/4563.org\/?p=383391"},"modified":"2021-03-11T05:23:47","modified_gmt":"2021-03-10T21:23:47","slug":"%e7%bf%bb%e8%af%91%ef%bc%9a%e3%80%8a%e5%ae%9e%e7%94%a8%e7%9a%84-python-%e7%bc%96%e7%a8%8b%e3%80%8b03_01_script","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=383391","title":{"rendered":"\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b03_01_Script"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b03_01_Script               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codists <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>3.1 \u811a\u672c<\/h1>\n<p> <\/p>\n<p>\u5728\u8be5\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u7814\u7a76\u7f16\u5199 Python \u811a\u672c\u7684\u60ef\u4f8b\u3002<\/p>\n<h3>\u4ec0\u4e48\u662f\u811a\u672c\uff1f<\/h3>\n<p>\u811a\u672c\u5c31\u662f\u8fd0\u884c\u548c\u7ec8\u6b62\u4e00\u7cfb\u5217\u8bed\u53e5\u7684\u7a0b\u5e8f\u3002<\/p>\n<pre><code># program.py  statement1 statement2 statement3 ... <\/code><\/pre>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u4e3b\u8981\u5728\u7f16\u5199\u811a\u672c\u3002<\/p>\n<h3>\u95ee\u9898<\/h3>\n<p>\u5982\u679c\u4f60\u7f16\u5199\u4e00\u4e2a\u6709\u7528\u7684\u811a\u672c\uff0c\u5b83\u7684\u7279\u6027\u548c\u529f\u80fd\u5c06\u4f1a\u589e\u52a0\u3002\u4f60\u53ef\u80fd\u60f3\u8981\u5c06\u5176\u5e94\u7528\u4e8e\u76f8\u5173\u7684\u95ee\u9898\u3002\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u5b83\u53ef\u80fd\u4f1a\u6210\u4e3a\u4e00\u4e2a\u5173\u952e\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5982\u679c\u4f60\u4e0d\u6ce8\u610f\u7684\u8bdd\uff0c\u5b83\u53ef\u80fd\u4f1a\u53d8\u6210\u4e00\u56e2\u4e71\u9ebb\u3002\u56e0\u6b64\uff0c\u8ba9\u6211\u4eec\u6709\u6761\u7406\u7684\u7ec4\u7ec7\u7a0b\u5e8f\u5427\u3002<\/p>\n<h3>\u5b9a\u4e49\u53d8\u91cf<\/h3>\n<p>\u540d\u79f0\u5fc5\u987b\u5728\u4f7f\u7528\u4e4b\u524d\u5b9a\u4e49\u3002<\/p>\n<pre><code>def square(x):     return x*x  a = 42 b = a + 2     # Requires that `a` is defined  z = square(b) # Requires `square` and `b` to be defined <\/code><\/pre>\n<p><strong>\u987a\u5e8f\u5f88\u91cd\u8981\u3002<\/strong><\/p>\n<p>\u51e0\u4e4e\u603b\u662f\u628a\u53d8\u91cf\u548c\u51fd\u6570\u7684\u5b9a\u4e49\u653e\u5230\u9876\u90e8\u9644\u8fd1\u3002<\/p>\n<h3>\u5b9a\u4e49\u51fd\u6570<\/h3>\n<p>\u628a\u6240\u6709\u4e0e\u5355\u4e2a<em>\u4efb\u52a1<\/em>\u76f8\u5173\u7684\u4ee3\u7801\u90fd\u653e\u5230\u4e00\u4e2a\u5730\u65b9\u662f\u4e2a\u597d\u4e3b\u610f\u3002\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u5b9e\u73b0\uff1a<\/p>\n<pre><code>def read_prices(filename):     prices = {}     with open(filename) as f:         f_csv = csv.reader(f)         for row in f_csv:             prices[row[0]] = float(row[1])     return prices <\/code><\/pre>\n<p>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7b80\u5316\u91cd\u590d\u7684\u64cd\u4f5c\u3002<\/p>\n<pre><code>oldprices = read_prices('oldprices.csv') newprices = read_prices('newprices.csv') <\/code><\/pre>\n<h3>\u4ec0\u4e48\u662f\u51fd\u6570\uff1f<\/h3>\n<p>\u51fd\u6570\u662f\u547d\u540d\u7684\u8bed\u53e5\u5e8f\u5217\u3002<\/p>\n<pre><code>def funcname(args):   statement   statement   ...   return result <\/code><\/pre>\n<p><em>\u4efb\u4f55<\/em> Python \u8bed\u53e5\u90fd\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002<\/p>\n<pre><code>def foo():     import math     print(math.sqrt(2))     help(math) <\/code><\/pre>\n<p>Python \u4e2d\u6ca1\u6709<em>\u7279\u6b8a<\/em>\u8bed\u53e5\uff08\u8fd9\u4f7f\u5b83\u5f88\u5bb9\u6613\u8bb0\u4f4f\uff09\u3002<\/p>\n<h3>\u51fd\u6570\u5b9a\u4e49<\/h3>\n<p>\u53ef\u4ee5\u6309\u4efb\u4f55\u987a\u5e8f<em>\u5b9a\u4e49<\/em>\u51fd\u6570\u3002<\/p>\n<pre><code>def foo(x):     bar(x)  def bar(x):     statements  # OR def bar(x):     statements  def foo(x):     bar(x) <\/code><\/pre>\n<p>\u5728\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\uff0c\u51fd\u6570\u5fc5\u987b\u5728\u5b9e\u9645\u4f7f\u7528\u4e4b\u524d\uff08\u8c03\u7528\uff09\u5b9a\u4e49\u3002<\/p>\n<pre><code>foo(3)        # foo must be defined already <\/code><\/pre>\n<p>\u5728\u6587\u4f53\u4e0a\uff0c\u51fd\u6570\u4ee5\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u5f0f\u5b9a\u4e49\u53ef\u80fd\u66f4\u5e38\u89c1\u3002<\/p>\n<h3>\u81ea\u5e95\u5411\u4e0a\u7684\u98ce\u683c<\/h3>\n<p>\u51fd\u6570\u88ab\u5f53\u505a\u6784\u5efa\u5757\u3002\u8f83\u5c0f \/\u8f83\u7b80\u5355\u7684\u5757\u4f18\u5148\u3002<\/p>\n<pre><code># myprogram.py def foo(x):     ...  def bar(x):     ...     foo(x)          # Defined above     ...  def spam(x):     ...     bar(x)          # Defined above     ...  spam(42)            # Code that uses the functions appears at the end <\/code><\/pre>\n<p>\u540e\u9762\u7684\u51fd\u6570\u57fa\u4e8e\u524d\u9762\u7684\u51fd\u6570\u6784\u5efa\u3002\u518d\u6b21\u8bf4\u660e\uff0c\u8fd9\u4ec5\u4ec5\u662f\u4e00\u79cd\u98ce\u683c\u95ee\u9898\u3002\u5728\u4e0a\u9762\u7a0b\u5e8f\u4e2d\u552f\u4e00\u91cd\u8981\u7684\u4e8b\u60c5\u662f <code>spam(42)<\/code> \u7684\u8c03\u7528\u662f\u5728\u6700\u540e\u4e00\u6b65\u3002<\/p>\n<h3>\u51fd\u6570\u8bbe\u8ba1<\/h3>\n<p>\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u5e94\u8be5\u662f\u4e00\u4e2a<em>\u9ed1\u76d2<\/em>\u3002\u5b83\u4eec\u5e94\u8be5\u4ec5\u5bf9\u8f93\u5165\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e76\u907f\u514d\u5168\u5c40\u53d8\u91cf\u548c\u5947\u602a\u7684\u526f\u4f5c\u7528\u3002\u9996\u8981\u76ee\u6807\uff1a\u6a21\u5757\u5316\u548c\u53ef\u9884\u6d4b\u6027\u3002<\/p>\n<h3>\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<p>\u4ee5\u6587\u6863\u5b57\u7b26\u4e32\uff08 doc-string \uff09\u7684\u5f62\u5f0f\u5305\u542b\u6587\u6863\u662f\u826f\u597d\u7684\u5b9e\u8df5\u3002\u6587\u6863\u5b57\u7b26\u4e32\u662f\u7d27\u63a5\u7740\u51fd\u6570\u540d\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u4eec\u7528\u4e8e <code>help()<\/code> \u51fd\u6570\uff0c\u96c6\u6210\u5f00\u53d1\u73af\u5883\u548c\u5176\u5b83\u7684\u5de5\u5177\u3002<\/p>\n<pre><code>def read_prices(filename):     '''     Read prices from a CSV file of name,price data     '''     prices = {}     with open(filename) as f:         f_csv = csv.reader(f)         for row in f_csv:             prices[row[0]] = float(row[1])     return prices <\/code><\/pre>\n<p>\u4e00\u4e2a\u597d\u7684\u6587\u6863\u5b57\u7b26\u4e32\u5b9e\u8df5\u662f\u5199\u4e00\u53e5\u7b80\u77ed\u7684\u8bdd\u603b\u7ed3\u8be5\u51fd\u6570\u505a\u4ec0\u4e48\u3002\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u4fe1\u606f\uff0c\u8bf7\u5305\u542b\u4e00\u4e2a\u7b80\u77ed\u7684\u5e26\u6709\u66f4\u8be6\u7ec6\u7684\u53c2\u6570\u8bf4\u660e\u7684\u4f7f\u7528\u793a\u4f8b\uff0c<\/p>\n<h3>\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<p>\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u53ef\u9009\u7684\u7c7b\u578b\u63d0\u793a\u5230\u51fd\u6570\u5b9a\u4e49\u4e2d\u3002<\/p>\n<pre><code>def read_prices(filename: str) -&gt; dict:     '''     Read prices from a CSV file of name,price data     '''     prices = {}     with open(filename) as f:         f_csv = csv.reader(f)         for row in f_csv:             prices[row[0]] = float(row[1])     return prices <\/code><\/pre>\n<p>\u63d0\u793a\u5728\u64cd\u4f5c\u4e0a\u4ec0\u4e48\u4e5f\u4e0d\u505a\u3002\u5b83\u4eec\u7eaf\u7cb9\u662f\u4fe1\u606f\u6027\u7684\u3002\u4f46\u662f\uff0c\u96c6\u6210\u5f00\u53d1\u5de5\u5177\uff0c\u4ee3\u7801\u68c0\u67e5\u5668\uff0c\u4ee5\u53ca\u5176\u5b83\u5de5\u5177\u53ef\u80fd\u4f1a\u4f7f\u7528\u5b83\u6765\u6267\u884c\u66f4\u591a\u7684\u64cd\u4f5c\u3002<\/p>\n<h2>\u7ec3\u4e60<\/h2>\n<p>\u5728\u7b2c 2 \u8282\u4e2d\uff0c\u7f16\u5199\u4e86\u4e00\u4e2a\u540d\u4e3a <code>report.py <\/code>\u7684\u7a0b\u5e8f\uff0c\u8be5\u7a0b\u5e8f\u53ef\u4ee5\u6253\u5370\u51fa\u663e\u793a\u80a1\u7968\u6295\u8d44\u7ec4\u5408\u7ee9\u6548\u7684\u62a5\u544a\u3002\u6b64\u7a0b\u5e8f\u5305\u542b\u4e00\u4e9b\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code># report.py import csv  def read_portfolio(filename):     '''     Read a stock portfolio file into a list of dictionaries with keys     name, shares, and price.     '''     portfolio = []     with open(filename) as f:         rows = csv.reader(f)         headers = next(rows)          for row in rows:             record = dict(zip(headers, row))             stock = {                 'name' : record['name'],                 'shares' : int(record['shares']),                 'price' : float(record['price'])             }             portfolio.append(stock)     return portfolio ... <\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u7a0b\u5e8f\u7684\u6709\u4e9b\u90e8\u5206\u4ec5\u6267\u884c\u4e00\u7cfb\u5217\u7684\u811a\u672c\u8ba1\u7b97\u3002\u8fd9\u4e9b\u4ee3\u7801\u51fa\u73b0\u5728\u7a0b\u5e8f\u7ed3\u5c3e\u5904\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>...  # Output the report  headers = ('Name', 'Shares', 'Price', 'Change') print('%10s %10s %10s %10s'  % headers) print(('-' * 10 + ' ') * len(headers)) for row in report:     print('%10s %10d %10.2f %10.2f' % row) ... <\/code><\/pre>\n<p>\u5728\u672c\u7ec3\u4e60\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u51fd\u6570\u6765\u5bf9\u8be5\u7a0b\u5e8f\u8fdb\u884c\u6709\u6761\u7406\u7684\u7ec4\u7ec7\uff0c\u4f7f\u7a0b\u5e8f\u66f4\u5065\u58ee\u3002<\/p>\n<h3>\u7ec3\u4e60 3.1\uff1a\u5c06\u7a0b\u5e8f\u6784\u9020\u4e3a\u51fd\u6570\u7684\u96c6\u5408<\/h3>\n<p>\u8bf7\u4fee\u6539 <code>report.py<\/code> \u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u6240\u6709\u4e3b\u8981\u64cd\u4f5c\uff08\u5305\u62ec\u8ba1\u7b97\u548c\u8f93\u51fa\uff09\u90fd\u7531\u4e00\u7ec4\u51fd\u6570\u6267\u884c\u3002\u7279\u522b\u5730\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u6253\u5370\u62a5\u544a\u7684\u51fd\u6570 <code>print_report(report)<\/code>\u3002<\/li>\n<li>\u4fee\u6539\u7a0b\u5e8f\u7684\u6700\u540e\u4e00\u90e8\u5206\uff0c\u4f7f\u5176\u4ec5\u662f\u4e00\u7cfb\u5217\u51fd\u6570\u8c03\u7528\uff0c\u800c\u65e0\u9700\u8fdb\u884c\u5176\u5b83\u8fd0\u7b97\u3002<\/li>\n<\/ul>\n<h3>\u7ec3\u4e60 3.2\uff1a\u4e3a\u7a0b\u5e8f\u6267\u884c\u521b\u5efa\u4e00\u4e2a\u9876\u5c42\u51fd\u6570<\/h3>\n<p>\u628a\u7a0b\u5e8f\u7684\u6700\u540e\u4e00\u90e8\u5206\u6253\u5305\u5230\u5355\u4e2a\u51fd\u6570 <code>portfolio_report(portfolio_filename, prices_filename)<\/code> \u4e2d\u3002\u8ba9\u7a0b\u5e8f\u8fd0\u884c\uff0c\u4ee5\u4fbf\u4e0b\u9762\u7684\u51fd\u6570\u8c03\u7528\u50cf\u4e4b\u524d\u4e00\u6837\u521b\u5efa\u62a5\u544a\u3002<\/p>\n<pre><code>portfolio_report('Data\/portfolio.csv', 'Data\/prices.csv') <\/code><\/pre>\n<p>\u5728\u6700\u7ec8\u7248\u672c\u4e2d\uff0c\u7a0b\u5e8f\u53ea\u4e0d\u8fc7\u662f\u4e00\u7cfb\u5217\u51fd\u6570\u5b9a\u4e49\uff0c\u6700\u540e\u662f\u5bf9\u5355\u4e2a\u51fd\u6570<code>portfolio_report()<\/code> \u7684\u8c03\u7528\uff08\u5b83\u6267\u884c\u7a0b\u5e8f\u4e2d\u6d89\u53ca\u7684\u6240\u6709\u6b65\u9aa4\uff09\u3002<\/p>\n<p>\u901a\u8fc7\u5c06\u7a0b\u5e8f\u8f6c\u6362\u4e3a\u5355\u4e2a\u51fd\u6570\uff0c\u5728\u4e0d\u540c\u7684\u8f93\u5165\u540e\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u8fd0\u884c\u5b83\u3002\u4f8b\u5982\uff0c\u5728\u8fd0\u884c\u7a0b\u5e8f\u540e\u4ee5\u4ea4\u4e92\u65b9\u5f0f\u5c1d\u8bd5\u8fd9\u4e9b\u8bed\u53e5\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; portfolio_report('Data\/portfolio2.csv', 'Data\/prices.csv') ... look at the output ... &gt;&gt;&gt; files = ['Data\/portfolio.csv', 'Data\/portfolio2.csv'] &gt;&gt;&gt; for name in files:         print(f'{name:-^43s}')         portfolio_report(name, 'Data\/prices.csv')         print()  ... look at the output ... &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u8bf4\u660e<\/h3>\n<p>Python \u4f7f\u5728\u6709\u4e00\u7cfb\u5217\u8bed\u53e5\u7684\u6587\u4ef6\u4e2d\u7f16\u5199\u76f8\u5bf9\u65e0\u7ed3\u6784\u7684\u811a\u672c\u53d8\u5f97\u5f88\u8f7b\u677e\u3002\u603b\u4f53\u6765\u8bf4\uff0c\u65e0\u8bba\u4f55\u65f6\uff0c\u5c3d\u53ef\u80fd\u5730\u5229\u7528\u51fd\u6570\u901a\u5e38\u603b\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u5728\u67d0\u4e9b\u65f6\u5019\uff0c\u811a\u672c\u4f1a\u4e0d\u65ad\u589e\u52a0\uff0c\u5e76\u4e14\u6211\u4eec\u5e0c\u671b\u5b83\u66f4\u6709\u7ec4\u7ec7\u3002\u53e6\u5916\uff0c\u4e00\u4e2a\u9c9c\u4e3a\u4eba\u77e5\u7684\u4e8b\u5b9e\u662f\uff0c\u5982\u679c\u4f7f\u7528\u51fd\u6570\uff0cPython \u7684\u8fd0\u884c\u4f1a\u66f4\u5feb\u4e00\u4e9b\u3002<\/p>\n<p>\u6ce8\uff1a\u5b8c\u6574\u7ffb\u8bd1\u89c1 https:\/\/github.com\/codists\/practical-python-zh<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/383391"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=383391"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/383391\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=383391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=383391"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=383391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}