pre-learn-of-aix-week-2/3

pre-learn-of-aix-week-2/3

Content

In this blog, I’ll combine pre learning 2 and 3.

Interviews with AI Leaders

// watching…

Learning Python

Complex

Python actually has complex numbers as a primitive data type. There are two ways to make a complex number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a = complex(1, 2)
print type(a)
a = 1+2j
print type(a)
print a.imag
print a.real

"""
result:
<type 'complex'>
<type 'complex'>
2.0
1.0
"""

ps: the textbook mentioned something interesting:

You’re probably used to using i for (-1)0.5. Just to confuse you, we’re going to use j instead. Why? Because to an electrical engineer, i stands for current, and there’s no arguing

Funtional Style

What is Funtional Style? In the prelearning material, it is defined as:

In the functional programming style, one tries to use a very small set of primitives and means of combination. We’ll see that recursion is a very powerful primitive, which could allow us to dispense with all other looping constructs (while and for) and results in code with a certain beauty.

Another element of functional programming style is the idea of functions as first-class objects. That means that we can treat functions or procedures in much the same way we treat numbers or strings in our programs: we can pass them as arguments to other procedures and return them as the results from procedures. This will let us capture important common patterns of abstraction and will also be an important element of object-oriented programming.

A exmple of ancient algorithm:

It is an iterative algorithm: it starts with a guess about the square root, and repeatedly asks whether the guess is good enough. It’s good enough if, when we square the guess, we are close to the number, x, that we’re trying to take the square root of. If the guess isn’t good enough, we need to try to improve it. We do that by making a new guess, which is the average of the original guess and x / guess.

1
2
3
4
5
6
7
8
9
10
def sqrt(x):
def goodEnough(guess):
return abs(x-square(guess)) < .0001
guess = 1.0
while not goodEnough(guess):
guess=average(guess,x/guess)
return guess

>>> sqrt(2)
1.4142156862745097

Extra:

  1. inline function in function. We called it closure in a more professional way.

    • I once write a blog about this, see here
  2. *arg, **kwargs

    • we called it Variable Argument, *arg means positional arguments and **kwargs is keyword arguments. We’ve learn it the first day we use Python, but we have overlooked the most important symbol——*(star).
    • the star symbol has two functions: pack and unpack, a sample:
1
2
3
4
5
6
7
8
9
10
11
12
def star_wrapper(a, b, *c):
print c
print type(c)


star_wrapper(1, 2, 3, 4, 5)
# (3, 4, 5)
# <type 'tuple'>

star_wrapper(*[1, 2, 4, 5, 6])
# (4, 5, 6)
# <type 'tuple'>

When convey the arguments to a function, * allocate the paramters to the arguments in function in sequence. When the interpretor found a formal parameter with *, it will pack the unallocated actual parameter and convey it as a tuple to the formal parameter. Alike *, the double star(**) pack the keyword into a dictionary.

Author

Ctwo

Posted on

2020-12-26

Updated on

2021-01-16

Licensed under

Comments