Into the Python World (iv) Basic grammar

Source: Internet
Author: User

Python identifiers

A python identifier is a name used to identify a variable, function, class, module, or other object. An identifier begins with the letter A to Z or A?z or followed by 0 or more letter underscores (_), underscores, and numbers (0?9).

Punctuation is not allowed within identifiers in Python, such as @,$ and%. Python is a case-sensitive programming language. Therefore, Manpower and Manpower are two different identifiers in Python.

Here is the Python identifier naming convention:

    • The class name is in uppercase letters and all other identifiers in lowercase letters.

    • An identifier that begins with a single leading underscore is represented by the identifier convention meaning private.

    • The identifier that begins with two leading underscores represents a strong private identifier.

    • If the end of the identifier also has two underscores at the end, the identifier is a language-defined special name.

Reserved words

According to official documents, reserved words have

False class finally is return

None continue for Lambda try

True def from Nonlocal while

And Del Global not with

As Elif if or yield

Assert Else Import Pass

Break except in raise


Line and indent

When a programmer learns Python, the first area to be aware of is the class and function definition block or process control that does not use parentheses to represent the code.

Code blocks are indented by lines, which are strictly performed representations.

The number of indents is variable, but all statements in the block must be indented the same amount. In this example, two function blocks are well-used:

If True:print "True" Else:print "False"

However, the second block in this embodiment will produce an error:

If True:print "Answer" print "True" Else:print "Answer" print "False"

As a result, the number of spaces indented in Python for all successive lines will also be formed into blocks. The following are examples of various statement blocks:

Note: Do not attempt to understand the logic used or the different functions. Just make sure you understand that even if they have various modules without parentheses.


Multi-line statements

Python statements usually end with a new line. However, Python allows you to use the continuation character (\) to indicate that the line should continue (across rows). For example:

Total = Item_one + Item_two + item_three

Statements contained within [],{} or () brackets do not need to use a continuation character. For example:

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

Python Quotes

Python accepts single quotes ('), double quotation marks ("), and three (" or "") references to denote string constants as long as they are the same type of quotation marks that start and end the string.

Sanchong quotes can be used for strings that span multiple lines. For example, all of the following are legal:

Word = ' word ' sentence = "This is a sentence." Paragraph = "" "This is a paragraph. It ismade up of multiple lines and sentences. "" "


Python comments

A pound sign (#), which is not a comment at the beginning of a string literal. After the "#" sign and to the physical line are part of the comment, the Python interpreter ignores them.

#!/usr/bin/python# First Commentprint "Hello, python!"; # second Comment

This will produce the following results:

Hello, python!.

You can use multiple lines of comments as follows:

# This was a comment.# this is a comment, too.# this is a comment, and too.# I said that already.


Multiple statements in a row

a semicolon (;) allows multiple statements to be written in a single line, regardless of whether the statement starts a new block of code. Under

Polygons are examples of using semicolons:

Import SYS; x = ' Foo '; Sys.stdout.write (x + ' \ n ')


Multiple statement groups as suites

A separate set of statements, in Python, a single code block is called a sequence. Complex statements, such as if, while, Def, and class, require a header row and a suite.

The header line begins with the declaration (with the keyword), and terminates with a colon (:)), followed by one or more lines that make up the package. For example:

If Expression:suiteelif expression:suite else:suite


Into the Python World (iv) Basic grammar

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.