Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the basic grammars that Python3 must learn

Shulou Source: shulou.com Published: 2022-06-01 21:27:59 09月15日 Update

This article introduces the relevant knowledge of "what are the basic grammars that must be learned by Python3". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The design of Python is highly readable. Compared with other languages, it often uses English keywords and some punctuation marks. It has more distinctive grammatical structures than other languages.

Coding

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

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

The above definition allows the use of character encodings in the Windows-1252 character set in the source file, and the corresponding languages are Bulgarian, Blanc, Macedonian, Russian, and Serbian.

Identifier

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

The rest of the identifier consists of letters, numbers, and underscores.

Identifiers are case sensitive.

In Python 3, you can use Chinese as the variable name, and non-ASCII identifiers are also allowed.

Python reserved word

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

> import keyword

> keyword.kwlist

['False',' None', 'True',' and', 'as',' assert', 'break',' class', 'continue',' def', 'del',' elif', 'else',' 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:

Instance (Python 3.0 +)

#! / usr/bin/python3

# first comment

Print ("Hello, Python!") # second comment

Execute the above code, and the output is as follows:

Hello, Python!

Multiple # signs can be used for multiline comments, as well as''and'':

Instance (Python 3.0 +)

#! / usr/bin/python3

# first comment

# second comment

''

The third note

''

The fourth note

"

The fifth note

The sixth note

"

Print ("Hello, Python!")

Execute the above code, and the output is as follows:

Hello, Python!

This is the end of the content of "what are the basic grammars that Python3 must learn". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Comments characters identifiers identifiers encodings languages outputs grammars keywords keywords letters instances foundations underscores codes content strings situations files more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia Shulou Information macOS Shulou Tech Info