Quantitative analyst's Python Diary "Day 1th: Who's going to tell me about Python?" "
Shikun Kelvin No. No. 001 staff 2015-01-28 15:48 - 144 Cloning
# # # "Who's going to tell me about Python?" ”
As a beginner without foundation, just want to know about Python, casually make a small program, and can read the general procedures, those what Java Ah, C ah, inheritance ah, abnormal ah all do not understand how to do, so I find a lot of information, write the following this diary, We hope to understand Python's increasingly important language in the field of quantification by starting with a complete beginner's perspective.
# # #一, familiar with basic
Before you formally introduce Python, it is good to understand the following two basic operations for later learning:
1) Basic input and output can be used in Python + 、-、 *,/Direct arithmetic.
1
1+3*3
10
(2) Import module using import can be imported module, after import, you can use the function below this module. For example, import the math module and then use the SQRT function below the Math module:
1
Math
2
Math. sqrt (9)
3.0
Then I have a question:
"Does the math module prefix take every time you reference a function?" Would you mind not taking it? ”
Direct input sqrt (9) will be error, good annoying, then there is no way to take the prefix every time? The solution is to use the "From module Import function" format first to "take" the function out.
1
sqrt
2
sqrt (9)
3.0
So you don't have to add the math prefix every time you use the SQRT function. When I'm ready to skip, there's another problem?
"There are so many functions under the Math module, can you write a statement, and then all the functions below math are available directly?" ”
Called the math below the SQRT function, write a from...import ..., and then call the floor below, but also to write a, so also very troublesome, there is a way to "take" all the functions out:
1
*
2
sqrt (9)
3
Floor (32.9)
3.032.0
# # #二, container 1, what is a container
When I started to learn python, it was confused by its data structure, what dictionaries, sequences, tuples, and so on, and I was estimated to have the same beginner as myself, so I combed the remains: first of all, from the container, Python has a data structure called a container, as the name implies, the container, is the device that is loaded with data, It consists mainly of sequences and dictionaries, in which the sequence consists mainly of lists, tuples, strings, etc. (see figure below).
The basic form of a list such as: [1,3,6,10] or [' Yes ', ' no ', ' OK ']
Basic forms of tuples such as: (1,3,6,10) or (' Yes ', ' no ', ' OK ')
The basic form of a string such as: ' Hello '
The above types belong to the sequence, each element of the sequence is assigned an ordinal-that is, the position of the element, also called the "Index", the first index, that is, the position of the first element is 0, the second is 1, and so on. The difference between a list and a tuple is that the list can be modified, whereas tuples cannot (note that the list is in brackets and the tuple is in parentheses). This feature of the sequence allows us to use an index to access one or several elements of a sequence, such as:
1
a=[1,3,6,ten]
2
a[2]
6 1
b= (1,3,6,Ten)
2
b[2]
6invalid syntax (line 2) 1
c=' Hello '
2
c[0:3]
' Hel '
and the dictionary corresponding to the sequence is not the same, it is an unordered container,
Its basic forms such as: d={7: ' Seven ', 8: ' Eight ', 9: ' Nine '}
This is the structure of a "key-value" mapping, so the dictionary cannot access its elements through an index, and the elements in it are accessed by the key:
1
d={7:' Seven ',8:' eight ',9:' nine '}
2
d[8]
' Eight '
2, the sequence of some common operations
In addition to the indexes mentioned above, lists, tuples, strings, and other sequences have some common operations.
(1) index (supplement above)
The index of the last element of the sequence, which can also be-1, the second-lowest can also be used-2, and so on:
1
a=[1,3,6,ten]
2
a[3]
3
a[-1]
1010
(2) sharding
Use the Shard operation to access a range of elements in the following format:
a[Start Index: End index: Step]
Then the access is, from the start index number of the element, to the end index number-1 of the element, each interval step of the element access once, the step can be ignored, the default step is 1.
1
c=' Hello '
2
c[0:3]
' Hel '
It's like dividing a sequence into pieces, so it's called "Shards."
(3) Sequence addition
That is, two sequences are merged together, and two sequences of the same type can be added
1
[1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]invalid syntax (line 2) 1
' Hello, '+' world! '
' hello,world! '
(4) Membership
In order to check whether a value is in a sequence, you can use the in operator
1
a=' Hello '
2
A
3
A
Truefalseinvalid syntax (line 2)
3. List operation
These are some of the operations common to the sequence, and the list has its own unique operations, which are not in the other sequences
(1) List function
You can convert a sequence into a list by using the list (sequence) function:
1
List (' Hello ')
[' H ', ' e ', ' l ', ' l ', ' O ']
(2) Element assignment, deletion
Element Delete--del a[index number]
Element Assignment--a[index number]= value
1
A
' Hello ' 1
b=List (a)
2
B
[' H ', ' e ', ' l ', ' l ', ' O '] 1
b[2]
2
B
[' H ', ' e ', ' l ', ' O '] 1
b[2]=' t '
2
B
[' H ', ' e ', ' t ', ' O ']
Shard Assignment--a[Start index number: End index number]=list (value)
Assigns a value to an element in a range of the list, that is, a number of elements in the interval between the start index and the end index number-1, for example, using the above statement, how to turn hello into heyyo?
1
b=list (' hello ')
2
B
[' H ', ' e ', ' l ', ' l ', ' O '] 1
b[2:4]=list (' yy ')
2
B
[' H ', ' e ', ' y ', ' y ', ' o ']
Note that although "ll" is in the position of index 2nd and 3rd of the word "hello", it is assigned with B[2:4] instead of b[2:3], and note that the list () is enclosed in parentheses.
(3) List method
As mentioned above, the list function, which is found in many languages, such as the IF function in Excel, the VLOOKUP function, the Count function in SQL, and the SQRT function in various languages, there are many functions in Python. The method in Python is a function that is "closely related to certain objects," so a list method is a function that belongs to a list, and it can implement some more in-depth operations on the list by calling:
Object. Method (Parameter)
Then the invocation of the list method is, of course:
List. Methods (Parameters)
A list of commonly used methods is so few, with a=[' h ', ' e ', ' l ', ' l ', ' o ', for example:
1
A=[' h ',' E ',' l ',' l ',' o ']
2
A
[' H ', ' e ', ' l ', ' l ', ' O ']
Inserts an element into the N index position of List a m:a.insert (n,m)
1
A.insert (2,' t ')
2
A
[' H ', ' e ', ' t ', ' l ', ' l ', ' O ']
Add the last element to the list m:a.append (m)
1
A.append (' q ')
2
A
[' H ', ' e ', ' t ', ' l ', ' l ', ' o ', ' Q ']
Returns the index position of the first occurrence of the element m in the A list: A.index (m)
1
A.index (' e ')
1
Delete the first M element in a: A.remove (m)
1
A.remove (' e ')
2
A
[' H ', ' t ', ' l ', ' l ', ' o ', ' Q ']
Arrange list A from large to small: A.sort ()
1
A.sort ()
2
A
[' H ', ' l ', ' l ', ' o ', ' q ', ' t ']
4. Dictionary operation
(1) Dict function
The Dict function can create a dictionary with keyword parameters in the following format:
Dict (parameter 1= value 1, parameter 2= value 2, ...) ={parameter 1: value 1, Parameter 2= value 2, ...}
For example, how do I create a dictionary with the name of Jiayounet, age 28?
1
Dict (name=' jiayounet ',age=)
{' Age ': ' Name ': ' Jiayounet '}
(2) Basic operation
The basic behavior of the dictionary is similar to the list in many places, the following example takes the sequence a=[1,3,6,10], the dictionary f={' age ':, ' name ': ' Shushuo '} for example
Diary Summary: Today I learned Python's basic pages, operations, and several major types of containers, and days to learn Python's functions, loops and conditions, classes, and then a general understanding of Python.
Quantitative analyst's Python Diary "Day 1th: Who's going to tell me about Python?" 】