{"id":49,"date":"2026-03-01T14:52:39","date_gmt":"2026-03-01T09:52:39","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=49"},"modified":"2026-03-12T11:43:29","modified_gmt":"2026-03-12T06:43:29","slug":"type-casting","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/type-casting\/","title":{"rendered":"Type Casting"},"content":{"rendered":"\n<p>Type casting in Python means converting one data type into another data type. Sometimes in programming, you need to change the type of a variable to perform specific operations. Python provides built in functions that allow you to convert data types easily.<\/p>\n\n\n\n<p>Type casting is commonly used when working with numbers, strings, and Boolean values.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY TYPE CASTING IS IMPORTANT<\/strong><\/h1>\n\n\n\n<p>Different operations require specific data types. For example, you cannot add a number and a string directly. Type casting allows you to convert data into the correct format so calculations and operations can be performed without errors.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>COMMON TYPE CASTING FUNCTIONS<\/strong><\/h1>\n\n\n\n<p>Python provides several built in functions for type conversion:<\/p>\n\n\n\n<p>int() converts a value into an integer<br>float() converts a value into a floating point number<br>str() converts a value into a string<br>bool() converts a value into a Boolean<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>CONVERTING TO INTEGER<\/strong><\/h1>\n\n\n\n<p>You can convert a float or string into an integer using the int() function.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>x = 10.8<br>y = int(x)<\/p>\n\n\n\n<p>The value of y will be 10 because decimals are removed.<\/p>\n\n\n\n<p>Example with string:<\/p>\n\n\n\n<p>num = &#8220;25&#8221;<br>number = int(num)<\/p>\n\n\n\n<p>Now number becomes an integer.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>CONVERTING TO FLOAT<\/strong><\/h1>\n\n\n\n<p>Use the float() function to convert integers or strings into decimal numbers.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>x = 10<br>y = float(x)<\/p>\n\n\n\n<p>The value of y will be 10.0<\/p>\n\n\n\n<p>Example with string:<\/p>\n\n\n\n<p>price = &#8220;99.5&#8221;<br>new_price = float(price)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>CONVERTING TO STRING<\/strong><\/h1>\n\n\n\n<p>Use the str() function to convert numbers into text.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>age = 25<br>text_age = str(age)<\/p>\n\n\n\n<p>Now text_age is a string.<\/p>\n\n\n\n<p>This is useful when combining numbers with text.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>age = 20<br>print(&#8220;My age is &#8221; + str(age))<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>CONVERTING TO BOOLEAN<\/strong><\/h1>\n\n\n\n<p>The bool() function converts values into True or False.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>print(bool(1))<br>Output will be True<\/p>\n\n\n\n<p>print(bool(0))<br>Output will be False<\/p>\n\n\n\n<p>Empty values such as empty string or zero return False. Most other values return True.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPLICIT AND EXPLICIT TYPE CASTING<\/strong><\/h1>\n\n\n\n<p>Implicit type casting happens automatically when Python converts smaller data types into larger ones.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>x = 10<br>y = 2.5<br>result = x + y<\/p>\n\n\n\n<p>Python automatically converts x into a float.<\/p>\n\n\n\n<p>Explicit type casting is done manually using functions like int(), float(), or str().<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>CONCLUSION<\/strong><\/h1>\n\n\n\n<p>Type casting helps ensure compatibility between different data types in your program. It prevents errors and allows smooth data processing. Understanding type casting is essential for performing calculations, handling user input, and building efficient Python applications.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773297854834\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FUNDAMENTALS (PYF) > Variables and Data Types > Type Casting<\/span><\/span><\/div>","protected":false},"menu_order":10,"template":"","class_list":["post-49","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Type Casting - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python type casting: convert int, float, str, and bool with built-in functions for error-free data processing and calculations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/type-casting\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Type Casting - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python type casting: convert int, float, str, and bool with built-in functions for error-free data processing and calculations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/type-casting\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-12T06:43:29+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/type-casting\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/type-casting\\\/\",\"name\":\"Type Casting - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T09:52:39+00:00\",\"dateModified\":\"2026-03-12T06:43:29+00:00\",\"description\":\"Learn Python type casting: convert int, float, str, and bool with built-in functions for error-free data processing and calculations.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/type-casting\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/type-casting\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/type-casting\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FUNDAMENTALS (PYF) > Variables and Data Types > Type Casting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Type Casting - One Language. Endless Possibilities","description":"Learn Python type casting: convert int, float, str, and bool with built-in functions for error-free data processing and calculations.","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:\/\/gigz.pk\/python\/lesson\/type-casting\/","og_locale":"en_US","og_type":"article","og_title":"Type Casting - One Language. Endless Possibilities","og_description":"Learn Python type casting: convert int, float, str, and bool with built-in functions for error-free data processing and calculations.","og_url":"https:\/\/gigz.pk\/python\/lesson\/type-casting\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-12T06:43:29+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/type-casting\/","url":"https:\/\/gigz.pk\/python\/lesson\/type-casting\/","name":"Type Casting - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T09:52:39+00:00","dateModified":"2026-03-12T06:43:29+00:00","description":"Learn Python type casting: convert int, float, str, and bool with built-in functions for error-free data processing and calculations.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/type-casting\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/type-casting\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/type-casting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FUNDAMENTALS (PYF) > Variables and Data Types > Type Casting"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}