In this we will learn how to implement mathematics using python. Now a day mathematics is the part of every programming language. Most of programming language logic work as per mathematics. So it is important to know some important mathematical concepts. In this Blog we will learn how to solve mathematics problem using python.
Here we learn some mathematical problem like, Numeric, linear equation, complex numbers, integral calculus, differential calculus, etc.
Example 1
How to write complex number in python
>>> z = complex(input('Enter a complex number: '))
z
Output :
Enter a complex number: 4+5j # enter without using space
(4+5j)
Example 2
Unit conversion using python
'''
Unit converter: Meter and Kilometers
'''
def print_menu():
print('1. Kilometers to Meter')
print('2. Meter to Kilometers')
def km_meter():
km = float(input('Enter distance in kilometers: '))
Meter = km* 1000
print('Distance in meter: {0}'.format(meter))
def meter_km():
miles = float(input('Enter distance in meter: '))
km = meter *(5/18)
print('Distance in kilometers: {0}'.format(km))
if __name__ == '__main__':
print_menu()
choice = input('Which conversion would you like to do?: ')
if choice == '1':
km_meter()
if choice == '2':
meter_km()
Example 3
Quadratic Equation - root calculator
'''
def roots(a, b, c):
D = (b*b - 4*a*c)**0.5
x_1 = (-b + D)/(2*a)
x_2 = (-b - D)/(2*a)
print('x1: {0}'.format(x_1))
print('x2: {0}'.format(x_2))
if __name__ == '__main__':
a = input('Enter a: ')
b = input('Enter b: ')
c = input('Enter c: ')
roots(float(a), float(b), float(c))
Output :
Enter a: 1
Enter b: 2
Enter c: 1
x1: -1.0
x2: -1.0
Solving Calculus Problems
First need to import library which is related to calculus
>>> from sympy import Limit, Symbol, S
Example 4
Simple example to find the Limit
>>> from sympy import Limit, Symbol, S
>>> x = Symbol('x')
>>> Limit(1/x, x, S.Infinity).doit()
Output : 0
Finding Derivative
Example 5
>>> from sympy import Symbol, Derivative
>>> t = Symbol('t')
>>> St = 5*t**2 + 2*t + 8
>>> d = Derivative(St, t)
>>> d.doit().subs({t:t1})
Sets and Probability
First need to import this
%matplotlib inline
Example 6
Finding Power set of a set
>>> from sympy import FiniteSet
>>> s = FiniteSet(1, 2, 3)
>>> ps = s.powerset()
Output :
{EmptySet(), {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
Example 7
Union of two set
>>> from sympy import FiniteSet
>>> s = FiniteSet(1, 2, 3)
>>> t = FiniteSet(2, 4, 6)
>>> s.union(t)
{1, 2, 3, 4, 6}
Output :
{1, 2, 3, 4, 6}
If you need any mathematics related help using python, please contact at given below link
If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion you can send mail at contact@codersarts.com.
Please write your suggestion in comment section below if you find anything incorrect in this blog post
Comments