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, 2020 in Python by امیرحمزه
• 120 points
3,480 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, 2020 by Niroj
• 82,880 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 4, 2018 in Python by charlie_brown
• 7,720 points
12,926 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
• 58,090 points
2,101 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
581 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
1,429 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
2,630 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
639 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, 2020 in Python by Niroj
• 82,880 points
10,994 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, 2020 in Python by Niroj
• 82,880 points
6,838 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP