超长行转化为多行
写python代码时如果一行太长,可以分为多行。用反斜杠 \ 来操作。
a = 10
b = 5
c = 100
d = 1
f = 8
total_1 = a + b + c + d + f
total_2 = a + \
b + \
c \
+ \
d + f \
print(total_1)
print(total_2)
输出:
在print语句中,字符不用 \ 也可以实现转行:
print('我的家乡在塔克拉'
'马干沙漠,那里'
'有许多的沙子,还有炎热的太阳。')
print('我的家乡在塔克拉'\
'马干沙漠,那里' \
'有许多的沙子,还有炎热的太阳。')
输出:
在 [], {}, 或 () 中的多行语句,不需要使用反斜杠 \ 。
total = ['item_one', 'item_two', 'item_three',
'item_four', 'item_five']