=

Python and Data Types

Table of Contents

    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.
    Code python
    name = "John"   # variable is : name , value is : John
    age = 11   # variable is : age , value is : 11
    Variable Table
    VariableAddress
    name0x1cafd5bda10
    age0x7ffc0e6dfb38

    (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.

    1. Primitive
      • Integer (int)
      • Floating point (float)
      • Boolean (bool)
      • String (str)
      • None (NoneType)
    2. Sequence
      • List (list)
      • Tuple (tuple)
      • Range (range)
    3. Set
      • Set (set)
      • FrozenSet (frozenset)
    4. Mapping
      • Dictonary (dict)
    5. 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.

    Integer python
    a = 5  #integer
    b = a   # integer
    c = 5   # integer
    d = 6   # integer
    Let's go some deeper / low level (for my curiosity of-course) :
    • 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
    VariableAddress
    a0x7ffc0e6dfb78
    b0x7ffc0e6dfb78
    c0x7ffc0e6dfb78
    c0x7ffc0e6e0338
    Code c
    struct _longobject {
        PyObject_VAR_HEAD
        digit ob_digit[5];  // Stores integer value
    };
    Memory Representation (High-Level)
    AddressContent
    0x7ffc0e6dfb78[ PyLongObject (5) ]
    0x7ffc0e6dfb79ob_refcnt = 3 (a, b, c)
    0x7ffc0e6dfb7Aob_type = &PyLong_Type
    0x7ffc0e6dfb7Bob_digit = 5
    -----------------
    0x7ffc0e6e0338[ PyLongObject (6) ]
    0x7ffc0e6e0339ob_refcnt = 1 (d)
    0x7ffc0e6e033Aob_type = &PyLong_Type
    0x7ffc0e6e033Bob_digit = 6
    (For memory optimization, Python caches small integers (-5 to 256), So in above code variable c shares same object in memory as a,b.)
    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.

    Float pytohn
    area = 5.1      # float
    rate = -1.88    # float

    Boolean

    Boolean represent two values 0,1 also known as True False.

    Boolean pytohn
    value1 = True   #bool
    v2 = False      # bool

    String

    String represent the unicode characters in python. Like : "John" 'Blob' 'F' etc.

    String python
    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.

    NoneType python
    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.

    List python
    name_list = ["Soccar","Football","Badminton"]

    Tuple

    Tuple is also a ordered collection of elements, but it is immutable. Initilize with parantheses ()

    (If you have any type of query / Question / suggestion .. feel free to ask below. We would be happy to connect you. Have a great day buddy!!)