DEV Community

AnkurRanpariya2005
AnkurRanpariya2005

Posted on • Updated on • Originally published at frontendscript.com

Python Integer Numeric Data Type

Table of Contents

A. Introduction
B. Numeric data type
C. Integer
D. Operators
1. Arithmetic Operators
2. Comparison operators
3. Bitwise operators
E. Class int() Constructor
F. Built-in function bin()
G. Integer Example Application
H. Boolean
I. Class bool() Constructor
1. Boolean example 1
2. Boolean example 2
3. Boolean example 3
4. Boolean example 4


A. Introduction

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 integer and its subtype bool, they belong to the numeric type category.


B. Numeric data type

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

Integer
Float
Complex
Fraction
Decimal
Enter fullscreen mode Exit fullscreen mode


C. Integer

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

100
-20
0
Enter fullscreen mode Exit fullscreen mode

Python's integer maximum value is more than that of a float. Note the maximum value of a float is 1.7976931348623157e+308.

>>> import sys
>>> int(sys.float_info.max)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
Enter fullscreen mode Exit fullscreen mode

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


D. Operators

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

Comparison operators.

image

Bitwise operators.

image

Boolean operators.

image


1. Arithmetic Operators

Addition and subtraction operators

Start your Python interpreter 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
Enter fullscreen mode Exit fullscreen mode

Multiplication and Division operators

>>> 100 // 2
50
>>> 4*3
12
Enter fullscreen mode Exit fullscreen mode

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

Python has 2 division operators.

/
//
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Remainder

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

>>> 10 % 3
1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode


2. Comparison operators

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
Enter fullscreen mode Exit fullscreen mode

Greater than or equal to ">=".

>>> a = 45
>>> b = 50
>>> a >= b
False
Enter fullscreen mode Exit fullscreen mode

Not equal.

>>> 100 != 200
True
Enter fullscreen mode Exit fullscreen mode


3. Bitwise operators

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


E. Class int() Constructor

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
Enter fullscreen mode Exit fullscreen mode
  • 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.

Tip
The built-in function type() returns the type of an object. Example, type(10) will return .


F. Built-in function bin()

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

>>> num = 4
>>> num_binary = bin(num)
>>> print(num_binary)
'0b100'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode


G. Integer Example Application

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))
Enter fullscreen mode Exit fullscreen mode

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

execute the code

c:\>python sample_1.py
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode


H. Boolean

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.


I. Class bool() Constructor

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'>
Enter fullscreen mode Exit fullscreen mode

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


1. Boolean example 1

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.
Enter fullscreen mode Exit fullscreen mode


2. Boolean example 2

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.


3. Boolean example 3

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.


4. Boolean example 4

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)