It is the basic syntax of Python

1. Encoding format

#coding gdk
uses the simplified Chinese character encoding in Windows.

Second, the identifier

  • It is composed of numbers, letters, and underscores.
  • Cannot start with a number.
  • Chinese characters can also be used as identifiers.

3. Keywords/reserved words

You cannot use keywords as identifiers.

You can use the following code to view all keywords:

  • import keyword
  • keyword.kwlist

Four, notes

Single line comment :
start with #

Multi-line comments :

  • ctrl+/
  • Multi-line # sign
  • Multi-line string object ``'comment statement''' or """ comment statement"""

Five, line and indentation

Python uses indentation to denote code blocks, without using braces.

Statements in the same code block must contain the same number of indented spaces.

Six, multi-line statements

If the sentence is very long, you can use "\" to realize a multi-line statement.

Multi-line statements in [], {}, or () do not need to use "\ ".

Seven, blank lines

  • Use blank lines to separate functions or methods of a class to indicate the beginning of a new piece of code.
  • The class and function entry are also separated by a blank line to highlight the beginning of the function entry.
  • Blank lines are different from code indentation. Blank lines are not part of Python syntax.
  • No blank lines are inserted when writing, and the Python interpreter runs without error.
  • The function of the blank line is to separate two sections of code with different functions or meanings, so as to facilitate future code maintenance or reconstruction.
  • Blank lines are also part of the program code.

8. Code group

  • A group of statements with the same indentation constitutes a code block, which we call a code group.
  • For compound statements like if, while, def, and class, the first line starts with a keyword and ends with a colon (: ). One or more lines of code after the line constitute a code group.
  • We call the first line and the following code group a clause.

Nine, input

Insert picture description here

Ten, print output

The default output of print is line break, if you want to achieve no line break, you need to add end="" at the end of the variable.

11. Import and from...import

Import the corresponding module.
Insert picture description here

12. How to run Python scripts

Interactive :
Start Python in the terminal and run in interactive programming mode, suitable for testing small pieces of code.
Script type :
Run the saved Python script file in the form of python [FileName].py on the terminal, which is suitable for running larger project files.

Guess you like

Origin blog.csdn.net/weixin_44366125/article/details/106160704