DATATYPES



Python - Datatypes


Data types are the classification or categorization of data items. Data types represent a kind of value which determines what operations can be performed on that data. Numeric, non-numeric and Boolean (true/false) data are the most used data types.


Built-in Data Types :


In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:


  • TEXT TYPE : str

  • NUMERIC TYPE : int, float, complex

  • SEQUENCE TYPE : list, tuple, range

  • MAPPING TYPE : dict

  • SET TYPE : set, frozenset

  • BOOLEAN TYPE : bool

  • BINARY TYPE : bytes, bytearray, memoryview


    Getting the Data Type :



    You can get the data type of any object by using the type() function.


    x = 5.5
    print(type(x))
    
    =====O/P=====
    
    <'class float'>
    

    Setting the Data Type :

    In Python, the data type is set when you assign a value to a variable:


    x=5  # int
    print(x)
    print(type(x))
    x="apple" # string
    print(x)
    print(type(x))
    x="a"  # string
    print(x)
    print(type(x))
    x=1.5  # float
    print(x)
    print(type(x))
    x=1+5j  # complex
    print(x)
    print(type(x))
    x=["apple","banana","mango"]  # list
    print(x)
    print(type(x))
    x=("apple","banana","mango")  # tuple
    print(x)
    print(type(x))
    x={"apple","banana","mango"}  # set
    print(x)
    print(type(x))
    x={"age":20,"name":"sanvi"}   # dictionary
    print(x)
    print(type(x))
    x=range(6)   # range
    print(x)
    print(type(x))
    x=frozenset({"apple","banana","mango"})   # frozenset
    print(x)
    print(type(x))
    x=bytearray(6)  #bytearray
    print(x)
    print(type(x))
    x= True       # bool 
    print(x)
    print(type(x))
    x=b"hello"    # bytes     
    print(x)
    print(type(x))
    x=memoryview(bytes(5))  # memoryview
    print(x)
    print(type(x))
    
    ========O/P==========
    5
    <class 'int'>
    apple
    <class 'str'>
    a
    <class 'str'>
    1.5
    <class 'float'>
    (1+5j)
    <class 'complex'>
    ['apple', 'banana', 'mango']
    <class 'list'>
    ('apple', 'banana', 'mango')
    <class 'tuple'>
    {'mango', 'banana', 'apple'}
    <class 'set'>
    {'age': 20, 'name': 'sanvi'}
    <class 'dict'>
    range(0, 6)
    <class 'range'>
    frozenset({'mango', 'banana', 'apple'})
    <class 'frozenset'>
    bytearray(b'\x00\x00\x00\x00\x00\x00')
    <class 'bytearray'>
    True
    <class 'bool'>
    b'hello'
    <class 'bytes'>
    <memory at 0x000001D3E6E7F7C8>
    <class 'memoryview'>
    

    Setting the Specific Data Type :


    If you want to specify the data type, you can use the following constructor functions.


    x=str("hello world")
    print(type(x))
    x=int(225)
    print(type(x))
    x=float(2.5)
    print(type(x))
    x=complex(4j)
    print(type(x))
    x = list(("appy", "bannu", "cherry"))
    print(type(x))
    x = tuple(("kushi", "happy", "cherry"))
    print(type(x))
    x = range(6)
    print(type(x))
    x = dict(name="John", age=36)
    print(type(x))
    x = set(("appie", "bunna", "berry"))
    print(type(x))
    x = frozenset(("seeta", "bunny", "cherry"))
    print(type(x))
    x = bool(5)
    print(type(x))
    x = bytes(5)
    print(type(x))
    x = bytearray(5)
    print(type(x))
    x = memoryview(bytes(5))
    print(type(x))
    
    
    ========O/P=======
    
    <class 'str'>
    <class 'int'>
    <class 'float'>
    <class 'complex'>
    <class 'list'>
    <class 'tuple'>
    <class 'range'>
    <class 'dict'>
    <class 'set'>
    <class 'frozenset'>
    <class 'bool'>
    <class 'bytes'>
    <class 'bytearray'>
    <class 'memoryview'>
    


    RegEx SETS

    Python - RegEx SETS

    posted on 2019-11-12 23:06:24 - Python Tutorials


    RegEx_Functions

    Python - RegEx_Functions

    posted on 2019-11-09 06:07:29 - Python Tutorials


    RegEx_Sets

    Python - RegEx_Sets

    posted on 2019-11-09 05:30:54 - Python Tutorials


    Prompt Examples

    ChatGPT Prompt Examples

    posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


    Use Cases

    Chat GPT Key Use Cases

    posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


    Prompt Frameworks

    Prompt Frameworks

    posted on 2023-06-21 19:33:06 - ChatGPT Tutorials