Part 3 days python coding standards []

The next day went to overtime!
Syntax conventions:
Multi-line statement
Python statements generally a newline as a statement terminator.
But we can use the slash (\) line will be divided into multiple lines of statements, as follows:
total = item_one + \ item_two + \ item_three
Statement includes [], {}, or () parenthesis not need to use multi-line connector. Following examples:
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
Waiting for user input
The following program after pressing the Enter key will wait for user input:
#!/usr/bin/python
 
raw_input("\n\nPress the enter key to exit.")
 
In the above code, "\ n \ n" before the result output two outputs new blank line. Once the user presses Enter (Enter) key to exit, the other key display.
 
Indent error message:
IndentationError: unexpected indent
The function encountered a temporary function alone put articles in
Python quotes
Python can use quotation marks ( '), double quote ( "), tris quotation marks ( ' '' or '' ') for strings, must start and end marks of the same type.
Three of the quotes may consist of multiple lines, multiple lines of text written in shorthand syntax, the common language documentation string, the specific location of the file, be treated as a comment.
word = 'word' sentence = "这是一个句子。" paragraph = """这是一个段落。 包含了多个语句"""
Python注释
python中单行注释采用 # 开头。
#!/usr/bin/python # -*- coding: UTF-8 -*- # 文件名:test.py # 第一个注释 print "Hello, Python!"; # 第二个注释
输出结果:
Hello, Python!
注释可以在语句或表达式行末:
name = "Madisetti" # 这是一个注释
python 中多行注释使用三个单引号(''')或三个双引号(""")。
#!/usr/bin/python # -*- coding: UTF-8 -*- # 文件名:test.py ''' 这是多行注释,使用单引号。 这是多行注释,使用单引号。 这是多行注释,使用单引号。 ''' """ 这是多行注释,使用双引号。 这是多行注释,使用双引号。 这是多行注释,使用双引号。 """
 
无限循环
如果条件判断语句永远为 true,循环将会无限的执行下去,如下实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- var = 1 while var == 1 : # 该条件永远为true,循环将无限执行下去 num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!"
以上实例输出结果:
Enter a number :20 You entered: 20 Enter a number :29 You entered: 29 Enter a number :3 You entered: 3 Enter a number between :Traceback (most recent call last): File "test.py", line 5, in <module> num = raw_input("Enter a number :") KeyboardInterrupt
注意:以上的无限循环你可以使用 CTRL+C 来中断循环。

Guess you like

Origin www.cnblogs.com/TomBombadil/p/10979594.html