[Technical Article] Python first, python first
Address: http://www.cnblogs.com/aiweixiao/p/8390413.html
Original article address
Click follow public account wenyuqinghuai
1. Preface
I have known the Python language for a long time. I can always see it in technical blogs and public accounts. I feel that the voice is higher than that of PHP and Java. It's 2018. Let's keep up with it. "Life is short, I use Python ".
My life is so short that I use Python2. the language itself has 2.1 characteristics.
[01 glue language]: Python has a rich and powerful library. It is often called the glue language, and can easily connect various modules (especially C/C ++) made in other languages.
[02 development philosophy]: the philosophy of Python developers is "one method, preferably only one method to do one thing ".
[03 strong type]: Javascript and PHP are weak types, while Python and Ruby are strong types. A weak type can be converted to an insecure type. A strong type is not allowed.
2.2 syntax
[01 indent is syntax]: Python developers intentionally disable compilation of programs that violate the indent rules to force programmers to develop good programming habits. In addition, the Python language uses indentation to indicate the start and exit of the statement block (Off-side rules), rather than using curly braces or certain keywords.
[02 Function Definition ]:
= The function code block starts with the def keyword, followed by the Function Identifier name and parentheses ()
= Python uses lambda to create anonymous functions.
Python language flag
[03 Note]: in python, multiple comments use three single quotes (''') or three double quotes (""").
[04 others]: print by default, the output is a line feed. To implement a line feed, you must add a comma at the end of the variable,
[05 string ]:
= If you want to obtain a substring from a string, you can use the variable [header Subscript: tail subscript] to extract the corresponding string. S = 'ilovepython '; the result of s [] is love.
= String link, printstr + "TEST" # output the connected string
[06 list ]:
= List is the most frequently used data type in Python.
= List. append ('Google ') # Use append () to add elements
[07 tuples ]:
= Tuples are another data type, similar to List ). The tuples are identified. Internal elements are separated by commas. However, tuples cannot be assigned values twice, which is equivalent to read-only lists.
[08 dictionary ]:
= Dictionary is the most flexible built-in data structure type in python except the list. A list is an ordered object set and a dictionary is an unordered object set. The difference between the two is that the elements in the dictionary are accessed by keys, rather than by offset.
The dictionary is identified. A dictionary consists of an index (key) and its corresponding value.
= Features of the dictionary key: The dictionary value can be any python object without restrictions. It can be either a standard object or a user-defined object, but not a key.
Python brain chart
[09 string-difference]: dictionaries, lists, and tuples are different in construction. The list is square brackets [], the tuples are Parentheses (), and the dictionary is curly brackets {}.
The same key cannot appear twice. If the same key is assigned twice during creation, the last value will be remembered.
[10 statement]: Python pass is a null statement to maintain the integrity of the program structure.
[11 whether variables can be changed ]:
In python, strings, tuples, and numbers are unchangeable objects, while list and dict are modifiable objects.
Immutable type: variable value a = 5 and then value a = 10. Here, a new int value object 10 is generated, and a points to it, and 5 is discarded, instead of changing the value of a, it is equivalent to generating.
Variable type: If the variable value is la = [1, 2, 3, 4], and then la [2] = 5, the value of the third element of list la is changed, and la itself is not dynamic, only some of its internal values have been modified.
[Module 12]: from fib import fig & import
[13 I/O ]:
= Input: raw_input, input
3. install and use Python 3.1
Follow these steps to install Python on Unix and Linux:
1) Open the WEB browser to access the http://www.python.org/download/
2) Select the source code compressed package for Unix/Linux.
3) download and decompress the compressed package.
4) if you need to customize some options to modify Modules/Setup
5) execute the./configure script
6) make
7) make install
After the above operations are performed, Python will be installed in the/usr/local/bin directory, and the Python library will be installed in the/usr/local/lib/pythonXX and XX Python version numbers for you.
Python-V
Python application 3.2 runs Python
There are three ways to run Python:
1. Interactive Interpreter:
2. Command Line script: python script. py
3. IDE running (such as PyCharm)
3.3 Chinese Encoding
In Python, the default encoding format is ASCII. When the encoding format is not modified, Chinese characters cannot be correctly printed. Therefore, an error is returned when reading Chinese characters.
The solution is to add #-*-coding: UTF-8-*-or # coding = UTF-8 at the beginning of the file.
Note: # Do not use spaces on both sides of coding = UTF-8 =.
Note: The Python3.X source code file uses UTF-8 encoding by default, so it can be resolved to Chinese, without the need to specify the UTF-8 encoding.
4. Code practice 4.1 hello world
$python
>>>print('Hello World!')
4.2 object-oriented
Source Code address on GitHub: http://t.cn/RQjcvfl
Code example: A CommonPyClass object is created here. The run () function is an entry function that calls the actual function to be run.
Python object-oriented 5. written at the end
The content of Python is far more than that. This article mainly refers to its syntax, simple object-oriented demo, and subsequent Python CGI, GUI programming, and linking to the database, and Applications in machine learning, big data, automated O & M, and many other aspects are written here today. You are welcome to pay attention to subsequent articles and updates.