Python编程风格与函数使用全解析
1. 代码换行与编程风格
在Python编程中,当需要在括号、方括号或大括号外换行时,有两种常见方法。可以添加额外的括号,也可以在换行处添加反斜杠。示例代码如下:
# 添加额外括号
if (len(syllables) > 4 and len(syllables[2]) == 3 and
syllables[2][2] in [aeiou] and syllables[2][3] == syllables[1][3]):
process(syllables)
# 添加反斜杠
if len(syllables) > 4 and len(syllables[2]) == 3 and \
syllables[2][2] in [aeiou] and syllables[2][3] == syllables[1][3]:
process(syllables)
1.1 编程风格对比
编程风格对程序开发有重要影响,主要分为过程式风格和声明式风格。以计算布朗语料库中单词的平均长度为例:
- 过程式风格 :
tokens = nltk.corpus.brown.words(categories='news')
count = 0
total = 0
for token in tokens:
count += 1
total += len(token)
print to
超级会员免费看
订阅专栏 解锁全文
722

被折叠的 条评论
为什么被折叠?



