[First Section]python basics

Source: Internet
Author: User
Tags spark rdd

Write in front:

The latest version of Python is now 3.6. This blog is based on version 3.6, and will mention the difference between version 2.7 and this version!

****************************************************************************

Directory:

1. Introduction to Python.

2. Features of Python.

3. Python's purpose.

4. Select the purpose of Python.

5. Install Python. (Linux environment)

6. The process of executing Python code.

7. Python interpreter.

8. Python programming style.

9. Python data type.

10. Run the first program.


Body:

1. Python Introduction: (from Baidu Encyclopedia)

The founder of Python is Guido van Rossum. During the Christmas of 1989, in Amsterdam, Guido was determined to develop a new script interpreter as an inheritance of the ABC language in order to get rid of the boredom of Christmas. Python was chosen as the name of the programming language because he was a fan of the comedy community called Monty Python.

2. Features of Python:

(1) Python is an object-oriented, interpreted computer programming language. The syntax is simple and clear, forcing whitespace characters to be indented as statements.

(2) Python has a rich and powerful library, also known as the glue language. It is easy to combine modules written in other languages, such as C + +.

(3) Advantages of Python:

1) Simple, elegant, clear
2) Powerful modular three-party library
3) Easy to transplant
4) Object-oriented
5) Expandable (c\java\c# ...)
(4) Python's drawbacks:

1) code cannot be encrypted
2) Slow speed
3) ...
3. Python's Purpose:

(1) Web site: There are many good open-source web frameworks, such as Django (the most popular), Tornado (lightweight, asynchronous), Flask (micro), web.py (simple), and so on.

(2) Data acquisition: There are good HTTP libraries, such as URLLIB2, requests and so on. There is also a high-level screen crawl and Web Capture Framework scrapy. And the page parsing also has a lot of libraries, such as lxml, XPath, BeautifulSoup and so on.

(3) Big Data analysis: Common modules have numpy, Pandas. and supports write mapreduce, Pyspark processing spark RDD (elastic distributed Datasets).

(4) Operation and maintenance Automation: scripting, Web Platform, automated daily work.

(5) Scientific calculation: In the scientific calculation is also used more and more widely, commonly used modules have NumPy, SciPy.

......

4. Select the purpose of Python:

First of all, Python syntax is simple and straightforward, not like Java, C and other programming languages complicated syntax is confusing;

Second, many people are not programming trained, without any programming skills, so learning python can quickly get started.

Third, Python excels in automating operations, and Python can quickly develop lightweight, automated operations tools that significantly reduce operational costs.

Finally, Python is a cross-platform programming language that runs on all of the current operating systems, with the feature of "once compiled and run everywhere".

Five, Python has a lot of others to develop a good standard library, that is, the former man-made good wheel, we can directly use, greatly shorten the program development cycle.

.....

5. Install Python

System Environment: CentOS7.3

[Email protected] ~]# cat/etc/redhat-release//view current system environment. CentOS Linux release 7.3.1611 (Core) [[email protected] ~]# python-v//View the Python version of the current system installed by default. Python 2.7.5[[email protected] ~]#

Note: The yum command under the CentOS system requires Python version 2.7.5, so it cannot be uninstalled directly.

First download the Python3.6 installation package on the official website:

Can be downloaded to the local and then uploaded to the Linux system, or directly through the wget command download, here to choose the second method.

Source Code Compilation Installation:

[Email protected] tool]# wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
[[email protected] tool]# yum-y Install Readline-devel//This package is not installed, after the installation of Python, in the interactive interpreter operation of the key, backspace, etc. will appear garbled. [Email protected] tool]# tar zxf python-3.6.0.tgz [[email protected] tool]# CD Python-3.6.0[[email protected] Python-3.6. 0]#./configure--prefix=/usr/local/python3.6 [[email protected] python-3.6.0]# yum-y install zlib zlib-devel [[Email Pro] Tected] python-3.6.0]# make && make install [[email protected] python-3.6.0]# echo $?0[[email protected] Python-3. 6.0]#

Then let the system automatically use Python 3.6:

[[email protected] bin]# ll python*lrwxrwxrwx. 1 root root     7 12 Month  15 04:32 python -> python2lrwxrwxrwx. 1 root  root    9 12 Month  15 04:32 python2 ->  Python2.7-rwxr-xr-x. 1 root root 7136 11 Month   6 08:29 python2.7[[ email protected] bin]# mv  python{,.bak}  //to rename the python file [[email  Protected] bin]# ll python*lrwxrwxrwx. 1 root root    9  12 Month  15 04:32 python2 -> python2.7-rwxr-xr-x. 1 root root  7136 11 Month   6 08:29 python2.7lrwxrwxrwx. 1 root root     7 12 Month  15 04:32 python.bak -> python2[[email protected]  Bin]# ln -sv /usr/local/python3.6/bin/python3.6 /usr/bin/python "/usr/bin/python"  ->  "/usr/local/python3.6/bin/python3.6"     //replaces the system default python[[email protected] bin]# with the latest python3.6

After compiling the installation, a Python3 connection is automatically generated under/usr/local/python3.6/bin, which points to python3.6 in the bin directory.

Because Yum uses python2, it does not work when replaced with Python3, and is set to continue using the python2.7.5

To modify a Yum configuration file:

[Email protected] ~]# ll/usr/bin/python*lrwxrwxrwx. 1 root root 21 January 3 15:31/usr/bin/python-/usr/local/python3.6/bin/python3.6lrwxrwxrwx. 1 root root 9 December 04:32/usr/bin/python2-Python2.7-rwxr-xr-x. 1 root root 7136 November 6 08:29/usr/bin/python2.7lrwxrwxrwx. 1 root root 7 December 04:32/usr/bin/python.bak-Python2[[email protected] ~]# Vim/usr/bin/yum #!/usr/bin/python2 .7//Change Python on the header line of the Yum config file to python2.7.

Now look at the default Python version of the system:

[[email protected] ~]# Python-vpython 3.6.0[[email protected] ~]# ll ' which Python ' lrwxrwxrwx. 1 root root 34 January 3 15:42/usr/bin/python/usr/local/python3.6/bin/python3.6[[email protected] ~]#

Already in effect!

6. The process of executing Python code.

Speaking of the implementation of Python code, first of all, explain what is interpreted language? In fact, Python is an interpretive programming language, of course, there is a language estimated to listen to not unfamiliar---compiled language!

Understanding of interpreted and compiled languages: computers are not able to recognize high-level languages, so when we run a high-level language program, we need a "translator" to engage in [the process of turning a high-level language into a computer-readable machine language]. This process is divided into two categories: The first is compilation, the second is interpretation. Compiled language before the execution of the program, the program through the compiler to perform a compilation process, that is, the program into machine language, so that the program runs without the need for translation, direct execution. The most typical example is the C language. The explanatory language does not have this compilation process, it is when the program runs, through the interpreter to the program to explain the line, and then run directly, the most typical example is the Ruby language.

Through the above description, we can summarize the advantages and disadvantages of the explanatory language and the compiled language: because the compiler language in the program before the program has already made a "translation", so at run time to ignore the "translation" process, so the efficiency is higher. But there are also some explanatory languages that can be optimized for the interpreter, optimizing the entire program when translating the program, thus exceeding the compiled language efficiently.

In addition, because of the existence of virtual machine-based languages such as Java, the language can not be purely divided into the interpretation of the type and the two types of compilation. Java, for example, begins by compiling the program into a bytecode file by the compiler and then interpreting it as a machine file at run time through the interpreter. So Java is a language that is compiled and interpreted first.

Speaking of which, what exactly is Python?

Python, like Java, is also a virtual machine-based language, and when we enter "Python hello.py" on the command line, it actually activates the Python interpreter, telling the interpreter: You're going to start working. But before "explain", the first thing Python does is compile, just like Java. Students familiar with Java can consider how to execute a Java program at the command line:

Javac Hello.java

Java Hello

It's just that when we're using an IDE like Eclipse, this two is part of the integration. In fact, Python is also the same, when we execute "python hello.py", Python also executes such a process, so our description of Python should be this:Python is a first compiled after the interpretation of the language.

Now the topic-The Python code is executed : Python compiles the source code (. py file) into a bytecode (. pyc file), and then hands it to the bytecode virtual machine, a virtual machine executes a bytecode instruction, The byte code is translated into the corresponding machine instruction (machine code)to complete the execution of the program.

Two concepts: Pycodeobject and PYC. The PYC we see on the hard drive is naturally needless to say, and pycodeobject is actually a result of the Python compiler actually compiling (the code object). When the Python program runs, the results of the compilation are saved in the memory Pycodeobject, and when the Python program finishes running, the Python interpreter writes Pycodeobject back to the PYc file. When the Python program runs for the second time, the program will first look for the PYc file on the hard disk, and if it is found, simply load it, or repeat the process above, and the benefit is to not repeat the compilation and improve execution efficiency. so we should be able to locate Pycodeobject and pyc files, we say that PYc file is actually a kind of persistent saving way of pycodeobject. (a general understanding of how Python code executes).

7. Python Interpreter

1) CPython

When we finish Python, the default interpreter is CPython, which is the official default interpreter. CPython is written in C and translates code into bytecode (bytecode) when executing code.

2) IPython

Based on the CPython of an interactive interpreter, equivalent to an enhanced version of the default interpreter, the most significant feature is automatic completion, very useful.

3) PyPy

The PyPy itself is written by Python and uses the JIT compiler (instant compiler) technique, which translates code into machine code when executing code. Performance is better than CPython. Java also uses the JIT compiler.

4) Jython

Jython is an interpreter written by Java that can be used to load Java modules into a Python module, or to package Python code into a jar package, meaning that Java programs are allowed to be written in Python. Code is translated into Java bytecode when executing the code and then executed using the JRE (Java Runtime Environment).

5) IronPython

The Python language that works on the. NET platform.

8. Python programming style.

In fact, not only python, for any kind of programming language, the formation of a good programming style, in the subsequent code review and management for themselves and others to bring a lot of convenience, saving valuable time.

1) Indent Unity

Python is indented with whitespace as a statement, meaning that the statement does not have a trailing character, giving benefits to people who tend to write less than one fi, preferably in python with 4 spaces as the indent, which is what most people are accustomed to!

2) Code Comment

This is particularly important. It is said that high-quality code, the explanatory note is more than the amount of code, detailed code comments regardless of their later maintenance or open source, it is necessary. Like a popular software, if there is no rich use of the document, how many people do you think will be patient to spend a lot of time to study it!

3) Space use

Add 1 spaces to both sides of the operator, followed by a comma. But there are no spaces around the parentheses.

In functions, classes, and some function code blocks, empty a row to separate them.

4) naming

Module: Write your own module, file name All lowercase, long name words are separated by an underscore.

Class: Big/Small Hump named. I usually use the big hump name, that is, the first letter of each word capitalized. A private property, a private method, in a class, prefixed by a double underscore.

Function: The first word is lowercase and the remaining first letter is capitalized.

Variable: All lowercase, with the words underlined.

reminder: All names must be able to briefly describe the meaning of this code.

5) Code line wrapping

by following the syntax rules, such as a long expression, you can wrap a line on either side of one of the small expressions, rather than splitting the small expression so that it is easier to read.

6) Meaning of the symbol

"-" indicates a minus sign instead of a horizontal line

9. Python data type.

1) string

A. Use single quotation marks ("'), which are enclosed in single quotation marks to denote strings;

>>> str = ' This is String ' >>> print (str)

B. Use double quotation marks (""), which are enclosed in double quotation marks to denote a string;

>>> str = "This is string" >>> print (str)

C. Three quotation marks ("" "" "")

>>> str = "" "This is ... string, ... xxxx" "" >>> print (str) This is a string, xxx xxxx >>>

Note: The string in single quotation marks is exactly the same as the string usage in double quotation marks.

2) Integer

>>> int = >>> print (int)

3) floating point number

>>> float = 2.3 >>> print (float)

4) Boolean type

>>> bool = False >>> print (bool) >>> bool = True >>> print (BOOL)

5) Number

Includes integers and floating-point numbers.

10. Run the first program.

A. Run under the interactive interpreter:

[[email protected] ~]# Pythonpython 3.6.0 (default, Jan 3, 15:07:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on Linu XType "Help", "copyright", "credits" or "license" for more information.>>> print ("Hello World") Hello world> >>

B. Write a script to run:

[[email protected] python]# vim hello.py #!/usr/bin/python print ("Hello World")
[[email protected] python]# python hello.py hello world[[email protected] python]#


Finish the first day!



This article is from the "Yangbin" blog, make sure to keep this source http://13683137989.blog.51cto.com/9636221/1888684

[First Section]python basics

Related Article

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.