A Data structure is a collection of data of different data types. The data collection can be represented with an object and used throughout the program.
It is crucial to organize, manage, and store data so that it can be accessed easily and modified more efficiently.
With the help of a data structure, you can organize your data to store a collection of data, relate them, and perform operations on them.
A data types are variables to which only a given type of value can be assigned, and this value can be used throughout the program.
NoneType | None |
Logical | bool |
Numeric | int , float , complex |
Sequence | list ,
tuple ,
range |
Text | str |
Binary | bytes , bytearray , memoryview |
Map | dict |
Set | set ,
frozenset |
Python is dynamically typed, which means variables' data types are checked during execution.
It is not necessary to declare a data type for a variable; you can only assign a value to the variable.
number = 100
name = "John"
print(number)
print(name)
100
John
You can check the data type of a variable by using the type()
function in Python.
number = 100
name = "John"
print(type(number))
print(type(name))
<'class' 'int'>
<'class' 'str'>
Declare the variable and assign a value to it to set the data type.
# Code
# Data Type
text = "Text"
# str
number = 100
# int
complext_number = 100j
# complex
colors_list = ["Red", "Blue", "Green"]
# list
colors_tuple = ("Red", "Blue", "Green")
# tuple
numbers_range = range(10)
# range
colors_set = {"Red", "Blue", "Green"}
# set
colors_frozenset = frozenset({"Red", "Blue", "Green"})
# frozenset
company_dictionary = {"name": "Google", "country": "USA"}
# dict
boolean = True
# bool
bytes_sequence = b"Text"
# bytes
bytearray_sequence = bytearray(10)
# bytearray
memoryview_sequence = memoryview(bytes(10))
# memoryview
none_type = None
# NoneType
Constructor functions use to set the specific data types for variables.
# Code
# Data Type
text = str("Text")
# str
number = int(100)
# int
complext_number = complex(100j)
# complex
colors_list = list(["Red", "Blue", "Green"])
# list
colors_tuple = tuple(("Red", "Blue", "Green"))
# tuple
numbers_range = range(10)
# range
colors_set = set(("Red", "Blue", "Green"))
# set
colors_frozenset = frozenset({"Red", "Blue", "Green"})
# frozenset
company_dictionary = dict({"name": "Google", "country": "USA"})
# dict
boolean = bool(1)
# bool
bytes_sequence = bytes(5)
# bytes
bytearray_sequence = bytearray(10)
# bytearray
memoryview_sequence = memoryview(bytes(10))
# memoryview