Understanding Python Data Types: A Comprehensive Guide

Vicente
6 min readMar 27, 2023

--

Photo by Tamas Tuzes-Katai on Unsplash

In Python, a data type is a classification that specifies the type of value a variable can hold. Python is a dynamically-typed language, which means that the type of a variable is determined at runtime based on the value it holds. Python has several built-in data types such as numeric types (int, float, complex), boolean type (bool), sequence types (list, tuple, range, string), mapping type (dictionary), and set types (set, frozenset), among others. Understanding and properly using these data types is important for writing efficient and effective Python programs.

Let’s see some examples of each data type

Numeric types are used to represent numbers in Python. There are three numeric types in Python:

  1. Int: The int type is used to represent integers (whole numbers). In Python 3.x, int type can represent arbitrarily large integers.
# Numeric type
a = 10
b = -15
c = 0
d = a + b

print(type(a)) #output <class 'int'>
print(type(b)) #output <class 'int'>
print(type(c)) #output <class 'int'>
print(type(d)) #output <class 'int'>

2. Float: The float type is used to represent floating-point numbers (numbers with a decimal point). In Python, floats are represented internally as a combination of a sign, a mantissa, and an exponent.

# Float type
a = 3.14
b = -0.5
c = 1.0e3 # exponential notation, same as 1000.0
d = a+b

print(type(a)) #output <class 'float'>
print(type(b)) #output <class 'float'>
print(type(c)) #output <class 'float'>
print(type(d)) #output <class 'float'>

3. Complex: The complex type is used to represent complex numbers (numbers with a real and imaginary part). In Python, complex numbers are represented as a pair of floating-point numbers, where the first number represents the real part and the second number represents the imaginary part. Complex numbers can be created by using the j suffix or by calling the complex() function.

a = 2 + 3j
b = -1j
c = complex(1, 2) # same as 1 + 2j

Boolean type: In Python is used to represent truth values. There are only two possible Boolean values: True and False. Boolean values are often used in programming to control the flow of code execution based on certain conditions. For example, you might use a Boolean variable to determine whether or not to execute a certain block of code.

Here are some examples of Boolean variables in Python:

x = True
y = False

print(type(x)) #output <class 'bool'>
print(type(y)) #output <class 'bool'>

Sequence types: In Python are used to represent a collection of values in a specific order. There are four built-in sequence types in Python: list, tuple, range, and string.

  1. List: A list is a mutable sequence type in Python that can hold a collection of values of any data type. Lists are created using square brackets [] and each element is separated by a comma ,. Lists can be modified by adding, removing, or changing elements.
my_list = [1, 2, "hello", True]
print(type(my_list)) #output <class 'list'>

To know more about Lists check the following article.

2. Tuple: A tuple is an immutable sequence type in Python that can hold a collection of values of any data type. Tuples are created using parentheses () and each element is separated by a comma ,. Tuples cannot be modified once they are created.

my_tuple = (1, 2, "hello", True)
print(type(my_tuple)) #output <class 'tuple'>

3. Range: A range is a sequence of numbers that are generated based on a starting point, an ending point, and a step size. The range type is immutable and can be useful for iterating over a sequence of numbers.

my_range = range(0, 10, 2)  # generates the sequence [0, 2, 4, 6, 8]
print(type(my_range)); #output <class 'range'>

4. String: A string is a sequence of characters. Strings are immutable in Python, which means that once a string is created, it cannot be modified.

my_string = "Hello, World!"
print(type(my_string)); #output <class 'str'>

To know more about Srings read the following article

The mapping type in Python is used to represent a collection of key-value pairs, where each key is associated with a corresponding value. The built-in mapping type in Python is called a dictionary.

A dictionary is an unordered collection of key-value pairs. Each key in a dictionary must be unique, and keys can only be of certain immutable types, such as strings, integers, or tuples. The corresponding values can be of any data type, including other dictionaries.

Here’s an example of a dictionary in Python:

my_dict = {"name": "Alice", "age": 30, "city": "New York"}
print(type(my_dict)); #output <class 'dict'>

In this example, the keys are strings ("name", "age", and "city") and the values are a string ("Alice"), an integer (30), and another string ("New York").

Dictionaries can be modified by adding, removing, or changing key-value pairs. Here are some examples:

# Adding a new key-value pair
my_dict["occupation"] = "Programmer"

# Changing the value associated with a key
my_dict["age"] = 31

# Removing a key-value pair
del my_dict["city"]

Dictionaries can be accessed using the square bracket notation []. To retrieve the value associated with a key, you can simply use the key as the index. Here are some examples:

print(my_dict["name"])       # prints "Alice"
print(my_dict.get("age")) # prints 31
print(my_dict.get("height")) # prints None (since "height" is not a key in the dictionary)

You can also iterate over a dictionary using a for loop. By default, a for loop over a dictionary will iterate over its keys. To access the values, you can use the square bracket notation []. Here's an example:

for key in my_dict:
value = my_dict[key]
print(key, value)

# prints
name Alice
age 31
occupation Programmer

The set type in Python is an unordered collection of unique elements. The built-in set type in Python is mutable, while the frozenset type is immutable.

Here’s an example of a set in Python:

my_set = {1, 2, 3, 4}

In this example, the set contains four integers (1, 2, 3, and 4). Notice that the elements are not in any particular order, and there are no duplicates.

You can also create a set from a list or tuple using the set() function. Here's an example:

my_list = [1, 2, 3, 3, 4, 4]
my_set = set(my_list)
print(my_set) # prints {1, 2, 3, 4}

Sets can be modified by adding or removing elements. Here are some examples:

my_set.add(5)       # adds the element 5 to the set
my_set.remove(2) # removes the element 2 from the set

You can perform set operations such as union, intersection, and difference using the |, &, and - operators, respectively. Here are some examples:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

union = set1 | set2 # computes the union of the two sets
intersection = set1 & set2 # computes the intersection of the two sets
difference = set1 - set2 # computes the difference of the two sets

The frozenset type in Python is an immutable version of the set type. It is used to represent an unordered collection of unique elements, just like a regular set, but it cannot be modified after it is created.

Here’s an example of a frozenset in Python:

my_frozenset = frozenset([1, 2, 3, 4])

In this example, the frozenset contains four integers (1, 2, 3, and 4). The elements are not in any particular order, and there are no duplicates.

You can perform set operations such as union, intersection, and difference using the |, &, and - operators, respectively. Here are some examples:

frozenset1 = frozenset([1, 2, 3, 4])
frozenset2 = frozenset([3, 4, 5, 6])

union = frozenset1 | frozenset2 # computes the union of the two frozensets
intersection = frozenset1 & frozenset2 # computes the intersection of the two frozensets
difference = frozenset1 - frozenset2 # computes the difference of the two frozensets

Frozensets are iterable, which means you can use a for loop to iterate over the elements in a frozenset. Here's an example:

my_frozenset = frozenset([1, 2, 3, 4])

for element in my_frozenset:
print(element)

# prints
1
2
3
4

In summary, understanding Python data types is essential for writing efficient and effective Python code. It allows you to manipulate data in a structured and organized way, making it easier to analyze and understand. Additionally, using the appropriate data types for your needs can help you optimize your code for performance and scalability.

Thank you for Reading 😊!

--

--

Vicente

Hi,I'm a software developer with a passion for front-end development. I'm also a talented designer.