identifiers
- The first character must be a letter in the alphabet or an underscore ' _ '.
- The other parts of the identifier are composed of letters, numbers, and underscores.
- Identifiers are case sensitive.
- Can exist in Chinese identifiers
python reserved word
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 the keywords for the current version:
>>> ImportKeyword>>>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 ' ]
Notes
#!/usr/bin/python3 This is written in front of the program, identifying the path of the program, but for Windows systems, this line can be used without adding
The single-line comment in Python begins with # , with the following example:
#!/usr/bin/python3# first comment print("Hello, python!" )# A second comment
Execute the above code and the output is:
Hello,Python!
Multi-line comments can be "" ":
#!/usr/bin/python3 ""
First comment second comment
‘‘‘
Print("Hello, python!" )
Execute the above code and the output is:
Hello,Python!
Line and indent
Python's most distinctive feature is the use of indentation to represent blocks of code, without the need for curly braces ({}).
The number of spaces to indent is variable, but the statement of the same code block must contain the same number of indentation spaces. Examples are as follows:
IfTrue: print("True")else: print("False")
The following code does not match the number of spaces in the last line of the statement, resulting in a run error:
if true : print ( "Answer" ) print ( "True" ) else: print ( "Answer" print ( " False ") # indentation inconsistent, resulting in a run error
The above program is not consistent due to indentation, after execution will appear similar to the following error:
File"test.py",6print("False")# indentation inconsistent, resulting in a run error ^Indentationerror :not match no outer indentation level
Multi-line statements
Python is usually a line to complete a statement, but if the statement is long, we can use a backslash (\) to implement a multiline statement, for example:
=+ + item_three
In [], {}, or () a multiline statement, you do not need to use a backslash (\), for example:
=[' Item_one ',' Item_two ',' Item_three ',' Item_four ',' item_ Five ']
Data Type
There are four types of Python: integers, long integers, floating-point numbers, and complex numbers.
- integers, such as 1
- Long integers are relatively large integers.
- Floating-point numbers such as 1.23, 3E-2
- Complex numbers such as 1 + 2j, 1.1 + 2.2j
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 ' \ '
- Natural string, by adding R or R before the string. such as R "This was a line with \ n \" will be displayed, not newline.
- Python allows processing of Unicode strings, prefixed with u or u, such as u "This is a Unicode string".
- The string is immutable.
- The literal cascading string, such as "This" and "is" and "string", is automatically converted to this is string.
=' string '= 'This is a sentence. "=" "This is a paragraph that can consist of multiple lines " ""
/// Air Line //
A blank line separates between functions or methods of a class, representing the beginning of a new piece of code. The class and function entries are also separated by a line of blank lines to highlight the beginning of the function entry.
Blank lines are different from code indentation, and empty lines are not part of the Python syntax. When you write without inserting a blank line, the Python interpreter runs without errors. However, the purpose of a blank line is to separate the code of two different functions or meanings, which facilitates the maintenance or refactoring of future code.
Remember: blank lines are also part of your program code.
Show multiple statements on the same line
Python can use multiple statements in the same row, with semicolons (;) split between statements, and here's a simple example:
#!/usr/bin/python3import sys; =' Runoob '; sys. StdOut. Write(+' \ n ')
Execute the above code and enter the result:
Runoob
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:
If: Suiteelif:else: Suite
Print Output
For the print () function, the function of the print () function of Python is undoubtedly more powerful than the format output of printf (), which is different from the C language, and is shown in the following three points
1. Formatted output
Similar to printf in C
>>> s="holle! ">>> l=len (s)print(" Thelength of%s is%d"% (s,l) is 6
It's worth noting that the format output of the print function of the Python language compared to the C language differs somewhat from the format output of C: printf.
1. No less f (when I didn't say),
2. There is no comma after the quotation mark, no comma, no comma!!! (That's why I've been making mistakes when I just learned C and started to learn Python.)
3. The output variable order is placed in parentheses of% ()
Besides, there are also!!!
(1).% character: The beginning of the token conversion specifier
(2). Convert flag:-Indicates left alignment, + indicates a positive sign before the value is converted, "" (white space character) to retain a white space before, and 0 for the conversion value if the number of digits is not enough. 0 padding
(3). Minimum field width: The converted string should have at least the width specified by the value. If it is *, the width is read from the value tuple.
(4). dot (.) Heel Precision Value: If the conversion is real, the precision value represents the number of digits after the decimal point. If the string is converted, the number represents the maximum field width. If it is *, then the precision will be read from the tuple
(5). string format conversion type
Conversion type meaning
D,i signed Decimal integer
o unsigned octal
U non-signed decimal
x hexadecimal without symbol (lowercase)
X hexadecimal without symbol (uppercase)
Floating point number represented by the e-scientific notation (lowercase)
The floating-point number represented by the E-scientific notation (uppercase)
F,f decimal Floating-point number
G If the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
G if the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
C Single-character (accepts integers or single character strings)
R string (convert any Python object using repr)
s string (convert any Python object using str)
2. Variable no matter what type, value, Boolean, List, dictionary ... can be directly output
For example:
1>>> x =' You'2>>>Print(x)3 You4>>> y = 6665>>>Print(y)66667>>> z = [1,2.3,{4,5,6},'Holle']8>>>Print(z)9[1, 2.3, {4, 5, 6},'Holle']Ten>>> i = {'Holle': 1,'Hello': 3} One>>>Print(i) A{'Holle': 1,'Hello': 3}
The print default output is newline, and if you want to implement no newline, add end= ""to the end of the variable:
#!/usr/bin/python3X=AY="B"# line break outputPrint(X)print ( y ) print ( '---------' < Span class= "com" ># does not wrap output print ( X, end= ") print ( y , end= "" print ()
The result of the above instance execution is:
AB---------a b
Import and From...import
Import the appropriate modules in Python using Import or from...import.
Imports the entire module (Somemodule (it does not exist)) 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 *
To some extent, it can be said that import Somemodule and from somemodule Import * function Similar, are imported all functions, but the difference is that import somemodule the whole library, call the function should be some Mduble.firstfunc, and a direct call to Firstfunc from the Somemodule import *
I learned Python's first day, "Basic syntax for Python 1"