{"id":621,"date":"2024-07-26T11:41:00","date_gmt":"2024-07-26T06:41:00","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=621"},"modified":"2024-10-05T09:05:32","modified_gmt":"2024-10-05T04:05:32","slug":"python-type-casting","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-type-casting\/","title":{"rendered":"Python: Type Casting"},"content":{"rendered":"\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>As you are now already familiar with the <strong>input<\/strong> function which is used for data input. Also, you must be familiar now that the <strong>input<\/strong> function returns data as <strong>String<\/strong> type. Scenarios where you want your users to input the value as a number (<strong>int<\/strong> or <strong>float<\/strong> type). Python provides functions to typecast String types to numbers and numbers to String types.<\/p>\n\n\n\n<p><strong>Type Casting<\/strong>\u00a0(or type conversion) allows you to<strong> change the data type of a variable<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>operand = int (input ('Enter the Number: '))\noperand = float (input ('Enter Number in fractional value'))\nprint('You entered the value = + str(operand))<\/strong><\/code><\/pre>\n\n\n\n<p>There are two main types of type casting: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Implicit Type Casting<\/strong>:   In this method, Python automatically converts one data type into another without user intervention.<\/li>\n\n\n\n<li><strong>Explicit Type Casting<\/strong>:    In this method, you explicitly convert a variable to a specific data type using predefined functions.<\/li>\n<\/ol>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading has-base-2-color has-text-color has-link-color wp-elements-b7a91ca9e506d4218930663b83b583d5\">Implicit Type Casting<\/h3>\n\n\n\n<p>Python implicitly converts\u00a0<code>a<\/code><strong>\u00a0(an integer) to a float<\/strong> for addition and multiplication.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>a = 7\nb = 3.0\nc = a + b<\/strong>     # <em>Result: 10.0 (float)<\/em>\n<strong>d = a * b<\/strong>     # <em>Result: 21.0 (float)<\/em><\/code><\/pre>\n\n\n\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-b30579f5\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading has-base-2-color has-text-color has-link-color wp-elements-1982f27f488a793f939604d26d7763c6\">Explicit Type Casting<\/h3>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>Convert\u00a0<code>int<\/code>\u00a0to\u00a0<code>float<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>a = 5\nn = float(a)<\/strong>     <em># Result: 5.0 (float)<\/em><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-tw-dotted\" style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30)\"\/>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>Convert\u00a0<code>float<\/code>\u00a0to\u00a0<code>int<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>a = 5.9\nn = int(a)<\/strong>  <em># Result: 5 (integer)<\/em><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-tw-dotted\" style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30)\"\/>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>Convert\u00a0<code>int<\/code>\u00a0to\u00a0<code>str<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>a = 5\nn = str(a)<\/strong>  <em># Result: '5' (string)<\/em><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-tw-dotted\" style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30)\"\/>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>Convert\u00a0<code>str<\/code>\u00a0to\u00a0<code>float<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>a = \"5.9\"\nn = float(a)<\/strong>  <em># Result: 5.9 (float)<\/em><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-tw-dotted\" style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30)\"\/>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>Convert\u00a0<code>str<\/code>\u00a0to\u00a0<code>int<\/code>\u00a0(with error handling)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>a = \"5\"\ntry:\n    n = int(a)<\/strong>  <em># Result: 5 (integer)<\/em><strong>\nexcept ValueError:\n    print(\"Invalid input\")<\/strong><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-tw-dotted\" style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30)\"\/>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>As you are now already familiar with the input function which is used for data input. Also, you must be familiar now that the input function returns data as String type. Scenarios where you want your users to input the value as a number (int or float type). Python provides functions to typecast String types [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_robots_primary_cat":"5","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-621","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":22,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"predecessor-version":[{"id":657,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/621\/revisions\/657"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}