3

1ST DATATYPE: NUMBERS

by

The Education Machine

[support@theeducationmachine.com]

Numbers Datatype


Numbers in python can be Integers, Float or Complex. We will see each of these number datatypes in detail in this tutorial.

Integers

Integers are the real numbers on number line which starts from -infinity to +infinity.

Let us focus on the syntax to declare a integer variable.

In the following code, num variable is created and 10 is the value assigned to variable.
In next line, print() function is used to print the output and type() function is used inside the print() function to print the datatype of variable.

  num = 10
  print(num, type(num))


Output:
10 <class 'int'>

Arguments/Parameters - The number of variables listed inside the parentheses while calling the function.
In the below code, 10 value is assigned to 3 variables - x, y and z. Then the values of x, y and z are printed using print() function. But while printing the datatype of x, y and z : an error has occurred because type() function can take only one argument however print() can take multiple arguments.

  x = y = z = 10
  print(x,y,z)
  print(type(x,y,z))


Output:
reserved_keywords

Below code can be used to print the datatype of multiple variables.

  print(type(x), type(y), type(z))


Output:
<class 'int'> <class 'int'> <class 'int'>

The following code is used to assign the same tuple() to 3 variables.
Tuple is a datatype, which is explained briefly further.

  x = y = z = 10,20,30
  print(x,y,z)
  print(type(x),type(y),type(z))


Output:
(10, 20, 30) (10, 20, 30) (10, 20, 30)
  <class 'tuple'> <class 'tuple'> <class 'tuple'>

To store individual values to variables

  x, y, z = 10,20,30
  print(x,y,z)
  print(type(x),type(y),type(z))


Output:
10 20 30
  <class 'int'> <class 'int'> <class 'int'>

To store integer and string values to variables.

  empl_id, name = 315, '315'
  print(empl_id, type(empl_id), name, type(name))


Output:
315 <class 'int'> 315 <class 'str'>

Float

All the numbers which are in the form decimal, fraction or rational numbers are 'Float Numbers'.

The datatype of float numbers will also be 'float'.

  num = 10.5
  print(num, type(num))


Output:
10.5 <class 'float'>

To check the memory allocation of float datatype using id() function.

  num = 10.5
  num1 = 10.5
  print(num, type(num), id(num))
  print(num1, type(num1), id(num1))


Output:
10.5 <class 'float'> 2587882019856
10.5 <class 'float'> 2587882020272

Complex

Complex numbers are the combination of real numbers and imaginary numbers.

In python, the imaginary number is always represented by 'j' or 'J' character.

As shown below:

  num = 10.5 + 3j
  print(num, type(num))


Output:
(10.5+3j) <class 'complex'>

As we learnt that variable names are case-sensitive, let's see with an example below:
We can see below that all 4 variables are indeed different that is why they have different memory addresses.

  num = 300
  Num = 300
  nUm = 300
  NUM = 300
  print(id(num), id(Num), id(nUm), id(NUM))


Output:
2587882422352 2587882422672 2587882422736 2587882423120

*Beautify your statement using print() function.

  age = 10
  empl_id = 1520
  print("Please see my details below: ")
  print("Age:",age)
  print("Employee ID:",empl_id)


Output:
Please see my details below:
Age: 10
Employee ID: 1520

Variable's Identity/Memory Allocation

Every variable/object is assigned to a specific number in memory to identify the variables uniquely.

Alternatively, we can call it as an address of the variable.

Using python's inbuilt id() function, we can find the object's identifier.

To print the variable, its datatype and id.

  num = 15
  print(num, type(num), id(num))


Output:
15 <class 'int'> 140712700401760

The IDs of x & y will be different in this case.

  x = 500
  y = 500
  print(x, id(x))
  print(y, id(y))


Output:
500 2332925956784
500 2332925956976

The IDs of x & y will be same in this case.

  x = 200
  y = 200
  print(x, id(x))
  print(y, id(y)))


Output:
200 140712700407680
200 140712700407680

Why the IDs are same in some cases and different in other cases?
In python, variables containing same integer values between -5 to +256 get same address in memory. It means all those variables will be referencing to a single address in memory. This saves memory upto some extent.

  num1 = 162
  num2 = 162
  num3 = -6
  num4 = -6
  print(num1, id(num1))
  print(num2, id(num2))
  print(num3, id(num3))
  print(num4, id(num4))


Output:
162 140712700406464
162 140712700406464
-6 2332925957648
-6 2332925957680

Here, x is assigned the value: 500 and y = x (value of x is referred to value of variable y).
The ID of x & y should have been different as 500 is greater than 256.
In this case, the IDs are same because y is not assigned with the same value instead it is just referred to the variable x.

  x = 500
  y = x


Output:
500 2332925957136
500 2332925957136