Python Integer Numeric Data Type
Tags: python
Published: 2023-05-29 11:46:50

<section>
<a id="top" class="custom_anchor"></a>
<div id="toc-div">
<h3>Table of Contents<a id="toc-toogle" class="open"></a></h3>
<div id="toc-list">
<dl>
<dt><a href="#intro">A. Introduction</a></dt>
<dt><a href="#numeric">B. Numeric data type</a></dt>
<dt><a href="#int">C. Integer</a></dt>
<dt><a href="#operator">D. Operators</a></dt>

<dd><a href="#arith">1. Arithmetic Operators</a></dd>
<dd><a href="#compa">2. Comparison operators</a></dd>
<dd><a href="#bitwi">3. Bitwise operators</a></dd>

<dt><a href="#constructor">E. Class int() Constructor</a></dt>
<dt><a href="#builtin">F. Built-in function bin()</a></dt>
<dt><a href="#intex">G. Integer Example Application</a></dt>
<dt><a href="#bool">H. Boolean</a></dt>
<dt><a href="#boolcons">I. Class bool() Constructor</a><dt>

<dd><a href="#bool-1">1. Boolean example 1</a></dd>
<dd><a href="#bool-2">2. Boolean example 2</a></dd>
<dd><a href="#bool-3">3. Boolean example 3</a></dd>
<dd><a href="#bool-4">4. Boolean example 4</a></dd>

</dl>
</div>
</div>
</section>

<section>
<a id="intro" class="custom_anchor"></a>
<a style="color: black;" href="#intro"><h2>A. Introduction</h2></a>
</section>


Data types are names given to an entity or object to differentiate them. The idea is to create a rule which operators to use for each specific type that allows Python to run efficiently. Programmers must also be familiar with the data types and operators. They need to know which data and operator to use in their program so that Python can execute them faster, lesser memory usage, and without errors.

There are some data types in Python. We have integer, float, complex, bool, list, tuple, set, range, dictionary, string, and bytes. In this tutorial, I will only cover the <mark>integer</mark> and its subtype <mark>bool</mark>, they belong to the numeric type category.

<section><a id="numeric" class="custom_anchor"></a>
<a style="color: black;" href="#numeric"><h2>B. Numeric data type</h2></a>
</section>

Numeric refers to numbers. Python has 5 numeric data types.


    Integer
    Float
    Complex
    Fraction
    Decimal


<section><a id="int" class="custom_anchor"></a>
<a style="color: black;" href="#int"><h2>C. Integer</h2></a>
</section>


Integers are numbers that are whole numbers. It can be a positive, negative and zero.


    100
    -20
    0


Python's integer maximum value is more than that of a <a href="https://logiclair.org/1541/python-float-numeric-data-type">float</a>. Note the maximum value of a float is 1.7976931348623157e+308.

    >>> import sys
    >>> int(sys.float_info.max)
    179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368

You don't have to worry about overflow error then when processing huge integer values.

<section><a id="operator" class="custom_anchor"></a>
<a style="color: black;" href="#operator"><h2>D. Operators</h2></a>
</section>


Data needs an operator that will transform it to whatever the programmer wants. I will only cover some operators that are frequently used in integers.

Arithmetic operators.

![image](https://user-images.githubusercontent.com/22366935/235307167-278d5297-7345-4ac2-aa11-5ff95eb8cbd6.png)


Comparison operators.

![image](https://user-images.githubusercontent.com/22366935/235307252-3079db57-31c2-49a2-bf62-9b8df99bddc1.png)


Bitwise operators.

![image](https://user-images.githubusercontent.com/22366935/235307307-65d14e53-92f0-48e0-828a-44ec2875ccd4.png)

Boolean operators.

![image](https://user-images.githubusercontent.com/22366935/235307381-7f2798a3-49f5-482f-9328-ae1f6605e4af.png)


<section>
<a id="arith" class="custom_anchor"></a>
<a style="color: black;" href="#arith"><h3>1. Arithmetic Operators</h3></a>
</section>

Addition and subtraction operators

Start your <mark>Python interpreter</mark> from the terminal by typing python. It is assumed that you already installed Python on your computer.


    c:\>python
    Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 10 + 2
    12
    >>> 10 - 6
    4
    >>> 20 - 0
    20


Multiplication and Division operators


    >>> 100 // 2
    50
    >>> 4*3
    12

In the line "100//2", the "//" is called an integer division operator. Meaning it will return an integer value.

Python has 2 division operators.


    /
    //


The first will output a float data type while the second will output an integer. The latter will return the lowest whole number.


    >>> 15 / 4
    3.75
    >>> 15 // 4
    3

Remainder

Python has an operator that can calculate the remainder. It is called the modulo operator with symbol "%".


    >>> 10 % 3
    1


Power operator

The built-in function pow() will return a number given base and exponent. It can also be expressed using double asterisk.

    # a^b
    >>> pow(2, 4)
    16
    >>> 2**4
    16

<section>
<a id="compa" class="custom_anchor"></a>
<a style="color: black;" href="#compa"><h3>2. Comparison operators</h3></a>
</section>


Greater than operator ">".

    >>> a = 10
    >>> b = 6
    >>> if a > b:
    ...     print("a is greater than b")
    ... else:
    ...     print("a is not greater than b")
    ...
    a is greater than b


Greater than or equal to ">=".

    >>> a = 45
    >>> b = 50
    >>> a >= b
    False


Not equal.

    >>> 100 != 200
    True


<section>
<a id="bitwi" class="custom_anchor"></a>
<a style="color: black;" href="#bitwi"><h3>3. Bitwise operators</h3></a>
</section>

    >>> a = 1
    >>> b = 6
    >>> a | b  # bitwise or
    7
    >>> a ^ b  # bitwise exclusive
    7
    >>> a & b  # bitwise and
    0

<section><a id="constructor" class="custom_anchor"></a>
<a style="color: black;" href="#constructor"><h2>E. Class int() Constructor</h2></a>
</section>


Python has a built-in function called an int() or technically a class constructor that can set a different type into an integer type.


    >>> x_str = "8"
    >>> type(x_str)
    <class 'str'>
    >>> x_str
    '8'
    >>> x_int = int(x_str)
    >>> type(x_int)
    <class 'int'>
    >>> x_int
    8


* x_str is initially declared as a string data type.
* Get its type with another built-in function type(), and it is an str or string.
* Print the value.
* Convert it to an integer data type with the class constructor int().
* Get its type, and it is int .
* Print its value.

<div class="div-green">
<span class="alert-header">Tip</span>
<span class="alert-body">The built-in function <mark>type()</mark> returns the type of an object. Example, type(10) will return <class 'int'>.</span>
</div>

<section><a id="builtin" class="custom_anchor"></a>
<a style="color: black;" href="#builtin"><h2>F. Built-in function bin()</h2></a>
</section>


The function bin() converts an integer into a binary string format.

    >>> num = 4
    >>> num_binary = bin(num)
    >>> print(num_binary)
    '0b100'

The "0b" is a prefix to indicate that it is binary.

We can remove the "0b" by splitting at "0b" and take the right side.

    >>> type(num_binary)
    <class 'str'>
    >>> num_bin = num_binary.split("0b")[1]
    >>> print(num_bin)
    '100'

<section><a id="intex" class="custom_anchor"></a>
<a style="color: black;" href="#intex"><h2>G. Integer Example Application</h2></a>
</section>


Let us create a python script to solve the example problem and use integer data type.

Here is the problem: 

"You shop some fruits in the market. The fruits cost 55. Your money is 100. How much is your change?  Create a script that will ask the amount of money you have and the cost of fruits. Determine and print the change."

Create a file "sample_1.py" and write the code on it.

*code*


    # Asks the amount of money you have.
    my_money = input("How much money do you have? ")
    my_money = int(my_money)
    
    # Ask the cost of fruits.
    fruit_cost = input("How much is the cost of fruits? ")
    fruit_cost = int(fruit_cost)
    
    # Use subtraction operator to get the difference.
    change = my_money - fruit_cost
    print("change: " + str(change))


The line that starts with "#" is just a comment and will not be executed by python.

*execute the code*

    c:\>python sample_1.py

The "sample_1.py" must be in "C" drive for this particular example.

*output*

    How much money do you have? 100
    How much is the cost of fruits? 55
    change: 45

<section><a id="bool" class="custom_anchor"></a>
<a style="color: black;" href="#bool"><h2>H. Boolean</h2></a>
</section>


Boolean or bool data type is a subset of Integer. This type can have 2 values, either True or False. Technically it is just 1 or 0.

<section><a id="boolcons" class="custom_anchor"></a>
<a style="color: black;" href="#boolcons"><h2>I. Class bool() Constructor</h2></a>
</section>


Python has a built-in function called a bool() or technically a class constructor that can set a different type into a bool type.

    >>> a = 45
    >>> type(a)
    <class 'int'>
    >>> a_bool = bool(a)
    >>> a_bool
    True
    >>> type(a_bool)
    <class 'bool'>

We use bool() to convert an integer 45 to bool type.

<section>
<a id="bool-1" class="custom_anchor"></a>
<a style="color: black;" href="#bool-1"><h3>1. Boolean example 1</h3></a>
</section>


Boolean is usually used in if conditions.


    >>> is_raining = True
    >>> if is_raining:
    ...     print("It is raining.")
    ... else:
    ...     print("It is not raining.")
    ...
    It is raining.


<section>
<a id="bool-2" class="custom_anchor"></a>
<a style="color: black;" href="#bool-2"><h3>2. Boolean example 2</h3></a>
</section>


The "or" operator is acting on the bool type. 

We have a buy and not buy situations. We buy the vegetable if it is fresh. However if it is not fresh, we will still buy it if it is cheap.

Given:  

* The vegetable is not fresh.
* The vegetable is cheap.


    >>> is_vegetable_fresh = False
    >>> is_vegetable_cheap = True
    >>> if is_vegetable_fresh or is_vegetable_cheap:
    ...     print("Buy it.")
    ... else:
    ...     print("Don't buy it.")
    ...
    Buy it.

The first test is "is_vegetable_fresh", since its value is False, we will test the next condition. The "or" will test every conditions until the value is True.

<section>
<a id="bool-3" class="custom_anchor"></a>
<a style="color: black;" href="#bool-3"><h3>3. Boolean example 3</h3></a>
</section>


The "and" operator is acting on the bool type.

Here is an example conditions. We will go outside if it is not raining and the temperature is not hot.

Given:  

* It is not raining.  
* The temperature is not hot.


    >>> is_not_raining = True
    >>> is_temperature_not_hot = True
    >>> if is_not_raining and is_temperature_not_hot:
    ...     print("go outside")
    ... else:
    ...     print("do not go outside")
    ...
    go outside

The "go outside" is printed because the two conditions are both True.

<section>
<a id="bool-4" class="custom_anchor"></a>
<a style="color: black;" href="#bool-4"><h3>4. Boolean example 4</h3></a>
</section>

Here is an example "not" operator acting on the bool type. We will use "not" to convert the value to the opposite value.

    >>> is_good = True
    >>> is_good = not is_good
    >>> print(is_good)
    False

The "is_good" is initialized to True. To make it False, we use "not is_good".