python缩进区别分析

yipeiwu_com6年前Python基础

仔细观察下面两个python程序,代码一模一样,但是运行的结果却不同,就是因为最后一行return缩进的不同

复制代码 代码如下:

def powersum(power, *args):

'''Return the sum of each argument raised to specified power.'''
total = 0
for i in args:
total += pow(i, power)
return total


运行时输入powersum(2,3,4)输出25(3的平方加上4的平方)
复制代码 代码如下:

def powersum(power, *args):
'''Return the sum of each argument raised to specified power.'''
total = 0
for i in args:
total += pow(i, power)
return total

运行时输入powersum(2,3,4)输出9(3的平方)

由此可见,对于python编写代码时,不能随意的缩进

相关文章

Python中用于去除空格的三个函数的使用小结

函数:strip()  lstrip()  rstrip() 作用:去除字符串中的空格或指定字符 一、默认用法:去除空格 str.strip()  : 去除...

Python实现简单多线程任务队列

最近我在用梯度下降算法绘制神经网络的数据时,遇到了一些算法性能的问题。梯度下降算法的代码如下(伪代码): def gradient_descent(): # the gradie...

python中随机函数random用法实例

本文实例讲述了python中随机函数random用法。分享给大家供大家参考。具体如下: python中的random模块功能非常强大,可以生成各种随机值 #! python # ra...

python reverse反转部分数组的实例

python3中,list有个reverse函数,用来反转列表元素,但是如果想要反转部分元素呢? a = [1,2,3,4,5] a[0:3].reverse() # not wor...

Python中Numpy ndarray的使用详解

本文主讲Python中Numpy数组的类型、全0全1数组的生成、随机数组、数组操作、矩阵的简单运算、矩阵的数学运算。 尽管可以用python中list嵌套来模拟矩阵,但使用Numpy库更...