{"id":193,"date":"2025-02-17T13:06:06","date_gmt":"2025-02-17T04:06:06","guid":{"rendered":"https:\/\/pclife-qa.com\/?p=193"},"modified":"2025-02-17T13:06:06","modified_gmt":"2025-02-17T04:06:06","slug":"python%e3%81%ae%e5%9f%ba%e6%9c%ac%e6%a7%8b%e6%96%87","status":"publish","type":"post","link":"https:\/\/pclife-qa.com\/?p=193","title":{"rendered":"python\u306e\u57fa\u672c\u69cb\u6587"},"content":{"rendered":"<p>Python\u306f\u3001\u30b7\u30f3\u30d7\u30eb\u3067\u8aad\u307f\u3084\u3059\u3044\u69cb\u6587\u3092\u6301\u3064\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3059\u3002\u4ee5\u4e0b\u306bPython\u306e\u57fa\u672c\u69cb\u6587\u3092\u7c21\u5358\u306b\u307e\u3068\u3081\u307e\u3057\u305f\u3002<\/p>\n<p>&#8212;<\/p>\n<p>## **1. \u30b3\u30e1\u30f3\u30c8**<br \/>\n&#8211; **\u5358\u4e00\u884c\u30b3\u30e1\u30f3\u30c8**: `#`\u3092\u4f7f\u3046<br \/>\n&#8220;`python<br \/>\n# \u3053\u308c\u306f\u30b3\u30e1\u30f3\u30c8\u3067\u3059<br \/>\nprint(&#8220;Hello, World!&#8221;) # \u3053\u306e\u884c\u306e\u5f8c\u534a\u3082\u30b3\u30e1\u30f3\u30c8<br \/>\n&#8220;`<\/p>\n<p>&#8211; **\u8907\u6570\u884c\u30b3\u30e1\u30f3\u30c8**: `&#8221;&#8217;` \u307e\u305f\u306f `&#8221;&#8221;&#8221;` \u3092\u4f7f\u7528<br \/>\n&#8220;`python<br \/>\n&#8221;&#8217;<br \/>\n\u3053\u306e\u90e8\u5206\u306f\u30b3\u30e1\u30f3\u30c8\u3067\u3059\u3002<br \/>\n\u8907\u6570\u884c\u306b\u308f\u305f\u308a\u307e\u3059\u3002<br \/>\n&#8221;&#8217;<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **2. \u5909\u6570\u306e\u5ba3\u8a00\u3068\u30c7\u30fc\u30bf\u578b**<br \/>\n&#8211; **\u5909\u6570\u306e\u5ba3\u8a00**: \u578b\u3092\u660e\u793a\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\n&#8220;`python<br \/>\nx = 10 # \u6574\u6570\u578b<br \/>\ny = 3.14 # \u6d6e\u52d5\u5c0f\u6570\u70b9\u578b<br \/>\nname = &#8220;Alice&#8221; # \u6587\u5b57\u5217\u578b<br \/>\nis_active = True # \u30d6\u30fc\u30eb\u578b<br \/>\n&#8220;`<\/p>\n<p>&#8211; **\u578b\u306e\u78ba\u8a8d**: `type()`\u3092\u4f7f\u7528<br \/>\n&#8220;`python<br \/>\nprint(type(x)) # &lt;class &#8216;int&#8217;&gt;<br \/>\nprint(type(y)) # &lt;class &#8216;float&#8217;&gt;<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **3. \u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u578b**<br \/>\n| \u30c7\u30fc\u30bf\u578b | \u8aac\u660e | \u4f8b |<br \/>\n|&#8212;&#8212;&#8212;&#8212;-|&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;|&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;|<br \/>\n| `int` | \u6574\u6570\u578b | `10`, `-5` |<br \/>\n| `float` | \u6d6e\u52d5\u5c0f\u6570\u70b9\u578b | `3.14`, `-0.01` |<br \/>\n| `str` | \u6587\u5b57\u5217\u578b | `&#8221;Hello&#8221;`, `&#8217;World&#8217;` |<br \/>\n| `bool` | \u771f\u507d\u5024\uff08\u30d6\u30fc\u30eb\u578b\uff09 | `True`, `False` |<br \/>\n| `list` | \u30ea\u30b9\u30c8\uff08\u914d\u5217\u306e\u3088\u3046\u306a\u3082\u306e\uff09| `[1, 2, 3]` |<br \/>\n| `tuple` | \u30bf\u30d7\u30eb\uff08\u5909\u66f4\u4e0d\u53ef\u306a\u30ea\u30b9\u30c8\uff09| `(1, 2, 3)` |<br \/>\n| `dict` | \u8f9e\u66f8\uff08\u30ad\u30fc\u3068\u5024\u306e\u30da\u30a2\uff09 | `{&#8220;key&#8221;: &#8220;value&#8221;}` |<br \/>\n| `set` | \u96c6\u5408\uff08\u91cd\u8907\u306a\u3057\uff09 | `{1, 2, 3}` |<\/p>\n<p>&#8212;<\/p>\n<p>## **4. \u6761\u4ef6\u5206\u5c90**<br \/>\n&#8211; **if\u6587**<br \/>\n&#8220;`python<br \/>\nx = 10<br \/>\nif x &gt; 5:<br \/>\nprint(&#8220;x\u306f5\u3088\u308a\u5927\u304d\u3044&#8221;)<br \/>\nelif x == 5:<br \/>\nprint(&#8220;x\u306f5\u3068\u7b49\u3057\u3044&#8221;)<br \/>\nelse:<br \/>\nprint(&#8220;x\u306f5\u3088\u308a\u5c0f\u3055\u3044&#8221;)<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **5. \u30eb\u30fc\u30d7**<br \/>\n&#8211; **for\u30eb\u30fc\u30d7**<br \/>\n&#8220;`python<br \/>\nfor i in range(5): # 0\u304b\u30894\u307e\u3067\u7e70\u308a\u8fd4\u3059<br \/>\nprint(i)<br \/>\n&#8220;`<\/p>\n<p>&#8211; **while\u30eb\u30fc\u30d7**<br \/>\n&#8220;`python<br \/>\ncount = 0<br \/>\nwhile count &lt; 5:<br \/>\nprint(count)<br \/>\ncount += 1<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **6. \u95a2\u6570**<br \/>\n&#8211; **\u95a2\u6570\u306e\u5b9a\u7fa9**<br \/>\n&#8220;`python<br \/>\ndef greet(name):<br \/>\nreturn f&#8221;Hello, {name}!&#8221;<\/p>\n<p>print(greet(&#8220;Alice&#8221;)) # Hello, Alice!<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **7. \u30af\u30e9\u30b9\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8**<br \/>\n&#8211; **\u30af\u30e9\u30b9\u306e\u5b9a\u7fa9**<br \/>\n&#8220;`python<br \/>\nclass Person:<br \/>\ndef __init__(self, name, age):<br \/>\nself.name = name<br \/>\nself.age = age<\/p>\n<p>def greet(self):<br \/>\nreturn f&#8221;My name is {self.name} and I am {self.age} years old.&#8221;<\/p>\n<p>person = Person(&#8220;Alice&#8221;, 25)<br \/>\nprint(person.greet()) # My name is Alice and I am 25 years old.<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **8. \u30ea\u30b9\u30c8\u64cd\u4f5c**<br \/>\n&#8211; **\u30ea\u30b9\u30c8\u306e\u4f5c\u6210\u3068\u64cd\u4f5c**<br \/>\n&#8220;`python<br \/>\nfruits = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;]\nfruits.append(&#8220;orange&#8221;) # \u8981\u7d20\u3092\u8ffd\u52a0<br \/>\nfruits.remove(&#8220;banana&#8221;) # \u8981\u7d20\u3092\u524a\u9664<br \/>\nprint(fruits) # [&#8216;apple&#8217;, &#8216;cherry&#8217;, &#8216;orange&#8217;]\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **9. \u8f9e\u66f8\u64cd\u4f5c**<br \/>\n&#8211; **\u8f9e\u66f8\u306e\u4f5c\u6210\u3068\u64cd\u4f5c**<br \/>\n&#8220;`python<br \/>\nperson = {&#8220;name&#8221;: &#8220;Alice&#8221;, &#8220;age&#8221;: 25}<br \/>\nprint(person[&#8220;name&#8221;]) # Alice<br \/>\nperson[&#8220;age&#8221;] = 26 # \u5024\u3092\u66f4\u65b0<br \/>\nprint(person) # {&#8216;name&#8217;: &#8216;Alice&#8217;, &#8216;age&#8217;: 26}<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **10. \u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c**<br \/>\n&#8211; **\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u66f8\u304d**<br \/>\n&#8220;`python<br \/>\n# \u66f8\u304d\u8fbc\u307f<br \/>\nwith open(&#8220;example.txt&#8221;, &#8220;w&#8221;) as file:<br \/>\nfile.write(&#8220;Hello, World!&#8221;)<\/p>\n<p># \u8aad\u307f\u8fbc\u307f<br \/>\nwith open(&#8220;example.txt&#8221;, &#8220;r&#8221;) as file:<br \/>\ncontent = file.read()<br \/>\nprint(content) # Hello, World!<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>## **11. \u4f8b\u5916\u51e6\u7406**<br \/>\n&#8211; **try-except\u6587**<br \/>\n&#8220;`python<br \/>\ntry:<br \/>\nx = 10 \/ 0<br \/>\nexcept ZeroDivisionError:<br \/>\nprint(&#8220;\u30bc\u30ed\u3067\u5272\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093&#8221;)<br \/>\nfinally:<br \/>\nprint(&#8220;\u51e6\u7406\u7d42\u4e86&#8221;)<br \/>\n&#8220;`<\/p>\n<p>&#8212;<\/p>\n<p>\u3053\u308c\u3089\u306fPython\u306e\u57fa\u672c\u7684\u306a\u69cb\u6587\u306e\u4e00\u90e8\u3067\u3059\u3002\u3055\u3089\u306b\u8a73\u3057\u3044\u5185\u5bb9\u3084\u5177\u4f53\u4f8b\u304c\u5fc5\u8981\u3067\u3042\u308c\u3070\u3001\u6c17\u8efd\u306b\u8cea\u554f\u3057\u3066\u304f\u3060\u3055\u3044\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306f\u3001\u30b7\u30f3\u30d7\u30eb\u3067\u8aad\u307f\u3084\u3059\u3044\u69cb\u6587\u3092\u6301\u3064\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3059\u3002\u4ee5\u4e0b\u306bPython\u306e\u57fa\u672c\u69cb\u6587\u3092\u7c21\u5358\u306b\u307e\u3068\u3081\u307e\u3057\u305f\u3002 &#8212; ## **1. \u30b3\u30e1\u30f3\u30c8** &#8211; **\u5358\u4e00\u884c\u30b3\u30e1\u30f3\u30c8**: `#`\u3092\u4f7f\u3046 &#8220;`python # \u3053\u308c\u306f\u30b3\u30e1\u30f3\u30c8\u3067\u3059 print(&#8220;Hello, World!&#8221;) # \u3053\u306e\u884c\u306e\u5f8c\u534a\u3082\u30b3\u30e1\u30f3\u30c8 &#8220;` &#038;#8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":131,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/pclife-qa.com\/index.php?rest_route=\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pclife-qa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pclife-qa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pclife-qa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pclife-qa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=193"}],"version-history":[{"count":1,"href":"https:\/\/pclife-qa.com\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/pclife-qa.com\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions\/194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pclife-qa.com\/index.php?rest_route=\/wp\/v2\/media\/131"}],"wp:attachment":[{"href":"https:\/\/pclife-qa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pclife-qa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pclife-qa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}