{"id":330,"date":"2025-12-17T11:02:04","date_gmt":"2025-12-17T11:02:04","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=330"},"modified":"2025-12-17T12:35:33","modified_gmt":"2025-12-17T12:35:33","slug":"%f0%9f%93%98-lesson-4-input-output-and-type-casting-examples","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=330","title":{"rendered":"\ud83d\udcd8 Lesson 4 examples: Input, Output, and Type Casting"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 1. Output in Python (<code>print()<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>print()<\/code> function is used to <strong>display output on the screen<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 1: Simple Output<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Hello Python\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Python\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Text inside quotes is called a <strong>string<\/strong><\/li>\n\n\n\n<li><code>print()<\/code> shows it on the screen<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 2: Printing Multiple Values<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Sameer\"\nage = 25\n\nprint(name, age)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer 25\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python automatically adds a space between values<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 3: Output with Text<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Ayesha\"\ncity = \"Hyderabad\"\n\nprint(\"Name:\", name)\nprint(\"City:\", city)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Ayesha\nCity: Hyderabad\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 2. Input in Python (<code>input()<\/code>)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>input()<\/code> function is used to <strong>take input from the user<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u26a0 Important:<br><code>input()<\/code> <strong>always returns string data<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 4: Simple Input<\/h4>\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\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter your name: Chandini\nHello Chandini\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User types the name<\/li>\n\n\n\n<li>Value is stored in <code>name<\/code><\/li>\n\n\n\n<li>Printed using <code>print()<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 5: Input Without Message<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>city = input()\nprint(\"City:\", city)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Bangalore\nCity: Bangalore\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 3. Problem Without Type Casting<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>age = input(\"Enter age: \")\nprint(age + 1)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c <strong>Error Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TypeError: can only concatenate str (not \"int\") to str\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>input()<\/code> returns <strong>string<\/strong><\/li>\n\n\n\n<li>Python cannot add number to string<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 4. Type Casting (Type Conversion)<\/h4>\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<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Common Type Casting Functions<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>Converts To<\/th><\/tr><\/thead><tbody><tr><td><code>int()<\/code><\/td><td>Integer<\/td><\/tr><tr><td><code>float()<\/code><\/td><td>Decimal<\/td><\/tr><tr><td><code>str()<\/code><\/td><td>String<\/td><\/tr><tr><td><code>bool()<\/code><\/td><td>Boolean<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 5. Input with Type Casting (MOST IMPORTANT)<\/h4>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 6: Converting Age to Integer<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>age = int(input(\"Enter age: \"))\nprint(\"Next year age:\", age + 1)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter age: 24\nNext year age: 25\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>input()<\/code> \u2192 string<\/li>\n\n\n\n<li><code>int()<\/code> converts it to number<\/li>\n\n\n\n<li>Mathematical operation becomes possible<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 7: Salary Input<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>salary = float(input(\"Enter salary: \"))\nbonus = 5000\n\ntotal = salary + bonus\nprint(\"Total salary:\", total)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter salary: 25000\nTotal salary: 30000.0\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 6. Multiple Inputs<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Example 8: Student Marks<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>marks1 = int(input(\"Enter marks 1: \"))\nmarks2 = int(input(\"Enter marks 2: \"))\n\ntotal = marks1 + marks2\naverage = total \/ 2\n\nprint(\"Total:\", total)\nprint(\"Average:\", average)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter marks 1: 80\nEnter marks 2: 90\nTotal: 170\nAverage: 85.0\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 7. Type Casting Between Variables<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Example 9: Integer to String<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>age = 22\ntext = \"Age is \" + str(age)\n\nprint(text)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Age is 22\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 10: String to Float<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>price = \"99.5\"\nnew_price = float(price)\n\nprint(new_price + 10)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>109.5\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 8. Boolean Type Casting<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Example 11<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>print(bool(1))\nprint(bool(0))\nprint(bool(\"\"))\nprint(bool(\"Python\"))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nFalse\nFalse\nTrue\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>0<\/code> or empty values \u2192 False<\/li>\n\n\n\n<li>Non-zero or non-empty \u2192 True<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 9. Real-Life Example: Employee Details<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>name = input(\"Enter name: \")\nage = int(input(\"Enter age: \"))\nsalary = float(input(\"Enter salary: \"))\n\nprint(\"Employee Name:\", name)\nprint(\"Employee Age:\", age)\nprint(\"Employee Salary:\", salary)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter name: Gousya\nEnter age: 26\nEnter salary: 32000\nEmployee Name: Gousya\nEmployee Age: 26\nEmployee Salary: 32000.0\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 10. Common Mistakes (IMPORTANT)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c Forgetting type casting<br>\u274c Adding number with string<br>\u274c Wrong data type conversion<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Final Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u2714 <code>print()<\/code> \u2192 output<br>\u2714 <code>input()<\/code> \u2192 user input (string)<br>\u2714 Type casting converts data types<br>\u2714 Required for calculations<br>\u2714 Very important for real programs<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udd39 1. Output in Python (print()) The print() function is used to display output on the screen. Example 1: Simple Output Output \ud83d\udccc Explanation Example 2: Printing Multiple Values Output \ud83d\udccc Explanation Example 3: Output with Text Output \ud83d\udd39 2. Input in Python (input()) The input() function is used to take input from the user. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,1],"tags":[],"class_list":["post-330","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-examples","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/330","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=330"}],"version-history":[{"count":6,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":361,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions\/361"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}