scienti c computing using python - cse - iit kanpur · 2019-04-10 · the most popular programming...
TRANSCRIPT
![Page 1: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/1.jpg)
Scientific Computing using Python
Swaprava Nath
Dept. of CSEIIT Kanpur
mini-course webpage: https://swaprava.wordpress.com/a-short-course-on-python/
Disclaimer: the contents of this lecture series are based on several texts and online resources
![Page 2: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/2.jpg)
Outline of the Talk
1 Part 1: Preliminaries of Python
2 Part 2: Scientific Libraries
3 Part 3: Object Oriented Programming
1 / 24 Swaprava Nath Python
![Page 3: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/3.jpg)
Outline of the Talk
1 Part 1: Preliminaries of Python
2 Part 2: Scientific Libraries
3 Part 3: Object Oriented Programming
2 / 24 Swaprava Nath Python
![Page 4: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/4.jpg)
What is Python?
Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum
Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org
Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages
Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing
Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.
Academic usage: companion of many courses – for 101 CS courses or forsupportive computing
3 / 24 Swaprava Nath Python
![Page 5: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/5.jpg)
What is Python?
Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum
Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org
Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages
Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing
Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.
Academic usage: companion of many courses – for 101 CS courses or forsupportive computing
3 / 24 Swaprava Nath Python
![Page 6: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/6.jpg)
What is Python?
Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum
Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org
Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages
Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing
Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.
Academic usage: companion of many courses – for 101 CS courses or forsupportive computing
3 / 24 Swaprava Nath Python
![Page 7: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/7.jpg)
What is Python?
Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum
Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org
Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages
Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing
Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.
Academic usage: companion of many courses – for 101 CS courses or forsupportive computing
3 / 24 Swaprava Nath Python
![Page 8: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/8.jpg)
What is Python?
Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum
Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org
Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages
Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing
Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.
Academic usage: companion of many courses – for 101 CS courses or forsupportive computing
3 / 24 Swaprava Nath Python
![Page 9: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/9.jpg)
What is Python?
Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum
Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org
Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages
Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing
Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.
Academic usage: companion of many courses – for 101 CS courses or forsupportive computing
3 / 24 Swaprava Nath Python
![Page 10: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/10.jpg)
Relative popularity of Python
Image courtesy: www.quantecon.org
The plot, produced using Stack Overflow Trends, shows one measure of therelative popularity of Python
4 / 24 Swaprava Nath Python
![Page 11: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/11.jpg)
Features
High-level language, useful for rapid development
Relatively small core language – supported by many libraries
Multiparadigm language – the style of building the structure and elements ofcomputer programs can be
I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions
Usually implemented as interpreted as opposed to compiled (e.g. in C)
Syntax and design of a python code makes it easier to read, debug, anddevelop
5 / 24 Swaprava Nath Python
![Page 12: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/12.jpg)
Features
High-level language, useful for rapid development
Relatively small core language – supported by many libraries
Multiparadigm language – the style of building the structure and elements ofcomputer programs can be
I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions
Usually implemented as interpreted as opposed to compiled (e.g. in C)
Syntax and design of a python code makes it easier to read, debug, anddevelop
5 / 24 Swaprava Nath Python
![Page 13: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/13.jpg)
Features
High-level language, useful for rapid development
Relatively small core language – supported by many libraries
Multiparadigm language – the style of building the structure and elements ofcomputer programs can be
I procedural – based on routines/subroutines
I object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions
Usually implemented as interpreted as opposed to compiled (e.g. in C)
Syntax and design of a python code makes it easier to read, debug, anddevelop
5 / 24 Swaprava Nath Python
![Page 14: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/14.jpg)
Features
High-level language, useful for rapid development
Relatively small core language – supported by many libraries
Multiparadigm language – the style of building the structure and elements ofcomputer programs can be
I procedural – based on routines/subroutinesI object-oriented – created on abstract objects
I functional – treats computation as the evaluation of mathematical functions
Usually implemented as interpreted as opposed to compiled (e.g. in C)
Syntax and design of a python code makes it easier to read, debug, anddevelop
5 / 24 Swaprava Nath Python
![Page 15: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/15.jpg)
Features
High-level language, useful for rapid development
Relatively small core language – supported by many libraries
Multiparadigm language – the style of building the structure and elements ofcomputer programs can be
I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions
Usually implemented as interpreted as opposed to compiled (e.g. in C)
Syntax and design of a python code makes it easier to read, debug, anddevelop
5 / 24 Swaprava Nath Python
![Page 16: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/16.jpg)
Features
High-level language, useful for rapid development
Relatively small core language – supported by many libraries
Multiparadigm language – the style of building the structure and elements ofcomputer programs can be
I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions
Usually implemented as interpreted as opposed to compiled (e.g. in C)
Syntax and design of a python code makes it easier to read, debug, anddevelop
5 / 24 Swaprava Nath Python
![Page 17: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/17.jpg)
Features
High-level language, useful for rapid development
Relatively small core language – supported by many libraries
Multiparadigm language – the style of building the structure and elements ofcomputer programs can be
I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions
Usually implemented as interpreted as opposed to compiled (e.g. in C)
Syntax and design of a python code makes it easier to read, debug, anddevelop
5 / 24 Swaprava Nath Python
![Page 18: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/18.jpg)
Compiled vs Interpreted
The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement
Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code
Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code
Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors
I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again
I Step-by-step execution also helps in identifying errors
6 / 24 Swaprava Nath Python
![Page 19: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/19.jpg)
Compiled vs Interpreted
The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement
Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code
Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code
Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors
I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again
I Step-by-step execution also helps in identifying errors
6 / 24 Swaprava Nath Python
![Page 20: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/20.jpg)
Compiled vs Interpreted
The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement
Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code
Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code
Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors
I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again
I Step-by-step execution also helps in identifying errors
6 / 24 Swaprava Nath Python
![Page 21: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/21.jpg)
Compiled vs Interpreted
The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement
Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code
Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code
Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors
I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again
I Step-by-step execution also helps in identifying errors
6 / 24 Swaprava Nath Python
![Page 22: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/22.jpg)
Compiled vs Interpreted
The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement
Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code
Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code
Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors
I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again
I Step-by-step execution also helps in identifying errors
6 / 24 Swaprava Nath Python
![Page 23: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/23.jpg)
Compiled vs Interpreted
The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement
Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code
Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code
Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors
I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again
I Step-by-step execution also helps in identifying errors
6 / 24 Swaprava Nath Python
![Page 24: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/24.jpg)
Suitability for Scientific Computation
Availability of relevant libraries
I numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,
integration etc.I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling
used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.
7 / 24 Swaprava Nath Python
![Page 25: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/25.jpg)
Suitability for Scientific Computation
Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on them
I scipy – mathematical operations, e.g., linear algebra, optimization,integration etc.
I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling
used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.
7 / 24 Swaprava Nath Python
![Page 26: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/26.jpg)
Suitability for Scientific Computation
Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,
integration etc.
I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling
used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.
7 / 24 Swaprava Nath Python
![Page 27: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/27.jpg)
Suitability for Scientific Computation
Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,
integration etc.I matplotlib – all kinds of plotting, 2D and 3D
I pandas – for data handling
used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.
7 / 24 Swaprava Nath Python
![Page 28: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/28.jpg)
Suitability for Scientific Computation
Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,
integration etc.I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling
used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.
7 / 24 Swaprava Nath Python
![Page 29: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/29.jpg)
Suitability for Scientific Computation
Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,
integration etc.I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling
used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.
7 / 24 Swaprava Nath Python
![Page 30: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/30.jpg)
Example in action – 2D plot
A plot of surprise in the Brexit election
0.0 0.2 0.4 0.6 0.8 1.0
wG = 1− wI , weight of global observation
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
Fractionof
shocked
minoritypopulation
Intra-conn prob = 0.8, Inter-conn prob = 0.8
Bias = 0.01
Bias = 0.05
Bias = 0.1
Bias = 0.2
Used pandas, numpy, matplotlib
8 / 24 Swaprava Nath Python
![Page 31: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/31.jpg)
Example in action – 3D plot
X
40 30 20 10 0 10 20 30 40
Y
4030
2010
010203040
Z
1007550250255075100
Used matplotlib9 / 24 Swaprava Nath Python
![Page 32: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/32.jpg)
Setting up Your Python Environment
Native python2.7 is available in any linux distribution
Need to install the libraries
An alternative python distribution is by anaconda – www.anaconda.com
We will use the native python with libraries and an IDE
Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder
jupyter-notebook is useful for testing snippets of code and displaying
spyder is useful for a long codebase development
Examples
10 / 24 Swaprava Nath Python
![Page 33: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/33.jpg)
Setting up Your Python Environment
Native python2.7 is available in any linux distribution
Need to install the libraries
An alternative python distribution is by anaconda – www.anaconda.com
We will use the native python with libraries and an IDE
Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder
jupyter-notebook is useful for testing snippets of code and displaying
spyder is useful for a long codebase development
Examples
10 / 24 Swaprava Nath Python
![Page 34: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/34.jpg)
Setting up Your Python Environment
Native python2.7 is available in any linux distribution
Need to install the libraries
An alternative python distribution is by anaconda – www.anaconda.com
We will use the native python with libraries and an IDE
Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder
jupyter-notebook is useful for testing snippets of code and displaying
spyder is useful for a long codebase development
Examples
10 / 24 Swaprava Nath Python
![Page 35: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/35.jpg)
Setting up Your Python Environment
Native python2.7 is available in any linux distribution
Need to install the libraries
An alternative python distribution is by anaconda – www.anaconda.com
We will use the native python with libraries and an IDE
Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder
jupyter-notebook is useful for testing snippets of code and displaying
spyder is useful for a long codebase development
Examples
10 / 24 Swaprava Nath Python
![Page 36: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/36.jpg)
Setting up Your Python Environment
Native python2.7 is available in any linux distribution
Need to install the libraries
An alternative python distribution is by anaconda – www.anaconda.com
We will use the native python with libraries and an IDE
Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder
jupyter-notebook is useful for testing snippets of code and displaying
spyder is useful for a long codebase development
Examples
10 / 24 Swaprava Nath Python
![Page 37: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/37.jpg)
Some Introductory Python Programs
Task 1: finding if a number is even/odd – if-else clause
Task 2: finding the smallest of three numbers
Task 3: finding if a natural number is prime or not – while loop and for loop
Notice the indentation and absence of any braces or brackets
Makes the code clutter-free and more readable
11 / 24 Swaprava Nath Python
![Page 38: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/38.jpg)
More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions
xt+1 =1
2
(xt +
n
xt
)
Finding r-th root of any non-negative number n – bisection method
h =, l = 0, guess =h+ l
2if guessr > n : h = guess
else : l = guess
Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f
xt+1 = xt −f(xt)
f ′(xt)
12 / 24 Swaprava Nath Python
![Page 39: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/39.jpg)
More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions
xt+1 =1
2
(xt +
n
xt
)
Finding r-th root of any non-negative number n – bisection method
h =, l = 0, guess =h+ l
2if guessr > n : h = guess
else : l = guess
Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f
xt+1 = xt −f(xt)
f ′(xt)
12 / 24 Swaprava Nath Python
![Page 40: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/40.jpg)
More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions
xt+1 =1
2
(xt +
n
xt
)
Finding r-th root of any non-negative number n – bisection method
h =, l = 0, guess =h+ l
2if guessr > n : h = guess
else : l = guess
Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f
xt+1 = xt −f(xt)
f ′(xt)
12 / 24 Swaprava Nath Python
![Page 41: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/41.jpg)
More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions
xt+1 =1
2
(xt +
n
xt
)
Finding r-th root of any non-negative number n – bisection method
h = n, l = 0, guess =h+ l
2if guessr > n : h = guess
else : l = guess
Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f
xt+1 = xt −f(xt)
f ′(xt)
12 / 24 Swaprava Nath Python
![Page 42: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/42.jpg)
More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions
xt+1 =1
2
(xt +
n
xt
)
Finding r-th root of any non-negative number n – bisection method
h = max{1, n}, l = 0, guess =h+ l
2if guessr > n : h = guess
else : l = guess
Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f
xt+1 = xt −f(xt)
f ′(xt)
12 / 24 Swaprava Nath Python
![Page 43: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/43.jpg)
More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions
xt+1 =1
2
(xt +
n
xt
)
Finding r-th root of any non-negative number n – bisection method
h = max{1, n}, l = 0, guess =h+ l
2if guessr > n : h = guess
else : l = guess
Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f
xt+1 = xt −f(xt)
f ′(xt)
12 / 24 Swaprava Nath Python
![Page 44: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/44.jpg)
More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions
xt+1 =1
2
(xt +
n
xt
)
Finding r-th root of any non-negative number n – bisection method
h = max{1, n}, l = 0, guess =h+ l
2if guessr > n : h = guess
else : l = guess
Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f
xt+1 = xt −f(xt)
f ′(xt)
12 / 24 Swaprava Nath Python
![Page 45: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/45.jpg)
Python Standard Data Types
Python data types: mutable and immutable
Primitive data types: int, float, bool
Lists, Tuples, Strings
Dictionaries, Sets
Functions
Example of a function – the root finding algorithm as a function
Example of recursion of a function – checking a palindrome
Exercise: solve the ‘Tower of Hanoi’ problem using recursion
13 / 24 Swaprava Nath Python
![Page 46: Scienti c Computing using Python - CSE - IIT Kanpur · 2019-04-10 · the most popular programming languages Typical application domains: I scienti c computing { simulations I web](https://reader034.vdocument.in/reader034/viewer/2022043002/5f7deb5e5ab210478b5d5e22/html5/thumbnails/46.jpg)
Python Standard Data Types
Python data types: mutable and immutable
Primitive data types: int, float, bool
Lists, Tuples, Strings
Dictionaries, Sets
Functions
Example of a function – the root finding algorithm as a function
Example of recursion of a function – checking a palindrome
Exercise: solve the ‘Tower of Hanoi’ problem using recursion
13 / 24 Swaprava Nath Python