A detailed explanation of basic grammar in Python3

Source: Internet
Author: User
One, the code

By default, Python3 source files are encoded in UTF-8, and all strings are Unicode strings. Of course you can also specify a different encoding for the source file:

1 #-*-CODING:GBK-*-

Second, identifiers

1. The first character must be a letter or underscore ' _ '.

2. The other parts of the identifier are made up of letters, numbers, and underscores.

3, the identifier is case sensitive.

In Python3, non-ASCII identifiers are also allowed.

Third, Python reserved word

Reserved words are keywords, and we cannot use them as any identity names. Python's standard library provides a keyword module that can output all the keywords for the current version:

>>> import keyword>>> keyword.kwlist[' False ', ' None ', ' True ', ' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' finally ', ' for ', ' from ', ' global ', ' if ', ' import ', '-', ' is ', ' LAMBD A ', ' nonlocal ', ' not ', ' or ', ' Pass ', ' raise ', ' return ', ' Try ', ' when ', ' with ', ' yield ']>>>

Iv. notes

# single Comment

"" or "" "multi-line comment, three single (double) quotation marks are paired, you can also use this symbol to represent a piece of content

Five, line and indent

Python's most distinctive feature is the use of indentation to represent the code block structure. The number of spaces to indent is variable, but the same number of indentation spaces must be used within the same code block

Vi. Types of data

The Python data type has

Numbers (digital)

String (String)

List (lists)

Tuple (tuple)

Sets (collection)

Digital type:

Categories in digital: integers, long integers, floating-point numbers, and complex numbers

Integers: 1

Long integer: is a larger integer

Floating-point bellow: 1.23 3E-2

Plural: 1 + 2j, 1.1 + 2.2j

String:

* The single and double quotation marks in Python are used exactly the same.

* Use three quotation marks ("' or" ") to specify a multiline string.

Escape character ' \ '

Natural characters, by adding R or R before the string. Such as

Print (r "This was a line with \ n") shows the result this is a line with \ n

The string is immutable

cascading strings by literal meaning, such as

>>> a = "This" "was" "string" >>> a ' this is string '

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.