{"id":19,"date":"2024-01-24T12:50:51","date_gmt":"2024-01-24T12:50:51","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=19"},"modified":"2024-01-24T12:50:52","modified_gmt":"2024-01-24T12:50:52","slug":"python-numbers","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-numbers\/","title":{"rendered":"Python Numbers"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python numbers and how to use them in programs.<\/p>\n\n\n\n<p>Python supports\u00a0integers,\u00a0floats, and complex numbers. This tutorial discusses only integers and floats.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Integers<\/h2>\n\n\n\n<p>The\u00a0integers\u00a0are numbers such as -1, 0, 1, 2, and 3, .. and they have type\u00a0<code>int<\/code>.<\/p>\n\n\n\n<p>You can use Math operators like +, -, *, and \/ to form expressions that include integers. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 20 + 10 30 >>> 20 - 10 10 >>> 20 * 10 200 >>> 20 \/ 10 2.0 <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To calculate exponents, you use two multiplication symbols (<code>**<\/code>). For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 3**3 27<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To modify the order of operations, you use the parentheses\u00a0<code>()<\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 20 \/ (10 + 10) 1.0<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Floats<\/h2>\n\n\n\n<p>Any number with a decimal point is a\u00a0floating-point number. The term float means that the decimal point can appear at any position in a number.<\/p>\n\n\n\n<p>In general, you can use floats like integers. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 0.5 + 0.5 1.0 >>> 0.5 - 0.5 0.0 >>> 0.5 \/ 0.5 1.0 >>> 0.5 * 0.5 0.25<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The division of two integers always returns a float:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 20 \/ 10 2.0<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you mix an integer and a float in any arithmetic operation, the result is a float:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 1 + 2.0 3.0<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Due to the internal representation of floats, Python will try to represent the result as precisely as possible. However, you may get the result that you would not expect. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 0.1 + 0.2 0.30000000000000004<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Just keep this in mind when you perform calculations with floats. And you\u2019ll learn how to handle situations like this in\u00a0later tutorials.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Underscores in numbers<\/h2>\n\n\n\n<p>When a number is large, it\u2019ll become difficult to read. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>count = 10000000000<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To make the long numbers more readable, you can group digits using underscores, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>count = 10_000_000_000<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When storing these values, Python just ignores the underscores. It does so when displaying the numbers with underscores on the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>count = 10_000_000_000 print(count)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>10000000000<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The underscores also work for both integers and floats.<\/p>\n\n\n\n<p>Note that the underscores in numbers have been available since Python 3.6<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python numbers and how to use them in programs. Python supports\u00a0integers,\u00a0floats, and complex numbers. This tutorial discusses only integers and floats. Integers The\u00a0integers\u00a0are numbers such as -1, 0, 1, 2, and 3, .. and they have type\u00a0int. You can use Math operators like +, -, *, and \/ [&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-19","post","type-post","status-publish","format-standard","hentry","category-1-fundamentals"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/19","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=19"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":20,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/19\/revisions\/20"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}