{"id":22,"date":"2025-10-19T02:52:16","date_gmt":"2025-10-19T02:52:16","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=22"},"modified":"2025-12-17T11:02:50","modified_gmt":"2025-12-17T11:02:50","slug":"lesson-4-input-output-and-type-casting","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=22","title":{"rendered":"Lesson 4: Input, Output, and Type Casting"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand how to take user input, display output, and convert data types in Python programs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>1. Input in Python<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>input()<\/code> function allows users to enter data from the keyboard.<br>By default, it <strong>returns data as a string<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"Enter your name: \")\nprint(\"Hello,\", name)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Example 2: Taking numeric input<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num = input(\"Enter a number: \")\nprint(num + 10)  # \u274c Error (string + int not allowed)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To fix this, convert input to an integer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num = int(input(\"Enter a number: \"))\nprint(num + 10)  # \u2705 Works correctly\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udcac <strong>2. Output in Python<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the <code>print()<\/code> function to display information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Ali\"\nage = 25\nprint(\"My name is\", name, \"and I am\", age, \"years old.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Formatting Output<\/strong><br>You can format text neatly using <strong>f-strings<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Sara\"\nage = 21\nprint(f\"My name is {name} and I am {age} years old.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Another method: <code>format()<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"My name is {} and I am {} years old.\".format(name, age))\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd04 <strong>3. Type Casting<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Type casting<\/strong> means converting one data type to another.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common conversion functions:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>Description<\/th><th>Example<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td><code>int()<\/code><\/td><td>Converts to integer<\/td><td><code>int(\"10\")<\/code><\/td><td>10<\/td><\/tr><tr><td><code>float()<\/code><\/td><td>Converts to float<\/td><td><code>float(\"3.14\")<\/code><\/td><td>3.14<\/td><\/tr><tr><td><code>str()<\/code><\/td><td>Converts to string<\/td><td><code>str(25)<\/code><\/td><td>&#8220;25&#8221;<\/td><\/tr><tr><td><code>bool()<\/code><\/td><td>Converts to boolean<\/td><td><code>bool(0)<\/code><\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = \"100\"\nprint(int(x) + 50)   # 150\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d <strong>4. Combining Input, Output &amp; Type Casting<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Example Program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"Enter your name: \")\nage = int(input(\"Enter your age: \"))\nprint(f\"Welcome {name}! Next year you will be {age + 1} years old.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter your name: Ahmed\nEnter your age: 20\nWelcome Ahmed! Next year you will be 21 years old.\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde9 <strong>5. Real-Life Example<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you are building a <strong>billing program<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>item = input(\"Enter item name: \")\nprice = float(input(\"Enter item price: \"))\nquantity = int(input(\"Enter quantity: \"))\ntotal = price * quantity\nprint(f\"Total cost for {item} = \u20b9{total}\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>6. Practice Ideas<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Take age as input and display how old the person will be after 10 years.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Write a program to input two numbers and display their sum, difference, and product.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a greeting program that asks for the user\u2019s name and city, then prints a personalized message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Convert a string <code>\"123.45\"<\/code> to a float and print its type.<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-fe317a90-a20d-4ee9-8c00-fbdefc5e2aac\" href=\"https:\/\/codetypingpro.com\/wp-content\/uploads\/2025\/10\/Lesson_4_Input_Output_and_Type_Casting_Practice_Sheet.docx\">Lesson_4_Input_Output_and_Type_Casting_Practice_Sheet<\/a><a href=\"https:\/\/codetypingpro.com\/wp-content\/uploads\/2025\/10\/Lesson_4_Input_Output_and_Type_Casting_Practice_Sheet.docx\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-fe317a90-a20d-4ee9-8c00-fbdefc5e2aac\">Download<\/a><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/codetypingpro.com\/?p=330\" data-type=\"post\" data-id=\"330\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">more examples<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand how to take user input, display output, and convert data types in Python programs. \ud83e\udde0 1. Input in Python The input() function allows users to enter data from the keyboard.By default, it returns data as a string. Example: \ud83d\udd39 Example 2: Taking numeric input To fix this, convert input to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22"}],"version-history":[{"count":3,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":333,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions\/333"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}