Skip to content

See below for godzilla.dev materials about: AI x Quant Trader Series - Day 2

"Who will teach me about Python?"

On the first day, We learned the basic operations of Python and several main container types.

Today, We will learn Python's functions, loops and conditionals, and classes. With this, We will have a general understanding of Python. The learning outline for today is as follows:

3. Functions

a) Defining a function

4. Loops and Conditionals

a) if statements

b) while True / break statements

c) for loops

d) List comprehensions

5. Classes

a) A casual talk about classes and objects

b) Defining a class

3. Functions

a) Defining a function

(1) Definition Rules

When introducing list methods, we already briefly mentioned functions. Anyone who has studied mathematics knows what a function is — it takes an input (a parameter) and returns a value. Functions can also be defined by yourself, using the following format:

def function_name(parameter):
    # function code

In the function code, return indicates the value to be returned. For example, to define a square function square(x) that takes x as input and returns the square of x:

def square(x):return x*x

square(9)
the output:
81

(2) Defining Functions with Variable Parameters

Sometimes you need to define a function with a variable number of parameters. There are several ways to do this:

Assign default values to parameters For example, define a function like f(a, b=1, c='hehe'). In this case, the last two parameters are optional — if not specified during the function call, they will default to b=1 and c='hehe'. Therefore, the following calls are all valid:

f('dsds')
f('dsds', 2)
f('dsds', 2, 'hdasda')

Keyword arguments The method above fixes the order of parameters — the first value is assigned to the first parameter. With keyword arguments, however, you can specify which value goes to which parameter by name. For example, still using the function f(a, b=1, c='hehe'), you can call it like this:

f(b=2, a=11)
The order of parameters can be changed as long as you specify them using their keywords.

4. Loops and Conditionals

Note that Python uses indentation to indicate which block of code belongs to the loop.

a) if statements

Also note two things: first, indentation; and second, a colon (:) is required after the condition.

j=2.67
if j<3:
    print('j<3')
the output:
j<3

For multiple conditions, note that elseif should be written as elif. The standard format is:

if condition1:
    statement1
elif condition2:
    statement2
else:
    statement3
Note that if, elif, and else are at the same indentation level — there should be no indentation before them.

t=3
if t<3:
    print('t<3')
elif t==3:
    print('t=3')
else:
    print('t>3')
the output:
t=3

b) while True / break statements

The format of this statement is:

while True:  # condition is true
    statement
    if break_condition:
        break

Here’s an example:

a=3
while a<10:
    a=a+1
    print(a)
    if a==8: break
the output:
4
5
6
7
8

Although the condition after while is a < 10, meaning the loop will continue as long as a is less than 10, the if condition specifies that the loop should break when a equals 8. Therefore, the output will only go up to 8.

c) for loops

No more explanation needed — you can iterate over a sequence, dictionary, etc.

a=[1,2,3,4,5]
for i in a:
    print(i)
the output:
1
2
3
4
5

d) List comprehensions

List comprehensions are a way to create a new list from an existing one, working similarly to a for loop. The format is:

[output_value for condition]

When the condition is met, an output value is generated, and the final result is a list.

[x*x for x in range(10)]
the output:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

[x*x for x in range(10) if x%3==0]
the output:
[0, 9, 36, 81]

The above example uses the sequence [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] to generate a new sequence.

5. Classes

a) A casual talk about classes and objects

A class is an abstract concept — it doesn't exist in the physical world in terms of time or space. A class simply defines the abstract attributes and behaviors for all its objects. For example, the class "Person" can represent many individuals, but the class itself doesn't exist as a tangible entity in the real world.

An object, on the other hand, is a concrete instance of a class. It is something that actually exists. If "Person" is an abstract class, then you, yourself, are a specific object of that class.

An object of a class is also called an instance of the class. To give another analogy, a class is like a mold, and objects are the concrete things produced using that mold — each with the same attributes and methods. As the saying goes, "They look just alike, as if made from the same mold" — that’s exactly the idea here.

The process of using a mold to create a concrete thing is called instantiation of the class. Let’s take a look at a specific class example below:

b) Defining a class

class boy:
    gender='male'
    interest='girl'
    def say(self):
        return 'i am a boy'

The statement above defines a class called boy. Now let’s use this class model to construct a specific object:

peter=boy()

Now let’s take a look at the attributes and methods of the specific instance peter.

“What are attributes and methods?”

They are two forms of a class:

Attributes are the static aspects

Methods are the dynamic aspects

For example, the class “Person” may have attributes such as name, gender, height, age, and weight. It may also have methods such as walking, running, and jumping.

peter.gender
the output:
'male'

peter.interest
the output:
'girl'

peter.say()
the output:
'i am a boy'

Here, gender and interest are attributes of peter, while say is his method. If we instantiate another object, for example sam:

sam=boy()

Then sam and peter have the same attributes and methods — you could say, “They were truly made from the same mold!”