Python Programming: From Basics to Advance
Dr. Pramod Bokde
Contents
1 Introduction to Python for Mathematics 5
1.1 Why Use Python for Mathematics? . . . . . . . . . . . . . . . . . . . . 5
1.2 Setting up Python Environment . . . . . . . . . . . . . . . . . . . . . 6
1.3 Libraries for Mathematical Computing . . . . . . . . . . . . . . . . . . 7
2 Python Basics Refresher 11
3
Chapter 1
Introduction to Python for Mathematics
1.1 Why Use Python for Mathematics?
Mathematics is a subject that helps us understand numbers, patterns, shapes, and real-
world problems. In today’s world, we don’t just solve math problems using pen and pa-
per—we also use computers. One of the best tools for doing math on a computer is a pro-
gramming language called Python.
Python is popular because it is easy to learn and easy to read. You don’t need to write
long or complicated code to solve math problems. In fact, Python allows you to write your
ideas in a way that looks very similar to regular math formulas. For example, if you want to
find the square of a number, you can write just one short line of code. This helps beginners
focus on understanding math rather than struggling with programming.
Another big reason to use Python is the large number of helpful tools it has for math-
ematics. These tools are called libraries, and some of the most useful ones are NumPy,
SymPy, and SciPy. With NumPy, you can do fast calculations with large lists of numbers.
SymPy lets you solve equations just like you do on paper, and SciPy is great for more ad-
vanced topics like solving integrals or working with signals. These tools help you solve even
difficult math problems in just a few steps.
Python is also really good for drawing graphs and charts, which is important when
learning math. With Python, you can draw a curve, make a bar chart, or even create 3D
shapes. This makes it easier to understand what a math problem looks like and what it
means. You can actually see how math works, which helps you learn faster.
One great thing about Python is that it is used in many other subjects too, like science,
engineering, economics, and data science. That means if you learn how to use Python for
math, you will also be learning a useful skill for many different careers. Python helps you
connect math to the real world, like calculating interest in a bank account, predicting the
weather, or studying how a disease spreads.
Python is also free to use, and many people around the world use it. There are lots of
5
websites, videos, and books that teach you how to use Python for math. You can also write
and test your code online using tools like Jupyter Notebooks or Google Colab, so you don’t
even need to install anything special on your computer.
Most importantly, Python is great for trying out ideas. You can write a few lines of
code, run it, and instantly see what happens. If you make a mistake, you can fix it easily
and try again. This makes Python fun to use and helps you become more confident in math
and problem-solving.
In short, Python is a wonderful tool for learning and using mathematics. It is simple,
powerful, and fun. Whether you are a student, a teacher, or someone who just loves math,
Python can help you explore, understand, and enjoy mathematics in a whole new way.
1.2 Setting up Python Environment
Before we start solving math problems with Python, we need to set up the environment
where we will write and run our Python programs. Luckily, setting up Python is simple
and doesn’t take much time. First, we need to install Python on our computer. Python is
free and works on Windows, Mac, and Linux. You can download the latest version from
the official website [Link]. During installation, make sure to check the option that
says “Add Python to PATH”—this will allow you to run Python easily from anywhere on
your system.
Once Python is installed, the next step is choosing where you want to write and test
your Python code. There are many good options, but one of the best tools for learning
and doing math with Python is Jupyter Notebook. Jupyter lets you write code and see the
output right below it. You can also write notes, explanations, and even math formulas,
which makes it perfect for learning and teaching. You can install Jupyter by first installing
pip, Python’s package installer, and then running the command pip install notebook. After
that, you can open it by typing jupyter notebook in the command line. It will open in your
web browser, and you can start coding right away.
If you don’t want to install anything on your computer, you can use Google Colab. It
is a free online tool that works just like Jupyter Notebook but runs in your web browser.
Just go to [Link], sign in with your Google account, and create a new
notebook. You can start writing Python code immediately and even save your work in
Google Drive.
Sometimes, we also need special tools or libraries to do advanced math with Python.
These include libraries like NumPy, SymPy, and Matplotlib. You can install them easily
using pip. For example, to install NumPy, just type pip install numpy in your command
line or notebook. We will use these libraries a lot in the upcoming chapters, so it’s good to
have them ready from the start.
Setting up your Python environment is the first and most important step in your learn-
ing journey. Once it’s ready, you’ll be able to write, test, and explore math problems in a
fun and interactive way. Whether you choose to work on your own computer or online
with Google Colab, Python gives you all the tools you need to dive into the world of math-
ematical programming.
1.3 Libraries for Mathematical Computing
One of the greatest strengths of Python is its large collection of libraries that make math-
ematical computing simple, efficient, and powerful. These libraries are pre-written sets
of tools that help us perform a wide range of mathematical tasks—from basic arithmetic
and algebra to calculus, statistics, data analysis, and more. Instead of writing long code
from scratch, we can use these libraries to solve problems quickly and accurately. Below
are some of the most important libraries for mathematical computing in Python, each
serving a special purpose in the world of math and data.
1. NumPy (Numerical Python)
NumPy is the foundation of numerical computing in Python. It is used for work-
ing with arrays, matrices, and large sets of numerical data. With NumPy, you can
perform mathematical operations on entire lists of numbers at once, which makes
it very fast and efficient. It includes functions for basic arithmetic, linear algebra,
trigonometry, statistics, and more. NumPy’s array objects are much more powerful
than Python’s built-in lists because they take up less memory and allow for complex
operations like broadcasting and vectorization. For any task that involves numbers
and performance, NumPy is the go-to library.
2. SymPy (Symbolic Python)
SymPy is a library for symbolic mathematics. This means it allows us to perform
algebraic operations just like we do by hand. With SymPy, you can simplify ex-
pressions, solve equations, factor polynomials, perform calculus (like differentia-
tion and integration), and even work with matrices symbolically. Unlike NumPy,
which works with numerical values, SymPy keeps the math in exact form. This is es-
pecially helpful in education, where we want to see the full steps of a solution rather
than just a decimal answer. It also allows you to display mathematical expressions
in LaTeX-style format for clean, readable outputs.
3. SciPy (Scientific Python)
SciPy builds on top of NumPy and adds even more tools for scientific and technical
computing. It includes modules for optimization, integration, solving differential
equations, signal processing, and more. If you’re working on problems in physics,
engineering, or applied mathematics, SciPy offers powerful methods that can save
you hours of programming. Whether you’re finding the root of a complex equation
or solving a system of differential equations, SciPy provides high-level functions
that are both fast and accurate.
4. Matplotlib
Matplotlib is the most widely used library for creating graphs and visualizations
in Python. It helps you understand mathematical data by turning numbers into
pictures. You can plot lines, bars, histograms, scatter plots, and even 3D surfaces.
This is extremely useful in mathematics, where visualizing functions, equations, or
datasets can make patterns and solutions easier to see. Matplotlib works well with
NumPy and Pandas, and it can be used inside Jupyter Notebooks to display plots
right next to your code.
5. Pandas
Pandas is mainly used for data manipulation and analysis. While not a pure math-
ematical library, it plays an important role when working with data in table format.
Pandas makes it easy to clean, filter, reshape, and summarize datasets, which is es-
sential for real-world mathematical modeling. It works seamlessly with NumPy and
Matplotlib, and is often used in statistics, data science, and economics. With Pan-
das, you can load data from Excel, CSV, or databases and perform mathematical
operations directly on rows and columns.
6. Statsmodels
Statsmodels is a library built for statistical modeling. It includes tools for regression
analysis, hypothesis testing, and other advanced statistical methods. It is especially
useful when you want to understand the relationships between variables, test as-
sumptions, or build models from data. While Pandas is used to handle the data,
Statsmodels provides the mathematical engine to analyze it deeply. It is widely used
in economics, finance, and social sciences.
7. scikit-learn
Though primarily a machine learning library, scikit-learn also includes many tools
for mathematical computations such as statistics, linear algebra, dimensionality
reduction, and clustering. It is very useful when you start applying mathematical
knowledge to real-world problems like predictions, pattern recognition, or classifi-
cation. It works closely with NumPy and SciPy and serves as a bridge between pure
mathematics and applied machine learning.
8. mpmath
mpmath is a library for high-precision arithmetic. It allows calculations with arbi-
trary precision, which means you can get very accurate results even when dealing
with very small or very large numbers. It supports real and complex numbers and
can handle calculus, special functions, root-finding, and linear algebra with preci-
sion far beyond what most libraries offer. This is especially helpful in mathematical
research and advanced computations.
9. Theano
Theano is a deep mathematical library that lets you define, optimize, and evaluate
mathematical expressions efficiently, especially ones that involve multi-dimensional
arrays. It was one of the first tools used in deep learning and mathematical mod-
eling. Though newer tools like TensorFlow and PyTorch have become more popu-
lar, Theano remains important in mathematical computing because of its powerful
symbolic differentiation and optimization capabilities.
10. TensorFlow and PyTorch
These two libraries are mainly used in artificial intelligence, but they are built on
strong mathematical foundations. They provide tools for working with tensors (multi-
dimensional arrays), performing matrix operations, and using calculus-based opti-
mization methods like gradient descent. They are useful if you want to explore how
mathematics is used in machine learning, neural networks, and advanced model-
ing.
Chapter 2
Python Basics Refresher
Python Code
Let x = 10;
Let y = 20;
c= a + b;
if (a > b) :
print("a is greater than b");
11