How to pass values to a formula?

Viewed 63

How Do I pass values say 12,32,34 to formula x+y+z in python without assigning manually? I have tried using **args but the results is None.

def myFormula(*args):
    lambda x, y: x+y+z(*args)
print(myFormula(1,2,3))
3 Answers

A regular function doesn't require doing any manual assignments:

def myFormula(x,y,z):
    return sum((x,y,z))

print(myFormula(1,2,3))

You can handle an arbitrary number of arguments in this situation like this:

def myFormula(*args):
    return sum(args)

print(myFormula(1,2,3,4))

try this:

formula = lambda x,y,z: x+y+z
print(formula(1,2,3))

there is no need to use *args there.

here is an example of using a function for a formula

# a = (v - u)/t
acceleration = lambda v, u, t: (v - u)/t
print(acceleration(23, 12, 5)

Just use sum:

print(sum([1, 2, 3]))

Output:

6

If you want a def try this:

def myFormula(*args):
    return sum(args)
print(myFormula(1, 2, 3))

Output:

6
Related