Python3 basic syntax and python3 syntax
I. Encoding
By default, the python3 source code file is encoded in UTF-8, and all strings are unicode strings. Of course, you can also specify different codes for the source code file:
1 # -*- coding: gbk -*-
Ii. identifier
1. The first character must be a letter or underscore '_'.
2. Other parts of the identifier are composed of letters, numbers, and underscores.
3. The identifier is case sensitive.
In python3, non-ASCII identifiers are also allowed.
Iii. Reserved python words
Reserved Words are keywords. We cannot use them as any identification names. The standard python Library provides a keyword module that can output all the 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']>>>
Iv. Notes
# Single line comment
''' Or "multi-line comment. Three single (double) quotation marks appear in pairs. You can also use this symbol to represent a piece of content.
V. Line and indent
Python uses indentation to represent the structure of code blocks. The number of indented spaces is variable, but the same number of indented spaces must be used in the same code block.
Vi. Data Types
Python data types include
Numbers (number)
String (String)
List)
Tuple (tuples)
Sets)
Number Type:
Number Type: integer, long integer, floating point, and plural
Integer: 1
Long Integer: a relatively large integer.
Floating Point: 1.23 3E-2
Plural: 1 + 2j, 1.1 + 2.2j
String:
* In python, single quotes and double quotes are identical.
* You can use three quotation marks (''' or ") to specify a multi-line string.
Escape Character '\'
Natural character, by adding r or R before the string. For example
Print (r "this is a line with \ n") show the result this is a line with \ n
The string is unchangeable.
Cascade strings literally, such
>>> a = "this " "is " "string">>> a'this is string'
Dictionarys (dictionary)