是否可以在python中的变量中保存加号和减号

时间:2017-05-28 23:03:56

标签: python math

这只是一个普遍的问题,因为我很好奇。当我运行以下代码时,我得到一个语法错误,这是可以理解的。但是有没有办法在变量中保存+符号,所以我得到4的答案?

# Selecting the time information in the names of the rows of DJI:
tt = time(DJI) 
# We select points spaced by approximately 1 business year:
# 365 days - 2 days off each weekend - 9 Holidays in the USA
ix = seq(1, nrow(DJI), by = 365 - 2*4*12 - 9) 
# Formatting the labels as just simply the year with two digits:
fmt = "%y"
# Generating vector of potential labels:
labs = format(tt, fmt) 
# Plotting the x axis:
axis(side = 1, at = tt[ix], labels = labs[ix],
     cex.axis = 0.7, las = 2)

1 个答案:

答案 0 :(得分:0)

您可以为+运算符创建函数:

def add(a, b):
    return a + b

并存储它:

operation = add
unit = 1
unit2 = 3

print(operation(unit, unit2))

运营商的功能甚至是built into Python

from operator import add