Data Types That We Encounter In Python

The type of an object tells us how the object behaves or may be used; for example: what kind of values the object may have; under what conditions we can compare two objects; how much memory the object takes up; whether an operation will succeed or fail due to overflow or underflow; and so on. This section will briefly introduce Python's different data types, with examples.

There are four fundamental data types in Python: 

- Numeric: Integers (e.g., 2848), Floats or Decimal numbers 
                   (e.g., 2.71828182845904), Complex numbers (e.g., 2.71828182845904)
- Boolean:  True or False values (e.g., True, False)
- Sequence Type: lists, tuples and string: Textual content (e.g., “Hello, world!”)
- Dictionary: Collection of keys and values
- Set: Sets are unordered collections of unique elements.


Numeric 

The numeric data type is one of the most important data types in Python. It is used to represent all the integers, complex and floating point numbers. While there are various ways to represent numbers in Python, the most common type for general purpose calculations is the "integer" type and “float” type. The "complex" data type is often used to represent complex numbers, but it's not recommended for general calculations because of its prohibitively high computational cost.

num_1 = 29
print(type(num_1))
num_2 = 2.4
print(type(num_2))
num_3 = 2 + 4j print(type(num_3
))
Here, type() is used to determine the type of data type.

<class 'int'>
<class 'float'>
<class 'complex'>

One of the major uses of numeric data type is to represent values that can be added, subtracted, multiplied and divided without any problem.


Boolean 

The boolean type represents truth values. Python’s logical operators are not part of the boolean type but instead are specific language constructs. Bool is an alias for creating a new boolean value from two other values. It can also be used to convert any value to a boolean. Boolean operations work by comparing two true or false values (referred to as operands). The result of the comparison is another true or false value that represents whether the result of the operation was logically accurate (true) or logically false (false).

a = True
b = False
print(type(a))
print(type(b))
The output of the above code.

<class 'bool'>
<class 'bool'>


Sequence Type

The sequence data type is a container for storing the following:

empty_lists = []
empty_string = ' '
empty_tuple = ()

They are created using the square brackets [] for a list, ' ' for a string and () for a tuple. [], " " and () are used to denote their type.

<class 'list'>
<class 'str'>
<class 'tuple'>


Dictionary

A dictionary is a data structure with keys and values. Keys in python are just the object's string representation. It can be any kind of object, but it needs to be hashable, meaning it needs to have a unique string representation that doesn't change when the object changes.

Python dictionaries are created by placing brackets ([]) around a list of key-value pairs, separated by commas and optionally enclosed in curly braces ({}). The keys and values themselves don't need to be wrapped in anything, but they may be if you want to use them as part of expressions.

example_dict = {'key1':'value1', 'key2':'value2'}
print(type(example_dict))

The value for each key must also be present somewhere in the list of pairs for that key; an input for one pair cannot act as the value for another pair.

<class 'dict'>

Set

A set is a basic programming data structure. It is a collection of unique, unordered objects. Elements in a set must be of the immutable type, and sets cannot contain duplicate elements. A set can be thought of as an abstract concept or mathematical object with no specific limitations on its size and order. We can create a set by using curly {} braces separated by a comma.

Examples:

my_set = {1, 2, 3, 4, 5}
my_set = {(1,2), '142', 4, 5}
empty_set = set()
print(type(empty_set))

But we can not create an empty set using {}, because it is already reserved for the dictionary instead we use set() function as shown above.

<class 'set'>
Previous Post Next Post