Python basic syntax
Python identifiers
1. Python identifiers consist of letters, underscores and numbers, and numbers cannot be used as the beginning of an identifier;
2. Python identifiers are case-sensitive;
3. Identifiers starting with an underscore have special meaning, and those starting with an underscore (such as: _name) represent class attributes that cannot be directly accessed and need to be accessed through the interface provided by the class
4. (__name) starting with a double underscore represents a private member of the class, and starting and ending with a double underscore represents a special method, such as __Init__() represents a constructor
Python reserved characters
There are about 30 reserved characters in Python, which can be viewed in IDLE by command
line and indent
The biggest difference between Python and other languages is that classes are controlled by indentation without using {}. functions, and other logical judgments. The most distinctive feature of python is to use indentation to write modules;
Indented whitespace is usually variable, but usually in groups of 4
if True: print("True"); else: print("False");
The following code will give an error message
if True: print ("Answer"); print ("True"); else: print ("Answer"); print ("False"); runfile('D:/New folder/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/New folder/WinPython-64bit-3.3. 5.9/settings/.spyder2-py3/My Python Code') File "D:/New Folder/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py", line 13 print ("False"); ^ IndentationError: unindent does not match any outer indentation level
multi-line statement
In Python statements, a new line is generally used as the terminator of the statement, but we can display a statement on multiple lines through the backslash \
total = item_one + \ item_two + \ item_three
However, if the statement contains [] or {}, there is no need to use the multi-line connector, as follows:
days = ['Monday','Tuesday','Wednesday', 'Thursday','Friday']
Python quotes
Python has three kinds of quotes: '' or "" or """ """
The triple quotation marks can be composed of multiple lines, a shortcut syntax for writing multi-line text, and commonly used docstrings are used as comments in specific places in the file
word='word' sentence="This is a sentence" paragraph=""" is just a paragraph, Contains multiple statements """
Python comments
Python single-line comments start with #, multi-line comments use triple quotes """ """
Python blank line
A blank line is used to separate between functions or methods of a class, indicating the beginning of a new piece of code. Class and function entries are also separated by a line of spaces to highlight the beginning of the function entry
Empty lines are different from code indentation. Empty lines are not part of Python syntax; no empty lines are inserted into the code, and the Python interpreter will run without error. The role of empty lines is to separate two different pieces of code for future code maintenance. and refactoring
wait for user input
In Python3.X:
input("\n\n Press the Enter to exit");
\n\n: There will be two new blank lines before and after the result output, once the user presses the key, the program will exit
Display multiple statements on the same line
Python can use multiple statements on the same line, use between statements; split
import sys;x='runoob';sys.stdout.write(x+'\n');
Output result:
runfile('D:/New folder/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/New folder/WinPython-64bit-3.3. 5.9/settings/.spyder2-py3/My Python Code') runoob
Multiple statements form a code group
A group of statements with the same indentation forms a code block, which we call a code group
Compound statements like if, while, def, and class start with a keyword on the first line and end with a colon: , and one or more lines after that line form a code group. The group of code following our first line is called a clause.
if expression: suite else expression: suite else: suite
command line arguments
Many programs can perform some operations to view some basic information, Python can use -h to view the help information of each parameter