Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
Arithmetic Operators in LISP
Next article icon

NumPy – Arithmetic Operations

Last Updated : 10 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

NumPy is an open-source Python library for performing array computing (matrix operations). It is a wrapper around the library implemented in C and used for performing several trigonometric, algebraic, and statistical operations.  NumPy objects can be easily converted to other types of objects like the Pandas data frame and the tensorflow tensor. Python list can be used for array computing, but it is much slower than NumPy. NumPy achieves its fast implementation using vectorization. One of the important features of NumPy arrays is that a developer can perform the same mathematical operation on every element with a single command.

Let us understand arithmetic operations using NumPy.

Addition

Python3

import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing addition using arithmetic operator
add_ans = a+b
print(add_ans)
 
# Performing addition using numpy function
add_ans = np.add(a, b)
print(add_ans)
 
# this would work
c = np.array([1, 2, 3, 4])
add_ans = a+b+c
print(add_ans)
 
# but here NumPy only considers the first two arrays (a and b) and ignores the third one (c).
add_ans = np.add(a, b, c)
print(add_ans)
                      
                       

Output

[  7  77  23 130]
[ 7 77 23 130]
[ 8 79 26 134]
[ 7 77 23 130]


As we can see that the matrixes are of the same shape, if they are different than, Numpy will try broadcasting if it is possible. The reader can see that the same operation (addition) can be done using arithmetic operation (+) as well as numpy function (np.add).

Subtraction

Python3

import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing subtraction using arithmetic operator
sub_ans = a-b
print(sub_ans)
 
# Performing subtraction using numpy function
sub_ans = np.subtract(a, b)
print(sub_ans)
                      
                       

Output

[ 3 67  3 70]
[ 3 67 3 70]

The user can also perform broadcasting with a matrix and a constant

Python3

import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing subtraction using arithmetic operator
sub_ans = a-b-1
print(sub_ans)
 
# Performing subtraction using numpy function
sub_ans = np.subtract(a, b, 1)
print(sub_ans)
                      
                       

Output

[ 2 66  2 69]
[ 2 66 2 69]

Multiplication

Python3

import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing multiplication using arithmetic operator
mul_ans = a*b
print(mul_ans)
 
# Performing multiplication using numpy function
mul_ans = np.multiply(a, b)
print(mul_ans)
                      
                       

Output

[  10  360  130 3000]
[ 10 360 130 3000]

Division

Python3

import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing division using arithmetic operators
div_ans = a/b
print(div_ans)
 
# Performing division using numpy functions
div_ans = np.divide(a, b)
print(div_ans)
                      
                       

Output

[ 2.5        14.4         1.3         3.33333333]
[ 2.5 14.4 1.3 3.33333333]

There is a myriad number of other functions which in NumPy let us see some of them one by one.

mod() and power() function

Example

Python3

# Performing mod on two matrices
mod_ans = np.mod(a, b)
print(mod_ans)
 
#Performing remainder on two matrices
rem_ans=np.remainder(a,b)
print(rem_ans)
 
# Performing power of two matrices
pow_ans = np.power(a, b)
print(pow_ans)
                      
                       

Output

[ 1  2  3 10]
[ 1 2 3 10]
[ 25 1934917632 137858491849
1152921504606846976]

Some aggregation and statistical functions

Example

Python3

# Getting mean of all numbers in 'a'
mean_a = np.mean(a)
print(mean_a)
 
# Getting average of all numbers in 'b'
mean_b = np.average(b)
print(mean_b)
 
# Getting sum of all numbers in 'a'
sum_a = np.sum(a)
print(sum_a)
 
# Getting variance of all number in 'b'
var_b = np.var(b)
print(var_b)
                      
                       

Output

47.5
11.75
190
119.1875


Next Article
Arithmetic Operators in LISP

A

apurva__007
Improve
Article Tags :
  • Python
  • Python numpy-Mathematical Function
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • Arithmetic Operations
    Arithmetic Operations are the basic mathematical operations—Addition, Subtraction, Multiplication, and Division—used for calculations. These operations form the foundation of mathematics and are essential in daily life, such as sharing items, calculating bills, solving time and work problems, and in
    10 min read
  • Arithmetic Operators in LISP
    Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. There are 7 arithmetic operators in LISP that are listed in the below table: OperatorSyntaxDescriptionAddition Operator(+)+ num1 num2Add the two numbersSubtraction Operator(-)-
    2 min read
  • Numpy | Binary Operations
    Binary operators acts on bits and performs bit by bit operation. Binary operation is simply a rule for combining two values to create a new value. numpy.bitwise_and() : This function is used to Compute the bit-wise AND of two array element-wise. This function computes the bit-wise AND of the underly
    8 min read
  • JavaScript Arithmetic Operators
    JavaScript Arithmetic Operators are the operator that operate upon the numerical values and return a numerical value. Addition (+) OperatorThe addition operator takes two numerical operands and gives their numerical sum. It also concatenates two strings or numbers. [GFGTABS] JavaScript // Number + N
    6 min read
  • Arithmetic Operators in Solidity
    Arithmetic operators are used to perform arithmetic or mathematical operations. Solidity has the following types of arithmetic operators: Addition: The addition operator takes two operands and results in a sum of these operands. It is denoted by +.Subtraction: The subtraction operator takes two oper
    2 min read
  • Python Arithmetic Operators
    Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS
    5 min read
  • VBA Arithmetic Operators in Excel
    Arithmetic Operators are the operators which perform mathematical operation or which perform any operation between two numbers like addition(+), subtraction(-), multiplication(*), division( / ), exponentiation(^), modulus(Mod), Bit-Shift operations. In this article, we will learn about the excel VBA
    3 min read
  • Arithmetic Operators in SQL Server
    Arithmetic operators play a crucial role in performing mathematical calculations within SQL Server. These operators allow you to perform addition, subtraction, multiplication, division, and more on numeric data stored in your database tables. In this article, we'll explore the various arithmetic ope
    4 min read
  • Arithmetic Operators in PostgreSQL
    PostgreSQL is an advanced relational database system that supports both relational (SQL) and non-relational (JSON) queries. It is free and open-source. One of the most fundamental properties of database systems is arithmetical operations, without which a multitude of tasks, from simple arithmetic to
    8 min read
  • Bash Script - Arithmetic Operators
    In this article, we will see arithmetic operators in bash script. Arithmetic operators is used to perform arithmetic operations. Bash script supports 11 arithmetic operators. All the operators with their uses is given below: OperatorNameUseExample+AdditionIt adds two operandsresult= a+b-SubtractionI
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences