Post on 16-Jun-2020
CSC321 - Python Tutorial
Kaustav Kundu
January 15, 2015
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 1 / 14
Why Python?
1 High level scripting language.
2 Rich library of modules, including third party modules/add-ons.
3 FOSS (Free and Open Source Software) - unlike Matlab.
4 Extremely good documentation (https://www.python.org/doc/) andsupport (stackoverflow, etc.).
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 2 / 14
Python Development Environment
1 Command Line InterpreterType python from the command line to use the python interpreter
2 Editor(a) vim, emacs(b) IDE: Spyder
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 3 / 14
Python Development Environment
1 Command Line InterpreterType python from the command line to use the python interpreter
2 Editor(a) vim, emacs(b) IDE: Spyder
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 3 / 14
Operations
1 Arithmetic+,−, ∗, /,%(modulus), ∗ ∗ (exponent), //(floor division)
2 Relational>,<,>=, <=,==, ! =
3 Logicalor, and, not
4 Bitwise&, |, ∧, ∼ , <<,>>
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 4 / 14
Lists
1 Initialization
2 Methods: append, extend, insert, remove, count, index,
sort, reverse
3 List comprehensions
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 5 / 14
Lists
1 Initialization
2 Methods: append, extend, insert, remove, count, index,
sort, reverse
3 List comprehensions
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 5 / 14
Lists
1 Initialization
2 Methods: append, extend, insert, remove, count, index,
sort, reverse
3 List comprehensions
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 5 / 14
Lists
1 “Sum” of lists
2 2D lists
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 6 / 14
Lists
1 “Sum” of lists
2 2D lists
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 6 / 14
Lists
1 “Sum” of lists
2 2D lists
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 6 / 14
Arrays
Initialization
Element-wise operations
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 7 / 14
Arrays
Initialization
Element-wise operations
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 7 / 14
Arrays
Dot Products
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 8 / 14
Arrays
Matrix Multiplication
Transpose
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 9 / 14
Arrays
Matrix Multiplication
Transpose
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 9 / 14
Arrays vs Lists
1 Difference between lists and arrays is similar to that between matricesand cell arrays in MATLAB.
2 All elements in arrays have to be of the same data type, specified atthe time of creation.
3 Usually memory efficient, may not be time efficient compared to lists.
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14
Arrays vs Lists
1 numpy.array provides a lot of advantages to perform matrixoperations, like transpose, inverse, eigen values, etc.
2 For more details look at numpy and scipy documentations.
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 11 / 14
Dictionary
Equivalent to std::map in C++.
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 12 / 14
Modules
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 13 / 14
Classes
Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 14 / 14