Python Road "First":P Ython introduction and Getting Started

Source: Internet
Author: User
Tags lua python script

Introduction to Python:

First, what is Python

Python (United Kingdom pronunciation:/paθn/American pronunciation:/paθɑn/), is an object-oriented, literal translation of computer programming language.

Each language has its own philosophy:

Pythonde design philosophy is: "Elegant", "clear", "simple"

Ii. The origin of Python

during the 1989 Christmas period, Guido van Rossum to spend time in Amsterdam, determined to develop a new script interpreter, as an inheritance of the ABC language. Python was chosen as the name of the program because he was a fan of the BBC TV show-Monty Python's Flying Circus (Monty Python's Flying Circus).

Iii. What Python can do

Web Program

Python is often used for web development. For example, with the Mod_wsgi module, Apache can run web programs written in Python. Gunicorn, written in the Python language, is also capable of running web programs written in the Python language as a Web server. Python defines the WSGI (Web server Gateway Interface) standard application interface to coordinate the communication between the HTTP server and the Python-based Web program. Some web frameworks, such as Django, Pyramid, TurboGears, Tornado, Web2py, Zope, and flask, can make it easy for programmers to develop and manage complex web programs.

Python's support for various network protocols is perfect and is often used to write server software and network worms. The third-party library twisted supports asynchronous online authoring programs and most standard network protocols (including clients and servers), and provides a variety of tools that are widely used to write high-performance server software. Another gevent, the popular third-party library, is also capable of supporting high-performance, highly concurrent network development.

GUI Development

Python itself contains a tkinter library that supports the development of a simple GUI (graphical User Interface). But more and more Python programmers choose GUI packages such as Wxpython or PYQT to develop cross-platform desktop software. Desktop software that is developed with them runs fast and matches the user's desktop environment. You can also publish a program as a standalone installation package through Pyinstaller.

Operating system

In many operating systems, Python is the standard system component. Most Linux distributions, as well as NetBSD, OpenBSD, and Mac OS X, are integrated with Python, which can be run directly under the terminal. Some installers for Linux distributions are written in Python language, such as Ubuntu's ubiquity installer, Red Hat Linux, and Fedora's Anaconda installer. Gentoo Linux uses Python to write its Portage package management system. The Python standard library contains several libraries that invoke the functionality of the job system. By Pywin32 This third-party package, Python has access to Windows COM services and other Windows APIs. Use the Ironpython,python program to directly invoke the. Net Framework.

Other

NumPy, SciPy, matplotlib can allow Python programmers to write scientific computing programs. Some companies use scons instead of make to build C + + programs.

Many games use C + + to write high-performance modules such as graphical display, while using Python or Lua to write game logic, servers. Compared to Python,lua, the functionality is simpler and smaller, while Python supports more features and data types. Many games, such as Eve Online, use Python to handle a variety of logic in the game.

YouTube, Google, Yahoo!, and NASA all use Python in their own way. Most of the software for the Sugar project is written using Python

So his range of applications is roughly divided into:

Data analysis

System programming

Component integration

Network Services

Image processing

Numerical calculation and scientific calculation

Iv. design philosophy and positioning

Python's design philosophy is "elegant", "clear", "simple". The Python developer's philosophy is "in one way, it's best to have only one way to do something." When designing the Python language, Python developers tend to reject the fancy syntax when faced with a variety of choices, and choose a syntax that is unambiguous or rarely ambiguous. These guidelines are called "Python maxims." Run import this in the Python interpreter to get a complete list

1. Beauty is better than ugliness (Python aims to write graceful code)

2. Clarity is better than obscure (graceful code should be clear, naming specification, style similar)

3. Simplicity is better than complexity (beautiful code should be concise, don't have complex internal implementations)

4. Complexity is better than messy (if the complexity is unavoidable, the code can not be difficult to understand the relationship, to keep the interface concise, more comments!) )

5. Flat is better than nesting (graceful code should be flat, not too much nesting)

6. The interval is better than the compact (graceful code has the appropriate interval, do not expect a line of code to solve the problem, a line of code can not exceed 80 characters, you can change line or start a new logic to write)

7. Readability is important (graceful code is readable)

8. Even in the name of the practicality of the special case, the rules must not be violated (these rules are paramount), and it is not permissible for a special column to follow this rule

9. Do not tolerate all errors unless you are sure you need to (catch exceptions accurately, do not write Except:pass-style code)

10. When there are many possibilities, don't try to guess!

11. Instead try to find one, preferably the only obvious solution (if unsure, use the brute-lifting method)

12. Although this is not easy, because you are not the father of Python (here Dutch refers to Guido)

13. It may be better not to do it, but it's better not to do it without thinking about it (before you do it)

14. If you can't describe your plan to someone, it's certainly not a good plan; and vice versa (Programme evaluation criteria)

15. Namespaces are a wonderful idea, and we should use them more (advocacy and calling

Ps:

Simply compare PHP with Python:

Python is a comprehensive language, and he can not only do web development. The contrast between them should be limited to web development directions. Don't see its phpweb development don't python good, PHP is preconceived, Python's web frame is only a bit late in 2000.

Django got up late, and their web development was pretty good. You can do all of the python you do with PHP. Python gives you a complete solution, both front-end and back-end can be used with Python.

Simply take C and python for comparison:

Python slow, slow serious!!!! The same program is hundreds of times times slower than C is normal.

Programming Style:

First, the grammar requirements

Indentation Unification

Unified-Level code indentation must be consistent! (And it is recommended that all levels of code indentation be the same, 4 spaces recommended in the development specification) do not use tab because if you want to run code on Windows, the definition of Linux and Windows tab is different!!!!

A good editor can improve the efficiency of code development!

All Python, the first line must be top to the wardrobe! The new one at the same level is going to be the top of the wardrobe.

Second, the variable

The first character of an identifier must be a letter in the alphabet (uppercase or lowercase) or an underscore ('_');

Other parts of the identifier name can consist of a letter (uppercase or lowercase), an underscore ('_'), or a number (0-9);

Valid identifier names such as: My, _my_name, name_z12, and A1W2_C3;

Invalid identifier name For example: 2things, this is dog and my-name;

The identifier name is case-sensitive. For example, MyName and myname are not the same identifiers. Note the lowercase y in the former and the uppercase Y in the latter;

Constants: Amount of constant value

Variable: The amount of the value that will change

There is no constant in Python, all values can be changed, but he still has a constant concept, but is artificial you do not change him, the definition of a constant should be in uppercase form;

For example:

Age = 10 This is a constant, it's capitalized! It is a conventional one, but it can be changed!!

Name = ' Ray Zhu ' This is the variable

##### #需要特别注意的是, when defining variables, you cannot define the built-in method that Python comes with.

For example:

Type

The following keywords cannot be declared as variable names:

[' and',' as','assert',' Break','class','Continue','def','del','elif','Else','except','exec','finally',' for',' from','Global','if','Import','inch',' is','Lambda',' not','or','Pass','Print','Print','Raise','return','Try',' while',' with','yield']
View Code

The way a variable is named is the identifier name, which invokes the data in memory through an identifier.

python Environment: First, install Python

Windows:

1. Download the installation package    https://www.python.org/downloads/2, install the    default installation path: C:\python353, configure environment variable    " Right-click Computer "-" "Properties"-"Advanced system Settings"-"Advanced"-"Environment variable"-"in the second content box to find the variable named path of a row, double-click"- "Python installation directory appended to the variable value, with;    such as: the original value; C:\python35, remember that there's a semicolon in front
View Code

Linux:

Linux Yum relies on its own python, to prevent errors, the update here is to install a higher version of python!

  View the default Python version python -v  1, install gcc for compiling python source yum install gcc  2, download the source package, Https://www.python.org/ftp/python/3, unzip and enter the source file  4, compile the installation.  /configure make all make install  5, view version /u sr/local/bin/python3.5-v  6, modify the default Python version mv /usr/bin/python/usr/bin/python2.6 ln -s/usr/local/bin/python3.5/usr/ Bin/python  7/usr/bin/yum head  #  !/usr/bin/python modified to #!/usr/bin/python2.6  
View CodeGetting Started with Python One, the first sentence of Python

Vim hello_world.py Create a file:

# !/usr/bin/env Python3 # _*_ conding:utf-8 _*_ Print ('HelloWorld')
View Code

Perform:

+x hello.py. /hello.py

Attention:

# !/usr/bin/env python Note: The reason why the script was started with Env: because the script interpreter and Linux may be installed in a different directory, env can be found in the system's path directory. At the same time, Env also prescribes some system environment variables. ENV is the abbreviation for the envirnment environment variable #  _*_ coding:utf-8 _*_ The    specified encoding type is utf-8, in Python3 the default encoding is UTF8, plus this does not matter

Second, the code

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)

ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols

It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode

Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...

Therefore, when the Python interpreter loads the code in the. py file, the content is encoded (the default Ascill), so if you do not specify an encoding type, an error will occur if you have Chinese.

Third, the working process of Python

1, Python to read the code into memory, 2, lexical parsing, 3, put to the compiler ======= "generate bytecode, 4, execute bytecode =======" "Generate machine code; 5, CPU execution

iv. executing script incoming parameters

Python has a large number of modules, which makes developing Python programs very concise. There are three types of class libraries:

Python-supplied modules

Industry-Open Source modules

Modules developed by programmers themselves

Within Python, a SYS module is provided, where SYS.ARGV is used to capture the parameters passed in when executing a python script

# !/usr/bin/env python # _*_ coding:utf-8 _*_ Import SYS Print (SYS.ARGV)
v.. pyc file

When you execute Python code, if you import a different. py file, a. pyc file with the same name is automatically generated during execution, which is the bytecode file generated by the Python interpreter after compilation;

The same is true: code is compiled to generate bytecode, and bytecode can be obtained by deserializing the code. (Any bytecode can be code by anti-compilation)

Vi. Variables

Variable: can only be composed of letters, numbers, underscores

It is particularly important to note that variables cannot start with a number when defining a variable

Python keyword cannot be used for variables

Python's built-in functions or classes

It is best not to repeat with Python's built-in stuff;

The value of the string can not be modified, he is in memory is continuous, if you want to change the words must be reserved in the back so do not support the modification!

String attributes, once modified, re-created

Cases:

" Zhurui "  ="boy", guess the next name2 will change it??

In fact, Name2 is immutable, combined with the above rules: string attributes, once modified, re-created

name1 = "Boy" Name1 's value has changed, he will re-open a piece of memory space!

Attention:

" Zhurui "  "zhurui" here although their memory addresses are the same, but their principle is that each in memory to open up a piece of memory space, Python optimized for him. There is a pool concept (make a point of your most used values)! 

The collection is not the same:

name1 = ['ray',  'Karen',  ' Eric '  = name1

Print (name2)
[' Ray ',  ' Karen ',  ' Eric ']
= name1 Point, name1 Add an element will not open a new space, name2 will follow the change;

Vii. Conditional Statementsviii. Basic data Typesix. Follow the bad

Python Road "First":P Ython introduction and Getting Started

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.