{"id":27,"date":"2024-01-24T13:07:18","date_gmt":"2024-01-24T13:07:18","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=27"},"modified":"2024-01-24T13:07:19","modified_gmt":"2024-01-24T13:07:19","slug":"python-type-conversion","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-type-conversion\/","title":{"rendered":"Python Type Conversion"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about type conversion in Python and some useful type conversion functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to type conversion in Python<\/h2>\n\n\n\n<p>To get input from users, you use the\u00a0<code>input()<\/code>\u00a0function. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>value = input('Enter a value:') print(value)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When you execute this code, it\u2019ll prompt you for input on the Terminal:<code>Enter a value:<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you enter a value, for example, a\u00a0number, the program will display that value back:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Enter a value:100 100<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>However, the\u00a0<code>input()<\/code>\u00a0function returns a\u00a0string, not an\u00a0integer.<\/p>\n\n\n\n<p>The following example prompts you to enter two input values: net price and tax rate. After that, it calculates the tax and displays the result on the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>price = input('Enter the price ($):') tax = input('Enter the tax rate (%):') tax_amount = price * tax \/ 100 print(f'The tax amount price is ${tax_amount}')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When you execute the program and enter some numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Enter the price ($):100 Enter the tax rate (%):10<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>\u2026 you\u2019ll get the following error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Traceback (most recent call last): File \"main.py\", line 4, in &lt;module> tax_amount = price * tax \/ 100 TypeError: can't multiply sequence by non-int of type 'str'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since the input values are strings, you cannot apply the multiply operator.<\/p>\n\n\n\n<p>To solve this issue, you need to convert the strings to numbers before performing calculations.<\/p>\n\n\n\n<p>To convert a string to a number, you use the&nbsp;<code>int()<\/code>&nbsp;function. More precisely, the&nbsp;<code>int()<\/code>&nbsp;function converts a string to an integer.<\/p>\n\n\n\n<p>The following example uses the\u00a0<code>int()<\/code>\u00a0function to convert the input strings to numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>price = input('Enter the price ($):') tax = input('Enter the tax rate (%):') tax_amount = int(price) * int(tax) \/ 100 print(f'The tax amount is ${tax_amount}')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you run the program, and enter some values, you\u2019ll see that it works correctly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code> Enter the price ($): 100 Enter the tax rate (%): 10 The tax amount is $10.0<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Other type conversion functions<\/h2>\n\n\n\n<p>Besides the&nbsp;<code>int(str)<\/code>&nbsp;functions, Python supports other type conversion functions. The following shows the most important ones for now:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>float(str)<\/code>\u00a0\u2013 convert a string to a\u00a0floating-point number.<\/li>\n\n\n\n<li><code>bool(val)<\/code>\u00a0\u2013 convert a value to a\u00a0boolean\u00a0value, either\u00a0<code>True<\/code>\u00a0or\u00a0<code>False<\/code>.<\/li>\n\n\n\n<li><code>str(val)<\/code>&nbsp;\u2013 return the string representation of a value.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Getting the type of a value<\/h2>\n\n\n\n<p>To get the type of value, you use the\u00a0<code>type(value)<\/code>\u00a0function. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> type(100) &lt;class 'int'> >>> type(2.0) &lt;class 'float'> >>> type('Hello') &lt;class 'str'> >>> type(True) &lt;class 'bool'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>As you can see clearly from the output:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number&nbsp;<code>100<\/code>&nbsp;has the type of&nbsp;<code>int<\/code>.<\/li>\n\n\n\n<li>The number&nbsp;<code>2.0<\/code>&nbsp;has the type of&nbsp;<code>float<\/code>.<\/li>\n\n\n\n<li>The string&nbsp;<code>'Hello'<\/code>&nbsp;has the type of&nbsp;<code>str<\/code>.<\/li>\n\n\n\n<li>And the&nbsp;<code>True<\/code>&nbsp;value has the type of&nbsp;<code>bool<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>In front of each type, you see the\u00a0<code>class<\/code>\u00a0keyword. It isn\u2019t important for now. And you\u2019ll learn more about the\u00a0class\u00a0later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about type conversion in Python and some useful type conversion functions. Introduction to type conversion in Python To get input from users, you use the\u00a0input()\u00a0function. For example: When you execute this code, it\u2019ll prompt you for input on the Terminal:Enter a value: If you enter a value, for example, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-1-fundamentals"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":28,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/27\/revisions\/28"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}