Python 3.4.3
IIT Bombay is leading the effort to popularise Python for Scientific Computing in India. This is part of the Free and Open source Software for Science and Engineering Education (http://fossee.in) project, supported by the National Mission on Education through ICT of MHRD.
IIT Bombay is using Spoken Tutorials (http://spoken-tutorial.org) to create learning material for FOSS. This is the main page for the organisation of the scripts required for Standard two day workshop we conduct on "Python for Scientific Computing" spoken tutorials.
Learners: UG/PG CSE/IT/CS students and CBSE high school students
Contents
Note: Each numbered topic corresponds to a single spoken tutorial. Each bulleted point corresponds to a command or topic that must be covered in the given spoken tutorial.
Module 1: Basic Plotting
Getting started with ipython
- Use Python 3.5.2
- Use Ipython version 5.1.0
- IPython is an enhanced interactive Python interpreter.
- Invoke the IPython interpreter
- Quit the IPython interpreter
- Navigate the IPython session history
- Use tab-completion to work faster.
- See the documentation of functions using question mark.
- Interrupt commands using Ctrl + C when we make an error.
- round command
Using the plot command interactively
- Use Python 3.4.3
- Use Ipython version 5.1.0
- Start IPython with pylab.
- ImportError if matplotlib is not installed
- clf() function
- Use the linspace function to create equally spaced points in a region.
- Find the length of sequences using len function.
- Plot mathematical functions using plot.
- Clear drawing area using clf.
- Usage of buttons in the UI of the plot window such as - save, zoom, move axis, back and forward and Home
Embellishing a plot
- Use Python 3.4.3
- Use Ipython version 5.1.0
- Modify the attributes of a plot
- Pass additional keyword arguments to plot command
- Add title to a plot using 'title' command.
- Incorporate LaTeX style formatting by adding a $ sign before and after the string.
- Label x and y axes using xlabel() and ylabel() commands.
- Add annotations to a plot using annotate() command.
- Get the limits of axes using xlim() and ylim() commands.
- Set the limits of axes using xlim() and ylim() commands.
Saving plots
- Use Python 3.4.3
- Use Ipython version 5.1.0
- Save plots using the savefig() function.
- Save the plots in different formats like
- ps
- png
- svg
- eps
Multiple plots
- Use Python 3.4.3
- Use IPython 5.1.0
- Draw multiple plots which are overlaid.
- Operations on individual plots.
- Use the figure command.
- Distinguish between multiple overlaid plots.
- Use the legend command.
- Serial number of the plot to select corresponding plot.
- Switch between the plots
- Saving individual plots.
Subplots
- Use Python 3.4.3
- Use IPython 5.1.0
- Creating subplots
- Switching between subplots.
- Subplot command
- Passing arguments to subplot command.
- First argument is the number of rows of subplots.
- Second argument is the number of columns of subplots
- Third argument specifies the serial number for subplot.
Additional features of IPython
- Use Python 3.4.3
- Use IPython 5.1.0
- Retrieve the history using %history command.
- View only a part of history by passing argument to %history command.
- Pass arguments to %history to get particular lines of code
- Save the required lines of code in required order using '%save' command.
- Use '%run -i' command to run a saved script.
Module 2: Plotting Experimental Data
loading data from files
- load data from file
- single column
- multiple columns separated by delimiter
- cat command
- loadtxt()
- columns separated by spaces
- columns separated by semi-colon
- unpack argument
- delimiter argument
- three columns of data
Plotting the data
- plotting data
- list
- list element-wise squaring
- plot data points
- clear plots
- errorbar function
- dots or filled circles in plot
- plot with red pluses
- explore documentation in ipython
- plot with errorbars
- using format argument
Other types of plots
- scatter plot
- scatter function
- scatter plot with various arguments
- logarithmic plot
- loglog function
- cat command
- loadtxt function
- unpack parameter of loadtxt
- linspace
- scatter versus plot
Plotting charts
- Use Python 3.4.3
- Use IPython 5.1.0
- To produce scatter plot
- Plot a pie chart using pie() function
- Plot a bar chart using bar() function
- Access the matplotlib online help
- Charts with line hatching
Module 4: Handling Large Data Files
Getting started with lists
- What is a list?
- Define List
- List index
- Create:
- List with elements
- Empty list
- List within a list
- Find out the list length using len function
- Access elements using their index numbers
- Append elements to list using the function append
- Delete element from list using the del and remove function
Getting started with for
- For loop syntax
- Example to use For loop
- Indentation in for loop
- Create blocks in python using for
- Iterate over a list using for loop
- How to get out of the block
- use of Range() function
- Range function in for loop
- How to use Python interpreter
- IPython interpreter to specify blocks
Getting started with strings
- What are strings?
- How are strings denoted in Python?
- String concatenation
- Multiply a string with an integer
- Accessing individual elements of a string
- Accessing elements of a string using negative indices
- Split() function
- Join() function
- Define a string in different ways
- Print a string repeatedly
Getting started with files
- Open a file
- Open() function
- Different Modes of opening a file
- Read() method
- Read the content of the file line by line
- Read the entire content of the file
- Append the lines of a file to a list
- Close a file
- Demonstration using a txt file
- Splitlines() method
Parsing data
- What is Parsing data?
- split function and its syntax
- What is string tokenizing?
- How to split a string on whitespace?
- split function with argument
- strip function and example
- Converting string into floats and integers
- Example to read a huge .txt file line by line and parse each record
- Perform computations on the .txt file
- Execute the file using %run command
Statistics
- Statistical operations in Python
- Installation of Numpy for mathematical and logical operations
- Installation of pip to install python libraries
- loadtxt() function with example
- Getting the shape of an array
- Getting the sum of a column in an array
- How to calculate mean?
- Calculate mean across each of the axis of the array
- How to calculate median?
- How to calculate standard deviation?
Module 5: Arrays and Matrices
Getting started with arrays
- Overview of array
- Usage of numpy library
- How to create arrays
- How to create two dimensional array
- arange() method
- reshape() method
- How to find the shape of an array?
- Create a new array with elements of different datatypes
- Identity matrix
- Zeros method
Accessing parts of arrays
- Create a one-dimensional array
- Create a two-dimensional array
- Accessing individual elements of an array
- How to change the value of an array
- How to change more than one elements at a time
- Negative indexing of arrays
- Slicing of an array
- Striding of an array
- Access only the odd rows and columns of an array
- Examples to demonstrate all the manipulations of arrays
Image manipulation using Arrays
- Read images into arrays
- How to access parts of an array?
- imread command
- imshow command
- show command
- How to check the dimensions of an array?
- Example to access parts of an image
- How to stride over an array?
- Example to access an RGB image
- Slice an image of different dimension
Basic Matrix Operations
- Create matrices from lists
- asmatrix method
- arange and reshape methods
- Basic matrix operation
- Addition, subtraction and multiplication of a matrix
- Determinant of a matrix
- eye(), allclose() functions
- Inverse of a matrix
- eigenvalues and eigenvectors of a matrix
- diag() function
Advanced Matrix Operations
- flatten() function
- Example to convert a multidimensional matrix to single dimension matrix
- Frobenius norm of a matrix
- Demonstration of Frobenius norm of a matrix
- Inverse of a matrix
- Infinity norm of a matrix
- norm documentation
- Singular value decomposition
- svd() function
- smat function
Least square fit
- Generating a Least Square fit line
- Generating L vs t square
- loadtxt function
- Usage of numpy library
- Plotting L vs t square
- Steps for least square fit line
- Matrix formulation tsq=A.p
- Generating the two matrices tsq and A
- Finding transpose of a matrix
- lstsq() function
Module 6: Python Language: Basics
Basic datatypes & operators
- Data types in Python
- Demonstration of int, float and complex data types with examples
- Different functions associated with int data type
- Complex numbers and their functions
- Boolean operations with examples
- Operator precedence with parentheses
- Different operators available in Python3
- Modulo operator with examples
- How to do exponent operation in Python?
- How to find the square root of a number in Python?
Sequence datatypes
- List, string and tuple sequence data types with examples
- How to access a list using index numbers?
- Access the string elements
- Access the tuple elements
- How to add different sequence data types?
- How to find the length of a variable?
- Find the sum of a list
- Convert list to tuple
- Convert tuple to list
- Convert string to list and list to string
Input/output
- Input Output in Python
- Various output statements
- Print a string
- Print a string with newline character
- How to use format operators?
- Example for integer format
- Example for string format
- Example for float format
- Getting input from the user using Input()function
- Display a prompt to get the input
- Save the script as filename.py and execute using %run command
Conditionals Statements
- if condition statement
- Demonstration of if statement with example
- if/else condition statement with example
- Importance of indentation in a program
- Usage of colon in program
- Condition statement using elif
- Examples using if/elif/else block
- Ternary conditional statement
- Difference between if/else and ternary conditional statements
- How to use pass statement?
Loops
- Explanation of while loop
- Demonstration of while loop with example
- Print the squares of all the even numbers below 10 using while loop
- How to use for loop?
- Print the squares of all the even numbers below 10 using for loop
- for loop with range function
- How to use break statement in for loop
- pass statement in for loop
- continue statement in for loop
- Demonstration of pass, break and continue statements
Module 7: Python Language: Datastructures
Manipulating lists
- Various manipulation in lists
- Slicing of lists
- Syntax and demonstration of slicing of lists
- How to use step value in slicing
- Striding of list
- Examples with various parameters in striding of list
- sort method in list
- Usage of sorted() built-in function
- Reverse a list
- Striding with negative values
- How to store a new reversed list in another variable
Manipulating strings
- How to slice a string
- Various way to get substrings using index
- Reverse a string
- How to check if a given string is a palindrome or not
- Replace characters in a string
- Convert a string to uppercase
- Convert a string to lowercase
- How to use for loop in a list
- Join method
- Join list elements to form a string
Getting started with tuples
- What are tuples?
- How to declare tuples?
- Examples to declare tuples
- Demonstration of creating tuple
- Accessing tuples by their index positions
- Iteration over tuples
- Demonstration of immutability property of tuples
- How to swap values in tuples
- Similarities of tuples with lists
- Tuple packing and unpacking
Dictionaries
- Overview of dictionaries
- Creating an empty dictionary
- Creating a non empty dictionary
- About key:value pair
- How to access the dictionary elements
- Demonstration of wrong key
- Add, delete and modify an item in a dictionary
- Usage of method in
- Retrieve the keys and values by using the methods keys() and values()
- Iterate over elements of a dictionary using a for loop
Sets in Python
- What are sets in python?
- Input sets
- Create sets
- How to create empty sets?
- Operations on sets
- Add and remove methods
- Union and intersection methods
- Difference and symmetric_difference methods
- Subset and superset
- Length and containership on sets
Module 8: Python Language: Advanced
Getting started with functions
- About Functions
- How to define a function
- Example for defining a function
- Calling a function with arguments
- Calling a function without arguments
- Return values from a function
- Indentation in coding
- Documenting or commenting code
- How to use docstrings in python function
- How to write a function circle to return area and perimeter with radius r
Advanced features of functions
- Functions with default arguments
- Various examples for default arguments
- Interchanging the default and non-default arguments
- Call a function with keyword arguments
- Call a function without keyword arguments
- Functions with positional arguments
- Functions with arbitrary arguments
- Demonstration of arbitrary arguments
- Usage of * and ** in defining a function
- Python built-in-functions
Using python modules
- Python modules
- Run a Python script from command line
- How to import modules in python scripts?
- How to import required functions from a module?
- Usage of namespace
- Advantages of using import functions
- Using alias to the module
- Demonstration of import functions
- Run python scripts in ipython interpreter
- Python standard library of modules
Writing python scripts
- import
Testing and debugging
Testing and debugging
Handling Errors and Exceptions
- ValueError exception
- ZeroDivisionError exception
- try except clause in Python
- debugging