If you are not familiar with the practical application of the Python syntax, you can use the following articles to give a detailed introduction to the basic concepts of the Python syntax, I hope you will understand some specific applications of computer applications in the following articles.
Although the Python syntax is very concise, I have sorted out some basic points.
Basic Concepts
1. Number: integer, long integer, floating point number example: 3.23 or 52.3E-4, plural: (-5 + 4j) or (2.3-4.4.7 )]
2. String:
1) the strings in double quotes are exactly the same as those in single quotes.
2) using three quotation marks (') or "], you can indicate a multi-line string.
3) Escape Character \], but a separate backslash at the end of a string line is used to continue the line
4) Natural string: The character string is specified by adding the prefix r or R, so that it is not escaped as it is)
5) Unicode string: prefix u or U before the string, for example, u. This is unicode. <o: p> </o: p>
3. variables: when using variables, you only need to assign them a value. No need to declare or define data types
4. identifier: uppercase or lowercase letters), underscore '_', or number 0-9), and the first character must be an uppercase or lowercase letter) or underscore '_')
5. indent: In the Python syntax, statement blocks are marked in indentation instead of braces. That is, statements at the same level must have the same indentation.
6. Operators)
(1) power operator **: x ** y returns the y Power of x. For example, 3 ** 4 = 3*3*3*3*3.
(2) Remove the integer part of the operator //: returns the integer part of the operator. For example, 4 // 3.0 returns 1.0.
(3) Boolean "Non" operator not
(4) Boolean "and" operator and
(5) Boolean "or" operator or
7. Control Flow
(1) if statement: the form is as follows: (Note that the if statement contains a colon at the end, telling the Python syntax to follow a statement block; and there is no switch statement)
- if condition:
- statement
- elif condition:
- statement
- else
- statement
(2) while statement: the form is as follows:
- while condition:
- statement
- else:
(3) for statement: the form is as follows:
- for element in collection:
- statement
- else:
(4) break statement: similar to other languages, it should be noted that if it is terminated from the for or while LOOP, no corresponding loop else block will be executed
(5) continue statement: same as other languages
8. function: defined by the def keyword. The def keyword is followed by the identifier name of a function, followed by a pair of parentheses. Variable names can be included in parentheses, which end with a colon. Next is a sentence, which is a function body, for example:
- def function_name():
- statement
9. module: a module is a python file. It is generally a tool set or reusable code, similar to the tool class in C.