Python basic syntax

Source: Internet
Author: User

Python Basic Syntax

The Python language has many similarities with languages such as Perl,c and Java. However, there are some differences.

In this chapter we will learn the basic syntax of Python and let you quickly learn Python programming.


The first Python programInteractive Programming

Interactive programming does not require the creation of a script file, it is written in the interactive mode of the Python interpreter.

On Linux you only need to enter the Python command at the command line to start interactive programming, prompting the following window:

650) this.width=650; "title=" 1.PNG "src=" Http://s3.51cto.com/wyfs02/M00/80/78/wKiom1dCqnPyr24IAAARPQS5Sgg821.png " alt= "Wkiom1dcqnpyr24iaaarpqs5sgg821.png"/>

The default interactive programming client is installed on window when you install Python, with the following prompt:

650) this.width=650; "title=" 2.PNG "src=" Http://s4.51cto.com/wyfs02/M02/80/78/wKiom1dCq13gv-hnAAAbaDA6hpM465.png " alt= "Wkiom1dcq13gv-hnaaabada6hpm465.png"/>

Enter the following text information in the Python prompt, and then press ENTER to view the results of the operation:

650) this.width=650; "title=" 3.PNG "src=" Http://s1.51cto.com/wyfs02/M01/80/76/wKioL1dCrRuTy_5dAAAGuUgBw3o740.png " alt= "Wkiol1dcrruty_5daaaguugbw3o740.png"/>

Script-type programming

As the Linux system runs through Python scripts, let's write a simple Python script. All Python files will have a. py extension, so let's write a Python script named hello.py.

650) this.width=650; "title=" 6.PNG "src=" Http://s2.51cto.com/wyfs02/M01/80/77/wKioL1dCtESj2VizAAAFaXBpekY003.png " alt= "Wkiol1dctesj2vizaaafaxbpeky003.png"/>

650) this.width=650; "title=" 4.PNG "src=" Http://s2.51cto.com/wyfs02/M02/80/79/wKiom1dCs4TBbAL1AAAG0gC11fk365.png " alt= "Wkiom1dcs4tbbal1aaag0gc11fk365.png"/>

After the script editing is complete: Wq saves the exit and gives the file the permissions it can perform.

650) this.width=650; "title=" 5.PNG "src=" Http://s3.51cto.com/wyfs02/M01/80/78/wKioL1dCtQ-yx3gaAAAUTFx96R4657.png " alt= "Wkiol1dctq-yx3gaaaautfx96r4657.png"/>

Let's run the script.

650) this.width=650; "Title=" runs the python script "src=" http://s1.51cto.com/wyfs02/M00/80/78/ Wkiol1dctysar89vaaapiumghlo707.png "alt=" Wkiol1dctysar89vaaapiumghlo707.png "/>


Python identifiers

In Python, identifiers are made up of letters, numbers, and underscores.

In Python, all identifiers can include English, numeric, and underscore (_), but cannot begin with a number.

In Python, identifiers in Python are case-sensitive.

Identifiers that begin with an underscore are of special significance. A class attribute that begins with a single underscore (_foo) cannot be accessed directly, and must be accessed through the interface provided by the class, and cannot be imported with "from XXX import *".

A double underscore (__foo) represents a private member of a class; a double underscore (__foo__) represents a special method-specific identifier for Python, such as __init__ (), which represents the constructor of a class.


Python reserved characters

The following list shows the reserved words in Python. These reserved words cannot be used as constants or variables, or any other identifier names.

All Python keywords contain only lowercase letters.


Font and
Exec
Not
Assert
Finally
Or
Break
For
Pass
Class
From
Print
Continue
Global
Raise
Def
If
Return
Del
Import
Try
Elif
Inch
While
Else
Is
With
Except
Lambda
Yield


Line and indent

The biggest difference between learning Python and other languages is that Python's code block does not use curly braces ({}) to control classes, functions, and other logical judgments. Python's most distinctive feature is the use of indentation to write modules.

The amount of whitespace indented is variable, but all code block statements must contain the same amount of indentation whitespace, which must be strictly enforced. Let's run a piece of code as follows:

x,y=1,2

650) this.width=650; "title=" 8.PNG "src=" Http://s1.51cto.com/wyfs02/M01/80/7B/wKiom1dCvefg2BteAAAVfLcTQZM016.png " alt= "Wkiom1dcvefg2bteaaavflctqzm016.png"/>

indentationerror:unexpected Indent The error is that the Python compiler is telling you "Hi, man, your file is not in the right format, it may be a question of misaligned tab and Space", all Python is very strict in format.

If the indentationerror:unindent does not match any outer indentation level error indicates that you are using an inconsistent indentation, there are tab indents, some spaces are indented, instead The same can be done.

Therefore, you must use the same number of indent spaces in a Python code block.

It is recommended that you use a single tab at each indent level or two spaces or four spaces, remember not to mix

The code after the modification is as follows:

650) this.width=650; "title=" 9.PNG "src=" Http://s1.51cto.com/wyfs02/M00/80/7A/wKioL1dCv9nTPzW3AAAZ1XAg8TM044.png " alt= "Wkiol1dcv9ntpzw3aaaz1xag8tm044.png"/>


Multi-line statements

A new line is generally used as the Terminator for a statement in a Python statement.

But we can use a slash (\) to divide a line of statements into multiple lines of display, as follows:

650) this.width=650; "title=" 10.PNG "src=" http://s1.51cto.com/wyfs02/M00/80/7A/wKioL1dCwQ6QMOJiAAAG21Qkm3M215.png "alt=" Wkiol1dcwq6qmojiaaag21qkm3m215.png "/>

The statement contains [], {}, or () parentheses do not need to use a multiline connector. As follows

650) this.width=650; "title=" 11.PNG "src=" http://s1.51cto.com/wyfs02/M00/80/7C/wKiom1dCwN6DICDEAAAKJzEnzms579.png "alt=" Wkiom1dcwn6dicdeaaakjzenzms579.png "/>


Python Quotes

Python receives single quotation marks ('), double quotation marks (""), three quotation marks ("" "" "" ") to denote a string, and the opening and closing of quotation marks must be of the same type.

Where three quotation marks can be composed of multiple lines, writing the shortcut syntax of multiple lines of text, common language file strings, at a specific location of the file, as a comment.

650) this.width=650; "title=" 12.PNG "src=" http://s5.51cto.com/wyfs02/M01/80/7C/wKiom1dCwhizrLd8AAARtVG_em8382.png "alt=" Wkiom1dcwhizrld8aaartvg_em8382.png "/>

Python comments

A single line comment in Python starts with # and the commented out part is not executed, let me show you:

650) this.width=650; "title=" 13.PNG "src=" http://s2.51cto.com/wyfs02/M00/80/7C/wKiom1dCw7PxtfOoAAALhOjhb1Y119.png "alt=" Wkiom1dcw7pxtfooaaalhojhb1y119.png "/>

Output Result:

650) this.width=650; "title=" 14.PNG "src=" http://s4.51cto.com/wyfs02/M00/80/7C/wKiom1dCw97w665wAAAEgofyw5U268.png "alt=" Wkiom1dcw97w665waaaegofyw5u268.png "/>

A multiline comment in Python uses three single quotation marks ("') or three double quotation marks (" ").

650) this.width=650; "title=" 15.PNG "src=" http://s3.51cto.com/wyfs02/M01/80/7C/wKiom1dCxLOhfGpzAAAPnjpLRJs383.png "alt=" Wkiom1dcxlohfgpzaaapnjplrjs383.png "/>


Python Empty Line

A blank line separates between functions or methods of a class, representing the beginning of a new piece of code. The class and function entries are also separated by a line of blank lines to highlight the beginning of the function entry.

Blank lines are different from code indentation, and empty lines are not part of the Python syntax. When you write without inserting a blank line, the Python interpreter runs without errors. However, the purpose of a blank line is to separate the code of two different functions or meanings, which facilitates the maintenance or refactoring of future code.

Remember: Blank lines are also part of your program code.


Write more than one statement on the same line

Python can use multiple statements in the same row, with semicolons (;) split between statements, and here's a simple example:

650) this.width=650; "title=" 16.PNG "src=" http://s3.51cto.com/wyfs02/M00/80/7A/wKioL1dCx_2Sr9_WAAALZwEtvd4650.png "alt=" Wkiol1dcx_2sr9_waaalzwetvd4650.png "/>


Multiple statements form a code group

Indenting the same set of statements constitutes a block of code, which we call the code group.

Compound statements such as if, while, Def, and class, the first line begins with a keyword, ends with a colon (:), and one or more lines of code after that line form the code group.

We refer to the first line and the following code group as a clause (clause).

The following example:

650) this.width=650; "title=" 17.PNG "src=" http://s4.51cto.com/wyfs02/M01/80/7B/wKioL1dCyRyA-h8CAAAHEDoNDhE046.png "alt=" Wkiol1dcyrya-h8caaahedondhe046.png "/>


END









This article is from the "Mr. Tumbler" blog, please be sure to keep this source http://kudangren.blog.51cto.com/11300146/1782201

Python basic syntax

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.