{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "numericDataTypes.ipynb", "provenance": [], "authorship_tag": "ABX9TyNM87ErSh5XRx9R31HFbz3P", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "4-S5419-vyqv" }, "source": [ "## Numeric data types\r\n", "\r\n", "Python is a dynamically typed language. The interpreter infers the data type of a value based on pre-determined rules. In the previous chapter, **string** values were coded using single quotes around a sequence of characters. Similarly, there are rules by which you can declare different numeric data types.\r\n", "\r\n", "### int\r\n", "\r\n", "Integer numbers are made up of digits `0` to `9` and can be prefixed with **unary** operators like `+` or `-`. There is no restriction to the size of numbers that can be used, only limited by the memory available on your system. Here's some examples:\r\n", "\r\n", "```ruby\r\n", ">>> 42\r\n", "42\r\n", ">>> 0\r\n", "0\r\n", ">>> +100\r\n", "100\r\n", ">>> -5\r\n", "-5\r\n", "```\r\n", "\r\n", "For readability purposes, you can use underscores in between the digits.\r\n", "\r\n", "```ruby\r\n", ">>> 1_000_000_000\r\n", "1000000000\r\n", "```\r\n", "\r\n", ">![warning](./images/warning.svg) Underscore cannot be used as the first or last character, and cannot be used consecutively.\r\n", "\r\n", "### float\r\n", "\r\n", "Here's some examples for floating-point numbers.\r\n", "\r\n", "```ruby\r\n", ">>> 3.14\r\n", "3.14\r\n", ">>> -1.12\r\n", "-1.12\r\n", "```\r\n", "\r\n", "Python also supports the exponential notation. See [wikipedia: E scientific notation](https://en.wikipedia.org/wiki/Scientific_notation#E_notation) for details about this form of expressing numbers.\r\n", "\r\n", "```ruby\r\n", ">>> 543.15e20\r\n", "5.4315e+22\r\n", ">>> 1.5e-5\r\n", "1.5e-05\r\n", "```\r\n", "\r\n", "Unlike integers, floating-point numbers have a limited precision. While displaying very small or very large floating-point numbers, Python will automatically convert them to the exponential notation.\r\n", "\r\n", "```ruby\r\n", ">>> 0.0000000001234567890123456789\r\n", "1.2345678901234568e-10\r\n", ">>> 31415926535897935809629384623048923.649234324234\r\n", "3.1415926535897936e+34\r\n", "```\r\n", "\r\n", ">![info](./images/info.svg) You might also get seemingly strange results as shown below. See [docs.python: Floating Point Arithmetic Issues and Limitations](https://docs.python.org/3/tutorial/floatingpoint.html) and [stackoverflow: Is floating point math broken?](https://stackoverflow.com/q/588004/4082052) for details and workarounds.\r\n", "\r\n", "```ruby\r\n", ">>> 3.14 + 2\r\n", "5.140000000000001\r\n", "```\r\n", "\r\n", "### Arithmetic operators\r\n", "\r\n", "All arithmetic operators you'd typically expect are available. If any operand is a floating-point number, result will be of `float` data type. Use `+` for addition, `-` for subtraction, `*` for multiplication and `**` for exponentiation. As mentioned before, REPL is quite useful for learning purposes. It makes for a good calculator for number crunching as well. You can also use `_` to refer to the result of the previous expression (this is applicable only in the REPL, not in Python scripts).\r\n", "\r\n", "```ruby\r\n", ">>> 25 + 17\r\n", "42\r\n", ">>> 10 - 8\r\n", "2\r\n", ">>> 25 * 3.3\r\n", "82.5\r\n", ">>> 32 ** 42\r\n", "1645504557321206042154969182557350504982735865633579863348609024\r\n", "\r\n", ">>> 5 + 2\r\n", "7\r\n", ">>> _ * 3\r\n", "21\r\n", "```\r\n", "\r\n", "There are two operators for division. Use `/` if you want a floating-point result. Using `//` between two integers will give only the integer portion of the result (no rounding).\r\n", "\r\n", "```ruby\r\n", ">>> 4.5 / 1.5\r\n", "3.0\r\n", ">>> 5 / 3\r\n", "1.6666666666666667\r\n", ">>> 5 // 3\r\n", "1\r\n", "```\r\n", "\r\n", "Use modulo operator `%` to get the remainder. Sign of the result is same as the sign of the second operand.\r\n", "\r\n", "```ruby\r\n", ">>> 5 % 3\r\n", "2\r\n", "\r\n", ">>> -5 % 3\r\n", "1\r\n", ">>> 5 % -3\r\n", "-1\r\n", ">>> 6.5 % -3\r\n", "-2.5\r\n", "```\r\n", "\r\n", ">![info](./images/info.svg) See [docs.python: Binary arithmetic operations](https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations) and [stackoverflow: modulo operation on negative numbers](https://stackoverflow.com/q/3883004/4082052) for more details.\r\n", "\r\n", "### Operator precedence\r\n", "\r\n", "Arithmetic operator precedence follows the familiar **PEMDAS** or **BODMAS** abbreviations. Precedence, higher to lower is listed below:\r\n", "\r\n", "* Expression inside parentheses\r\n", "* exponentiation\r\n", "* multiplication, division, modulo\r\n", "* addition, subtraction\r\n", "\r\n", "Expression is evaluated left-to-right when operators have the same precedence. Unary operator precedence is between exponentiation and multiplication/division operators. See [docs.python: Operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence) for complete details.\r\n", "\r\n", "### Integer formats\r\n", "\r\n", "The integer examples so far have been coded using base 10, i.e. **decimal** format. Python has provision for representing **binary**, **octal** and **hexadecimal** formats as well. To distinguish between these different formats, a prefix is used:\r\n", "\r\n", "* `0b` or `0B` for binary\r\n", "* `0o` or `0O` for octal\r\n", "* `0x` or `0X` for hexadecimal\r\n", "\r\n", "All four formats fall under the `int` data type. Python displays them in decimal format by default. Underscores can be used for readability for any of these formats.\r\n", "\r\n", "```ruby\r\n", ">>> 0b1000_1111\r\n", "143\r\n", ">>> 0o10\r\n", "8\r\n", ">>> 0x10\r\n", "16\r\n", "\r\n", ">>> 5 + 0xa\r\n", "15\r\n", "```\r\n", "\r\n", "Decimal format numbers cannot be prefixed by `0`, other than `0` itself.\r\n", "\r\n", "```ruby\r\n", ">>> 00000\r\n", "0\r\n", "\r\n", ">>> 09\r\n", " File \"\", line 1\r\n", " 09\r\n", " ^\r\n", "SyntaxError: leading zeros in decimal integer literals are not permitted;\r\n", " use an 0o prefix for octal integers\r\n", "```\r\n", "\r\n", "If code execution hits a snag, you'll get an error message along with the code snippet that the interpreter thinks caused the issue. In Python parlance, an **exception** has occurred. The exception has a name (`SyntaxError` in the above example) followed by the error message. See [Exception handling](#exception-handling) chapter for more details.\r\n", "\r\n", "### Other numeric types\r\n", "\r\n", "Python's standard data type also includes complex type (imaginary part is suffixed with the character `j`). Others like `decimal` and `fractions` are provided as modules.\r\n", "\r\n", "* [docs.python: complex](https://docs.python.org/3/library/stdtypes.html#typesnumeric)\r\n", "* [docs.python: decimal](https://docs.python.org/3/library/decimal.html)\r\n", "* [docs.python: fractions](https://docs.python.org/3/library/fractions.html)\r\n", "\r\n", ">![warning](./images/warning.svg) Some of the numeric types can have alphabets like `e`, `b`, `j`, etc in their values. As Python is a dynamically typed language, you cannot use variable names beginning with a number. Otherwise, it would be impossible to evaluate an expression like `result = input_value + 0x12 - 2j`.\r\n", "\r\n", ">![info](./images/info.svg) There are many third-party libraries that are useful for number crunching in mathematical context, engineering applications, etc. See my list [py_resources: Scientific computing](https://learnbyexample.github.io/py_resources/domain.html#scientific-computing) for curated resources.\r\n" ] }, { "cell_type": "code", "metadata": { "id": "kG9GfoVavWr1" }, "source": [ "" ], "execution_count": null, "outputs": [] } ] }