Juan Pablo Arcila
Juan Pablo Arcila

Reputation: 117

Python: creating a new function from math module functions

Good evening, I'd like to have the function x^3+x^2+sin(x) (for example) to work with, an intuitive try was this:

import math as m

h(x)=m.pow(x,3)+m.pow(x,2)+m.sin(x)

However I get a SyntaxError: can't assign to function call

How could I mix math module (or another module, it doesn't matter) functions in order to get the function I need?

Thanks

Upvotes: 0

Views: 566

Answers (1)

gilch
gilch

Reputation: 11651

The closest Python can get to your example would be with a lambda.

import math as m

h = lambda x: m.pow(x,3)+m.pow(x,2)+m.sin(x)

But lambdas are generally for anonymous functions. If you're going to give it a name, use def instead.

import math as m

def h(x):
    return m.pow(x,3)+m.pow(x,2)+m.sin(x)

Upvotes: 3

Related Questions