Several basic grammars that Python3 must learn

Several basic syntaxs that Python3 must learn

Python is designed to be very readable. Compared with other languages, English keywords are often used, and some punctuation marks in other languages ​​have a more distinctive grammatical structure than other languages.

coding

By default, Python 3 source code 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:

# -*- coding: cp-1252 -*-

The above definition allows the use of character codes in the Windows-1252 character set in the source files, and the corresponding suitable languages ​​are Bulgarian, Belarusian, Macedonian, Russian, and Serbian.

Identifier

The first character must be a letter in the alphabet or an underscore _.

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

Identifiers are case sensitive.

In Python 3, Chinese can be used as variable names, and non-ASCII identifiers are also allowed.

Python reserved words

Reserved words are keywords, and we cannot use them as any identifier 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']

Annotation

Single-line comments in Python begin with #, examples are as follows:

Examples (Python 3.0+)

#!/usr/bin/python3

# First comment

print ("Hello, Python!") # 2.comment

Execute the above code, the output result is:

Hello, Python!

Multi-line comments can use multiple # signs, as well as''' and """:

Examples (Python 3.0+)

#!/usr/bin/python3

# First comment

# 2.comment

'''

Third note

Fourth note

'''

"""

Fifth note

Sixth note

"""

print ("Hello, Python!")

Execute the above code, the output result is:

Hello, Python!

Reference: https://cloud.tencent.com/developer/article/1783039 Python3 must learn several basic syntaxs-Cloud + Community-Tencent Cloud