Numbers in python can be Integers, Float or Complex. We will see each of these number datatypes in detail in this tutorial.
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.
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.
Below code can be used to print the datatype of multiple variables.
The following code is used to assign the same tuple() to 3 variables.
Tuple is a datatype, which is explained briefly further.
To store individual values to variables
To store integer and string values to variables.
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'.
To check the memory allocation of float datatype using id() function.
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:
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.
*Beautify your statement using print() function.
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.
The IDs of x & y will be different in this case.
The IDs of x & y will be same in this case.
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.
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.