Python3 Basic Syntax
Encoding--By default, the Python3 source file is encoded in Utf-8 and all strings are Unicode strings
Python Reserved words-keywords
keyword, we can't use them as any identifier name, Python's standard library provides a keyword module that can output all the keywords in the current version:
Example:
Note: Single-line comments in Python begin with #
Multiline comments using six single quotes, "' What to Annotate"
Line and indent
Python's most distinctive feature is that using indentation to mark blocks of code, the number of spaces indented is variable, but the same code block statement must contain the same indentation space, indentation inconsistencies will be error, as follows
Multi-line statements
Python is usually a line to finish a statement, but if the statement is long, we can use a backslash (\) to implement a multiline statement
The above input result is 6, i.e. 1+2+3=6
In the list [], the dictionary {}, or the string () in the multiline statement, you do not need to use a backslash (\), for example
numeric (number) type
There are four types of numbers in Python: integers, booleans, floating-point numbers, and complex numbers
- int (integer), such as 1, has only one integer-type int, represented as a long integer, and no long in Python2.
- BOOL (Boolean), such as True.
- float (floating point), such as 1.23, 3E-2
- Complex (plural), such as 1 + 2j, 1.1 + 2.2j
Strings (String)
- The single and double quotation marks in Python are used exactly the same.
- Use the quotation marks ("' or" ") to specify a multiline string.
- Escape character ' \ '
- Backslashes can be used to escape, using R to allow backslashes to escape. such as R "This was a line with \ n \" will be displayed, not newline.
- The literal cascading string, such as "This" and "is" and "string", is automatically converted to this is string.
- Strings can be concatenated with the + operator and repeated with the * operator.
- There are two ways to index a string in Python, starting from left to right and starting at 0 from right to left with-1.
- The string in Python cannot be changed.
- Python does not have a separate character type, and one character is a string of length 1.
- The syntax for intercepting a string is as follows: variable [head subscript: tail subscript]
Output Result:
Multiple statements form a code group
Indenting the same set of statements constitutes a block of code, which we call the code group.
Compound statements such as if, while, Def, and class, the first line begins with a keyword, ends with a colon (:), and one or more lines of code after that line form the code group.
We refer to the first line and the following code group as a clause (clause).
The following example:
Print output
The print default output is newline, and if you want to implement no newline, add end= ""to the end of the variable:
Import and From...import
Import the appropriate modules in Python using Import or from...import.
Import the entire module (somemodule) in the format: import somemodule
Import a function from a module in the format: from somemodule import somefunction
Import multiple functions from a module in the format: from somemodule import Firstfunc, Secondfunc, Thirdfunc
Import all functions in a module in the format: from somemodule import *
Command-line arguments
python-Basic Syntax