In this program, we will be discussing how to find the roots of a quadratic equation in Python without using the lambda function. We will be using the ‘cmath‘ module to perform complex math operations, and the ‘input’ function to take the coefficients of the equation as input from the user.
Quadratic equations are a common topic in high school algebra and are widely used in various fields such as physics, engineering, and economics. A quadratic equation is a polynomial equation of degree 2 and can be represented in the form of ax² + bx + c = 0, where a, b, and c are constants and x represents the unknown variable.
Python Program to Find Out the Roots of the Quadratic Equation Without Lambda
The following is the code for finding the roots of a quadratic equation without using the lambda function in Python:
# Python Program to Find Out the Roots of the Quadratic Equation Without Lambda # import complex math module import cmath # we are taking a number from user as input # entered value will be converted to int from string a=int(input("Enter first number:")) b=int(input("Enter second number:")) c=int(input("Enter third number:")) # calculate the discriminant d = (b**2) - (4*a*c) # find two solutions sol1 = (-b-cmath.sqrt(d))/(2*a) sol2 = (-b+cmath.sqrt(d))/(2*a) print('The solution are {0} and {1}'.format(sol1,sol2))
In the code, we first import the ‘cmath‘ module which is used to perform complex math operations. Then, we take three inputs from the user, which are the coefficients of the quadratic equation. The input values are converted from strings to integers using the ‘int’ function.
Next, we calculate the discriminant ‘d’ which is used to find the roots of the equation. The discriminant is calculated using the equation (b² – 4ac). After that, we use the discriminant to find the two roots of the equation using the formulas (-b±√(d))/(2a). These roots are stored in the ‘sol1’ and ‘sol2’ variables.
Finally, we use the ‘print’ function to display the roots of the equation. The roots are displayed using the string format method ‘format’ which allows us to embed the values of ‘sol1’ and ‘sol2’ in the string.
It is important to note that the ‘cmath’ module can handle complex numbers, which means that if the discriminant is negative, the roots of the equation will be complex numbers. This is why we use the ‘cmath’ module instead of the standard ‘math’ module which can only handle real numbers.
Output:
Enter first number:1
Enter second number:5
Enter third number:6
The solution are (-3+0j) and (-2+0j)
In conclusion, the code provided in this article can be used to find the roots of a quadratic equation in Python without using the lambda function. It is a simple and effective way to solve quadratic equations in Python, and it can be a useful tool for students and educators alike.
For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions