Home  >  Article  >  Backend Development  >  What is Python? How to use Python?

What is Python? How to use Python?

PHP中文网
PHP中文网Original
2017-06-20 13:36:292077browse

Introduction to Python

## Python past and present life

The founder of Python is Guido van Rossum. During the Christmas period of 1989, in order to kill time in Amsterdam, Guido van Rossum decided to develop a new script interpreter as a successor to the ABC language.

In the latest TIOBE rankings, Python overtakes PHP to occupy the fifth place! ! !

As can be seen from the above figure, Python is showing an overall upward trend, reflecting that Python is becoming more and more widely used and is gradually recognized by the industry! ! !

Python can be used in many fields, such as: data analysis, component integration, network services, image processing, numerical computing and scientific computing and many other fields. At present, almost all large and medium-sized Internet companies in the industry are using Python, such as: Youtube, Dropbox, BT, Quora (China Zhihu), Douban, Zhihu, Google, Yahoo!, Facebook, NASA, Baidu, Tencent, Autohome, Meituan etc. Things that Internet companies widely use Python to do generally include:

Automated operation and maintenance, Automated testing, Big data analysis, crawlers, Web, etc.

Attention: The above highlighted font indicates that the company mainly uses Python language for development

Why Python instead of other languages?

C and Python, Java, C#, etc.

C language: The code is compiled to obtain machine code. The machine code is directly executed on the processor, and each instruction controls the CPU work

Other languages: The code is compiled to obtain bytecode, the virtual machine executes the bytecode and converts it into machine code and then executes it on the processor

Python and C Python is a language developed from C

For use: Python’s class library is complete and simple to use. If you want to achieve the same function, Python can solve it with 10 lines of code, while C may require 100 lines or more.

For speed: Python’s Compared with C, the running speed is definitely slower.

Python and Java, C#, etc.

For use: Linux original Python, other languages ​​​​are not available; the above languages ​​​​have very rich Class library support

Regarding speed: Python
maybe slightly inferior in speed

So, there is no essential difference between Python and other languages. The other differences are: being good at a certain field and having rich talents. , first impression.

Types of Python

      ##Cpython
    • The official version of Python, implemented in C language, The most widely used, CPython implementation converts source files (py files) into bytecode files (pyc files), and then runs on the Python virtual machine.


    • Jyhton
    • Java implementation of Python, Jython will dynamically compile Python code into Java bytecode, and then run on the JVM.


    • IronPython
    • A C# implementation of Python, IronPython compiles Python code into C# bytecode and then runs it on the CLR. (Similar to Jython)


    • PyPy (Special)
    • Python implemented by Python recompiles Python's bytecode bytecode into machine code.


    . RubyPython, Brython...

Except for PyPy, the corresponding relationships and execution processes of other Pythons are as follows:

PyPy, in Based on Python, Python's bytecode is further processed to improve execution speed!

Getting started with Python

1. The first sentence of Python code

is in the /home/dev/ directory Create hello.py file with the following content:

##1

Execute the hello.py file, that is: python /home/dev/hello.py

The internal execution process of python is as follows:

2. Interpreter

When executing python /home/dev/hello.py in the previous step, clearly indicate the hello.py script Executed by the python interpreter.

If you want to execute a python script similar to executing a shell script, for example: ./hello.py , then you need to specify the interpreter at the head of the hello.py file, as follows:

print
"hello,world"
##1
2
3
#!/usr/bin/env python
print "hello,world"
##In this way, execute: .
/hello .py

is enough. ps: You need to give hello.py execution permission before execution, chmod 755 hello.py

3. Content encoding

The python interpreter is loading .py file, the content will be encoded (default ascill)

ASCII (American Standard Code for Information Interchange, American Standard Information Interchange Code) is a computer coding system based on the Latin alphabet, mainly Used to display modern English and other Western European languages, it can only be represented by up to 8 bits (one byte), that is: 2**8 = 256, so the ASCII code can only represent up to 256 symbols.

Obviously ASCII code cannot represent all the various texts and symbols in the world, so a new one is needed that can represent The encoding of all characters and symbols, namely: Unicode

Unicode (Unicode, Universal Code, Unicode) is a character encoding used on computers. Unicode was created to solve the limitations of traditional character encoding schemes. It sets a unified and unique binary encoding for each character in each language, stipulating that all characters and symbols must be represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,

Note: What is mentioned here is at least 2 bytes, possibly more


UTF-8, which is the compression of Unicode encoding And optimization, he no longer uses at least 2 bytes, but classifies all characters and symbols: the content in the ascii code is saved in 1 byte, European characters are saved in 2 bytes, and East Asian characters Use 3 bytes to save...

So, when the python interpreter loads the code in the .py file, it will encode the content (default ascill), if it is the following code:

Error: ascii code cannot represent Chinese

##1
2
3
#!/usr/bin/env python
print
"Hello, world"
##Correction: The python interpreter should be told explicitly what encoding to use to execute the source code, that is:

##12
3
4
!/usr/bin/env python
# -*- coding: utf-8 -*-
print " Hello World"

4. Comments

Current line attention: # Annotated content

Multi-line comments: """ Annotated content """

5. Execute the script and pass in parameters

Python has a large number of modules, which makes developing Python programs very simple. The class library includes three types:

  • Modules provided internally by Python

  • Open source modules in the industry

  • Modules developed by programmers themselves

Python provides a sys module internally, in which sys.argv is used to capture the parameters passed in when executing python scripts

##1
2
3
4
5
6
#!/usr/bin/env python
# -* - coding: utf-8 -*-
##import
sys
print
sys.argv

The above is the detailed content of What is Python? How to use Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Login interfaceNext article:Login interface

Related articles

See more