Python from getting started to py trading, basic syntax, random notes (ii)

Source: Internet
Author: User
Tags python script

inheritance of the class:

The class itself is further subdivided into subclasses, known as inheritance, such as birds that belong to animals, and birds are sub-categories of animals.
Subclasses have all the properties of the parent class
1. Basic form of inheritance

Class name (parent class name):
Property
Method:
Method body

2. Example:

3. Parent class defaults to Object
4. Special usage of the class:
Each method in a class has a self parameter that calls its own property or method inside a method
Constructor Method Init(), which is called automatically when instantiating an object, is typically initialized

container container:

Objects that can store multiple elements are called containers, such as lists, dictionaries
Dictionary {} Unique mapping type, object variable, key must use immutable object, can use different type of key value
1. Dictionary: key:value,{}:

c = {"id": +, "name": "Wuxunjie", "" Age "": 22}

Value can be modified by key, key cannot be changed
such as c["id"] = 20
2. Add a new key value pair to the dictionary by assigning a value to the new key
Adding dic[' tel ']= ' 12345678 ' will be randomly inserted into the dictionary, because the dictionary is unordered
3. Common methods of dictionaries
Dic.keys (): Get all the keys
Del dci[' Tel '] Delete the Tel element in the DCI dictionary
Dic.clear Empty Dictionary
Del (DIC) Delete dictionary

operation of a text file in Python

Has the most basic text file read and write function
1. Create a File object
f = Open ("Text file")
2. READ:

str = f.read (N) #读取N bytes Data
STR = F.radline () #读取一行
STR = F.readlines () #读取所有行

3. Write:
F.write (' write content ') #将所需内容写入文件之中
4. Close:
F.close ()

the structure of the module in Python

Modules are designed to better organize existing programs to facilitate re-use
In Python, a. py file forms a module. You can call a program in another file
1. Introduction of Modules
Define a Module 1 to implement a method
Import Module 1 via import in Module 2 without the. py suffix
In Module 2 through the module. Object can be called
2. Other Introduction methods:

Import a as B # introduces module A and is named B
From a import B # The B object is introduced from module A. called without stating
From a import * # All objects are introduced from module A and are not required to be called

3. Modules can be placed in the same folder to form a module package that must contain an init. py file
Introduction Module Package: Import package name. module

Special parameter passing in Python

1. Can be passed by name

2. You can specify default values, but must be from left to right

3. Pass multiple parameters, define the function when the parameter is preceded by *

4. The pass-through dictionary needs to be added, or the parameter can be passed in a unpacked way

common functions in python related to loops

1.Range function, you can construct the list

Output Result:
A c e g i K
2.enumerate (): Subscript and element can be obtained at the same time in each loop

Output Result:

3.zip (): You can remove an element from each sequence in a single loop, traversing it in parallel

Output Result:

4. Take Max Max ()
5. Take min min ()
6. Length Len ()
7. Pick up and Die Divmod ()
8. Seek the second party POW ()
9. Floating-point round ()
10.CMP () Comparison of two strings
11.String function
String. Capitalize () capitalize the first letter of the string
Str.replace (old,new, number of replacements) replace
Str.split (mark, number) cut

function objects in Python

In Python, a function is also an object that can be assigned to another object name, or passed as a parameter.
1.LAMBDA-expression
A minimal function that quickly defines a single line, borrowed from Lisp and can be used wherever a function is needed

>
g = Lambda x,y:x*y
G (2,3)

Output: 6

Output: 18
2.map (function, list): is a python built-in function that takes each parameter into the lambda operation

Output:

3.filter (function, list): Filtering will filter out the result of true in the function

Output:

4.reduce (): Reduce successive operations list of each item, the number of parameters accepted is 2, generally used for recursive operations
Reduce (lambda X,y:x+y,rang (1,101)) #递归加法

l = range(1,6)def f(x,y)    return x*yreduce(f,l)

Output:
120
Example 2:

Output: 92

Regular Expressions (RE)

A small, highly specialized programming language. Embedded in Python, implemented with the RE module
You can specify rules for the corresponding set of strings that you want to match
The string set may contain English statements, e-mail addresses, commands, or anything you want to fix.
Modify or split a string in various ways

exception handling in Python

Example:

try:except exception1:else:finally:

If no exception is caught in this level, throw up

using the math library in Python

There are many built-in library functions in Python:
Plane ticket: http://python.usyiyi.cn/python_343/library/functions.html
1.math mainly deals with mathematical-related operations
MATH.E # Natural Constant E
Math.PI # pi Pi

Python's advantages in data mining

R file mining is still a bit weak, but it's already packaged, like a software
Python functions are more than R, faster than R
1.python Standard Library
2.Numpy and scipy: Common mathematical operations
3.Matplotlib: Visualization of data
4. Library of symbolic Operations: SymPy and Theano
5.Scikit Learn: Encapsulates almost all of the classic algorithms

python configuration installs Easy_install and PIP, plug-in management tool, similar to Yum in Redhat

1. Configure Python environment variables
2. Configure Python script environment variables
3. Download the Setuptools, unzip to any directory
4. Go to the directory above
5.Python setup.py Install
6. Verify Easy_install–showhelp
7.easy_install Common Commands
Easy_install installation Package
-U update
-M Delete Library
8.PIP installation is similar to Easy_install
PIP Install package
9. If an ASCII-related exception occurs
To modify the lib\mimetype.py file under the Python installation path, add the following lines under import:

If sys.getdefaultencoding ()! = ' GBK ';
Reload (SYS)
Sys.setfefaultencoding (' GBK ')

python is configured to install NumPy, an open source digital extension that can be used to handle storage and large matrices

Available Easy_install and PIP installation
or download the EXE format installation package
or source code installation

Basic exercises:
Calculator

#!/usr/bin/python#coding: Utf-8From _future_ import divisiondef Add (x, y)returnX+ydef minu (x, y)returnX-ydef Cheng (x, y)returnX*ydef divid (x, y)returnX/Y#def operator (x,o,y)# if O = = "+":# Add (x, y)# elif O = = "-":# minu (x, y)# elif O = = "*":# cheng (x, y)# elif O = = "/":# divID (x, y)operator = {"+": Add,"-": Minu,"*": Cheng,"/":d ivid}def F (x,o,y): print operator.Get(o) (x, Y)

Call F (3, "+", 2)
Output: 5

Python from getting started to py trading, basic syntax, random notes (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.