image source
Python is a high-level programming language that is very simple and easy to learn yet powerful. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. The Python interpreter and the extensive standard library can be downloaded at https://www.python.org/
This tutorial aims to solve quadratic equation in easiest way.
What Will I Learn?
Write here briefly the details of what the user is going to learn in a bullet list.
- Solving Quadratic Equation using Python
- Used of cmath module
Requirements
- Python IDLE
Difficulty
- Basic
Tutorial Contents
The Code:
First, import the cmath module which is already a package from the interpreter. This provides access to mathematical functions for complex numbers and it accepts integers, floating-point numbers or complex numbers as arguments.
import cmath
Second, take the coefficients a, b, and c from the user. It is treated as floating-point numbers so that it can accept non-decimal integers.
a = float(input('Enter value of a: '))
b = float(input('Enter value of b: '))
c = float(input('Enter value of c: '))
Third, calculate the discriminant with the formula b^2 - 4ac and assign it to a variable. In my case i used variable d.
d = (b**2) - (4*a*c)
Then with the discriminant value we can get our two solutions that can be solve using the formula x1 = -b+√(discriminant value) / 2a and x2 = -b-√(discriminant value) / 2a
solution1 = (-b-cmath.sqrt(d))/(2*a)
solution2 = (-b+cmath.sqrt(d))/(2*a)
And lastly, we print the answers of the solution.
print('The solution are {0} and {1}'.format(solution1,solution2))
Save the file and we can now run the code. Just press F5. The result will look like this:
This is my first contribution of an application using Python.
Thanks for reading! Hope you learn something.
Posted on Utopian.io - Rewarding Open Source Contributors
nice one sir!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thanks man!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
great sharing information :) keep it up!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
i hate math as i cant understand these algebric numbers
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Your contribution cannot be approved because it does not follow the Utopian Rules.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit