| id | Data-types | ||||
|---|---|---|---|---|---|
| title | Data-types in python | ||||
| sidebar_label | Data-types in python | ||||
| sidebar_position | 4 | ||||
| tags |
|
||||
| description | In this tutorial we will learn about data types in python, |
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.
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
You can get the data type of any object by using the type() function:
x = 5
print(type(x))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 # NoneTypeA 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'>An int (integer) is a whole number, positive or negative, without decimals, of unlimited length.
Example:
x = 20
print(type(x)) # <class 'int'>A float is a number, positive or negative, containing one or more decimals.
Example:
x = 20.5
print(type(x)) # <class 'float'>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'>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'>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'>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'>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'>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'>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'>A bool represents one of two values: True or False.
Example:
x = True
print(type(x)) # <class 'bool'>A bytes object is an immutable sequence of bytes.
Example:
x = b"Hello"
print(type(x)) # <class 'bytes'>A bytearray is a mutable sequence of bytes.
Example:
x = bytearray(5)
print(type(x)) # <class 'bytearray'>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'>NoneType is the type of the None object, which represents the absence of a value.
Example:
x = None
print(type(x)) # <class 'NoneType'>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
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'>You can check the data type of a variable using the isinstance() function:
x = 5
print(isinstance(x, int)) # TruePython 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'>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.