CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial...

21
CSC321 - Python Tutorial Kaustav Kundu January 15, 2015 Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 1 / 14

Transcript of CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial...

Page 1: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

CSC321 - Python Tutorial

Kaustav Kundu

January 15, 2015

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 1 / 14

Page 2: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 3: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 4: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 5: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 6: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 7: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 8: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 9: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Lists

1 “Sum” of lists

2 2D lists

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 6 / 14

Page 10: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Lists

1 “Sum” of lists

2 2D lists

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 6 / 14

Page 11: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Lists

1 “Sum” of lists

2 2D lists

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 6 / 14

Page 12: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Arrays

Initialization

Element-wise operations

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 7 / 14

Page 13: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Arrays

Initialization

Element-wise operations

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 7 / 14

Page 14: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Arrays

Dot Products

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 8 / 14

Page 15: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Arrays

Matrix Multiplication

Transpose

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 9 / 14

Page 16: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Arrays

Matrix Multiplication

Transpose

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 9 / 14

Page 17: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 18: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 19: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Dictionary

Equivalent to std::map in C++.

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 12 / 14

Page 20: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

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

Page 21: CSC321 - Python Tutorialrgrosse/csc321/tutorial1.pdf · Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 10 / 14. Arrays vs Lists 1 numpy.array provides a lot of advantages

Classes

Kaustav Kundu CSC321 - Python Tutorial January 15, 2015 14 / 14