Numbers in Python programming language are similar to other programming languages such as C and its derivatives, javascript, java etc. Numbers can be inputted to the console and acted upon using operators like in Matlab programming language to see immediate output. No compile time is required as they are high level interpreted language. In Python programming language there are different number types- integers, floating points, complex number, decimals with fixed precision, rationals and sets. Morever, binary, octal, hexadecimals are also numbers.

Number as Objects and their classes

In Python programming language numbers are objects in comparison to C/C++ where they are just data types. Numbers objects like integer and floating point belongs to integer and floating point classes. We can get the class information by using type( ) function.

For example,

x = 4

gives you an output that indicates that x is an integer class or belongs to integer class. Similarly goes for floating point numbers:

y = 4.5

and you will get the type as float class.

Basic Data Types

As mentioned before numbers in python can be ground into the following basic data type objects-
  • Integers
  • Floating point numbers
  • Octal, Hexadecimal, Binary literals
  • Complex  Numbers
  • Set
  • Decimal, Fraction
  • Boolean
 These are basic data types or number objects in the sense that Python provides these primitive notion of number for you to use. You can also define your own numbers using object and class notion(maybe of different base system!). But for the most part, these basic data types are sufficient enough.

Other then these, in python, there are also other basic number type objects-
  • build-in function and modules like round, math, random
  • vectors, plotting objects and other visualization objects

Numbers cannot do anything by themselves meaning we require operators. Operators like +, -, *, / have their usual meaning.

>>> 4+5
>>>4 *2

The last one being the exponentiation operator.

And this operators also applies to floating point numbers. However, there is caution when we deal with floating point numbers like in other programming language. It deals with the precision of representing floating point number which happens to depend on the memory size that can be assigned to the numbers. Floating point numbers in python are implemented  as C double. On the other hand and in contrast to other programming language, integer has unlimited precision meaning that integers can be represented by as many digits as the memory allows to do so. Also, in earlier python version 2, there was two types of integers- short and long. Now in python version 3 they have been merged together.

Binary, Octal and Hexadecimal Numbers

While the aforementioned paragraph on integer we talked about normal integer of base 10, it is possible to code integer in other base format- binary, octal and hexadecimal. To write binary, octal and hexadecimal numbers, first type 0(zero) followed by either b(or B), x(or X) or o(or O), each letter representing the corresponding base.

See the following picture coded in Python IDLE software:

 In the figure, you can see that they are integer numbers as was mentioned before.

Number Data type Conversions

To convert ordinary base 10 integer to binary, hexadecimal or octal you can use, bin(x), oct(x) and hex(x) respectively, where x is an integer. And conversely, to convert binary, hexadecimal or octal back to integer you can use int(y,base) where y is either binary, hexadecimal or octal string.

Following shows this in Pycharm/Anaconda IDE software:

 Complex number is written as a+bj(or a-bj) format or using build in function complex(x,y) where x is the real part and y is the imaginary part. Here numbers are treated internally as pair of floating points. See the complex number implementation in python below:

Operators and Expression

The mathematical operators in Python are the usual operators encountered in other programming language.  + is addition, - is subtraction, * is multiplication, / is division, % is remainder division, & is bit-wise AND, | is bit-wise operator and so on. It is impossible to show them all by examples, so they are listed as shown in the figure below.

 But some points should be noted here. // is called floor division.

/ is true division that truncates integer and keeps floating point.

>>> 4/3
>>> 1.3333333333333333

// is floor division that truncates fractional remainder

>>> 4//3
>>> 1

Decimal and Fraction basic type objects

Decimal and Fraction are other basic core data type objects. They are used in order to cope with evaluation of expression dealing with the problem of precision.

For example, what is the output of the following expression?

.>>> 0.1+0.2+0.3-0.6

You expect the answer to be zero. Wrong, the answer is,


Although close to Zero, it is not zero.


In order to deal with such kind of problems of inaccuracy, Python implements Decimal objects with functions. The above expression can be dealt with Decimal as follows.

>>> from decimal import Decimal
>>> Decimal('0.1')+Decimal('0.2')+Decimal('0.3')-Decimal('0.6')

In other words, Decimal converts a floating point number to truncated version of floating point number. In order to use it, you need to first import the Decimal method/object from the decimal module as shown in the above code.

There are also other functions within the decimal module other than Decimal. For example getcontext( ) function can be applied to set the precision to some decimal points.

>>> import decimal
>>> decimal.getcontext().prec = 5
>>> decimal.Decimal(3)/decimal.Decimal(9)


Another numeric type is Fraction. Like Decimal, Fraction has to be imported from fraction module and is used to deal with numerical inaccuracies or problem encountered in expression of floating point numbers.

>>> from fractions import Fraction
>>> x = Fraction(1, 3)
>>> x
Fraction(1, 3)

Fraction simplifies to Fraction with greatest common division:

>>> y = Fraction(4, 6)
>>> y
Fraction(2, 3)

Using Fraction we can print the fraction form format.

>>> print(y)

Fraction converts floating point number to fractional form:

>>> Fraction('1.25')
Fraction(5, 4)

Chained Comparison

 Another noteworthy explanation is for the chained comparison. Chained comparison allows you to compare magnitude of different numbers which return value is either false or true(Boolean).

>>> x = 4
>>> y=3
>>> z =5
>>> x < z < y
>>> False


 Sets in Python are the sets you have studied in your college- a collection of unordered unique and immutable objects that supports mathematical operations from Set Theory.

Set can be expanded or shrinked and contain different data types objects(strings, numbers, list, dictionaries etc). Unlike list and dictionary there is no mapping of index or keys to the elements of the set. Hence Set is not a sequence(list) or a map(dictionary). But they have other similar operational properties of list and dictionary.

Here is how you declare a set and perform different mathematical operations:

>>> x = set('dfgeergs')
>>> x
{'r', 'f', 'd', 's', 'g', 'e'}
>>> y=set('ljknsdfhwerndsxse')
>>> y
{'r', 'f', 'h', 'd', 'w', 'x', 'n', 'l', 's', 'k', 'j', 'e'}

>>> x - y

>>> x | y
{'h', 'd', 's', 'k', 'r', 'f', 'w', 'x', 'n', 'g', 'l', 'j', 'e'}

>>> x & y
{'s', 'r', 'f', 'd', 'e'}

Symmetric Difference(XOR)
>>> x ^ y
{'h', 'w', 'x', 'n', 'l', 'g', 'k', 'j'}

>>> x > y

>>> x < y

>>> 'p' in x
>>> set('pq') in x

Add member to set:

>>> x.add('t')
>>> x
{'r', 't', 'f', 'd', 's', 'g', 'e'}

Remove member from set:

>>> x
{'r', 't', 'f', 'd', 's', 'g', 'e'}
>>> x.remove('e')
>>> x
{'r', 't', 'f', 'd', 's', 'g'}


Boolean is another numeric data types in Python. It has two values- True or False. Internally they are processed as 1 or 0 so they are numbers. They belong to class bool which is subclass of class integer. Use the type( ) function to see the class type of True and False.

>>> type(True)
<class 'bool'>

Use the instance( ) function to see whether True and False are instance(object) of class integer.

>>> isinstance(True, int)

The followings are some pitfalls to be noted.

>>> True == 1
>>> True is 1
>>> True or False
>>> True + 4

Build-in numeric tools and Math Modules

Python has number of build in numeric functions to facilitate number processing such as pow, abs, min/max and others. Beside these build in functions you can also import the math module or math library. The examples are shown below.

This completes Numbers in Python programming language. The next tutorials will be on Strings in Python programming language starting with Writing Strings in Python Programming Language .


Post a Comment