Python Basic Syntax

Source: Internet
Author: User

One, interactive 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 on the command line to start interactive programming.

The default interactive programming client is already installed on Windows when you install Python.

Second, script-type programming

Invoking the interpreter from the script parameter begins execution of the script until the script finishes executing. When the script finishes executing, the interpreter is no longer valid.

Third, 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 the underscore _ are of special significance:

A single underscore _ (_foo) represents a class attribute that is not directly accessible and needs to be accessed through the interface provided by the class, and cannot be imported with "from XXX import";

A double-underlined (__foo) represents a private member of a class;

The __foo__, which begins with a double underscore and ends, represents a special method-specific identifier for Python, such as __int__ (), which represents the constructor of the class.

Four, Python reserved characters

Reserved words cannot be used as constants or variables, or as any other identifier name.

  and :assert,break,class,continue,def,del,elif,else,except;

  exec:finally,for,from,global,if,import,in,is,lambda;

  not: Or,pass,print,raise,return,try,while,with,yield;

Five, line and indent

Python's code block does not use curly braces ({}) to control classes, functions, and other logical judgments.

Python uses indentation to write modules!

The amount of white space to indent is variable, but all code block statements must contain the same amount of indentation whitespace , which must be strictly enforced!

Six, multi-line statements

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

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

Total = Item_one + \

Item_two + \

Item_three

A statement that contains [],{} or () parentheses does not need to use a multiline connector, as follows:

days={' Monday ', ' Tuesday ',

' Wednesday ',

' Thursday ', ' Friday '}

Seven, Python quotes

Python receives single quotation marks ('), double quotation marks ("), three quotation marks (" ") 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 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.

Viii. python annotations

A single-line comment begins with # and the comment can be at the end of a statement or expression;

Multiline comments use three single quotation marks ("') or three double quotation marks (" ").

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

Ten, waiting for user input

Program code:

#!/usr/bin/python

Raw_input ("\n\npress the Enter key to exit.")

Note: "\ n" will output two new blank lines before the result output, and the program will exit when the key is pressed ↓.

Xi. the same line shows multiple statements

Import SYS; x = ' Runoob ';

Sys.stdout.write (x + ' \ n ')

12. Multiple statements form a code group

Indenting the same set of statements constitutes a block of code, called a code group.

13. Command Line Parameters

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.