Python is a versatile and powerful coding language that can be used  to execute all sorts of functionalities and processes. One of the best ways to get a feel for how Python works is to use it to create algorithms and solve equations. In this example, we'll show you how to use Python to solve one of the more well-known mathematical equations: the quadratic equation (ax2 + bx + c = 0).

import cmath

print('Solve the quadratic equation: ax**2 + bx + c = 0')
a = float(input('Please enter a : '))
b = float(input('Please enter b : '))
c = float(input('Please enter c : '))
delta = (b**2) - (4*a*c)
solution1 = (-b-cmath.sqrt(delta))/(2*a)
solution2 = (-b+cmath.sqrt(delta))/(2*a)

print('The solutions are {0} and {1}'.format(solution1,solution2))

As you can see, in order to solve the equation the cmath module must be imported, and equation is solved by using multiplication, division, and the cmath.sqrt method (which can be used to find the square root of a number). The printed text can be customized to say anything you like.


To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

  • vijairam ganeshmoorthy

    Good and efficient use of Python