NUMERICAL METHOD
(MCSC-202)
USING PYTHON
By
Samir Shrestha
Department of Mathematics
Kathmandu University, Dhulikhel
Lecture-1
Needs of Computer Program
Numerical Method deals with designing and analysis of
algorithm for numerical approximations of the problems
from mathematics that might not be solve exactly
Implementing the numerical algorithm manually (pencil,
paper, calculator) is very tedious and time consuming
Needs computer program to implement the algorithm
and find the solution quickly
For this regard, we prefer to use computer programing
language PYTHON
Contents
Reference Books
Basics of computer program
About Python
Installation of Python
References
1. H. Bhasin, Python Basics A Self-Teaching Introduction, 2019
2. H. P. Langtangen, A Primer on Scientific Programming with Python,
2016
3. Jaan Kiusalaas, Numerical Methods in Engineering with Python 3, 2013
4. Robert Johansson, Numerical Python: Scientific Computing and Data
Science Applications with Numpy, SciPy and Matplotlib, 2019
5. https://www.javatpoint.com/python-tutorial
Languages
Languagage of Computer
Languages
Python C FORTRAN
High Level Language
Assembly Language
Machine Language
Hardware
Programming Hierarchy: Levels of programming language
Languages
Some influential ones:
FORTRAN
science / engineering
COBOL
business data
LISP
logic and AI
BASIC
a simple language
7
Compiler
A compiler is a program that
reads a program written in the
high-level language and
converts it into the machine or
low-level language and reports
the errors present in the
program.
It converts the entire source
code in one go or could take
multiple passes to do so, but at
last, the user gets the compiled
code which is ready to execute.
Examples: C, C++, C#, Scala
8
Compiler: 6 Phases
Lexical Analyzer: Scans the code
as a stream of characters into
lexemes. Output: Sequence of
tokens with reference to the
programming language
Syntax Analyzer: Tokens
generated in Lexical analyzer
phase are against grammar of
programming language. Checks
whether the expressions are
syntactically correct or not. It
makes parse trees
9
Compiler: 6 Phases
Semantic Analyzer: Checks
whether the expressions and
statements generated by
previous phase follow the rule of
programming language or not.
Creates annotated parse trees
Intermediate Code: Equivalent
intermediate code of the source
code.
Code Optimizer: Improves space
and time requirement of the
program. Eliminates the
redundant code
10
Compiler: 6 Phases
Code Generator: Final phase.
Target code for a particular
machine is generated.
Performs memory, register
management and machine
specific optimization
11
Interpreter
An alternative for implementing a
programming language and does
the same work as compiler Source
Code
It performs lexing, parsing and type
checking similar to compiler.
Processes syntax tree directly to
access expressions and executes
statements rather than generating Interpreter
code from the syntax tree
Require processing same syntax
tree more than once. Slower than
compiler
Output Code
Examples: JAVA, Python, Ruby, PHP
Compiler vs Interpreter
Compiler vs Interpreter
Process Compiler Interpreter
Input Takes an entire program Takes a single line of code
at a time at a time
Output Generates intermediate Won’t produce any
object code intermediate object code
When? Before execution Simultaneous
compilation and
execution
Speed Faster Slower
Memory Requirement More for object code less, no object code
Errors All errors at a time after Error, line by line, easier
compilation, difficult
Python
• Python is a simple, general purpose,
high level, and object-oriented
programming language
• Python is an interpreted language Born on 31 January, 1956
Over six years ago, in December 1989, I was
• Guido van Rossum is known as the looking for a "hobby" programming project that
would keep me occupied during the week around
founder of Python programming Christmas. My office (a government-run research
lab in Amsterdam) would be closed, but I had a
• The implementation of Python was home computer, and not much else on my hands.
I decided to write an interpreter for the new
started in the December 1989 scripting language I had been thinking about
by Guido van Rossum aIn January lately: a descendant of ABC that would appeal to
Unix/C hackers. I chose Python as a working title
2013, he startedt for the project, being in a slightly irreverent mood
(and a big fan of Monty Python's Flying Circus).
Center for Mathematics & Computer
--Guido van Rossum
Science (CWI) in Netherland Reston, VA, May 1996
• Name was coined from British comedy https://gvanrossum.github.io/
series Monty Python's Flying Circus
Python
Born on 31 January, 1956
Python Features
17
Python Features
Easy to Learn and Use: It is developer-friendly and high level
programming language
Python is an interpreted language : Interpreter executes the code line by
line at a time. This makes debugging easy and thus suitable for beginners.
Cross-platform Language: Python can run equally on different platforms
such as Windows, Linux, Unix and Macintosh etc. So, we can say that
Python is a portable language
Free and Open Source: Python language is freely available at offical web
address. The source-code is also available.
Object-Oriented Language: Python supports object oriented language
and concepts of classes and objects come into existence
Extensible: If needed, one can write some of the Python code in other
languages line C++. This makes Python an extensible language
Large Standard Library: Python has a large and broad library and provides
rich set of module and functions for rapid application development
GUI Programming Support: Graphical user interfaces can be developed
using Python
Integrated: It is also possible to put our Python code in a source code in
a different language like C++
18
Why Python
Clean Syntax: Easy to code and read
Dynamically Typed: No variable
declaration required
Although not a type-less language
Less development time
Require less training
19
Python Libraries
numpy: Multi-dimensional array
operations
matplotlib: plotting library
130K+ packages scipy: scientific computing
2K packages per pandas: data frame, data manipulation
month and analysis
• seaborn: For visualization of statistical
181657 projects data
• statsmodels: For statistical modeling and
advanced analysis
Symby: symbolic computation
SageMath: System of Algebra and
Geometry Experimentation
Python Libraries
PiCloud: Supercomputing
Many aspects processing capacity on the
Mathematics cloud
Algebra Scikit-learn: Machine Learning
toolkit
Combinatorics
Pygame: For game
Numerical development
Mathematics pyQT/pyGtk: To develop GUI
Calculus Nltk: natural language kit
Number theory TensorFlow: machine learning
library
Keras: machine learning library
Applications for Python
22
Applications for Python
Python Installation
Two different ways out of many
Python can be downloaded from python office web
page: www.python.org
OR
Python can be downloaded from anaconda
distribution:
https://www.anaconda.com/products/individual
Recommended version 3
24
Python Installation
Download python from www.python.org
Recommended version 3.8.3
Packages numpy, matplotlib, scipy should
be installed separately
Visit YouTube tutorials for Python installations
25
Python Installation
https://www.python.org/
26
Python Installation
https://www.python.org/
Click
27
Python Installation
https://www.python.org/downloads/
28
Python Installation
https://www.python.org/downloads/
29
Basics
Console: The text box onto which output is printed.
Some source code editors pop up the console as an
external window, and others contain their own console
window.
Code or Source Code: The sequence of instructions in a
program.
Syntax: The set of legal structures and commands that
can be used in a particular programming language.
Output: The messages printed to the user by a program.
30
Python Console Python Cell
Interactive cell to display all outputs and instant calcualtion
31
Python Editor Python IDLE
press F5 or run module to execute
the program to get the output
Figure as a output
32
Python Installation
Download python from Anaconda
Distributions:
https://www.anaconda.com/products/individual
Recommended version 3.7
Packages like numpy, matplotlib, scipy
come with the distribution
Visit YouTube tutorials for Python installations
through anaconda distributions
33
https://www.anaconda.com/products/individual Python Installation
Scroll down
34
Anaconda Navigator
After installation
35
Anaconda Navigator
After installation
36
Spyder
Python IDE - Spyder
37
Spyder
Python IDE - Spyder
38
Jupyter Notebook
Web-based, interactive computing notebook environmnt
39
Jupyter Notebook
Web-based, interactive computing notebook environmnt
40
End of Lecture-1
Lecture-2
Data Types, Operators, Math
Functions,
41