See below for godzilla.dev materials about: AI x Quant Trader Series - Day 1
"Why I should learn Python even if Cursor do everything for me?"¶
True. you can ask Cursor to generate python code for you now in 2025. but still the ability to read and run the code is important
These series help you to be an expert to work with AI to trade
"Who will teach me about Python?"¶
As a complete beginner with no background, I just want to get a rough idea of Python, write some simple programs, and be able to understand common code. I don't know anything about Java, C, inheritance, exceptions, and so on. So, I looked up a lot of information and wrote the following diary entry, hoping to understand Python — an increasingly important language in the field of quantitative trading — from the perspective of an absolute beginner.
1. Learning the basic¶
Before formally introducing Python, it's beneficial to understand the following two basic operations for later learning:
a) Basic Input and Output¶
You can use +, -, *, / to perform basic arithmetic operations directly in Python.
the output:b) Importing Modules¶
You can import modules using the import statement. Once imported, you can use the functions within that module.
For example, you can import the math module and then use the sqrt function from the math module.
the output:At this point, I had a question:
"Do I have to include the 'math' module prefix every time I reference a function? Can I use it without the prefix?"
Directly typing sqrt(9) will result in an error, which is quite annoying. So, is there a way to use the function without always including the prefix? Yes, there is a way — you can use the format from module import function to "extract" the function directly.
the output:This way, I don't need to add the 'math' prefix every time I use the sqrt function. Just as I was about to move on, another question arose:
"With so many functions under the math module, is there a way to write one statement and then use all functions from math directly?"
After using the sqrt function from the math module with a from...import..., and then I need to use floor, I would have to write another import. This is also quite bothersome. However, there is a way to "extract" all functions at once:
the output:2. Containers¶
a) What are Containers¶
When starting to learn Python, I was quite confused by its data structures, such as dictionaries, sequences, tuples, etc. I suspect there are other beginners who feel the same, so I organized some information for retention:
First, let's start with containers. In Python, there is a type of data structure known as containers. As the name implies, a container is a vessel for storing data. It mainly includes sequences and dictionaries, where sequences mainly comprise lists, tuples, strings, etc. (see the diagram below).
The basic form of a list, for example: [1, 3, 6, 10] or ['yes', 'no', 'OK']
The basic form of a tuple, for example: (1, 3, 6, 10) or ('yes', 'no', 'OK')
The basic form of a string, for example: 'hello'
The above are all types of sequences. Each element in a sequence is assigned a number — its position, also known as an "index." The index of the first element is 0, the second is 1, and so on. The main difference between lists and tuples is that lists can be modified, while tuples cannot (note that lists use square brackets while tuples use parentheses). This characteristic of sequences allows us to access specific elements in a sequence using indices, for example:
the output: the output: the output: In contrast to sequences, the "dictionary" is a different type of container—it is unordered.Its basic form, for example, is: d = {7: 'seven', 8: 'eight', 9: 'nine'}
This is a "key-value" mapping structure. Therefore, elements in a dictionary cannot be accessed through indices like in sequences, but rather by using the keys to access the elements:
the output:b) Some Common Operations on Sequences¶
In addition to the indexing mentioned above, lists, tuples, strings, and other sequences share some common operations.
(1) Indexing (supplementing the above)
The index of the last element in a sequence can also be -1, the second to last can be -2, and so forth:
the output:(2) Slicing
Use slicing to access elements within a specific range. The format for slicing is:
a[start_index:end_index:step]
This accesses elements starting from the element at the start index up to, but not including, the element at the end index, visiting every 'step' elements along the way. The step can be omitted, in which case the default step is 1.
the output:This is like dividing a sequence into several slices, which is why it's called "slicing."
(3) Sequence Concatenation
This involves merging two sequences together. Only sequences of the same type can be concatenated.
the output: the output:(4) Membership
To check if a value is in a sequence, you can use the in operator.
the output:c) List Operations¶
The above are some operations common to all sequences, but lists also have some unique operations that other sequences do not have.
(1) List Function
You can convert a sequence into a list using the list(sequence) function.
the output:(2) Element Assignment and Deletion
Element Deletion — del a[index]
Element Assignment — a[index] = value
the output: the output: the output: the output:Slice Assignment — a[start_index:end_index] = list(value)
This assigns values to elements within a specific range of the list, from the start index up to but not including the end index. For example, using the above statement, how would you change 'hello' to 'heyyo'?
the output: the output:Note that although "ll" is at positions index 2 and 3 in the word "hello", when assigning values, you use b[2:4] instead of b[2:3]. Also, remember that list() uses parentheses.
(3) List Methods
As mentioned above, the list function is something found in many languages, like the IF function and VLOOKUP function in Excel, the COUNT function in SQL, and the sqrt function common to various languages, among many others. Python also has many such functions. In Python, a method is a function that is "closely associated with certain objects." Therefore, list methods are functions that belong to lists and can perform some deeper operations on them. Methods are called in this format:
object.method(arguments)
Thus, the call for list methods naturally follows this pattern:
list.method(arguments)
Here are some commonly used list methods, using a = ['h', 'e', 'l', 'l', 'o'] as an example:
the output:Insert an element m at index n of list a: a.insert(n, m)
the output:Add element m to the end of the list a: a.append(m)
the output:Return the index of the first occurrence of element m in list a: a.index(m)
the output:Remove the first occurrence of element m from list a: a.remove(m)
the output:Sort list a in descending order: a.sort(reverse=True)
the output:d) Dictionary Operations¶
(1) dict Function
The dict function can create a dictionary using keyword arguments, with the format:
dict(key1=value1, key2=value2, ...) → {key1: value1, key2: value2, ...}
For example, how do you create a dictionary with the name name set to "jiayounet" and age age set to 28?
the output:(2) Basic Operations
In many ways, the basic behavior of dictionaries is similar to that of lists. The following examples use the sequence a = [1, 3, 6, 10] and the dictionary f = {'age': 27, 'name': 'shushuo'}.
Diary Summary:
Today, We learned about Python’s basic interface, operations, and several main container types. Next, We need to study Python's functions, loops and conditionals, and classes — only then We will have a general understanding of Python.