Detailed explanation of basic syntax in python3

黄舟
Release: 2017-07-27 14:44:18
Original
1481 people have browsed it

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 -*-
Copy after login

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']>>>
Copy after login

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'
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template