Numbers

Python Numbers

A programming language needs to have support for numbers to carry out calculations. To do this in Python, the numbers are categorized into different data-types.

In this tutorial, you will look at how numbers work in Python, how to assign them to variables, and how to do basic operations using those variables.

Numbers:

What are numbers in Python? All numbers that are handled will belong to some type. A type is a way to define various data structures and containers and define the functionality associated with them. Types are implemented in Python as classes. If you want to verify if an integer belongs to the class int you can use isinstance. You will need to pass number as the first argument and class name as the second argument, and it will check if the object is an instance of the class. This is done by the following construct.

isinstance(object, class)

This checks if the object belongs to the class. We will throw more light on objects and how they belong to a class, or “object instantiation”, in the objects and class tutorial. For now, just remember that an integer number is categorized into int class and hence if you check whether the number 2 is an instance of the class int, the interpreter will return True.

>>> isinstance(2, int)
True

Numeric types and operations using numbers There are three numeric types in Python: int for integers, float for decimal numbers, and complex for complex numbers. General mathematical operations such as addition, subtraction, and multiplication can be done on them.

Operations when the type are the same: When the type of the input numbers are the same the result will be a number of the same class that the input numbers belong. This is true except the case of division as will be shown in the following examples.

For example, you can assign the integers 2 and 3 to variables int_var and int_var1and then print the sum of the two.

>>> # assign the integer 2 to a var int_var
>>>  int_var = 2

>>> # assign the integer 3 to a var int_var1
>>>  int_var1 = 3

>>> # print the sum of the two variables int_var and int_var1
>>>  print(int_var + int_var1)
5

Similarly, assign two decimal numbers to two variables, say, float_var1 and float_var2, and print the sum of the two.

>>> # assign a decimal number to a variable float_var
>>>  float_var = 5.6

>>> # assign another decimal number to a variable float_var1
>>>  float_var1 = 6.7

>>> # print the sum of the two decimal numbers
>>>  print(float_var + float_var1)
12.3

You can finally check if the same operation can be done on complex numbers. Assign two complex numbers to two variables. say, complex_var and complex_var1 and print the sum of the two.

>>> # assign a complex number to a variable complex_var
>>>  complex_var = (1 + 2j)

>>> # assign another complex number to a variable complex_var1
>>>  complex_var1 = (2 + 3j)

>>> # print the sum of the two complex numbers
>>>  print(complex_var + complex_var1)
(3+5j)

In case of division, dividing two integers will always result in a float.

>>> # divide two integers and assign it to a variable result
>>> result = 3 / 2

>>> # print the result
>>> print(result)

1.5

>>> # check if the result belongs to the class float
>>> print(isinstance(result, float))

True

Operations between numbers of different types: The operations work when operate of variables that are of different numeric types as well.

For example you can add two numbers, one with class int and another with class float. The result will be in float. This is done by changing the integer to a float and then adding them.

>>> # add two numbers, one is int and the other is float
>>> result = 2 + 5.1

>>> # print the result, note that the result is a float
>>> print(result)
7.1

>>> # check that the result is an instance of float
>>> print(isinstance(result, float))
True

Other operations like adding int to complex or float to complex works in similar manner. The results of both the operations belong to the complex class.

>>> # add two numbers belonging to complex and int class
>>> result = (2 + 3j) + 4

>>> # print the result
>>> print(result)
(6+3j)

>>> # check if the result is of class complex
>>> print(isinstance(result, complex))
True

>>> # add two numbers belonging to complex and float class
>>> result = (2 + 3j) + 4.12

>>> # print the result
>>> print(result)
(6.12+3j)

>>> # check if the result is of class complex
>>> print(isinstance(result, complex))
True

Similarly, other operators are also supported. You can check more on operators in our basic programming tutorials.

Contributed by: Joydeep Bhattacharjee
Notifications
View All Notifications

?