Python DataTypes

Introduction :

In Python, every value has a specific data type associated with it. Data types define the nature of the variables, objects, or values you can work with in your code. Understanding different data types is crucial for effective programming. Let’s explore the commonly used data types in Python.

Numeric Types:

  • Integer (int): Represents whole numbers without decimal points.

In Python, the integer data type (int) represents whole numbers without any decimal points. Integers can be positive or negative, and they have no limit on their size, allowing you to work with both small and large numbers.

Example:

num1 = 10
  • Floating-Point (float): Represents decimal numbers.

In Python, the float data type represents decimal numbers. Floats are used to represent real numbers, including both positive and negative values. Unlike integers, floats can have decimal points and can represent numbers with fractional parts.

Example:

num2 = 3.14
  • Complex (complex): Represents numbers with real and imaginary parts.

In Python, the complex data type represents numbers with both real and imaginary parts. Complex numbers are often used in mathematical and scientific computations that involve complex arithmetic operations.

Example:

num3 = 4 + 5j

Sequence Types:

  • String (str): Represents a sequence of characters enclosed in quotes.

In Python, a string is a sequence of characters enclosed in single quotes (‘ ‘) or double quotes (” “). Strings are immutable, which means once created, they cannot be modified. However, you can create new strings based on existing ones.

In Python, a string is a sequence of characters enclosed in single quotes (‘ ‘) or double quotes (” “). Strings are immutable, which means once created, they cannot be modified. However, you can create new strings based on existing ones.

Example:

name = "John"
  • List (list): Represents an ordered collection of items enclosed in square brackets.

In Python, a list is a versatile and mutable data type that represents an ordered collection of items. Lists can contain elements of different data types, such as numbers, strings, or even other lists. Lists are denoted by square brackets [ ].

Example:

numbers = [1, 2, 3, 4, 5]
  • Tuple (tuple): Represents an ordered collection of items enclosed in parentheses.

In Python, a tuple is an ordered collection of elements, similar to a list. However, unlike lists, tuples are immutable, meaning they cannot be modified once created. Tuples are typically denoted by parentheses ( ) or can be created without any delimiters.

Example:

coordinates = (10, 20)

Dictionary (dict):

Represents a collection of key-value pairs enclosed in curly braces.

In Python, a dictionary is an unordered collection of key-value pairs. It is also known as an associative array or a hash map. Dictionaries are enclosed in curly braces { }, and each item in the dictionary is represented as a key-value pair separated by a colon (:).

Example:

person = {"name": "John", "age": 25, "city": "New York"}

Set Types:

Set (set): Represents an unordered collection of unique items enclosed in curly braces.

In Python, a set is an unordered collection of unique elements. Sets are denoted by curly braces { }. Each element in a set must be unique, and duplicate values are automatically removed. Sets are mutable, meaning you can modify them after creation.

Example:

fruits = {"apple", "banana", "orange"}

Boolean Type:

Boolean (bool): Represents either True or False.

In Python, the Boolean data type represents truth values. A Boolean value can be either True or False. Booleans are often used for logical operations and comparisons.

In Python, the keywords True and False (with the first letter capitalized) are used to represent Boolean values.

Example:

is_active = True
is_admin = False

Datatype Conversion:

Below is a table that demonstrates the conversion of various Python data types into other data types. Each entry in the table includes the original data type, the target data type, a code snippet to perform the conversion, and an example output.

Integer(int) : Convert int Data Type to all other Data Type.

Data TypeTarget Data TypeCodeOutput


int

floatnum_int = 10  

num_float = float(num_int)
10.0


int

strnum_int = 42

num_str = str(num_int)
’42’


int

complexnum_int = 5  

num_complex = complex(num_int)
(5+0j)


int

listnum_int = 123  

num_list = list(num_int)
TypeError: ‘int’ object is not iterable  
Conversion is not possible


int

dictnum_int = 1  

num_dict = dict(num_int)
TypeError: ‘int’ object is not iterable
Conversion is not possible


int

tuplenum_int = 7  

num_tuple = tuple(num_int)
TypeError: ‘int’ object is not iterable
Conversion is not possible
intboolnum_int = 0
num_bool = bool(num_int)
print(num_bool)  

num_int1 = 123
num_bool1 = bool(num_int1)
print(num_bool1)
False


True


Float: Convert Float Data Type to all other Data Type.

Data Type Target Data Type Code Output
floatintnum_float = 10.5  

num_int = int(num_float)
10
float

str

num_float = 3.14  

num_str = str(num_float)
‘3.14’
float

complex

num_float = 3.0  
num_complex = complex(num_float,4.5)
(3+4.5j)
float
list

num_float = 3.14  
num_list = list(num_float)
TypeError: ‘int’ object is not iterable   Conversion is not possible
float
dict

num_float = 2.5  
num_dict = dict(num_float)
TypeError: ‘int’ object is not iterable   Conversion is not possible
floattuple
num_float = 1.5  
num_tuple = float(num_float)
TypeError: ‘int’ object is not iterable   Conversion is not possible
floatboolnum_int = 0
num_bool = bool(num_int)
print(num_bool)  

num_int1 = 12.3
num_bool1 = bool(num_int1)
print(num_bool1)
False


True

Complex : Convert complex data type to all other data type.

Data Type Target Data TypeCodeOutput
complexintnum_complex = complex(3, 4)
 
num_int = int(num_complex)

TypeError: int() argument must be a string, a bytes-like object or a real number, not ‘complex’   Conversion is not possible.
complexfloat
i).
num_complex = complex(3, 4)
num_float = float(num_complex)


ii).
num_complex = complex(3, 4)
num_float = float(num_complex.real)
i).TypeError: float() argument must be a string or a real number, not ‘complex’   Conversion is not possible.



ii).  3
complexstr

num_complex = complex(1, 2)<br>num_str = str(num_complex)

‘(1+2j)’
complexlist
num_complex = complex(2, 3)

num_list = [num_complex.real, num_complex.imag]
[2.0, 3.0]
complexdict
num_complex = complex(2, 1)  

num_dict = {‘real’: num_complex.real, ‘imag’: num_complex.imag}
{‘real’: 2.0, ‘imag’: 1.0}
complextuple
num_complex = complex(4, 5)

num_tuple = (num_complex.real, num_complex.imag)
(4.0, 5.0)
complexbool
num_complex = complex(0, 0)  

num_bool = bool(num_complex)
False

String(str) : Convert str data type to all other data type.

Data TypeTarget Data TypeCodeOutput

str
int
num_str = ‘987’  

num_int = int(num_str)
987

str
float
num_str = ‘2.71’  

num_float = float(num_str)
2.71

str
complex
num_str = ‘1+2j’  

num_complex = complex(num_str)
(1+2j)

str
list
str_val = ‘hello’  

list_val = list(str_val)
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

str
dict
str_val = ‘hello’  

dict_val = {str_val: len(str_val)}
{‘hello’: 5}

str
tuple
str_val = ‘abc’  

tuple_val = tuple(str_val)
(‘a’, ‘b’, ‘c’)

str
bool
str_val = ‘True’  

bool_val = bool(str_val)
True

List(list) : Convert list data type to all other data type.

Data TypeTarget Data Type CodeOutput
listint
num_list = [1, 2, 3]  

num_int = int(”.join(map(str, num_list)))
123
listfloat
num_list = [3, 1, 4]  

num_float = float(”.join(map(str, num_list)))
314.0
liststr
num_list = [10, 20, 30]  

num_str = ‘, ‘.join(map(str, num_list))
’10, 20, 30′
listcomplex
num_list = [2, 3]  

num_complex = complex(num_list[0], num_list[1])
(2+3j)
listdict
num_list = [1, 2]  

num_dict = dict(zip(num_list, [‘one’, ‘two’]))
{1: ‘one’, 2: ‘two’}
listtuple
num_list = [7, 8, 9]  

num_tuple = tuple(num_list)
(7, 8, 9)
listbool
num_list = [0, 1, 0]  

num_bool = any(num_list)
True

Tuple(tuple) : Convert tuple data type to all other data type.

Data Type Target Data Type Code Output
tupleint
num_tuple = (4, 5, 6)  

num_int = int(”.join(map(str, num_tuple)))
456
tuplefloat
num_tuple = (1, 2)  

num_float = float(‘.’.join(map(str, num_tuple)))
1.2
tuplestr
num_tuple = (7, 8, 9)  

num_str = ‘, ‘.join(map(str, num_tuple))
‘7, 8, 9’
tuplecomplex
num_tuple = (3, 4)  

num_complex = complex(num_tuple[0], num_tuple[1])
(3+4j)
tuplelist
num_tuple = (10, 20)  

num_list = list(num_tuple)
[10, 20]
tupledict
num_tuple = (‘x’, 1)  

num_dict = dict([num_tuple])
{‘x’: 1}
tuplebool
num_tuple = (0, 0)  

num_bool = any(num_tuple)
False

Dictionary(dict) : Convert dict data type to all other data type.

Data Type Target Data Type Code Output
ictint
num_dict = {‘a’: 1}  

num_int = int(list(num_dict.keys())[0])
97
dictfloat
num_dict = {‘pi’: 3.14}  

num_float = float(list(num_dict.values())[0])
3.14
dictstr
num_dict = {1: ‘one’}  

num_str = str(list(num_dict.keys())[0])
‘1’
dictcomplex
num_dict = {3: 5}
 
num_complex = complex(list(num_dict.keys())[0], list(num_dict.values())[0])
(3+5j)
dictlist
num_dict = {‘a’: 1, ‘b’: 2}  

num_list = list(num_dict.items())
[(‘a’, 1), (‘b’, 2)]
dicttuple
num_dict = {‘x’: 10, ‘y’: 20}  

num_tuple = tuple(num_dict.items())
((‘x’, 10), (‘y’, 20))
dictbool
num_dict = {‘flag’: True}  

num_bool = bool(list(num_dict.values())[0])
True

Set : Convert set data type to all other data type.

Data Type Target Data Type CodeOutput
setint
num_set = {1, 2, 3}  

num_int = int(”.join(map(str, num_set)))
123
setfloat
num_set = {3, 1, 4}  

num_float = float(”.join(map(str, num_set)))
314.0
setstr
num_set = {10, 20, 30}  

num_str = ‘, ‘.join(map(str, num_set))
’10, 20, 30′
setcomplex
num_set = {2, 3}  

num_complex = complex(list(num_set)[0], list(num_set)[1])
(2+3j)
setlist
num_set = {1, 2, 3}  

num_list = list(num_set)
[1, 2, 3]
setdict
num_set = {1, 2, 3}  

num_dict = dict.fromkeys(num_set, ‘value’)
{1: ‘value’, 2: ‘value’, 3: ‘value’}
settuple
num_set = {7, 8, 9)  

num_tuple = tuple(num_set)
(8, 9, 7)
setbool
num_set = set()  

num_bool = bool(num_set)
False

Boolean(bool) : Convert bool data type to all other data type.

Data TypeTarget Data Type CodeOutput
boolint
num_bool = False  

num_int = int(num_bool)
0
boolfloat
num_bool = True  

num_float = float(num_bool)
1.0
boolstr
num_bool = True  

num_str = str(num_bool)
‘True
boolcomplex
num_bool = True  

num_complex = complex(int(num_bool), 0)
(1+0j)
boollist
num_bool = False  

num_list = [int(num_bool)]
[0]
booldict
num_bool = True  

num_dict = {str(num_bool): ‘boolean’}
{‘True’: ‘boolean’}
booltuple
num_bool = False  

num_tuple = (int(num_bool),)
(0,)

Leave a Comment