Solve the quadratic equation using lambda functions in Python

0 votes

I want to convert this code, which is to solve the quadratic equation, into a lambda function

from math import sqrt
def Equation(a,b,c):
    r = b ** 2 - 4 * a * c
    if r > 0:
        num_roots = 2
        x1 = (((-b) + sqrt(r)) / (2 * a))
        x2 = (((-b) - sqrt(r)) / (2 * a))
        print("There are 2 roots: %f and %f" % (x1, x2))
    elif r == 0:
        num_roots = 1
        x = (-b) / 2 * a
        print("There is one root: ", x)
    else:
        num_roots = 0
        print("No roots, discriminant < 0.")

Equation(1,2,0)
Jul 11 in Python by امیرحمزه
• 120 points
38 views

1 answer to this question.

0 votes

Hello @ Ø§Ù…یرحمزه ,

from math import sqrt

Root = lambda a, b, c: ((-b + sqrt((b * b) - (4 * a * c))) / (2 * a), (-b - sqrt((b * b) - (4 * a * c))) / (2 * a))

print(Root(1,2,0))      # call the lambda function

Hope it helps!!
Thank You!!

answered Jul 13 by Niroj
• 41,480 points

Related Questions In Python

+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,780 points
5,501 views
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 57,550 points
740 views
0 votes
1 answer
0 votes
1 answer

How Lambda() is used with filter() in python?

The filter() function in Python takes in ...READ MORE

answered May 20, 2019 in Python by Rakshi
62 views
0 votes
1 answer

What is the purpose of using lambda functions in Python?

The main purpose of anonymous functions come ...READ MORE

answered Jun 11, 2019 in Python by Nisa
• 1,090 points
130 views
0 votes
1 answer

Using lambda functions to solve Algebra

Python lambda functions can be used to ...READ MORE

answered Jun 17, 2019 in Python by anonymous
203 views
0 votes
1 answer

Why are Python lambdas useful?

The main purpose of anonymous functions come ...READ MORE

answered Jun 19, 2019 in Python by Wajiha
• 1,950 points
57 views
0 votes
1 answer

How to get the latest file in a folder using python?

Hello @kartik,  would suggest using glob.iglob() instead of the glob.glob(), as ...READ MORE

answered May 27 in Python by Niroj
• 41,480 points
249 views
0 votes
1 answer

What is the best way to remove accents in a Python unicode string?

Hello @kartik, Some languages have combining diacritics as ...READ MORE

answered May 11 in Python by Niroj
• 41,480 points
236 views