{"id":4425,"date":"2012-12-30T15:36:21","date_gmt":"2012-12-30T07:36:21","guid":{"rendered":"http:\/\/jpuyy.com\/?p=4425"},"modified":"2014-09-13T17:13:07","modified_gmt":"2014-09-13T09:13:07","slug":"python-simple-programs-a","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=4425","title":{"rendered":"python\u7684\u5165\u95e8\u7684\u51e0\u4e2a\u4f8b\u5b50(\u4e0a)"},"content":{"rendered":"<p>\u5728Python for Unix and Linux System Administration\u5217\u51fa\u4e86\u51e0\u4e2a\u9009\u62e9python\u7684\u7406\u7531\uff1a<\/p>\n<div>\n<p>\u6613\u5165\u95e8<\/p>\n<div>\u4e0a\u624b\u540e\u53ef\u8fdb\u884c\u7cfb\u7edf\u7ba1\u7406<\/div>\n<div>\u80fd\u89e3\u51b3\u590d\u6742\u95ee\u9898<\/div>\n<div>\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u8bfb<\/div>\n<div>\u5173\u952e\u5b57\u4f7f\u964c\u751f\u4ee3\u7801\u6613\u8bfb<\/div>\n<div>\u9762\u5411\u5bf9\u8c61<\/div>\n<div>\u793e\u533a\u4f18\u79c0<\/div>\n<div>\u5f88\u597d\u7684\u6807\u51c6\u5e93<\/div>\n<\/div>\n<p>python\u7684\u57fa\u7840\u4f8b\u5b50\u5728\u8fd9\u91cc\uff0c\u5f88\u6709\u610f\u601d\uff0c\u4e0b\u4e00\u4e2a\u4f8b\u5b50\u7684\u884c\u6570\u662f\u9012\u589e\u7684<\/p>\n<p>http:\/\/wiki.python.org\/moin\/SimplePrograms<\/p>\n<p>\u4f8b1.\u8f93\u51fa\u663e\u793a&#8211;Output<\/p>\n<pre>print 'Hello world'<\/pre>\n<p>\u4f8b2.\u53d8\u91cf\u4ee5\u63d0\u793a\u8f93\u5165\u7684\u65b9\u5f0f\u8d4b\u503c\uff0c\u5e76\u6253\u5370\u51fa\u6765&#8211;Input, assignment<\/p>\n<pre>name = raw_input('What is your name?\\n')\r\nprint 'Hi, %s.' % name<\/pre>\n<p>\u4f8b3.for\u5faa\u73af\uff0c\u5185\u7f6e\u7684\u5217\u4e3e\u51fd\u6570\uff0cformat\u683c\u5f0f&#8211;For loop, built-in enumerate function, new style formatting<\/p>\n<pre>friends = ['jhon', 'pat', 'gary', 'micheal']\r\nfor i, name in enumerate(friends):\r\n\u00a0 \u00a0 print \"iteration {iteration} is {name}\".format(iteration=i, name=name)<\/pre>\n<p>\u4f8b4.\u6590\u6ce2\u7eb3\u5951\uff0c\u5143\u7ec4\u7684\u8d4b\u503c&#8211;Fibonacci, tuple assignment<\/p>\n<pre>parents, babies = (1, 1)\r\nwhile babies &lt; 100:\r\n\u00a0 \u00a0 print 'This generation has {0} babies'.format(babies)\r\n\u00a0 \u00a0 parents, babies = (babies, parents + babies)<\/pre>\n<p>\u4f8b5.\u51fd\u6570&#8211;Functions<\/p>\n<pre>def greet(name):\r\n    print 'hello', name\r\ngreet('Jack')\r\ngreet('Jill')\r\ngreet('Bob')<\/pre>\n<p>\u4f8b6.import\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f&#8211;Import, regular expressions\u3002\u8fd9\u91cc\u662f\u5339\u914d3\u4f4d\u6570\u5b57-4\u4f4d\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre>import re\r\nfor test_string in [ '555-1212', 'ILL-EGAL' ]:\r\n    if re.match(r'^\\d{3}-\\d{4}', test_string):\r\n        print test_string, 'is a valid US local phone number'\r\n    else:\r\n        print test_string, 'rejected'<\/pre>\n<p>\u4f8b7.\u5b57\u5178\uff0c\u751f\u6210\u8868\u8fbe\u5f0f&#8211;Dictionaries, generator expressions<\/p>\n<pre>prices = { 'apple': 0.40, 'banana': 0.50 }\r\nmy_purchase = {\r\n\u00a0 \u00a0 'apple': 1,\r\n\u00a0 \u00a0 'banana':6}\r\ngrocery_bill = sum(prices[fruit] * my_purchase[fruit]\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0for fruit in my_purchase)\r\nprint 'I owe the grocer $%.2f' % grocery_bill<\/pre>\n<p>\u4f8b8.\u8c03\u7528\u547d\u4ee4\u53c2\u6570\uff0c\u5f02\u5e38\u5904\u7406&#8211;Command line arguments, exception handling<\/p>\n<pre>#!\/usr\/bin\/env python\r\n# This program adds up integers in the command line\r\nimport sys\r\ntry:\r\n    total = sum(int(arg) for arg in sys.argv[1:])\r\n    print 'sum =', total\r\nexcept ValueError:\r\n    print 'Please supply integer arguments'<\/pre>\n<p>\u4e0a\u9762\u662f\u5c06\u8ddf\u7684\u53c2\u6570\u76f8\u52a0\uff0c\u5982\u679c\u4e0d\u662f\u6574\u6570\uff0c\u5219\u63d0\u793a\u9700\u8f93\u5165\u6574\u6570<\/p>\n<pre>$ python se8.py 1 2 3 4 5 6\r\nsum = 21\r\n$ python se8.py 1 2 3 4 5 a\r\nPlease supply integer arguments<\/pre>\n<p>\u4f8b9.\u6253\u5f00\u6587\u4ef6&#8211;Opening files<\/p>\n<pre># indent your Python code to put into an email\r\nimport glob\r\n# glob supports Unix style pathname extensions\r\npython_files = glob.glob('*.py')\r\nfor file_name in sorted(python_files):\r\nprint ' ------' + file_name\r\n\r\nwith open(file_name) as f:\r\nfor line in f:\r\nprint ' ' + line.rstrip()\r\n\r\nprint<\/pre>\n<p>\u4f8b10.\u65f6\u95f4\uff0c\u6761\u4ef6\uff0cfrom..import,for ..else\u5faa\u73af &#8212;\u00a0Time, conditionals, from..import, for..else<\/p>\n<div>\n<div dir=\"ltr\" lang=\"en\">\n<pre id=\"CA-a9fda34466f574c62437396976e4f2d8bc3dc9db\" dir=\"ltr\" lang=\"en\">from time import localtime\r\n\r\nactivities = {8: 'Sleeping',\r\n              9: 'Commuting',\r\n              17: 'Working',\r\n              18: 'Commuting',\r\n              20: 'Eating',\r\n              22: 'Resting' }\r\n\r\ntime_now = localtime()\r\nhour = time_now.tm_hour\r\n\r\nfor activity_time in sorted(activities.keys()):\r\n    if hour &lt; activity_time:\r\n        print activities[activity_time]\r\n        break\r\nelse:\r\n    print 'Unknown, AFK or sleeping!'<\/pre>\n<p>\u6574\u7406\u4e00\u4e9b\u5c0f\u77e5\u8bc6<\/p>\n<p>\u6bb5\u843d\u6ce8\u91ca\u7684\u65b9\u6cd5<\/p>\n<pre>\"\"\" \"\"\"<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python for Unix and Linux System Administration\u5217\u51fa\u4e86\u51e0\u4e2a\u9009\u62e9python\u7684\u7406\u7531\uff1a \u6613\u5165\u95e8 \u4e0a\u624b\u540e\u53ef\u8fdb\u884c\u7cfb\u7edf\u7ba1\u7406 \u80fd\u89e3\u51b3\u590d\u6742\u95ee\u9898 \u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u8bfb \u5173\u952e\u5b57\u4f7f\u964c\u751f\u4ee3\u7801\u6613\u8bfb \u9762\u5411\u5bf9\u8c61 \u793e\u533a\u4f18\u79c0 \u5f88\u597d\u7684\u6807\u51c6\u5e93 python\u7684\u57fa\u7840\u4f8b\u5b50\u5728\u8fd9\u91cc\uff0c\u5f88\u6709\u610f\u601d\uff0c\u4e0b\u4e00\u4e2a\u4f8b\u5b50\u7684\u884c\u6570\u662f\u9012\u589e\u7684 http:\/\/wiki.python.org\/moin\/SimplePrograms \u4f8b1.\u8f93\u51fa\u663e\u793a&#8211;Output print &#8216;Hello world&#8217; \u4f8b2.\u53d8\u91cf\u4ee5\u63d0\u793a\u8f93\u5165\u7684\u65b9\u5f0f\u8d4b\u503c\uff0c\u5e76\u6253\u5370\u51fa\u6765&#8211;Input, assignment name = raw_input(&#8216;What is your name?\\n&#8217;) print &#8216;Hi, %s.&#8217; % name \u4f8b3.for\u5faa\u73af\uff0c\u5185\u7f6e\u7684\u5217\u4e3e\u51fd\u6570\uff0cformat\u683c\u5f0f&#8211;For loop, built-in enumerate function, new style formatting friends = [&#8216;jhon&#8217;, &#8216;pat&#8217;, &#8216;gary&#8217;, &#8216;micheal&#8217;] for i, name in enumerate(friends): \u00a0 \u00a0 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[23],"class_list":["post-4425","post","type-post","status-publish","format-standard","hentry","category-python","tag-summary"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/4425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4425"}],"version-history":[{"count":28,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/4425\/revisions"}],"predecessor-version":[{"id":6680,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/4425\/revisions\/6680"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}