# Ufunc Arithmetic

## In this tutorial you will learn:

• What are Arithmetic functions?
• How to implement simple arithmetic function?
• Python Syntax

## Arithmetic function

Arithmetic operations are the simple operations of addition, subtraction, multiplication and division. The simple arithmetic universal functions in python can take in an array as well as lists and tuples to perform the index wise operations of addition, subtraction, multiplication and division. The simple arithmetic functions can be performed unconditionally and conditionally as well. The ufunc arithmetic functions takes a parameter “where” that ensures that the results are produced conditionally.

## Implement simple arithmetic function

Inorder to comprehend the implementation of arithmetic functions, lets take an example, here we will be adding up two tuples. Here in the first line of code we are importing the NumPy lib, and declaring each tuple in subsequent lines. Here you can observe that the arithmetic functions can perform operations on data structures other than arrays as well.

`#importing the numpy libimport numpy as np#Declaring the first tupletup1 = (1,3,5,7,9,11)#Declaring the second tupletup2 = (2,4,6,8,10,12)#adding up two tuples using add() arithmetic ufuncres = np.add(tup1,tup2)print('Result after adding up two tuples:')print(res)`

Now we will apply the add() arithmetic to two 1D arrays the values we will be using will be the same as used for the tuple.
`#importing the numpy libimport numpy as np#Declaring the first arrayarray_1 = np.array([1,3,5,7,9,11])#Declaring the second arrayarray_2 = np.array([2,4,6,8,10,12])#adding up two arrays using add() arithmetic ufuncres_arr = np.add(array_1,array_2)print('Result after adding up two arrays:')print(res_arr)`

In this example we be carrying out the subtraction op on two tuples. The same operation can be applied on the arrays and list as well with minor changes while declaring each data structure.
`#importing the numpy libimport numpy as np#Declaring the first arraytup1 = (11,13,15,17,19,21)#Declaring the second arraytup2 = (1,2,3,4,5,6)#subtracting two arrays using subtract() arithmetic ufunctup_res = np.subtract(tup1, tup2)print('Result after subtracting two tuples:')print(tup_res)`

This example demonstrates the multiplication operation of two tuples.The same function can be applied for lists and arrays as well.
`#importing the numpy libimport numpy as np#Declaring the first tupletup1 = (1,2,3,4,5,6)#Declaring the second tupletup2 = (20,30,40,50,60,70)#multiplying two arrays using multiply() arithmetic ufunctup_res = np.multiply(tup1, tup2)print('Result after multiplying two tuples:')print(tup_res)`

In this example we will use the divide one tuple by another tuple and index wise result will be produced.
`#importing the numpy libimport numpy as np#Declaring the first tupletup1 = (10,20,30,40,50,60)#Declaring the second tupletup2 = (1,2,3,4,5,6)#divide two arrays using divide() arithmetic ufunctup_res = np.divide(tup1, tup2)print('Result after dividing two tuples:')print(tup_res)`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.