Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Data Types


Data Structure

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.


Data Types

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 a Dynamically Typed Language

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.

Example
number = 100
name = "John"

print(number)
print(name)
100
John

Get the Data Type of a Variable

You can check the data type of a variable by using the type() function in Python.

Example
number = 100
name = "John"

print(type(number))
print(type(name))
<'class' 'int'>
<'class' 'str'>

Set the Data Type

Declare the variable and assign a value to it to set the data type.

Example
# 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

Set a Specific Data Type

Constructor functions use to set the specific data types for variables.

Example
# 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