{"id":31707,"date":"2024-11-01T09:01:57","date_gmt":"2024-11-01T09:01:57","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31707"},"modified":"2024-11-01T11:48:35","modified_gmt":"2024-11-01T11:48:35","slug":"04-4-input-and-output-in-python-input-and-output-of-programs","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31707\/","title":{"rendered":"04-4 Input and Output in Python, Input and Output of Programs"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n<p>In this course, we will take a deep dive into input and output methods using Python. Input and output are fundamental elements of programming languages, providing the functionality to read and write data. In Python, various methods can be used for input and output, each with its unique use cases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Standard Input\/Output<\/h2>\n\n\n\n<p>Python outputs data to the standard output (console) through the <code>print()<\/code> function. This function is a basic way to display strings on the screen without additional specifications. The <code>print()<\/code> function can also accept one or more pieces of data separated by commas and allows for various formats to be specified, including newline characters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Hello, Python!\")  # Basic output\nprint(\"Name:\", \"Hong Gil Dong\", \"Age:\", 25)  # Output multiple items<\/code><\/pre>\n\n\n\n<p>Conversely, to receive standard input, the <code>input()<\/code> function is used. It accepts user input as a string, enabling the design of dynamic programs. Data obtained using the <code>input()<\/code> function is always stored as a string, so it is essential to convert it into the appropriate data type as needed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"Please enter your name: \")  # User input\nprint(\"Hello,\", name)  # Output the received data<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">File I\/O<\/h2>\n\n\n\n<p>File I\/O refers to the method of storing data in files or reading data from files. In Python, the <code>open()<\/code> function is used to open files, and data can be read and written using the methods of the file object. Files are typically opened in text mode but can also be opened in binary mode. File modes can be specified as read (&#8216;r&#8217;), write (&#8216;w&#8217;), append (&#8216;a&#8217;), etc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Writing data to a file\nwith open(\"example.txt\", \"w\") as file:\n    file.write(\"Hello, world!\")  # Write a string to the file<\/code><\/pre>\n\n\n\n<p>In the above example, the <code>with<\/code> statement is used to open the file. The <code>with<\/code> statement ensures that the file is opened, operations are performed, and the file is automatically closed afterward.<\/p>\n\n\n\n<p>Here\u2019s how to read data from a file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Reading data from a file\nwith open(\"example.txt\", \"r\") as file:\n    content = file.read()  # Read all content of the file\n    print(content)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding File Modes<\/h2>\n\n\n\n<p>There are various modes that can be used when opening files in Python. Each mode determines how the file will be handled.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>r<\/strong>: Opens the file in read-only mode. An error occurs if the file does not exist.<\/li>\n\n\n\n<li><strong>w<\/strong>: Opens the file in write-only mode. If the file already exists, it will be overwritten.<\/li>\n\n\n\n<li><strong>a<\/strong>: Opens the file in append mode. If the file exists, data will be added at the end.<\/li>\n\n\n\n<li><strong>r+<\/strong>: Opens the file in read\/write mode. The file must exist.<\/li>\n\n\n\n<li><strong>w+<\/strong>: Opens the file in read\/write mode. If the file already exists, it will be overwritten.<\/li>\n\n\n\n<li><strong>a+<\/strong>: Opens the file in read\/append mode. If the file exists, data will be added at the end.<\/li>\n\n\n\n<li><strong>b<\/strong>: Opens the file in binary mode.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Reading Binary Files<\/h3>\n\n\n\n<p>When opening binary files (images, videos, etc.) instead of text files, Python provides powerful capabilities.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"example.jpg\", \"rb\") as binary_file:\n    data = binary_file.read()\n    print(\"File size:\", len(data), \"bytes\")  # Output the file size in bytes<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced I\/O Techniques<\/h2>\n\n\n\n<p>Advanced Python input\/output features include advanced usage of processing files in specific ways. For instance, handling CSV files or reading JSON files can be considered an expanded form of input and output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">CSV File Handling<\/h3>\n\n\n\n<p>Using Python&#8217;s <code>csv<\/code> module makes it easier to handle CSV files. CSV files are a simple file format that stores data separated by commas (`,`).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\n\n# Writing to a CSV file\nwith open(\"data.csv\", \"w\", newline='') as csvfile:\n    writer = csv.writer(csvfile)\n    writer.writerow([\"Name\", \"Age\"])\n    writer.writerow([\"Alice\", 25])\n    writer.writerow([\"Bob\", 30])\n\n# Reading from a CSV file\nwith open(\"data.csv\", \"r\") as csvfile:\n    reader = csv.reader(csvfile)\n    for row in reader:\n        print(row)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">JSON File Handling<\/h3>\n\n\n\n<p>JSON serves as a means to handle JavaScript object notation in Python. Python&#8217;s <code>json<\/code> module provides functionality to convert JSON format into Python data types and vice versa.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\n# Writing JSON data\ndata = {\n    \"name\": \"Charlie\",\n    \"age\": 35,\n    \"city\": \"Seoul\"\n}\n\nwith open(\"data.json\", \"w\") as jsonfile:\n    json.dump(data, jsonfile)\n\n# Reading JSON data\nwith open(\"data.json\", \"r\") as jsonfile:\n    data_loaded = json.load(jsonfile)\n    print(data_loaded)<\/code><\/pre>\n\n\n\n<p><strong>NOTE:<\/strong> JSON and CSV are widely used for data exchange on the web. It is crucial to handle data in the correct format to minimize risks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Character Encoding and Decoding<\/h2>\n\n\n\n<p>When performing input and output, it is important to properly set character encoding and decoding. Particularly in programs that support multiple languages, using UTF-8 is common.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Writing a UTF-8 encoded file\nwith open(\"utf8_file.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello. This is a UTF-8 encoded file.\")\n\n# Reading a UTF-8 encoded file\nwith open(\"utf8_file.txt\", \"r\", encoding=\"utf-8\") as file:\n    content = file.read()\n    print(content)<\/code><\/pre>\n\n\n\n<p>If encoding is not specified, Python will follow the default settings of the individual operating system. This can cause compatibility issues when using files on different systems. Therefore, it is always recommended to specify the encoding explicitly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Python provides powerful and flexible input\/output capabilities. The input\/output process does not just end with displaying simple data on the screen or reading from files; it includes methods for reading and writing various formats of data. Understanding and handling standard input\/output to file I\/O, as well as file modes, are all essential skills.<\/p>\n\n\n\n<p>Continue to practice various input\/output methods and applications to expand your programming skills. In the next session, we will take a closer look at exception handling in Python. If you have any questions, feel free to leave a comment!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this course, we will take a deep dive into input and output methods using Python. Input and output are fundamental elements of programming languages, providing the functionality to read and write data. In Python, various methods can be used for input and output, each with its unique use cases. Standard Input\/Output Python outputs data &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31707\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;04-4 Input and Output in Python, Input and Output of Programs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[98],"tags":[95],"class_list":["post-31707","post","type-post","status-publish","format-standard","hentry","category--en","tag--en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>04-4 Input and Output in Python, Input and Output of Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/31707\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"04-4 Input and Output in Python, Input and Output of Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this course, we will take a deep dive into input and output methods using Python. Input and output are fundamental elements of programming languages, providing the functionality to read and write data. In Python, various methods can be used for input and output, each with its unique use cases. Standard Input\/Output Python outputs data &hellip; \ub354 \ubcf4\uae30 &quot;04-4 Input and Output in Python, Input and Output of Programs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31707\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:01:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:48:35+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"5\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31707\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31707\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"04-4 Input and Output in Python, Input and Output of Programs\",\"datePublished\":\"2024-11-01T09:01:57+00:00\",\"dateModified\":\"2024-11-01T11:48:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31707\/\"},\"wordCount\":707,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"keywords\":[\"\ud30c\uc774\uc36c\uac15\uc88c\"],\"articleSection\":[\"Python Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31707\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31707\/\",\"name\":\"04-4 Input and Output in Python, Input and Output of Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:01:57+00:00\",\"dateModified\":\"2024-11-01T11:48:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31707\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31707\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31707\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"04-4 Input and Output in Python, Input and Output of Programs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"04-4 Input and Output in Python, Input and Output of Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/31707\/","og_locale":"ko_KR","og_type":"article","og_title":"04-4 Input and Output in Python, Input and Output of Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this course, we will take a deep dive into input and output methods using Python. Input and output are fundamental elements of programming languages, providing the functionality to read and write data. In Python, various methods can be used for input and output, each with its unique use cases. Standard Input\/Output Python outputs data &hellip; \ub354 \ubcf4\uae30 \"04-4 Input and Output in Python, Input and Output of Programs\"","og_url":"https:\/\/atmokpo.com\/w\/31707\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:01:57+00:00","article_modified_time":"2024-11-01T11:48:35+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"5\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31707\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31707\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"04-4 Input and Output in Python, Input and Output of Programs","datePublished":"2024-11-01T09:01:57+00:00","dateModified":"2024-11-01T11:48:35+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31707\/"},"wordCount":707,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"keywords":["\ud30c\uc774\uc36c\uac15\uc88c"],"articleSection":["Python Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31707\/","url":"https:\/\/atmokpo.com\/w\/31707\/","name":"04-4 Input and Output in Python, Input and Output of Programs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:01:57+00:00","dateModified":"2024-11-01T11:48:35+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31707\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31707\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31707\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"04-4 Input and Output in Python, Input and Output of Programs"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=31707"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31707\/revisions"}],"predecessor-version":[{"id":31708,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31707\/revisions\/31708"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}