“一起学习,一起成长!”
1. 使用#注释
概述:在Python中使用#字符标记注释,从#开始到当前行结束的部分都是注释。
注释可以单独一行,也可与代码同行,同行放在代码后面。
特殊说明:
- Python没有多行注释的符号。需要明确地在注释部分的每一行开始处加上一个#。
- 如果#出现在文本串中,将回归普通字符#的角色
>>>print("No comment:quotes make the # harmless.")
No comment:quotes make the # harmless.
2. 使用连接
程序在合理的长度下是易读的。一行程序的(非强制性)最大长度建议为80个字符。
如果在该长度下写不完代码,可以使用连接符(反斜线)。把它放在一行的技术位置,python将其解释为同一行。
3. 使用if、elif和else进行比较
例子:多层判断语句的嵌套
>>>furry=True
>>>small=True
>>>if furry:
... if small:
... print("It's a cat.")
... else:
... print("It's a bear!")
... else:
... if small:
... print("It's a skink!")
... else:
... print("It's a human.Or a hairless bear.")
如果要检查超过两个条件,可以使用if、elif(即else if)和else:
>>>color="puce"
>>>if color=="red"
... print("It's a tomato.")
... elif color=="green":
... print("It's a green pepper.")
... elif color=="bee purple":
... print("I don't know what it is,but only bees can see it")
... else:
... print("I've never heard of the color