UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Using the Math Library in Python

Ashish K Sharma
01/06/2022 0 0

            Using the Math Library in Python.

Python provides many useful mathematical functions in a special math library. A library is a module that contains some useful definitions. We now consider writing a program which illustrates the use of this library.
                                   Task.
To compute the roots of a quadratic equation.


Now, as many of the students know, A Quadratic Equation has the form:
         X = -b±(√b*b – 4 * a * c) / 2 * a

For any value of x, such an equation has two solutions. Now, let us write a Python program which will find out the two solutions. The input to the program will be the values of the coefficients a,b and c. and, the output will be the two solutions, or values.


import os
import math ## importing the math module

a = input("Please enter the coefficient a ")
b = input("Please enter the coefficient b ")
c = input("Please enter the coefficient c ")

a = int(a)
b = int(b)
c = int(c)

discRoot = math.sqrt(b*b - 4 * a * c)

root1 = (-b+discRoot) / (2 * a)
root2 = (-b-discRoot) / (2 * a)

print("Solutions are = ",root1,root2)


                           Result
Please enter the coefficient a 3
Please enter the coefficient b 4
Please enter the coefficient c -2
Solutions are = 0.38742588672279316 -1.7207592200561266

The student must remember that this program only works when b*b – 4 * a * c is > 0,since sqrt function does not work on negative numbers.!!! The user must trap this error in order to make the program run smoothly.

Thus, we have seen the use of the math library, the first place to look in case we need a mathematical function. The following table shows some other functions available in the math library.

Python Mathematics English
pi p An approximation of pi.
e e An approximation of e.
sin(x) sinx The sine of x.
cos(x) cosx The cosine of x.
tan(x) tanx The tangent of x.
asin(x) arcsinx The inverse of sine x.
acos(x) arccosx The inverse of cosine x.
atan(x) arctanx The inverse of tangent x.
log(x) lnx The natural (base e) logarithm of x
log10(x) log10 x The common (base 10) logarithm of x.
exp(x) e^x The exponential of x
ceil(x) ┌x┐ The smallest whole number >= x
floor(x) └x┘ The largest whole number <= x












 

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Black in Python
When you are upturn in your career from beginner to experienced in programming world, your team will start looking at ‘how you are writing?’ Here the responsibility piling up. Okay,...

Learn Python : Formatting with the .format () method
A good way to format objects into your strings for print statements is with the string.format() method. The syntax is :'String here {} then also {}'.format('something1','something2')Example 1:print("This...
B

Biswanath Banerjee

0 1
0

Python SubProcess
Question: As a Network Administrator, one of my trainee wants to know the below: He has certain number of Machines(IP’s). On daily basic he wants to know how many IP’s are reachable and...

Python is a popular programming language. It was created by Guidovan Rossum, and released in 1991.
Python is a popular programming language. It was created by Guidovan Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. What...

Built-In Functions (Python)
Built-in Functions: The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order. Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() eval() isinstance() pow() sum() basestring() execfile() issubclass() print() super() bin() file() iter() property() tuple() bool() filter() len() range() type() bytearray() float() list() raw_input() unichr() callable() format() locals() reduce() unicode() chr() frozenset() long() reload() vars() classmethod() getattr() map() repr() xrange() cmp() globals() max() reversed() zip() compile() hasattr() memoryview() round() __import__() complex() hash() min() set() ...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more