Python and Perl, C, and Java language, there are many similarities. However, there are some differences.
In this chapter we'll learn Python syntax, the basis of let you quickly learn Python programming.
The first Python program
Interactive programming
Interactive programming does not need to create a script file, it is through the Python interpreter interaction patterns in writing code.
You only need to input in the command line on Linux Python command to start an interactive programming, prompt window is as follows:

$pythonPython 2.7.6 (default, Sep 9 2014, 15:04:36)[GCC 2 Compatible Apple LLVM 6.0 (clang - 600.0.39)] on DarwinType "help", "copyright", "credits" or "license" for more information.>>>

Window when installing a Python client has already installed the default interactive programming, prompt Window is as follows:

In python prompt, Enter the following text messages, and then press the Enter key to check the running effect:

>>>Print "Hello, Python!" ; 

In Python 2.7.6 version, the above example output results are as follows:

Hello, Python! 

If you are running the new version of Python, so you need to use parentheses in print statements such as:

>>>Print (" Hello, Python! "); 

Script programming
Call the interpreter began to execute the script through script parameters, until completion of the script execution. When the script is completed, the interpreter is no longer valid.
Let's write a simple Python script. To all the Python files. Py for extension. Copies the following source code to test. Py file.

Print "Hello, Python!" ; 

Here, assume that you have already set up the Python interpreter PATH variable. Use the following command to run the program:

$python test. Py 


Hello, Python! 

Let's try another way to perform the Python scripts. Modify the test. Py files, as shown in the following:

#! The/usr/bin/pythonPrint "Hello, Python!" ;

Here, assume that your Python interpreter in/usr/bin directory, use the following command to execute the script:

#$chmod + x test. Py script file to add executable permissions$./test. Py 


Hello, Python!

Python identifierIn python, the identifier is composed of letters, Numbers, underscores.
In python, all identifiers can include English, Numbers, and underscores (_), but you can't begin with Numbers.
In python identifiers are case sensitive.
The following marking the beginning of the identifier is that have a special meaning. Begin with a single underscore (_foo) representatives cannot direct access to the class attribute, must provide interface accessed through the class, can't use "from XXX import *" import;
With double underscore (__foo) on behalf of the private members of a class; To double the underline the beginning and end (__foo__) represent python special identity in a particular way, such as the __init__ () on behalf of the class constructor.
Python reserved characters
The following list shows the reserved words in Python. The reserved word cannot be used as a constant or variable, or any other identifier name.
All Python keywords that contain only lowercase letters.

Line and indent the
The biggest difference is that learning Python and other languages, Python block of code that does not use curly braces ({}) to control class, function, and other logic judgment. The most distinctive python is written in indentation module.
The number of blank indentation is variable, but all of the code block statements must contain the same number of indentation blank, this must be strictly implemented. As shown in the following:

If True:Print "True"The else:Print "False"

The following code will perform error:

#! The/usr/bin/python#- * - coding: utf-8 - * -#file name: test. PyIf True:Print "Answer"Print "True"The else:Print "Answer"#no strict indentation, maintain at execution timePrint "False"
$python test. PyThe File "test. Py", line 5If True:^IndentationError: unexpected indent

IndentationError: unexpected text-indent error is python compiler is trying to tell you "Hi, man, your file format is not correct, may be the TAB and the problem of space not aligned", all the python is very strict with the format.
If it is IndentationError: unindent does not match any outer indentation level error shows that do not match the indentation method you use, have a plenty of the TAB key to indent, have a plenty of white space indentation, instead of simply.
In Python code block, therefore, must use the same number of rows in the first indent number of Spaces.
Suggest that you use in each indentation level individual tabs or two Spaces or four Spaces, remember not to mix
Multi-line statement
Python statements in general with the new row as a statement terminator.
But we can use the backslash (\) line statements can be divided into multiple lines, as shown in the following:

Total=item_one + \Item_two + \item_three

statements contained in the [] and {} or parentheses () there is no need to use multiple lines connector. The following example:

Days=[' Monday, Tuesday, Wednesday,'Thursday', 'Friday] 

Python quotes
Python receiving single quotes (') and double quotation marks ("), three quotation marks (" '" "") to represent the string, the beginning and the end must be the same type of quotes.
Three quotes can be composed of multiple lines, write multiline text shortcut syntax, the phrase documentation string, a specific location in the file, are treated as comments.

The word='word'Sentence="this is a sentence."Com.lowagie.text.paragraph="" "this is a paragraph.Contains multiple statements "" "

Python annotation
Python single-line comments using #at the beginning.

#! The/usr/bin/python#- * - coding: utf-8 - * -#file name: test. Py#the first annotation print "Hello, Python!" ; The second comment #


Hello, Python! 

Annotations can be at the end of the line statement or expression:

Name="Madisetti" #this is a comment

python multiline comment using three single quotes (') or three single quotation marks (" ").

#! The/usr/bin/python#- * - coding: utf-8 - * -#file name: test. Py' ' 'This is a multi-line comment, the use of single quotation marks.This is a multi-line comment, the use of single quotation marks.This is a multi-line comment, the use of single quotation marks.' ' '"" "This is a multi-line comment, use double quotation marks.This is a multi-line comment, use double quotation marks.This is a multi-line comment, use double quotation marks."" "

Python empty lines
Function or class method with a blank line between between space, the beginning of a new code. Also use a line or limits between classes and functions entrance space, the beginning of the function entry is highlighted.
Empty line is different from code indentation, blank line is not part of the Python syntax. Writing is not insert a blank line, run the Python interpreter will not go wrong. But empty lines lies in the separation of two different function or meaning of the code, facilitate code maintenance or reconstruction in the future.
Remember: a blank line is a part of program code.
Waiting for the user to enter
The following program will wait for the user input after press the return key:

#! The/usr/bin/pythonRaw_input (" \ n \ nPress the enter key to exit. ")

the above code, the "\ n \ n" before the results output can output two new blank line. Once the user press the button, the program will exit.
The same line showed multiple statements
Python can use multiple statements on the same line, between statements use a semicolon (;) Segmentation, the following is a simple example:

The import sys. X='foo'; Sys. Stdout. Write (x + '\ n') 

Multiple statements constitute a code group
Indent the same set of statements that constitute a code block, we call it the code group.
Like the if, while, def and class compound statement, first line begins with a keyword, ends with a colon (:), after the bank code groups one or more lines of code.
We will first line and the back of the code set is called a clause (clause).
The following example:

The if expression:suiteElif expression:suiteThe else:suite

command line parametersMany programs can perform some operations to check some basic letters, Python can use -h parameter check the parameters help information:

$python - h
Usage: python [option]... [-c CMD | -m mod | file | -] [arg]...The Options and the arguments (and corresponding environment variables) :CMD - c: program passed in as a string (terminates the option list)- d: the debug output from parser (braking PYTHONDEBUG=x)E: ignore environment variables (to as PYTHONPATH)- h: print this help message and exit[etc.]

This concludes the body part