Python basic syntax

Source: Internet
Author: User

I. Basic input and OUTPUT

# !/usr/bin/python Print " Hello, python!. ";

Second, Python identifiers
In Python, identifiers are composed 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 (_foo) cannot be accessed directly, and is 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.

Third, 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.

 and    exec     notassert    finally    or Break     for    Passclass     from    PrintContinue    Global    Raisedef    if    returndel    Import    Tryelif    inch     whileElse     is withexcept    Lambda    yield

Four, 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. As shown below:

if True:     Print " True " Else :   Print " False "

The following code will execute the error:

if True:     Print " Answer "    Print " True " Else :     Print " Answer "  Print " False "   

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

Five, 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:

Total = Item_one ++ item_three


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

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

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.