Statements and syntax
There are some basic rules and special characters in the Python statement:
- Pound key "#" means the following character is a Python comment;
- Three quotes ("") can be multiple lines of comment
- A newline "\ n" is a standard line delimiter (usually a line of statements);
- Backslash "\" continues on line;
- Semicolon ";" joins two statements in one row
- Colon ":" separates the head and body of the Code;
- Statements (blocks of code) are expressed in the form of indentation;
- Different indentation depths separate different blocks of code;
- Python files are organized as modules.
1. Comments (#)
The Python comment statement starts with the # character, and the comment can start anywhere on a line, and the interpreter ignores everything after the line #;
1 # This is an example of Python annotations! 2 # This is an example of Python annotations! 3 # This is my road of learning python 4 " multiline comment 5 another 6 means example 7 " 8 Print ( " python comments using the method )
2. Continue (\)
Python statements, which are generally delimited by line breaks, that is, one statement at a line. A line that is too long can be broken up into lines using a backslash (\). The following example:
#Check conditionsAcount=input ('Please enter your account number:') Password=input ("Please enter your password:")ifacount[0].isdigit ()==true andPassword[0]. IsDigit ()==true:#statements that are too long can be separated by backslashes into multiple lines! Print("the first number of accounts/passwords cannot be numbers! ")Else : Print("account Number:%s\n Password:%s\n"% (Acount,password))
There are two exceptions to a statement that does not use backslashes or can span rows.
When using a closed operator, a single statement can span multiple lines, for example, when you can write more than one line with parentheses, brackets, curly braces, or a string that contains a triple quotation mark that can be written across lines.
names=['Zhang Sanfeng','Guo Jing','Zhu Zi','Li Bai','Arthur','Marco Polo']names1=['Zhang Sanfeng','Guo Jing' ,'Zhu Zi' ,'Li Bai','Arthur','Marco Polo']names2=['Zhang Sanfeng','Guo Jing','Zhu Zi' ,'Li Bai','Arthur','Marco Polo']names3=" "[' Zhang Sanfeng ', ' Guo Jing ', ' Zhu Zi ', ' Li Bai ', ' Arthur ', ' Marco Polo ']" "Print("names=%s\nnames1=%s\nnames2=%s\nnames3=%s"%(NAMES,NAMES1,NAMES2,NAMES3)) operation Result: Names=['Zhang Sanfeng','Guo Jing','Zhu Zi','Li Bai','Arthur','Marco Polo']names1=['Zhang Sanfeng','Guo Jing','Zhu Zi','Li Bai','Arthur','Marco Polo']names2=['Zhang Sanfeng','Guo Jing','Zhu Zi','Li Bai','Arthur','Marco Polo']names3=['Zhang Sanfeng','Guo Jing' ,'Zhu Zi' ,'Li Bai','Arthur','Marco Polo']
3.multiple statements form a code group (:)
Indenting the same set of statements constitutes a block of code, called a 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.
Python uses indentation to separate code groups. The hierarchical relationship of code is reflected by the same depth of space or tab indentation. Lines of code for the same code group must be strictly left-aligned (with the same number of spaces on the left or as many tabs);
4. Write multiple statements on the same line (;)
a semicolon (;) allows you to write multiple statements on the same line, separated by semicolons, and these statements cannot start a new block of code on this line.
1 #write a statement on one line2F=open ('f:\MyBook\ English Proverb','R', encoding='GBK')3 Print(F.read ())4 f.close ()5 #a line of multiple statements written, between statements with ";" Separated! 6F=open ('f:\MyBook\ English Proverb','R', encoding='GBK');Print(F.read ()); F.close ()
The above code runs the following results:
Python Basic Learning statements and syntax