Difference between revisions of "Python 3.4.3"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Loops)
(Handling Errors and Exceptions)
 
(10 intermediate revisions by the same user not shown)
Line 375: Line 375:
 
===  Manipulating lists ===
 
===  Manipulating lists ===
  
* concatenation <br />
+
#Various manipulation in lists
 
+
#Slicing of lists
* slicing <br />
+
#Syntax and demonstration of slicing of lists
 
+
#How to use step value in slicing
* striding <br />
+
#Striding of list
 
+
#Examples with various parameters in striding of list
* .sort <br />
+
#sort method in list
 
+
#Usage of sorted() built-in function
* sorted <br />
+
#Reverse a list
 
+
#Striding with negative values
* .reverse <br />
+
#How to store a new reversed list in another variable
 
+
* reversed <br />
+
  
 
=== Manipulating strings ===
 
=== Manipulating strings ===
 
+
#How to slice a string
* upper, lower, <br />
+
#Various way to get substrings using index
 
+
#Reverse a string
* replace <br />
+
#How to check if a given string is a palindrome or not
 
+
#Replace characters in a string
* slicing <br />
+
#Convert a string to uppercase
 
+
#Convert a string to lowercase
* [::-1] <br />
+
#How to use for loop in a list
 
+
#Join method
* reversed <br />
+
#Join list elements to form a string
 
+
* palindrome check <br />
+
  
 
=== Getting started with tuples ===
 
=== Getting started with tuples ===
 
+
#What are tuples?
 
+
#How to declare tuples?
* immutability <br />
+
#Examples to declare tuples
 
+
#Demonstration of creating tuple
* tuple packing, unpacking <br />
+
#Accessing tuples by their index positions
** a, b = b, a <br />
+
#Iteration over tuples
 
+
#Demonstration of immutability property of tuples
* accessing individual elements <br />
+
#How to swap values in tuples
 
+
#Similarities of tuples with lists
* slicing, striding <br />
+
#Tuple packing and unpacking
  
 
===  Dictionaries ===
 
===  Dictionaries ===
  
* empty <br />
+
#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
  
* filled <br />
+
=== Sets in Python ===
  
* accessing via keys <br />
+
#What are sets in python?
 
+
#Input sets
* .values(), .keys() <br />
+
#Create sets
 
+
#How to create empty sets?
* in <br />
+
#Operations on sets
 
+
#Add and remove methods
* iteration <br />
+
#Union and intersection methods
 
+
#Difference and symmetric_difference methods
=== Sets ===
+
#Subset and superset
Sets
+
#Length and containership on sets
 
+
About sets in python
+
  
 
==Module 8: Python Language: Advanced==
 
==Module 8: Python Language: Advanced==
Line 438: Line 441:
 
=== Getting started with functions ===  
 
=== Getting started with functions ===  
  
* defining function <br />
+
#About Functions
 
+
#How to define a function
* arguments <br />
+
#Example for defining a function
 
+
#Calling a function with arguments
* docstrings <br />
+
#Calling a function without arguments
 
+
#Return values from a function
* return values <br />
+
#Indentation in coding
** can return multiple values
+
#Documenting or commenting code
 
+
#How to use docstrings in python function
* code reading exercises <br />
+
#How to write a function circle to return area and perimeter with radius r
  
 
=== Advanced features of functions ===
 
=== Advanced features of functions ===
  
* default arguments <br />
+
#Functions with default arguments
 
+
#Various examples for default arguments
* keyword arguments <br />
+
#Interchanging the default and non-default arguments
 
+
#Call a function with keyword arguments
* built-in functions show off <br />
+
#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 ===  
 
=== Using python modules ===  
 
+
#Python modules
* scipy <br />
+
#Run a Python script from command line
 
+
#How to import modules in python scripts?
* pylab <br />
+
#How to import required functions from a module?
 
+
#Usage of namespace
* sys <br />
+
#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 ===  
 
=== Writing python scripts ===  
  
* import <br />
+
#About Python modules
 +
#What is importing?
 +
#How to write a function and save it as a script
 +
#Run a python script
 +
#Import a module
 +
#Example to import a module
 +
#usage of __name__ variable
 +
#Write test condition using the name variable
 +
#How importing works in new IPython console
 +
#Different ways of running the Python script
  
 
=== Testing and debugging ===
 
=== Testing and debugging ===
Testing and debugging
+
#What is software testing?
 +
#Write a simple function
 +
#How to write test cases?
 +
#Create simple tests for a function
 +
#Run the script and test the code
 +
#Automate tests
 +
#Example for test case fail
 +
#Coding style
 +
#How to give meaningful names in coding
 +
#Python coding standards
 +
 
 
=== Handling Errors and Exceptions ===
 
=== Handling Errors and Exceptions ===
  
* ValueError exception
+
#Errors in Python
* ZeroDivisionError exception
+
#Syntax errors and exception
* try except clause in Python
+
#Exceptions with example
* debugging
+
#Syntax error with example
 +
#Demonstration of ValueError exception
 +
#Demonstration of ZeroDivisionError exception
 +
#try except clause in Python
 +
#What is debugging?
 +
#Using %debug for debugging in ipython
 +
#try except with else clause

Latest revision as of 16:29, 9 October 2020

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

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

  1. Use Python 3.5.2
  2. Use Ipython version 5.1.0
  3. IPython is an enhanced interactive Python interpreter.
  4. Invoke the IPython interpreter
  5. Quit the IPython interpreter
  6. Navigate the IPython session history
  7. Use tab-completion to work faster.
  8. See the documentation of functions using question mark.
  9. Interrupt commands using Ctrl + C when we make an error.
  10. round command

Using the plot command interactively

  1. Use Python 3.4.3
  2. Use Ipython version 5.1.0
  3. Start IPython with pylab.
  4. ImportError if matplotlib is not installed
  5. clf() function
  6. Use the linspace function to create equally spaced points in a region.
  7. Find the length of sequences using len function.
  8. Plot mathematical functions using plot.
  9. Clear drawing area using clf.
  10. Usage of buttons in the UI of the plot window such as - save, zoom, move axis, back and forward and Home

Embellishing a plot

  1. Use Python 3.4.3
  2. Use Ipython version 5.1.0
  3. Modify the attributes of a plot
  4. Pass additional keyword arguments to plot command
  5. Add title to a plot using 'title' command.
  6. Incorporate LaTeX style formatting by adding a $ sign before and after the string.
  7. Label x and y axes using xlabel() and ylabel() commands.
  8. Add annotations to a plot using annotate() command.
  9. Get the limits of axes using xlim() and ylim() commands.
  10. Set the limits of axes using xlim() and ylim() commands.

Saving plots

  1. Use Python 3.4.3
  2. Use Ipython version 5.1.0
  3. Save plots using the savefig() function.
  4. Save the plots in different formats like
    • pdf
    • ps
    • png
    • svg
    • eps

Multiple plots

  1. Use Python 3.4.3
  2. Use IPython 5.1.0
  3. Draw multiple plots which are overlaid.
  4. Operations on individual plots.
  5. Use the figure command.
  6. Distinguish between multiple overlaid plots.
  7. Use the legend command.
  8. Serial number of the plot to select corresponding plot.
  9. Switch between the plots
  10. Saving individual plots.

Subplots

  1. Use Python 3.4.3
  2. Use IPython 5.1.0
  3. Creating subplots
  4. Switching between subplots.
  5. Subplot command
  6. Passing arguments to subplot command.
  7. First argument is the number of rows of subplots.
  8. Second argument is the number of columns of subplots
  9. Third argument specifies the serial number for subplot.

Additional features of IPython

  1. Use Python 3.4.3
  2. Use IPython 5.1.0
  3. Retrieve the history using  %history command.
  4. View only a part of history by passing argument to %history command.
  5. Pass arguments to %history to get particular lines of code
  6. Save the required lines of code in required order using '%save' command.
  7. Use '%run -i' command to run a saved script.

Module 2: Plotting Experimental Data

loading data from files

  1. load data from file
  2. single column
  3. multiple columns separated by delimiter
  4. cat command
  5. loadtxt()
  6. columns separated by spaces
  7. columns separated by semi-colon
  8. unpack argument
  9. delimiter argument
  10. three columns of data

Plotting the data

  1. plotting data
  2. list
  3. list element-wise squaring
  4. plot data points
  5. clear plots
  6. errorbar function
  7. dots or filled circles in plot
  8. plot with red pluses
  9. explore documentation in ipython
  10. plot with errorbars
  11. using format argument

Other types of plots

  1. scatter plot
  2. scatter function
  3. scatter plot with various arguments
  4. logarithmic plot
  5. loglog function
  6. cat command
  7. loadtxt function
  8. unpack parameter of loadtxt
  9. linspace
  10. scatter versus plot

Plotting charts

  1. Use Python 3.4.3
  2. Use IPython 5.1.0
  3. To produce scatter plot
  4. Plot a pie chart using pie() function
  5. Plot a bar chart using bar() function
  6. Access the matplotlib online help
  7. Charts with line hatching

Module 4: Handling Large Data Files

Getting started with lists

  1. What is a list?
    • Define List
    • List index
  2. Create:
    • List with elements
    • Empty list
    • List within a list
  3. 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

  1. For loop syntax
  2. Example to use For loop
  3. Indentation in for loop
  4. Create blocks in python using for
  5. Iterate over a list using for loop
  6. How to get out of the block
  7. use of Range() function
  8. Range function in for loop
  9. How to use Python interpreter
  10. IPython interpreter to specify blocks

Getting started with strings

  1. What are strings?
  2. How are strings denoted in Python?
  3. String concatenation
  4. Multiply a string with an integer
  5. Accessing individual elements of a string
  6. Accessing elements of a string using negative indices
  7. Split() function
  8. Join() function
  9. Define a string in different ways
  10. Print a string repeatedly

Getting started with files

  1. Open a file
  2. Open() function
  3. Different Modes of opening a file
  4. Read() method
  5. Read the content of the file line by line
  6. Read the entire content of the file
  7. Append the lines of a file to a list
  8. Close a file
  9. Demonstration using a txt file
  10. Splitlines() method

Parsing data

  1. What is Parsing data?
  2. split function and its syntax
  3. What is string tokenizing?
  4. How to split a string on whitespace?
  5. split function with argument
  6. strip function and example
  7. Converting string into floats and integers
  8. Example to read a huge .txt file line by line and parse each record
  9. Perform computations on the .txt file
  10. Execute the file using %run command

Statistics

  1. Statistical operations in Python
  2. Installation of Numpy for mathematical and logical operations
  3. Installation of pip to install python libraries
  4. loadtxt() function with example
  5. Getting the shape of an array
  6. Getting the sum of a column in an array
  7. How to calculate mean?
  8. Calculate mean across each of the axis of the array
  9. How to calculate median?
  10. How to calculate standard deviation?

Module 5: Arrays and Matrices

Getting started with arrays

  1. Overview of array
  2. Usage of numpy library
  3. How to create arrays
  4. How to create two dimensional array
  5. arange() method
  6. reshape() method
  7. How to find the shape of an array?
  8. Create a new array with elements of different datatypes
  9. Identity matrix
  10. Zeros method

Accessing parts of arrays

  1. Create a one-dimensional array
  2. Create a two-dimensional array
  3. Accessing individual elements of an array
  4. How to change the value of an array
  5. How to change more than one elements at a time
  6. Negative indexing of arrays
  7. Slicing of an array
  8. Striding of an array
  9. Access only the odd rows and columns of an array
  10. Examples to demonstrate all the manipulations of arrays

Image manipulation using Arrays

  1. Read images into arrays
  2. How to access parts of an array?
  3. imread command
  4. imshow command
  5. show command
  6. How to check the dimensions of an array?
  7. Example to access parts of an image
  8. How to stride over an array?
  9. Example to access an RGB image
  10. Slice an image of different dimension

Basic Matrix Operations

  1. Create matrices from lists
  2. asmatrix method
  3. arange and reshape methods
  4. Basic matrix operation
  5. Addition, subtraction and multiplication of a matrix
  6. Determinant of a matrix
  7. eye(), allclose() functions
  8. Inverse of a matrix
  9. eigenvalues and eigenvectors of a matrix
  10. diag() function

Advanced Matrix Operations

  1. flatten() function
  2. Example to convert a multidimensional matrix to single dimension matrix
  3. Frobenius norm of a matrix
  4. Demonstration of Frobenius norm of a matrix
  5. Inverse of a matrix
  6. Infinity norm of a matrix
  7. norm documentation
  8. Singular value decomposition
  9. svd() function
  10. smat function

Least square fit

  1. Generating a Least Square fit line
  2. Generating L vs t square
  3. loadtxt function
  4. Usage of numpy library
  5. Plotting L vs t square
  6. Steps for least square fit line
  7. Matrix formulation tsq=A.p
  8. Generating the two matrices tsq and A
  9. Finding transpose of a matrix
  10. lstsq() function

Module 6: Python Language: Basics

Basic datatypes & operators

  1. Data types in Python
  2. Demonstration of int, float and complex data types with examples
  3. Different functions associated with int data type
  4. Complex numbers and their functions
  5. Boolean operations with examples
  6. Operator precedence with parentheses
  7. Different operators available in Python3
  8. Modulo operator with examples
  9. How to do exponent operation in Python?
  10. How to find the square root of a number in Python?

Sequence datatypes

  1. List, string and tuple sequence data types with examples
  2. How to access a list using index numbers?
  3. Access the string elements
  4. Access the tuple elements
  5. How to add different sequence data types?
  6. How to find the length of a variable?
  7. Find the sum of a list
  8. Convert list to tuple
  9. Convert tuple to list
  10. Convert string to list and list to string


Input/output

  1. Input Output in Python
  2. Various output statements
  3. Print a string
  4. Print a string with newline character
  5. How to use format operators?
  6. Example for integer format
  7. Example for string format
  8. Example for float format
  9. Getting input from the user using Input()function
  10. Display a prompt to get the input
  11. Save the script as filename.py and execute using %run command

Conditionals Statements

  1. if condition statement
  2. Demonstration of if statement with example
  3. if/else condition statement with example
  4. Importance of indentation in a program
  5. Usage of colon in program
  6. Condition statement using elif
  7. Examples using if/elif/else block
  8. Ternary conditional statement
  9. Difference between if/else and ternary conditional statements
  10. How to use pass statement?

Loops

  1. Explanation of while loop
  2. Demonstration of while loop with example
  3. Print the squares of all the even numbers below 10 using while loop
  4. How to use for loop?
  5. Print the squares of all the even numbers below 10 using for loop
  6. for loop with range function
  7. How to use break statement in for loop
  8. pass statement in for loop
  9. continue statement in for loop
  10. Demonstration of pass, break and continue statements

Module 7: Python Language: Datastructures

Manipulating lists

  1. Various manipulation in lists
  2. Slicing of lists
  3. Syntax and demonstration of slicing of lists
  4. How to use step value in slicing
  5. Striding of list
  6. Examples with various parameters in striding of list
  7. sort method in list
  8. Usage of sorted() built-in function
  9. Reverse a list
  10. Striding with negative values
  11. How to store a new reversed list in another variable

Manipulating strings

  1. How to slice a string
  2. Various way to get substrings using index
  3. Reverse a string
  4. How to check if a given string is a palindrome or not
  5. Replace characters in a string
  6. Convert a string to uppercase
  7. Convert a string to lowercase
  8. How to use for loop in a list
  9. Join method
  10. Join list elements to form a string

Getting started with tuples

  1. What are tuples?
  2. How to declare tuples?
  3. Examples to declare tuples
  4. Demonstration of creating tuple
  5. Accessing tuples by their index positions
  6. Iteration over tuples
  7. Demonstration of immutability property of tuples
  8. How to swap values in tuples
  9. Similarities of tuples with lists
  10. Tuple packing and unpacking

Dictionaries

  1. Overview of dictionaries
  2. Creating an empty dictionary
  3. Creating a non empty dictionary
  4. About key:value pair
  5. How to access the dictionary elements
  6. Demonstration of wrong key
  7. Add, delete and modify an item in a dictionary
  8. Usage of method in
  9. Retrieve the keys and values by using the methods keys() and values()
  10. Iterate over elements of a dictionary using a for loop

Sets in Python

  1. What are sets in python?
  2. Input sets
  3. Create sets
  4. How to create empty sets?
  5. Operations on sets
  6. Add and remove methods
  7. Union and intersection methods
  8. Difference and symmetric_difference methods
  9. Subset and superset
  10. Length and containership on sets

Module 8: Python Language: Advanced

Getting started with functions

  1. About Functions
  2. How to define a function
  3. Example for defining a function
  4. Calling a function with arguments
  5. Calling a function without arguments
  6. Return values from a function
  7. Indentation in coding
  8. Documenting or commenting code
  9. How to use docstrings in python function
  10. How to write a function circle to return area and perimeter with radius r

Advanced features of functions

  1. Functions with default arguments
  2. Various examples for default arguments
  3. Interchanging the default and non-default arguments
  4. Call a function with keyword arguments
  5. Call a function without keyword arguments
  6. Functions with positional arguments
  7. Functions with arbitrary arguments
  8. Demonstration of arbitrary arguments
  9. Usage of * and ** in defining a function
  10. Python built-in-functions

Using python modules

  1. Python modules
  2. Run a Python script from command line
  3. How to import modules in python scripts?
  4. How to import required functions from a module?
  5. Usage of namespace
  6. Advantages of using import functions
  7. Using alias to the module
  8. Demonstration of import functions
  9. Run python scripts in ipython interpreter
  10. Python standard library of modules

Writing python scripts

  1. About Python modules
  2. What is importing?
  3. How to write a function and save it as a script
  4. Run a python script
  5. Import a module
  6. Example to import a module
  7. usage of __name__ variable
  8. Write test condition using the name variable
  9. How importing works in new IPython console
  10. Different ways of running the Python script

Testing and debugging

  1. What is software testing?
  2. Write a simple function
  3. How to write test cases?
  4. Create simple tests for a function
  5. Run the script and test the code
  6. Automate tests
  7. Example for test case fail
  8. Coding style
  9. How to give meaningful names in coding
  10. Python coding standards

Handling Errors and Exceptions

  1. Errors in Python
  2. Syntax errors and exception
  3. Exceptions with example
  4. Syntax error with example
  5. Demonstration of ValueError exception
  6. Demonstration of ZeroDivisionError exception
  7. try except clause in Python
  8. What is debugging?
  9. Using %debug for debugging in ipython
  10. try except with else clause

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, PoojaMoolya, Trupti