Start of Day 5: Final Step of Python Data Types
Today we are going to cover some of the most important data types in Python — Tuples, Sets, and Dictionaries. These concepts are especially important in the field of Algorithmic Trading and Quantitative Analysis. Today’s focus is mainly on Tuples, which is important to understand Immutable Data Structure.
What are Tuples? (What are Tuples in Python)
Tuple is a data type that looks like a list but it has a big feature — it cannot be changed once created. That is, Tuples are immutable while Lists are mutable. This is very important to understand the stability of data structure in Python.
Characteristics of Tuples
- Ordered: Both Tuple and List are ordered i.e. elements are stored in a fixed sequence.
- Immutable: You cannot make any changes to a Tuple after it is created.
Duplicate elements allowed: Just like List, Tuple can also have duplicate values.
Being immutable, it is often used in Algorithmic Trading Strategies, where we have to ensure that the data does not change inadvertently.
Creating Tuples in Python
Empty Tuple
T1 = ()
print(type(T1)) # Output: <class ‘tuple’>
Single-Element Tuple
T2 = (“BTC”,) # Note that a comma is required!
print(type(T2)) # Output: <class ‘tuple’>
If comma is not used then it will be considered as String and not Tuple.
Homogeneous vs Heterogeneous Tuples
Homogeneous Tuple:
Items with same data type
crypto = (“BTC”, “ETH”, “SOL”)
Heterogeneous Tuple:
Mixed data types like string, integer, float, and boolean
mixed = (“BTC”, 100, 1.5, True)
In Algorithmic Trading, many times we use Heterogeneous Tuples like (Ticker, Quantity, Price, Trade_Status).
Type Conversion and Tuple
In Python, you can convert any data type to a Tuple:
data = “BTC”
converted = tuple(data)
print(converted) # Output: (‘B’, ‘T’, ‘C’)
This property helps convert data into a secure immutable form, which is very useful in high-sensitive areas like algorithmic trading.
Watch this Day 5 video tutorial
Session 5: Tuples + Set + Dictionary