[Python] Basic tutorial (3), Python basic syntax

Source: Internet
Author: User

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.

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, which is not directly accessible by the _foo, must be accessed through the interface provided by the class and cannot be imported with the FROM XXX import *;

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

Python can display multiple statements on the same line, separated by semicolons , such as:

' Hello ' ' Runoob ' ; Hellorunoob
Python reserved characters

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

All Python keywords contain only lowercase letters.

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.

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:

Python Quotes

Python can use quotation marks ( ' ), double quotation marks ( " ), three quotation marks (" ' or "") to denote a string, and the beginning and end of the quotation mark must be of the same type.

Where three quotation marks can be composed of more than one line, writing a shortcut syntax for multiple lines of text, often used in a document string, at a specific location of the file, as a comment.

Python comments

A single-line comment in Python starts with #.

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

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

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.

Wait for user input

The following program executes and waits for user input, and then exits after pressing ENTER:

Raw_input ("Press theenter key to exit. ") ")

Show multiple statements on the same line

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

Import ' Runoob ' ' \tabc\n ')

Print output

The print default output is newline, and if you want to implement no wrap, you need to add a comma to the end of the variable:

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:

A=3b=5if a>B:   print"a big"elif A <B:    print"b Big"else  :     Print " a=b "
Command-line arguments

Many programs can perform some operations to see some basic information, and Python can use the- H parameter to view the Help information for each parameter:

[Python] Basic tutorial (3), Python basic syntax

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.