SoFunction
Updated on 2024-10-29

Python Panorama Series of Data Types Inventory

introductory

Welcome to our series of blogs, The Python Panorama Series! In this series, we'll take you from the basics of Python and dive step-by-step into advanced topics to help you master this powerful and flexible programming syntax. Whether you're new to programming or a developer with some basic knowledge, this series will provide you with the knowledge and skills you need.

Python, a powerful and flexible programming language, has a rich system of data types. This article describes each of the data types in Python in detail, including numeric, sequence, mapping, set, boolean, and None types. The characteristics of each data type, how it is used, and its application to real-world problems will be explored in depth. In addition, we will explore Python's dynamic typing features and how to fully utilize these data types in real-world programming to simplify code and increase efficiency. At the end of the article, I will also share a feature that you might not know about, but is very useful.

1. Numerics

Python's numeric types include Integers, Floating-point numbers, Complex numbers, Booleans, and Bytes.

# Integer
x = 10
print(type(x)) # <class 'int'>
# Floating point
y = 20.5
print(type(y)) # <class 'float'>
# Plural
z = 2 + 3j
print(type(z)) # <class 'complex'>
# Boolean
a = True
print(type(a)) # <class 'bool'>
# Binary
b = b"Hello"
print(type(b)) # <class 'bytes'>`

The integer types in Python support not only regular decimal, but also binary (0b10), octal (0o10), and hexadecimal (0x10). They are designed to give Python great power in mathematical operations. Notably, Python's integers have no size limit, which means you can work with very large integers without worrying about overflow.

2. Sequences

Sequence types include Lists, Tuples, Strings, Byte Arrays, and Ranges.

# List
list_ = [1, 2, 3]
print(type(list_)) # <class 'list'>
# Tuples
tuple_ = (1, 2, 3)
print(type(tuple_)) # <class 'tuple'>
# String
str_ = "Hello, Python!"
print(type(str_)) # <class 'str'>
# Byte arrays
bytes_array = bytearray(b"Hello")
print(type(bytes_array)) # <class 'bytearray'>
# Scope
range_ = range(5)
print(type(range_)) # <class 'range'>

Lists are mutable, while tuples and strings are immutable. This characteristic determines their usage scenarios in Python programming. For example, we can use lists to store data that needs to be dynamically modified, tuples to store invariant sequences of data, and strings to work with textual data.

3. Mappings

Python's mapping types include Dictionary.

# Dictionary
dict_ = {"name": "Python", "age": 30}
print(type(dict_)) # <class 'dict'>

The performance advantage of dictionaries is that lookups and insertions are very fast and do not depend on the size of the dictionary, due to the fact that the internal implementation of dictionaries uses hash tables. This makes dictionaries ideal for storing large amounts of data, especially when we need to look up data quickly.

Dictionaries have been optimized in Python 3.7 onwards to maintain insertion order, which means that when we traverse the dictionary, the order of the elements will be the same as when they were inserted. This makes dictionaries an alternative to OrderedDict in some cases.

4. Collection types (Sets)

Sets and FrozenSets are two types of collections in Python.

# Collections
set_ = {1, 2, 3}
print(type(set_)) # <class 'set'>
# Freeze the collection
frozenset_ = frozenset([1, 2, 3])
print(type(frozenset_)) # <class 'frozenset'>

Sets are useful in dealing with specific problems such as removing duplicate elements, checking for the existence of an element, finding intersection, union, difference, etc.

5. Type None

Python has a special type called NoneType, which has only one value: None. it is often used to represent missing values or null values.

# None type
none_ = None
print(type(none_)) # <class 'NoneType'>

Using None can help us distinguish whether a variable has been assigned a value or whether a function has returned a valid result.

In functions that do not have an explicit return statement, Python returns None by default.This makes it easy to tell if a function has an explicit return value.

One More Thing...

Python's data types are all classes. This means that we can work with this data as if it were an object, calling their methods and even adding properties to them.

# Add attributes to integers
x = 10
x.my_attribute = "Hello"
print(x.my_attribute) # "Hello"

Although this feature may not be commonly used, it opens up great possibilities for Python's dynamism. It is a reflection of Python as an object-oriented language, and of Python's "everything is an object" philosophy.

These are all the built-in data types of Python. Understanding and skillfully applying these data types is the foundation for improving your Python programming skills. I hope this blog can help you understand Python data types better, for more information about Python data types please follow my other related articles!