0

I want to make a lambda corresponding to the signum function in python.

i have tried the following ways

f = lambda x: if x>=0 1 else -1

Then, i want to plot this curve. my code is -

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(-10,10)
y=f(x)
plt.plot(x,y)
plt.show()

By this, I am not getting the desired result. Pls suggest a way in which this can be achieved.

  • 4
    Possible duplicate of [Does Python have a ternary conditional operator?](https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator) – Sneftel Sep 10 '19 at 14:22

1 Answers1

0
>>> f = lambda x: 1 if x>=0  else -1
>>> f(1)
1
>>> f(-2)
-1
Demi-Lune
  • 1,868
  • 2
  • 15
  • 26