Data-types in python
Introductionβ
In programming, the concept of data types is fundamental. Variables can store data of different types, and different types can perform different operations. Python provides a rich set of built-in data types, each designed for specific purposes. This document explores the various built-in data types in Python, providing detailed descriptions and examples for each type.
Built-in Data Typesβ
Python has the following built-in data types, categorized as follows:
- Text Type:
str
- Numeric Types:
int
,float
,complex
- Sequence Types:
list
,tuple
,range
- Mapping Type:
dict
- Set Types:
set
,frozenset
- Boolean Type:
bool
- Binary Types:
bytes
,bytearray
,memoryview
- None Type:
NoneType
Getting the Data Typeβ
You can get the data type of any object by using the type()
function:
x = 5
print(type(x))
Setting the Data Typeβ
In Python, the data type is set when you assign a value to a variable:
x = "Hello World" # str
x = 20 # int
x = 20.5 # float
x = 1j # complex
x = ["apple", "banana", "cherry"] # list
x = ("apple", "banana", "cherry") # tuple
x = range(6) # range
x = {"name" : "John", "age" : 36} # dict
x = {"apple", "banana", "cherry"} # set
x = frozenset({"apple", "banana", "cherry"}) # frozenset
x = True # bool
x = b"Hello" # bytes
x = bytearray(5) # bytearray
x = memoryview(bytes(5)) # memoryview
x = None # NoneType
Detailed Description of Data Typesβ
Text Typeβ
str
β
A str
(string) is a sequence of characters enclosed in quotes. Strings can be created using single, double, or triple quotes. They support various methods for manipulation and querying.
Example:
x = "Hello, World!"
print(type(x)) # <class 'str'>
Numeric Typesβ
int
β
An int
(integer) is a whole number, positive or negative, without decimals, of unlimited length.
Example:
x = 20
print(type(x)) # <class 'int'>
float
β
A float
is a number, positive or negative, containing one or more decimals.
Example:
x = 20.5
print(type(x)) # <class 'float'>
complex
β
A complex
number is a number with a real and an imaginary part, denoted as x + yj
.
Example:
x = 1j
print(type(x)) # <class 'complex'>
Sequence Typesβ
list
β
A list
is an ordered collection of items which can be of different types. Lists are mutable, meaning their elements can be changed.
Example:
x = ["apple", "banana", "cherry"]
print(type(x)) # <class 'list'>
tuple
β
A tuple
is similar to a list, but it is immutable, meaning its elements cannot be changed after creation.
Example:
x = ("apple", "banana", "cherry")
print(type(x)) # <class 'tuple'>
range
β
A range
represents a sequence of numbers, and is commonly used for looping a specific number of times in for loops.
Example:
x = range(6)
print(type(x)) # <class 'range'>
Mapping Typeβ
dict
β
A dict
(dictionary) is a collection of key-value pairs, where each key is unique and immutable, and values can be of any type.
Example:
x = {"name": "John", "age": 36}
print(type(x)) # <class 'dict'>
Set Typesβ
set
β
A set
is an unordered collection of unique items. Sets are mutable and support operations like union, intersection, and difference.
Example:
x = {"apple", "banana", "cherry"}
print(type(x)) # <class 'set'>
frozenset
β
A frozenset
is an immutable version of a set. Once created, elements cannot be added or removed.
Example:
x = frozenset({"apple", "banana", "cherry"})
print(type(x)) # <class 'frozenset'>
Boolean Typeβ
bool
β
A bool
represents one of two values: True
or False
.
Example:
x = True
print(type(x)) # <class 'bool'>
Binary Typesβ
bytes
β
A bytes
object is an immutable sequence of bytes.
Example:
x = b"Hello"
print(type(x)) # <class 'bytes'>
bytearray
β
A bytearray
is a mutable sequence of bytes.
Example:
x = bytearray(5)
print(type(x)) # <class 'bytearray'>
memoryview
β
A memoryview
object allows Python code to access the internal data of an object that supports the buffer protocol without copying.
Example:
x = memoryview(bytes(5))
print(type(x)) # <class 'memoryview'>
None Typeβ
NoneType
β
NoneType
is the type of the None
object, which represents the absence of a value.
Example:
x = None
print(type(x)) # <class 'NoneType'>
Additional Informationβ
Mutable vs Immutable Typesβ
In Python, data types can be categorized as mutable or immutable. Mutable types allow modification after creation, while immutable types do not.
- Mutable Types:
list
,dict
,set
,bytearray
- Immutable Types:
str
,int
,float
,tuple
,frozenset
,bytes
,complex
,bool
,NoneType
Type Conversionβ
Python provides several built-in functions to convert between data types:
int()
: Converts to an integerfloat()
: Converts to a floatstr()
: Converts to a stringlist()
: Converts to a listtuple()
: Converts to a tupleset()
: Converts to a setdict()
: Converts to a dictionaryfrozenset()
: Converts to a frozensetbool()
: Converts to a booleanbytes()
: Converts to bytesbytearray()
: Converts to a bytearray
Example:
x = 5.5
y = int(x)
print(type(y)) # <class 'int'>
Checking Data Typesβ
You can check the data type of a variable using the isinstance()
function:
x = 5
print(isinstance(x, int)) # True
Custom Data Typesβ
Python also allows the creation of custom data types using classes. This enables the definition of complex data structures and the implementation of specific behaviors.
Example:
class MyClass:
def __init__(self, value):
self.value = value
x = MyClass(5)
print(type(x)) # <class '__main__.MyClass'>
Conclusionβ
Understanding Python's built-in data types is essential for writing efficient and effective code. Each data type serves a specific purpose and offers unique functionality. By mastering these types, you can leverage Python's full potential to solve complex problems and create powerful applications.