sympy.Add() method in Python
In this tutorial, you will learn about the sympy.Add() method in Python.
To date we have performed mathematics operations on integers, floating point numbers, etc… but what if we could perform operations on variables.
sympy.Add() in Python
For this, we have the Sympy Python library which is used for symbolic mathematics operations in Python. Here we will only learn about sympy.Add() function.
Syntax: sympy.Add() Return: Returns the sum of the 2 variables.Pyt
We first import the Sympy library and then use the Add() function. Before using the Add() function, we need to declare the variables that we will be using for the operation.
from sympy import * #Declaring variables a,b= symbols('a b') result=Add(a , b) #printing result print(result)
Output: a + b
If we perform the same operation without declaring variables then we will get an error.
#Without declaring variables from sympy import * #a,b= symbols('a b') result=Add(a , b) print(result)
Output : name 'a' is not defined
We can also perform the addition of the same variable using this function. For this, we need to declare only 1 variable using the symbols() function that will be added to itself.
from sympy import * #declaring only 1 variable a= symbols('a') result=Add(a , a) #printing the result print(result)
Output : 2*a