Home  >  Article  >  Backend Development  >  Detailed explanation of basic syntax in python3

Detailed explanation of basic syntax in python3

黄舟
黄舟Original
2017-07-27 14:44:181464browse

1. Encoding

By default, python3 source files are encoded in UTF-8, and all strings are unicode strings. Of course, you can also specify different encodings for the source code files:

1 # -*- coding: gbk -*-

2. Identifier

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

2. The other parts of the identifier consist of letters, numbers and underscores.

3. Identifiers are case-sensitive.

In python3, non-ASCII identifiers are also allowed.

3. Python reserved words

Reserved words are keywords, and we cannot use them as any identification names. Python's standard library provides a keyword module that can output all keywords of 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', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']>>>

4. Comments

#   Single-line comments

''' or """ Multi-line comments, three single (double) quotation marks appear in pairs, you can also use this symbol to represent a piece of content

5. Lines and abbreviations

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

6. Data types

Python data types include

Numbers (numbers)

String (strings)

List (List)

Tuple (Tuple)

Sets (Set)

Number type:

Number type classification: Integers, long integers, floating point numbers and complex numbers

Integer: 1

Long integer: It is a relatively large integer

Floating point: 1.23 3E-2

  Plural numbers: 1 + 2j, 1.1 + 2.2j

String:

* Single quotes and double quotes are used exactly the same in Python

. * Use triple quotes (''' or """) to specify a multi-line string.

Escape character '\'

Natural characters, by adding r or R before the string. For example,

print(r"this is a line with \n") displays the result: this is a line with \n

The string is immutable

literally Concatenated strings such as

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


The above is the detailed content of Detailed explanation of basic syntax in python3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn