Python and Data Types
Variable
A Variable in Python is a reference (or name) that points to object in memory. Python doesn't store values directly into variable, instead it stores that value as object in memory and variable as reference.
- Python doesn't need any keyword (like: let,var,const in other languages)
- It also doesn't need to tell the DataType explicitly.
name = "John" # variable is : name , value is : John
age = 11 # variable is : age , value is : 11
Variable Table | |
---|---|
Variable | Address |
name | 0x1cafd5bda10 |
age | 0x7ffc0e6dfb38 |
(Variable initialization ⟶ Variable table ⟶ Memory (object))
Data Type
A data types defines the kind of data that a variable can store. It makes easy to perform specific operation on variables. Python have various built-in data type, some of them are classified as below.
- Primitive
- Integer (int)
- Floating point (float)
- Boolean (bool)
- String (str)
- None (NoneType)
- Sequence
- List (list)
- Tuple (tuple)
- Range (range)
- Set
- Set (set)
- FrozenSet (frozenset)
- Mapping
- Dictonary (dict)
- Binary
- Bytes (bytes)
- Bytearray (bytearray)
- Memoryview (memoryview)
Integer
Integer data types represents the whole numbers. like: 5 77 -84 -1etc. There are no limit to store a big integer into a variable, it depend's upon your availabe memory.
a = 5 #integer
b = a # integer
c = 5 # integer
d = 6 # integer
- Both variables and there reference are stored into a table (object)
- Reference address represent the actual address of object in memory where actual values (object) is.
Variable Table | |
---|---|
Variable | Address |
a | 0x7ffc0e6dfb78 |
b | 0x7ffc0e6dfb78 |
c | 0x7ffc0e6dfb78 |
c | 0x7ffc0e6e0338 |
struct _longobject {
PyObject_VAR_HEAD
digit ob_digit[5]; // Stores integer value
};
Memory Representation (High-Level) | |
---|---|
Address | Content |
0x7ffc0e6dfb78 | [ PyLongObject (5) ] |
0x7ffc0e6dfb79 | ob_refcnt = 3 (a, b, c) |
0x7ffc0e6dfb7A | ob_type = &PyLong_Type |
0x7ffc0e6dfb7B | ob_digit = 5 |
----------------- | |
0x7ffc0e6e0338 | [ PyLongObject (6) ] |
0x7ffc0e6e0339 | ob_refcnt = 1 (d) |
0x7ffc0e6e033A | ob_type = &PyLong_Type |
0x7ffc0e6e033B | ob_digit = 6 |
id() is used to get the reference of object in memory.
Floating point
It is used to represent the decimal numbers. like: 1.1 5.4 .22 -.5 etc.
area = 5.1 # float
rate = -1.88 # float
Boolean
Boolean represent two values 0,1 also known as True False.
value1 = True #bool
v2 = False # bool
String
String represent the unicode characters in python. Like : "John" 'Blob' 'F' etc.
name = "Alice"
n2 = 'Blob'
n3 = "F"
None
It represent nothing, loll. Actually none can also be usefull in programming. A NoneType have only a single instance in memory and every initialization refer to the same adress.
x = None # NoneType
List
List is a collection of elements. List is mutable and keep its elements in a order. It is created using squre bracekets.
name_list = ["Soccar","Football","Badminton"]
Tuple
Tuple is also a ordered collection of elements, but it is immutable. Initilize with parantheses ()