Data, Variables, Types,type(), id(), Garbage Blocks
Data can have different formats, and program should be able to work with them. Every software works on real data and perform some calculation.
Variables and data types
Variable provides a way to hold a data during execution. In C/C++ language variables are declared but in python we don't need to declare variable in python as it is dynamic programming. C/C++ language is statically typed.
Variable means something that can change, constant is static and cannot be changed. 💁
del variable_name.
if a variable is not defined and we perform some operation other than assignment we get name error.
Data Types
In python any data type can be assigned to a variable
x = 3
x = 3.14
x = "Hari "
All these are acceptable, that means there is no specific data type tagged to a variable.
How is this achieved?
In python every datatype is created using class (Define data category)
Say to store a integer value we can define a datatype using Integer class (Predefined/ Built-ins).
Every data is represented in the form of object of particular class in python.
Example -> x = 5 => here data 5 is as object of Integer class.
To validate we can use type () function
type(x)
<class int>
<class float>
<class str>
Interesting fact:
a = 5
this means a block of name 'a' is created and object of int class is created which hold value '5' and
'a' refer to that block. So, 'a' is a pointer variable which holds address or id of block.
i.e 'a' is referring to object which is holding value a.
Now, we can assign another datatype to same variable.
a = 3.5
now, here 3.5 is stored in object of class float and a will refer to the newly created float object.
similarly the same variable can hold str type data type and can be assessed. The address of newly allocated block will be visible. So, this concept is created using pointer.
In short Python concept is extended from C.
In python we say 'a' is called ID.
In C we can store any variable using void pointer as such, so dynamic programming can be achieved in C as well. Same pointer can point to any variable.
void *p.
p = malloc(sizeof(int)).
p = (float*) malloc(variable_size)
Same pointer p can point to any datatype. This can be achieved in C but not done explicitly.
Concept of ID
ID is address similar to pointer in C and no any computation is allowed in python for variable id.
ie. a = 5 ; ==> lets say address is 102356
then if we do a+1 , it would result 6 not the addition of 1 in address
We can check the address of a variable using id() method in python to compare addresses and perform some other logical operations.
Un-referred element which is not referred by any reference variable is called Garbage Block.
In C/C++ we need to use free/delete keyword to delete memory created using DMA but in Python it is done by program automatically. So memory leak doesn't happen in python.
How does it works ? - We will cover the details in next section ( Memory manager, name space, heap space etc.)
Type() - Function to check datatype
In Python data type is an instance of builtin classes.
Comments
Post a Comment