SlideShare une entreprise Scribd logo
1  sur  199
Télécharger pour lire hors ligne
Overview of Python
Programming
Samir Mohanty
samir2artificialintelligence@gmail.com
Mar, 2019
Disclaimer
 The slides presented here are obtained from
several authors of books and from various other
contributors. I hereby acknowledge all the
contributors for their material and inputs.
 I have added and modified a few slides to suit
the requirements of the training.
2Overview of Python Programming
2019
Agenda
Overview of Python Programming
2019
3
 Overview of Python basic syntax
 Modules in Python
 Overview of Data structures
 Overview of File I/O
 A few Modules
PYTHON BASICS
4Overview of Python Programming
2019
Introduction to Python
 A high-level programming language
 Open source and community driven
 Standard Distribution includes many modules
 Dynamic typed; Automatic Memory management
 Basically interpreted, but source can be compiled
 Easy to learn, yet powerful
 Robust support for OO programming
 Can integrate well with other languages
 Most popular for Data Analytics
5Overview of Python Programming
2019
6
Compiling and Interpreting
 Many languages require you to compile (translate) your program into
a form that the machine understands.
 Python is instead directly interpreted into machine instructions.
compile execute
outputsource code
Hello.java
byte code
Hello.class
interpret
outputsource code
Hello.py It generates a machine
independent bytecode
which is handled by the run
time
Overview of Python Programming
2019
Installing
 Python is pre-installed on most Unix systems,
including Linux and MAC OS X
 Set the path (/usr/bin/python) and ensure you have
execute permissions
 The pre-installed version may not be the most
recent one (2.7.15 and 3.7.2 as of Jan 19)
 Download the windows version from
http://python.org/download/
 Install as a standard windows application
 Python comes with a large library of standard
modules
9Overview of Python Programming
2019
Setup Python PATH
 Installing Python
 Setting up PATH
For Python:
 C:UsersMR GOKHLEAppDataLocalProgramsPythonPython37
For IDLE
 C:UsersMR GOKHLEAppDataLocalProgramsPythonPython37Libidlelib
 Python as a command processor
 Launch python at DOS shell prompt
 Execute python commands
 exit()
10Overview of Python Programming
2019
Launch from command prompt
11Overview of Python Programming
2019
IDEs
 There are several options for an IDE
 IDLE – works well with Windows
 Emacs with python-mode or your favorite text
editor
 Eclipse with Pydev
(http://pydev.sourceforge.net/) for Windows &
Linux
 PyCharm Community Edition (open source).
Professional Edition is Closed source. Available
for Windows & Linux
 Spyder ( MIT license). Windows & Linux
12Overview of Python Programming
2019
IDLE Development Environment
 IDLE is an Integrated DeveLopment Environment
for Python, typically used on Windows
 Multi-window text editor with syntax highlighting,
auto-completion, smart indent and other.
 Python shell with syntax highlighting.
 Integrated debugger
13Overview of Python Programming
2019
IDLE – Development
Environment
 IDLE helps you
program in Python
by:
 color-coding your
program code
 debugging
 auto-indent
 interactive shell
Comments are red
Strings are green
Definitions are blue
Keywords are orange
Output is blue
Shell prompt
 IDLE Shell:
15Overview of Python Programming
2019
Python Scripts
 When you call a python program from the
command line, the interpreter evaluates each
expression in the file
 Familiar mechanisms are used to provide
command line arguments and/or redirect input
and output
 Python also has mechanisms to allow a python
program to act both as a script and as a module
to be imported and used by another python
program
18Overview of Python Programming
2019
Python Scripts
 Code is interpreted
 You see the result immediately on the shell
window.
 If a statement is in error, execution stops and
error message is displayed.
 The interpretation does not guarantee display
of all errors. Errors are reported only when
encountered, it stops on the first error
19Overview of Python Programming
2019
A Code Sample (in IDLE)
x = 34 - 23 # A comment.
y = "Hello" # Another one.
z = 3.45
if z == 3.45 or y == "Hello":
x = x + 1
y = y + " World “ # String concat
print (x)
print (y)
Variables are used as needed.
No specific type of variable. It can hold
any type of data. The most recent
assignment determines the type
20Overview of Python Programming
2019
Data in Python
 Everything in Python is an object.
 All objects in Python can be either
mutable or immutable.
 A mutable object can be changed after it
is created, and an immutable object
can’t.
 Objects of built-in types like (int, float,
bool, str, tuple, unicode) are immutable.
Objects of built-in types like (list, set, dict)
are mutable
22Overview of Python Programming
2019
Basic Datatypes
 Numbers
 Strings
 List
 Tuple
 Set
 Dictionary
Data structures
23Overview of Python Programming
2019
Mutable / Immutable
 Mutable: meaning you can change their
content without changing their identity
 Immutable: are objects that can not be
changed
24Overview of Python Programming
2019
Modules & Packages
 Similar to libraries, called Modules
 It can be used (referenced) in standard python
code. Include the modules in the code.
 Usually a File containing python code
 Can define functions, classes, variables
 Multiple modules make up a Package, usually a
directory of python files.
 Official packages are listed in a directory (PyPI)
from where one can download / install
 pip is the tool to download and install packages
25Overview of Python Programming
2019
Basic Datatypes
 Integers (default for numbers)
z = 5 / 2 # Answer 2.5, real division
 Floats
x = 3.456
 Strings
 Can use " " or ' ' to specify with "abc" ==
'abc'
 Unmatched can occur within the string:
"matt's"
 Use triple quotes for multi-line strings or strings
than contain both ‘ and “ inside of them:
"""a'b"c""" 26Overview of Python Programming
2019
Data Structures
 List : An unordered collection of data items
 [35,23.15,"Hello", [2,3,'a','b']]
 Can be used as a stack, queue
 mutable
 Tuple : An unordered collection of data
items used to capture an entity’s data
 (25, 5.5, "My Data", ['a','b'], (1,2,3))
 immutable
27Overview of Python Programming
2019
Data Structures
 Set : An unordered set of values ( no
duplicates allowed)
 {2,3,"hello"}
 Dictionary : unordered collection of data
items, each data item is a <key, value>
pair
 {'name': 'John', 1: [2, 4, 3]}
28Overview of Python Programming
2019
Standard Operators
 Arithmetic
 Relational
 Logical
Overview of Python Programming
2019
29
Operator Description
** Exponentiation (raise to the power)
~ + -
Complement, unary plus and minus (method names for the last
two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'td>
^ | Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= +=
*= **=
Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
Control Structures
 if ..
 if ..else
 if .. elif
 while
 while .. else
 for
 break
 continue
Overview of Python Programming
2019
30
Functions & Classes
 def fname (abc) :
…
…
return(rval)
 class cname(pclass):
…
Overview of Python Programming
2019
31
Example function: fact.py
def fact(x):
"""Returns the factorial of its argument, assumed to be a posint"""
if x == 0:
return 1
return x * fact(x - 1)
print
print ("N fact(N)")
print ("---------")
for n in range(10):
print (n, " factorial is : ", fact(n))
32Overview of Python Programming
2019
Class definitions
Class ClassName:
<statement-1>
...
<statement-N>
 must be executed
 can be executed conditionally
 creates new namespace
33Overview of Python Programming
2019
Class objects
 obj.name references (plus module!):
class MyClass:
"A simple example class"
i = 123
def f(self):
return 'hello world'
>>> MyClass.i
123
 MyClass.f is method object
34Overview of Python Programming
2019
Files
 Files are manipulated by creating a file
object
 f = open("points.txt", "r")
 The file object then has new methods
 print f.readline() # prints line from file
 Files can be accessed to read or write
 f = open("output.txt", "w")
 f.write("Important Output!")
 Files are iterable objects, like lists
35Overview of Python Programming
2019
Error Capture
 Check for type assignment errors, items
not in a list, etc.
 Try & Except
try:
a block of code that might have an error
except:
code to execute if an error occurs in "try"
 Allows for graceful failure
– important for applications which use system resources
36Overview of Python Programming
2019
Popular Modules
 Cryptograpgy
 M2Crypto, OpenSSL
 GUI
 PyGTK, TKInter, PyQt
 Networking
 RPyC, HTTPLib2
 Plotting
 Matplotlib, plotly, SciPy
 Scientific
 Numpy, SciPy,
 Threading
 Threadpool
 Web development
 Django, web2py
Overview of Python Programming
2019
37
Hands On Bite
Comments
 Start comments with #, rest of line is ignored
 Can include a “documentation string” as the
first line of a new function or class you define
 Development environments, debugger, and
other tools use it: it’s good style to include one
def fact(n):
“““fact(n) assumes n is a positive
integer and returns factorial of n.”””
assert(n>0)
return 1 if n==1 else n*fact(n-1)
40Overview of Python Programming
2019
LETS GO HANDS ON..
41Overview of Python Programming
2019
Assignment
 Binding a variable in Python means setting a name to
hold a reference to some object
 Assignment creates references, not copies
 Names in Python do not have an intrinsic type, objects
have types
 Python determines the type of the reference automatically based
on what data is assigned to it
 You create a name the first time it appears on the left
side of an assignment expression:
x = 3
 A reference is deleted via garbage collection after any
names bound to it have passed out of scope
 Python uses reference semantics (more later)
42Overview of Python Programming
2019
Naming Rules
 Names are case sensitive and cannot start with a
number. They can contain letters, numbers, and
underscores.
bob Bob _bob _2_bob_ bob_2 BoB
 There are some reserved words:
and, assert, break, class, continue,
def, del, elif, else, except, exec,
finally, for, from, global, if, import,
in, is, lambda, not, or, pass, print,
raise, return, try, while
43Overview of Python Programming
2019
Naming Conventions
The Python community has these recommend-ed
naming conventions
joined_lower for functions, methods and,
attributes
joined_lower or ALL_CAPS for constants
StudlyCaps for classes
camelCase only to conform to pre-existing
conventions
Attributes: interface, _internal, __private
44Overview of Python Programming
2019
Assignment
You can assign to multiple names at the
same time
>>> x, y = 2, 3
>>> x
2
>>> y
3
This makes it easy to swap values
>>> x, y = y, x
Assignments can be chained
>>> a = b = x = 2
45Overview of Python Programming
2019
Accessing Non-Existent Name
Accessing a name before it’s been properly
created (by placing it on the left side of an
assignment), raises an error
>>> y
Traceback (most recent call last):
File "<pyshell#16>", line 1, in -toplevel-
y
NameError: name ‘y' is not defined
>>> y = 3
>>> y
3
46Overview of Python Programming
2019
Python Program Structure
 modules: Python source files or C extensions
 import, top-level via from, reload
 statements
 control flow
 create objects
 indentation matters – instead of { }
 case sensitive
 objects
 everything is an object
 automatically reclaimed when no longer needed (gc)
47Overview of Python Programming
2019
Basic operations
 Assignment:
 size = 40
 a = b = c = 3
 Numbers
 integer, float
 complex numbers: 1j+3, abs(z)
 Strings
 'hello world', 'it's hot'
 "bye world"
 continuation via  or use """ long text """"
48Overview of Python Programming
2019
String operations
 concatenate with + or neighbors
 word = 'Help' + x
 word = 'Help' 'a'
 subscripting of strings
 'Hello'[2]  'l'
 slice: 'Hello'[1:2]  'el'
 word[-1]  last character
 len(word)  5
 immutable: cannot assign to subscript
49Overview of Python Programming
2019
Lists
 lists can be heterogeneous
 a = ['spam', 'eggs', 100, 1234, 2*2]
 Lists can be indexed and sliced:
 a[0]  spam
 a[:2]  ['spam', 'eggs']
 Lists can be manipulated
 a[2] = a[2] + 23
 a[0:2] = [1,12]
 a[0:0] = []
 len(a)  5
50Overview of Python Programming
2019
Basic programming
a,b = 0, 1
# non-zero = true
while b < 10:
# formatted output, without n
print b,
# multiple assignment
a,b = b, a+b
51Overview of Python Programming
2019
Control flow: if
x = int(raw_input("Please enter #:"))
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
 no case statement
52Overview of Python Programming
2019
Control flow: for
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
 no arithmetic progression, but
 range(10)  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 for i in range(len(a)):
print i, a[i]
 do not modify the sequence being iterated over
53Overview of Python Programming
2019
Loops: break, continue, else
 break and continue like C
 else after loop exhaustion
for n in range(2,10):
for x in range(2,n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is prime'
54Overview of Python Programming
2019
Do nothing
 pass does nothing
 syntactic filler
while 1:
pass
55Overview of Python Programming
2019
List methods
 append(x)
 extend(L)
 append all items in list (like Tcl lappend)
 insert(i,x)
 remove(x)
 pop([i]), pop()
 create stack (FIFO), or queue (LIFO)  pop(0)
 index(x)
 return the index for value x
60Overview of Python Programming
2019
List methods
 count(x)
 how many times x appears in list
 sort()
 sort items in place
 reverse()
 reverse list
61Overview of Python Programming
2019
Functional programming tools
 filter(function, sequence)
def f(x): return x%2 != 0 and x%3 0
filter(f, range(2,25))
 map(function, sequence)
 call function for each item
 return list of return values
 reduce(function, sequence)
 return a single value
 call binary function on the first two items
 then on the result and next item
 iterate
62Overview of Python Programming
2019
List comprehensions (2.0)
 Create lists without map(), filter(),
lambda
 = expression followed by for clause + zero
or more for or of clauses
>>> vec = [2,4,6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [{x: x**2} for x in vec}
[{2: 4}, {4: 16}, {6: 36}]
63Overview of Python Programming
2019
List comprehensions
 cross products:
>>> vec1 = [2,4,6]
>>> vec2 = [4,3,-9]
>>> [x*y for x in vec1 for y in vec2]
[8,6,-18, 16,12,-36, 24,18,-54]
>>> [x+y for x in vec1 and y in vec2]
[6,5,-7,8,7,-5,10,9,-3]
>>> [vec1[i]*vec2[i] for i in
range(len(vec1))]
[8,12,-54]
64Overview of Python Programming
2019
List comprehensions
 can also use if:
>>> [3*x for x in vec if x > 3]
[12, 18]
>>> [3*x for x in vec if x < 2]
[]
65Overview of Python Programming
2019
del – removing list items
 remove by index, not value
 remove slices from list (rather than by assigning
an empty list)
>>> a = [-1,1,66.6,333,333,1234.5]
>>> del a[0]
>>> a
[1,66.6,333,333,1234.5]
>>> del a[2:4]
>>> a
[1,66.6,1234.5]
66Overview of Python Programming
2019
Tuples and sequences
 lists, strings, tuples: examples of
sequence type
 tuple = values separated by commas
>>> t = 123, 543, 'bar'
>>> t[0]
123
>>> t
(123, 543, 'bar')
67Overview of Python Programming
2019
Tuples
 Tuples may be nested
>>> u = t, (1,2)
>>> u
((123, 542, 'bar'), (1,2))
 kind of like structs, but no element names:
 (x,y) coordinates
 database records
 like strings, immutable  can't assign to
individual items
68Overview of Python Programming
2019
Tuples
 Empty tuples: ()
>>> empty = ()
>>> len(empty)
0
 one item  trailing comma
>>> singleton = 'foo',
69Overview of Python Programming
2019
Tuples
 sequence unpacking  distribute elements
across variables
>>> t = 123, 543, 'bar'
>>> x, y, z = t
>>> x
123
 packing always creates tuple
 unpacking works for any sequence
70Overview of Python Programming
2019
Dictionaries
 like Tcl or awk associative arrays
 indexed by keys
 keys are any immutable type: e.g., tuples
 but not lists (mutable!)
 uses 'key: value' notation
>>> tel = {'hgs' : 7042, 'lennox': 7018}
>>> tel['cs'] = 7000
>>> tel
71Overview of Python Programming
2019
Dictionaries
 no particular order
 delete elements with del
>>> del tel['foo']
 keys() method  unsorted list of keys
>>> tel.keys()
['cs', 'lennox', 'hgs']
 use has_key() to check for existence
>>> tel.has_key('foo')
0
72Overview of Python Programming
2019
Conditions
 can check for sequence membership with is and
is not:
>>> if (4 in vec):
... print '4 is'
 chained comparisons: a less than b AND b equals
c:
a < b == c
 and and or are short-circuit operators:
 evaluated from left to right
 stop evaluation as soon as outcome clear
73Overview of Python Programming
2019
Conditions
 Can assign comparison to variable:
>>> s1,s2,s3='', 'foo', 'bar'
>>> non_null = s1 or s2 or s3
>>> non_null
foo
 Unlike C, no assignment within expression
74Overview of Python Programming
2019
Comparing sequences
 unlike C, can compare sequences (lists,
tuples, ...)
 lexicographical comparison:
 compare first; if different  outcome
 continue recursively
 subsequences are smaller
 strings use ASCII comparison
 can compare objects of different type, but by
type name (list < string < tuple)
75Overview of Python Programming
2019
Comparing sequences
(1,2,3) < (1,2,4)
[1,2,3] < [1,2,4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1,2,3) == (1.0,2.0,3.0)
(1,2) < (1,2,-1)
76Overview of Python Programming
2019
Modules
 collection of functions and variables,
typically in scripts
 definitions can be imported
 file name is module name + .py
 e.g., create module fibo.py
def fib(n): # write Fib. series up to n
...
def fib2(n): # return Fib. series up to n
77Overview of Python Programming
2019
Modules
 import module:
import fibo
 Use modules via "name space":
>>> fibo.fib(1000)
>>> fibo.__name__
'fibo'
 can give it a local name:
>>> fib = fibo.fib
>>> fib(500)
78Overview of Python Programming
2019
Modules
 function definition + executable statements
 executed only when module is imported
 modules have private symbol tables
 avoids name clash for global variables
 accessible as module.globalname
 can import into name space:
>>> from fibo import fib, fib2
>>> fib(500)
 can import all names defined by module:
>>> from fibo import *
79Overview of Python Programming
2019
Module search path
 current directory
 list of directories specified in PYTHONPATH
environment variable
 uses installation-default if not defined, e.g.,
.:/usr/local/lib/python
 uses sys.path
>>> import sys
>>> sys.path
['', 'C:PROGRA~1Python2.2', 'C:Program
FilesPython2.2DLLs', 'C:Program
FilesPython2.2lib', 'C:Program
FilesPython2.2liblib-tk', 'C:Program
FilesPython2.2', 'C:Program FilesPython2.2libsite-
packages']
80Overview of Python Programming
2019
Compiled Python files
 include byte-compiled version of module if there
exists fibo.pyc in same directory as fibo.py
 only if creation time of fibo.pyc matches fibo.py
 automatically write compiled file, if possible
 platform independent
 doesn't run any faster, but loads faster
 can have only .pyc file  hide source
81Overview of Python Programming
2019
Standard modules
 system-dependent list
 always sys module
>>> import sys
>>> sys.p1
'>>> '
>>> sys.p2
'... '
>>> sys.path.append('/some/directory')
82Overview of Python Programming
2019
Module listing
 use dir() for each module
>>> dir(fibo)
['___name___', 'fib', 'fib2']
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__', '__st
din__', '__stdout__', '_getframe', 'argv', 'builtin_module_names', 'byteorder',
'copyright', 'displayhook', 'dllhandle', 'exc_info', 'exc_type', 'excepthook', '
exec_prefix', 'executable', 'exit', 'getdefaultencoding', 'getrecursionlimit', '
getrefcount', 'hexversion', 'last_type', 'last_value', 'maxint', 'maxunicode', '
modules', 'path', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setpr
ofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'version',
'version_info', 'warnoptions', 'winver']
83Overview of Python Programming
2019
Classes
 mixture of C++ and Modula-3
 multiple base classes
 derived class can override any methods of its
base class(es)
 method can call the method of a base class with
the same name
 objects have private data
 C++ terms:
 all class members are public
 all member functions are virtual
 no constructors or destructors (not needed)
84Overview of Python Programming
2019
Classes
 classes (and data types) are objects
 built-in types cannot be used as base
classes by user
 arithmetic operators, subscripting can be
redefined for class instances (like C++,
unlike Java)
85Overview of Python Programming
2019
Class definitions
Class ClassName:
<statement-1>
...
<statement-N>
 must be executed
 can be executed conditionally (see Tcl)
 creates new namespace
86Overview of Python Programming
2019
Namespaces
 mapping from name to object:
 built-in names (abs())
 global names in module
 local names in function invocation
 attributes = any following a dot
 z.real, z.imag
 attributes read-only or writable
 module attributes are writeable
87Overview of Python Programming
2019
Namespaces
 scope = textual region of Python program where
a namespace is directly accessible (without dot)
 innermost scope (first) = local names
 middle scope = current module's global names
 outermost scope (last) = built-in names
 assignments always affect innermost scope
 don't copy, just create name bindings to objects
 global indicates name is in global scope
88Overview of Python Programming
2019
Class objects
 obj.name references (plus module!):
class MyClass:
"A simple example class"
i = 123
def f(self):
return 'hello world'
>>> MyClass.i
123
 MyClass.f is method object
89Overview of Python Programming
2019
Class objects
 class instantiation:
>>> x = MyClass()
>>> x.f()
'hello world'
 creates new instance of class
 note x = MyClass vs. x = MyClass()
 ___init__() special method for initialization of
object
def __init__(self,realpart,imagpart):
self.r = realpart
self.i = imagpart
90Overview of Python Programming
2019
Instance objects
 attribute references
 data attributes (C++/Java data members)
 created dynamically
x.counter = 1
while x.counter < 10:
x.counter = x.counter * 2
print x.counter
del x.counter
91Overview of Python Programming
2019
Method objects
 Called immediately:
x.f()
 can be referenced:
xf = x.f
while 1:
print xf()
 object is passed as first argument of
function  'self'
 x.f() is equivalent to MyClass.f(x)
92Overview of Python Programming
2019
Notes on classes
 Data attributes override method attributes
with the same name
 no real hiding  not usable to implement
pure abstract data types
 clients (users) of an object can add data
attributes
 first argument of method usually called
self
 'self' has no special meaning (cf. Java)
93Overview of Python Programming
2019
Another example
 bag.py
class Bag:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
def addtwice(self,x):
self.add(x)
self.add(x)
94Overview of Python Programming
2019
Another example, cont'd.
 invoke:
>>> from bag import *
>>> l = Bag()
>>> l.add('first')
>>> l.add('second')
>>> l.data
['first', 'second']
95Overview of Python Programming
2019
Inheritance
class DerivedClassName(BaseClassName)
<statement-1>
...
<statement-N>
 search class attribute, descending chain of
base classes
 may override methods in the base class
 call directly via BaseClassName.method
96Overview of Python Programming
2019
Multiple inheritance
class DerivedClass(Base1,Base2,Base3):
<statement>
 depth-first, left-to-right
 problem: class derived from two classes
with a common base class
97Overview of Python Programming
2019
Private variables
 No real support, but textual replacement
(name mangling)
 __var is replaced by _classname_var
 prevents only accidental modification, not
true protection
98Overview of Python Programming
2019
~ C structs
 Empty class definition:
class Employee:
pass
john = Employee()
john.name = 'John Doe'
john.dept = 'CS'
john.salary = 1000
99Overview of Python Programming
2019
Exceptions
 syntax (parsing) errors
while 1 print 'Hello World'
File "<stdin>", line 1
while 1 print 'Hello World'
^
SyntaxError: invalid syntax
 exceptions
 run-time errors
 e.g., ZeroDivisionError, NameError,
TypeError
100Overview of Python Programming
2019
Handling exceptions
while 1:
try:
x = int(raw_input("Please enter a number: "))
break
except ValueError:
print "Not a valid number"
 First, execute try clause
 if no exception, skip except clause
 if exception, skip rest of try clause and use except
clause
 if no matching exception, attempt outer try statement
101Overview of Python Programming
2019
Handling exceptions
 try.py
import sys
for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except IOError:
print 'cannot open', arg
else:
print arg, 'lines:', len(f.readlines())
f.close
 e.g., as python try.py *.py
102Overview of Python Programming
2019
Language comparison
Tcl Perl Python JavaScript Visual
Basic
Speed development     
regexp   
breadth extensible   
embeddable  
easy GUI   (Tk) 
net/web     
enterprise cross-platform    
I18N    
thread-safe   
database access     
103Overview of Python Programming
2019
104Overview of Python Programming
2019
Sequence types:
Tuples, Lists, and
Strings
Sequence Types
1. Tuple: (‘john’, 32, [CMSC])
 A simple immutable ordered sequence of
items
 Items can be of mixed types, including
collection types
2. Strings: “John Smith”
 Immutable
 Conceptually very much like a tuple
3. List: [1, 2, ‘john’, (‘up’, ‘down’)]
 Mutable ordered sequence of items of
mixed types
106Overview of Python Programming
2019
Similar Syntax
 All three sequence types (tuples, strings,
and lists) share much of the same syntax
and functionality.
 Key difference:
 Tuples and strings are immutable
 Lists are mutable
 The operations shown in this section can
be applied to all sequence types
 most examples will just show the
operation performed on one
107Overview of Python Programming
2019
Sequence Types 1
 Define tuples using parentheses and commas
>>> tu = (23, ‘abc’, 4.56, (2,3),
‘def’)
 Define lists are using square brackets and
commas
>>> li = [“abc”, 34, 4.34, 23]
 Define strings using quotes (“, ‘, or “““).
>>> st = “Hello World”
>>> st = ‘Hello World’
>>> st = “““This is a multi-line
108Overview of Python Programming
2019
Sequence Types 2
 Access individual members of a tuple, list, or
string using square bracket “array” notation
 Note that all are 0 based…
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> tu[1] # Second item in the tuple.
‘abc’
>>> li = [“abc”, 34, 4.34, 23]
>>> li[1] # Second item in the list.
34
>>> st = “Hello World”
>>> st[1] # Second character in string.
‘e’
109Overview of Python Programming
2019
Positive and negative indices
>>> t = (23, ‘abc’, 4.56, (2,3),
‘def’)
Positive index: count from the left, starting with 0
>>> t[1]
‘abc’
Negative index: count from right, starting with –1
>>> t[-3]
4.56
110Overview of Python Programming
2019
Slicing: return copy of a subset
>>> t = (23, ‘abc’, 4.56,
(2,3), ‘def’)
Return a copy of the container with a subset of
the original members. Start copying at the first
index, and stop copying before second.
>>> t[1:4]
(‘abc’, 4.56, (2,3))
Negative indices count from end
>>> t[1:-1]
(‘abc’, 4.56, (2,3))
111Overview of Python Programming
2019
Slicing: return copy of a =subset
>>> t = (23, ‘abc’, 4.56,
(2,3), ‘def’)
Omit first index to make copy starting from
beginning of the container
>>> t[:2]
(23, ‘abc’)
Omit second index to make copy starting at first
index and going to end
>>> t[2:]
(4.56, (2,3), ‘def’)
112Overview of Python Programming
2019
Copying the Whole Sequence
 [ : ] makes a copy of an entire sequence
>>> t[:]
(23, ‘abc’, 4.56, (2,3), ‘def’)
 Note the difference between these two lines
for mutable sequences
>>> l2 = l1 # Both refer to 1
ref,
# changing one
affects both
>>> l2 = l1[:] # Independent 113Overview of Python Programming
2019
The ‘in’ Operator
 Boolean test whether a value is inside a
container:
>>> t = [1, 2, 4, 5]
>>> 3 in t
False
>>> 4 in t
True
>>> 4 not in t
False
 For strings, tests for substrings
>>> a = 'abcde'
>>> 'c' in a
True
>>> 'cd' in a
True
>>> 'ac' in a
False
 Be careful: the in keyword is also used in 114Overview of Python Programming
2019
The + Operator
The + operator produces a new tuple, list, or
string whose value is the concatenation of its
arguments.
>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> “Hello” + “ ” + “World” 115Overview of Python Programming
2019
The * Operator
 The * operator produces a new tuple, list, or
string that “repeats” the original content.
>>> (1, 2, 3) * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> “Hello” * 3
‘HelloHelloHello’
116Overview of Python Programming
2019
Mutability:
Tuples vs. Lists
Lists are mutable
>>> li = [‘abc’, 23, 4.34, 23]
>>> li[1] = 45
>>> li
[‘abc’, 45, 4.34, 23]
 We can change lists in place.
 Name li still points to the same memory
reference when we’re done.
118Overview of Python Programming
2019
Tuples are immutable
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> t[2] = 3.14
Traceback (most recent call last):
File "<pyshell#75>", line 1, in -toplevel-
tu[2] = 3.14
TypeError: object doesn't support item assignment
You can’t change a tuple.
You can make a fresh tuple and assign its
reference to a previously used name.
>>> t = (23, ‘abc’, 3.14, (2,3), ‘def’)
The immutability of tuples means they’re faster
than lists.
119Overview of Python Programming
2019
Operations on Lists Only
>>> li = [1, 11, 3, 4, 5]
>>> li.append(‘a’) # Note the
method syntax
>>> li
[1, 11, 3, 4, 5, ‘a’]
>>> li.insert(2, ‘i’)
>>>li 120Overview of Python Programming
2019
The extend method vs +
 + creates a fresh list with a new memory ref
 extend operates on list li in place.
>>> li.extend([9, 8, 7])
>>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7]
 Potentially confusing:
 extend takes a list as an argument.
 append takes a singleton as an argument.
>>> li.append([10, 11, 12])
>>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7, [10,
11, 12]] 121Overview of Python Programming
2019
Operations on Lists Only
Lists have many methods, including index, count,
remove, reverse, sort
>>> li = [‘a’, ‘b’, ‘c’, ‘b’]
>>> li.index(‘b’) # index of 1st
occurrence
1
>>> li.count(‘b’) # number of
occurrences
2
>>> li.remove(‘b’) # remove 1st
122Overview of Python Programming
2019
Operations on Lists Only
>>> li = [5, 2, 6, 8]
>>> li.reverse() # reverse the list *in place*
>>> li
[8, 6, 2, 5]
>>> li.sort() # sort the list *in place*
>>> li
[2, 5, 6, 8]
>>> li.sort(some_function)
# sort in place using user-defined comparison
123Overview of Python Programming
2019
Tuple details
The comma is the tuple creation operator,
not parens
>>> 1,
(1,)
Python shows parens for clarity (best
practice)
>>> (1,)
(1,)
Don't forget the comma!
>>> (1)
1
Trailing comma only required for
singletons others 124Overview of Python Programming
2019
Summary: Tuples vs. Lists
 Lists slower but more powerful than tuples
 Lists can be modified, and they have lots of
handy operations and mehtods
 Tuples are immutable and have fewer features
 To convert between tuples and lists use the list()
and tuple() functions:
li = list(tu)
tu = tuple(li)
125Overview of Python Programming
2019
126Overview of Python Programming
2019
127Overview of Python Programming
2019
More than just printing
 Python is an object oriented language
 Practically everything can be treated as an
object
 “hello world” is a string
 Strings, as objects, have methods that
return the result of a function on the string
128Overview of Python Programming
2019
String Methods
 Assign a string to a
variable
 In this case “hw”
 hw.title()
 hw.upper()
 hw.isdigit()
 hw.islower()
129Overview of Python Programming
2019
String Methods
 The string held in your variable remains
the same
 The method returns an altered string
 Changing the variable requires
reassignment
 hw = hw.upper()
 hw now equals “HELLO WORLD”
130Overview of Python Programming
2019
Other Python Objects
 Lists (mutable sets of strings)
 var = [] # create list
 var = [‘one’, 2, ‘three’, ‘banana’]
 Tuples (immutable sets)
 var = (‘one’, 2, ‘three’, ‘banana’)
 Dictionaries (associative arrays or ‘hashes’)
 var = {} # create dictionary
 var = {‘lat’: 40.20547, ‘lon’: -74.76322}
 var[‘lat’] = 40.2054
 Each has its own set of methods
131Overview of Python Programming
2019
Lists
 Think of a list as a stack of cards, on
which your information is written
 The information stays in the order you
place it in until you modify that order
 Methods return a string or subset of the
list or modify the list to add or remove
components
 Written as var[index], index refers to
order within set (think card number,
starting at 0) 132Overview of Python Programming
2019
List Methods
 Adding to the List
 var[n] = object
 replaces n with object
 var.append(object)
 adds object to the end of the list
 Removing from the List
 var[n] = []
 empties contents of card, but preserves order
 var.remove(n)
 removes card at n
 var.pop(n)
 removes n and returns its value
133Overview of Python Programming
2019
Lists in ArcToolbox
You will create lists:
 Layers as inputs
 Attributes to match
 Arrays of objects
You will work with
lists:
 List of field names
 List of selected
features
134Overview of Python Programming
2019
Tuples
 Like a list, tuples are iterable arrays of
objects
 Tuples are immutable –
once created, unchangeable
 To add or remove items, you must
redeclare
 Example uses of tuples
 County Names
 Land Use Codes
 Ordered set of functions 135Overview of Python Programming
2019
Dictionaries
 Dictionaries are sets of key & value pairs
 Allows you to identify values by a
descriptive name instead of order in a list
 Keys are unordered unless explicitly sorted
 Keys are unique:
 var[‘item’] = “apple”
 var[‘item’] = “banana”
 print var[‘item’] prints just banana
136Overview of Python Programming
2019
Indentation and Blocks
 Python uses whitespace and indents to
denote blocks of code
 Lines of code that begin a block end in a
colon:
 Lines within the code block are indented at
the same level
 To end a code block, remove the
indentation
 You'll want blocks of code that run only
when certain conditions are met 137Overview of Python Programming
2019
Conditional Branching
 if and else
if variable == condition:
#do something based on v == c
else:
#do something based on v != c
 elif allows for additional branching
if condition:
elif another condition:
…
else: #none of the above
138Overview of Python Programming
2019
Looping with For
 For allows you to loop over a block of code a
set number of times
 For is great for manipulating lists:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
Results:
cat 3
window 6
defenestrate 12
139Overview of Python Programming
2019
Looping with For
 We could use a for loop to perform
geoprocessing tasks on each layer in a list
 We could get a list of features in a feature
class and loop over each, checking
attributes
 Anything in a sequence or list can be used
in a For loop
 Just be sure not to modify the list while
looping
140Overview of Python Programming
2019
Modules
 Modules are additional pieces of code that
further extend Python’s functionality
 A module typically has a specific function
 additional math functions, databases,
network…
 Python comes with many useful modules
 arcgisscripting is the module we will use to
load ArcGIS toolbox functions into Python
141Overview of Python Programming
2019
Modules
 Modules are accessed using import
 import sys, os # imports two modules
 Modules can have subsets of functions
 os.path is a subset within os
 Modules are then addressed by
modulename.function()
 sys.argv # list of arguments
 filename = os.path.splitext("points.txt")
 filename[1] # equals ".txt"
142Overview of Python Programming
2019
Files
 Files are manipulated by creating a file
object
 f = open("points.txt", "r")
 The file object then has new methods
 print f.readline() # prints line from file
 Files can be accessed to read or write
 f = open("output.txt", "w")
 f.write("Important Output!")
 Files are iterable objects, like lists
143Overview of Python Programming
2019
Error Capture
 Check for type assignment errors, items
not in a list, etc.
 Try & Except
try:
a block of code that might have an error
except:
code to execute if an error occurs in "try"
 Allows for graceful failure
– important in ArcGIS
144Overview of Python Programming
2019
145Overview of Python Programming
2019
146Overview of Python Programming
2019
List
A compound data type:
[0]
[2.3, 4.5]
[5, "Hello", "there", 9.8]
[]
Use len() to get the length of a list
>>> names = [“Ben", “Chen", “Yaqin"]
>>> len(names)
3
147Overview of Python Programming
2019
Use [ ] to index items in the
list
>>> names[0]
‘Ben'
>>> names[1]
‘Chen'
>>> names[2]
‘Yaqin'
>>> names[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> names[-1]
‘Yaqin'
>>> names[-2]
‘Chen'
>>> names[-3]
‘Ben'
[0] is the first item.
[1] is the second item
...
Out of range values
raise an exception
Negative values
go backwards from
the last element.
148Overview of Python Programming
2019
Strings share many features with
lists
>>> smiles = "C(=N)(N)N.C(=O)(O)O"
>>> smiles[0]
'C'
>>> smiles[1]
'('
>>> smiles[-1]
'O'
>>> smiles[1:5]
'(=N)'
>>> smiles[10:-4]
'C(=O)'
Use “slice” notation to
get a substring
149Overview of Python Programming
2019
String Methods: find, split
smiles = "C(=N)(N)N.C(=O)(O)O"
>>> smiles.find("(O)")
15
>>> smiles.find(".")
9
>>> smiles.find(".", 10)
-1
>>> smiles.split(".")
['C(=N)(N)N', 'C(=O)(O)O']
>>>
Use “find” to find the
start of a substring.
Start looking at position 10.
Find returns -1 if it couldn’t
find a match.
Split the string into parts
with “.” as the delimiter
150Overview of Python Programming
2019
String operators: in, not in
if "Br" in “Brother”:
print "contains brother“
email_address = “clin”
if "@" not in email_address:
email_address += "@brandeis.edu“
151Overview of Python Programming
2019
String Method: “strip”, “rstrip”, “lstrip” are
ways to
remove whitespace or selected characters
>>> line = " # This is a comment line n"
>>> line.strip()
'# This is a comment line'
>>> line.rstrip()
' # This is a comment line'
>>> line.rstrip("n")
' # This is a comment line '
>>>
152Overview of Python Programming
2019
More String methods
email.startswith(“c") endswith(“u”)
True/False
>>> "%s@brandeis.edu" % "clin"
'clin@brandeis.edu'
>>> names = [“Ben", “Chen", “Yaqin"]
>>> ", ".join(names)
‘Ben, Chen, Yaqin‘
>>> “chen".upper()
‘CHEN'
153Overview of Python Programming
2019
Unexpected things about
strings
>>> s = "andrew"
>>> s[0] = "A"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item
assignment
>>> s = "A" + s[1:]
>>> s
'Andrew‘
Strings are read only
154Overview of Python Programming
2019
“” is for special characters
n -> newline
t -> tab
 -> backslash
...
But Windows uses backslash for directories!
filename = "M:nickel_projectreactive.smi" # DANGER!
filename = "M:nickel_projectreactive.smi" # Better!
filename = "M:/nickel_project/reactive.smi" # Usually works
155Overview of Python Programming
2019
Lists are mutable - some useful
methods
>>> ids = ["9pti", "2plv", "1crn"]
>>> ids.append("1alm")
>>> ids
['9pti', '2plv', '1crn', '1alm']
>>>ids.extend(L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
>>> del ids[0]
>>> ids
['2plv', '1crn', '1alm']
>>> ids.sort()
>>> ids
['1alm', '1crn', '2plv']
>>> ids.reverse()
>>> ids
['2plv', '1crn', '1alm']
>>> ids.insert(0, "9pti")
>>> ids
['9pti', '2plv', '1crn', '1alm']
append an element
remove an element
sort by default order
reverse the elements in a list
insert an element at some
specified position.
(Slower than .append())
156Overview of Python Programming
2019
Tuples: sort of an immutable
list
>>> yellow = (255, 255, 0) # r, g, b
>>> one = (1,)
>>> yellow[0]
>>> yellow[1:]
(255, 0)
>>> yellow[0] = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Very common in string interpolation:
>>> "%s lives in %s at latitude %.1f" % ("Andrew", "Sweden",
57.7056)
'Andrew lives in Sweden at latitude 57.7'
157Overview of Python Programming
2019
zipping lists together
>>> names
['ben', 'chen', 'yaqin']
>>> gender = [0, 0, 1]
>>> zip(names, gender)
[('ben', 0), ('chen', 0), ('yaqin', 1)]
158Overview of Python Programming
2019
Dictionaries
 Dictionaries are lookup tables.
 They map from a “key” to a “value”.
symbol_to_name = {
"H": "hydrogen",
"He": "helium",
"Li": "lithium",
"C": "carbon",
"O": "oxygen",
"N": "nitrogen"
}
 Duplicate keys are not allowed
 Duplicate values are just fine
159Overview of Python Programming
2019
Keys can be any immutable value
numbers, strings, tuples, frozenset,
not list, dictionary, set, ...
atomic_number_to_name = {
1: "hydrogen"
6: "carbon",
7: "nitrogen"
8: "oxygen",
}
nobel_prize_winners = {
(1979, "physics"): ["Glashow", "Salam", "Weinberg"],
(1962, "chemistry"): ["Hodgkin"],
(1984, "biology"): ["McClintock"],
}
A set is an unordered collection
with no duplicate elements.
160Overview of Python Programming
2019
Dictionary
>>> symbol_to_name["C"]
'carbon'
>>> "O" in symbol_to_name, "U" in symbol_to_name
(True, False)
>>> "oxygen" in symbol_to_name
False
>>> symbol_to_name["P"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'P'
>>> symbol_to_name.get("P", "unknown")
'unknown'
>>> symbol_to_name.get("C", "unknown")
'carbon'
Get the value for a given key
Test if the key exists
(“in” only checks the keys,
not the values.)
[] lookup failures raise an exception.
Use “.get()” if you want
to return a default value. 161Overview of Python Programming
2019
Some useful dictionary
methods
>>> symbol_to_name.keys()
['C', 'H', 'O', 'N', 'Li', 'He']
>>> symbol_to_name.values()
['carbon', 'hydrogen', 'oxygen', 'nitrogen', 'lithium', 'helium']
>>> symbol_to_name.update( {"P": "phosphorous", "S": "sulfur"} )
>>> symbol_to_name.items()
[('C', 'carbon'), ('H', 'hydrogen'), ('O', 'oxygen'), ('N', 'nitrogen'), ('P',
'phosphorous'), ('S', 'sulfur'), ('Li', 'lithium'), ('He', 'helium')]
>>> del symbol_to_name['C']
>>> symbol_to_name
{'H': 'hydrogen', 'O': 'oxygen', 'N': 'nitrogen', 'Li': 'lithium', 'He': 'helium'}
162Overview of Python Programming
2019
 Background
 Data Types/Structure
list, string, tuple, dictionary
 Control flow
 File I/O
 Modules
 Class
 NLTK
163Overview of Python Programming
2019
Control Flow
Things that are False
 The boolean value False
 The numbers 0 (integer), 0.0 (float) and 0j (complex).
 The empty string "".
 The empty list [], empty dictionary {} and empty set
set().
Things that are True
 The boolean value True
 All non-zero numbers.
 Any string containing at least one character.
 A non-empty data structure.
164Overview of Python Programming
2019
If
>>> smiles = "BrC1=CC=C(C=C1)NN.Cl"
>>> bool(smiles)
True
>>> not bool(smiles)
False
>>> if not smiles:
... print "The SMILES string is empty"
...
 The “else” case is always optional
165Overview of Python Programming
2019
Use “elif” to chain subsequent
tests
>>> mode = "absolute"
>>> if mode == "canonical":
... smiles = "canonical"
... elif mode == "isomeric":
... smiles = "isomeric”
... elif mode == "absolute":
... smiles = "absolute"
... else:
... raise TypeError("unknown mode")
...
>>> smiles
' absolute '
>>>
“raise” is the Python way to raise exceptions
166Overview of Python Programming
2019
Boolean logic
Python expressions can have “and”s and
“or”s:
if (ben <= 5 and chen >= 10 or
chen == 500 and ben != 5):
print “Ben and Chen“
167Overview of Python Programming
2019
Range Test
if (3 <= Time <= 5):
print “Office Hour"
168Overview of Python Programming
2019
For
>>> names = [“Ben", “Chen", “Yaqin"]
>>> for name in names:
... print smiles
...
Ben
Chen
Yaqin
169Overview of Python Programming
2019
Tuple assignment in for loops
data = [ ("C20H20O3", 308.371),
("C22H20O2", 316.393),
("C24H40N4O2", 416.6),
("C14H25N5O3", 311.38),
("C15H20O2", 232.3181)]
for (formula, mw) in data:
print "The molecular weight of %s is %s" % (formula, mw)
The molecular weight of C20H20O3 is 308.371
The molecular weight of C22H20O2 is 316.393
The molecular weight of C24H40N4O2 is 416.6
The molecular weight of C14H25N5O3 is 311.38
The molecular weight of C15H20O2 is 232.3181
170Overview of Python Programming
2019
Break, continue
>>> for value in [3, 1, 4, 1, 5, 9, 2]:
... print "Checking", value
... if value > 8:
... print "Exiting for loop"
... break
... elif value < 3:
... print "Ignoring"
... continue
... print "The square is", value**2
...
Use “break” to stop
the for loop
Use “continue” to stop
processing the current
item
Checking 3
The square is 9
Checking 1
Ignoring
Checking 4
The square is 16
Checking 1
Ignoring
Checking 5
The square is 25
Checking 9
Exiting for loop
>>>
171Overview of Python Programming
2019
Range()
 “range” creates a list of numbers in a specified range
 range([start,] stop[, step]) -> list of integers
 When step is given, it specifies the increment (or decrement).
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(5, 10)
[5, 6, 7, 8, 9]
>>> range(0, 10, 2)
[0, 2, 4, 6, 8]
How to get every second element in a list?
for i in range(0, len(data), 2):
print data[i]
172Overview of Python Programming
2019
 Background
 Data Types/Structure
 Control flow
 File I/O
 Modules
 Class
 NLTK
173Overview of Python Programming
2019
Reading files
>>> f = open(“names.txt")
>>> f.readline()
'Yaqinn'
174Overview of Python Programming
2019
Quick Way
>>> lst= [ x for x in open("text.txt","r").readlines() ]
>>> lst
['Chen Linn', 'clin@brandeis.edun', 'Volen 110n', 'Office
Hour: Thurs. 3-5n', 'n', 'Yaqin Yangn',
'yaqin@brandeis.edun', 'Volen 110n', 'Offiche Hour:
Tues. 3-5n']
Ignore the header?
for (i,line) in enumerate(open(‘text.txt’,"r").readlines()):
if i == 0: continue
print line
175Overview of Python Programming
2019
Using dictionaries to count
occurrences
>>> for line in open('names.txt'):
... name = line.strip()
... name_count[name] = name_count.get(name,0)+ 1
...
>>> for (name, count) in name_count.items():
... print name, count
...
Chen 3
Ben 3
Yaqin 3
176Overview of Python Programming
2019
File Output
input_file = open(“in.txt")
output_file = open(“out.txt", "w")
for line in input_file:
output_file.write(line)
“w” = “write mode”
“a” = “append mode”
“wb” = “write in binary”
“r” = “read mode” (default)
“rb” = “read in binary”
“U” = “read files with Unix
or Windows line endings”
177Overview of Python Programming
2019
 Background
 Data Types/Structure
 Control flow
 File I/O
 Modules
 Class
 NLTK
178Overview of Python Programming
2019
Modules
 When a Python program starts it only has
access to a basic functions and classes.
(“int”, “dict”, “len”, “sum”, “range”, ...)
 “Modules” contain additional functionality.
 Use “import” to tell Python to load a
module.
>>> import math
>>> import nltk
179Overview of Python Programming
2019
import the math module
>>> import math
>>> math.pi
3.1415926535897931
>>> math.cos(0)
1.0
>>> math.cos(math.pi)
-1.0
>>> dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh',
'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos',
'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod',
'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10',
'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan',
'tanh', 'trunc']
>>> help(math)
>>> help(math.cos)
180Overview of Python Programming
2019
“import” and “from ... import ...”
>>> import math
math.cos
>>> from math import cos, pi
cos
>>> from math import *
181Overview of Python Programming
2019
 Background
 Data Types/Structure
 Control flow
 File I/O
 Modules
 Class
 NLTK
182Overview of Python Programming
2019
Classes
class ClassName(object):
<statement-1>
. . .
<statement-N>
class MyClass(object):
"""A simple example class"""
i = 12345
def f(self):
return self.i
class DerivedClassName(BaseClassName):
<statement-1>
. . .
<statement-N>
183Overview of Python Programming
2019
 Background
 Data Types/Structure
 Control flow
 File I/O
 Modules
 Class
 NLTK
184Overview of Python Programming
2019
185Overview of Python Programming
2019
186
Expressions
 expression: A data value or set of
operations to compute a value.
Examples: 1 + 4 * 3
42
 Arithmetic operators we will use:
 + - * / addition,
subtraction/negation, multiplication, division
 % modulus, a.k.a. remainder
 ** exponentiation
Overview of Python Programming
2019
187
Integer division
 When we divide integers with / , the
quotient is also an integer.
3 52
4 ) 14 27 ) 1425
12 135
2 75
54
21
 More examples:
 35 / 5 is 7
 84 / 10 is 8
 156 / 100 is 1
 The % operator computes the remainderOverview of Python Programming
2019
188
Real numbers
 Python can also manipulate real numbers.
 Examples: 6.022 -15.9997 42.0
2.143e17
 The operators + - * / % ** ( ) all work
for real numbers.
 The / produces an exact answer: 15.0 /
2.0 is 7.5
 The same rules of precedence also apply to
real numbers:
Evaluate ( ) before * / % before + -Overview of Python Programming
2019
189
Math commands
 Python has useful commands for performing
calculations.
Command name Description
abs(value) absolute value
ceil(value) rounds up
cos(value) cosine, in radians
floor(value) rounds down
log(value) logarithm, base e
log10(value) logarithm, base 10
max(value1, value2) larger of two values
min(value1, value2) smaller of two values
round(value) nearest whole number
sin(value) sine, in radians
sqrt(value) square root
Constant Description
e 2.7182818...
pi 3.1415926...
Overview of Python Programming
2019
190
Variables
 variable: A named piece of memory that
can store a value.
 Usage:
 Compute an expression's result,
 store that result into a variable,
 and use that variable later in the program.
 assignment statement: Stores a value
into a variable.
 Syntax:
name = value
 Examples: x = 5Overview of Python Programming
2019
191
 print : Produces text output on the console.
 Syntax:
print "Message"
print Expression
 Prints the given text message or expression value
on the console, and moves the cursor down to the
next line.
print Item1, Item2, ..., ItemN
 Prints several messages and/or expressions on the
same line.
print
Overview of Python Programming
2019
192
 input : Reads a number from user input.
 You can assign (store) the result of input into a
variable.
 Example:
age = input("How old are you? ")
print "Your age is", age
print "You have", 65 - age, "years
until retirement"
Output:
How old are you? 53
Your age is 53
You have 12 years until retirement
input
Overview of Python Programming
2019
193
Repetition (loops)
and Selection (if/else)
194
The for loop
 for loop: Repeats a set of statements over a group of values.
 Syntax:
for variableName in groupOfValues:
statements
 We indent the statements to be repeated with tabs or spaces.
 variableName gives a name to each value, so you can refer to it in the statements.
 groupOfValues can be a range of integers, specified with the range function.
 Example:
for x in range(1, 6):
print x, "squared is", x * x
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Overview of Python Programming
2019
195
range
 The range function specifies a range of
integers:
 range(start, stop) - the integers between
start (inclusive)
and stop (exclusive)
 It can also accept a third value specifying the
change between values.
 range(start, stop, step) - the integers
between start (inclusive)
and stop (exclusive) by step
 Example:
for x in range(5, 0, -1):
print x
Overview of Python Programming
2019
196
Cumulative loops
 Some loops incrementally compute a value
that is initialized outside the loop. This is
sometimes called a cumulative sum.
sum = 0
for i in range(1, 11):
sum = sum + (i * i)
print "sum of first 10 squares is", sum
Output:
sum of first 10 squares is 385
 Exercise: Write a Python program that
computes the factorial of an integer.
Overview of Python Programming
2019
197
if
 if statement: Executes a group of
statements only if a certain condition is
true. Otherwise, the statements are
skipped.
 Syntax:
if condition:
statements
 Example:
gpa = 3.4
if gpa > 2.0:
print "Your application is accepted."
Overview of Python Programming
2019
198
if/else
 if/else statement: Executes one block of statements if a certain
condition is True, and a second block of statements if it is False.
 Syntax:
if condition:
statements
else:
statements
 Example:
gpa = 1.4
if gpa > 2.0:
print "Welcome to Mars University!"
else:
print "Your application is denied."
 Multiple conditions can be chained with elif ("else if"):
if condition:
statements
elif condition:
statements
else:
statements
Overview of Python Programming
2019
199
while
 while loop: Executes a group of statements as long as a condition is True.
 good for indefinite loops (repeat an unknown number of times)
 Syntax:
while condition:
statements
 Example:
number = 1
while number < 200:
print number,
number = number * 2
 Output:
1 2 4 8 16 32 64 128
Overview of Python Programming
2019
200
Logic
 Many logical expressions use relational operators:
 Logical expressions can be combined with logical operators:
 Exercise: Write code to display and count the factors of a number.
Operator Example Result
and 9 != 6 and 2 < 3 True
or 2 == 3 or -1 < 5 True
not not 7 > 0 False
Operator Meaning Example Result
== equals 1 + 1 == 2 True
!= does not equal 3.2 != 2.5 True
< less than 10 < 5 False
> greater than 10 > 5 True
<= less than or equal to 126 <= 100 False
>= greater than or equal to 5.0 >= 5.0 True
Overview of Python Programming
2019
201
Text and File Processing
202
 string: A sequence of text characters in a program.
 Strings start and end with quotation mark " or apostrophe ' characters.
 Examples:
"hello"
"This is a string"
"This, too, is a string. It can be very long!"
 A string may not span across multiple lines or contain a " character.
"This is not
a legal String."
"This is not a "legal" String either."
 A string can represent characters by preceding them with a backslash.
 t tab character
 n new line character
 " quotation mark character
  backslash character
 Example: "HellottherenHow are you?"
Strings
Overview of Python Programming
2019
203
Indexes
 Characters in a string are numbered with indexes starting at 0:
 Example:
name = "P. Diddy"
 Accessing an individual character of a string:
variableName [ index ]
 Example:
print name, "starts with", name[0]
Output:
P. Diddy starts with P
index 0 1 2 3 4 5 6 7
character P . D i d d y
Overview of Python Programming
2019
204
String properties
 len(string) - number of characters in a string
(including spaces)
 str.lower(string) - lowercase version of a string
 str.upper(string) - uppercase version of a string
 Example:
name = "Martin Douglas Stepp"
length = len(name)
big_name = str.upper(name)
print big_name, "has", length, "characters"
Output:
MARTIN DOUGLAS STEPP has 20 characters
Overview of Python Programming
2019
205
 raw_input : Reads a string of text from user
input.
 Example:
name = raw_input("Howdy, pardner.
What's yer name? ")
print name, "... what a silly name!"
Output:
Howdy, pardner. What's yer name? Paris
Hilton
Paris Hilton ... what a silly name!
raw_input
Overview of Python Programming
2019
206
Text processing
 text processing: Examining, editing, formatting text.
 often uses loops that examine the characters of a string one by
one
 A for loop can examine each character in a string in sequence.
 Example:
for c in "booyah":
print c
Output:
b
o
o
y
a
h
Overview of Python Programming
2019
207
Strings and numbers
 ord(text) - converts a string into a number.
 Example: ord("a") is 97, ord("b") is 98, ...
 Characters map to numbers using standardized mappings such as
ASCII and Unicode.
 chr(number) - converts a number into a string.
 Example: chr(99) is "c"
 Exercise: Write a program that performs a rotation
cypher.
 e.g. "Attack" when rotated by 1 becomes
"buubdl"
Overview of Python Programming
2019
208
File processing
 Many programs handle data, which often
comes from files.
 Reading the entire contents of a file:
variableName =
open("filename").read()
Example:
file_text =
open("bankaccount.txt").read()
Overview of Python Programming
2019
209
Line-by-line processing
 Reading a file line-by-line:
for line in open("filename").readlines():
statements
Example:
count = 0
for line in open("bankaccount.txt").readlines():
count = count + 1
print "The file contains", count, "lines."
 Exercise: Write a program to process a file of DNA text, such as:
ATGCAATTGCTCGATTAG
 Count the percent of C+G present in the DNA.
Overview of Python Programming
2019
210Overview of Python Programming
2019

Contenu connexe

Tendances

Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | EdurekaEdureka!
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Fariz Darari
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1Nicholas I
 
Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming KrishnaMildain
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaEdureka!
 
Python presentation by Monu Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu SharmaMayank Sharma
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on pythonwilliam john
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python amiable_indian
 

Tendances (20)

Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | Edureka
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Introduction to Python Basics Programming
Introduction to Python Basics ProgrammingIntroduction to Python Basics Programming
Introduction to Python Basics Programming
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1
 
Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Python ppt
Python pptPython ppt
Python ppt
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
 
Python presentation by Monu Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu Sharma
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on python
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Python Workshop
Python WorkshopPython Workshop
Python Workshop
 
Python basic
Python basicPython basic
Python basic
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
 

Similaire à Overview of python 2019

PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)HemaArora2
 
FDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonFDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonkannikadg
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python ProgrammingDozie Agbo
 
introduction of python in data science
introduction of python in data scienceintroduction of python in data science
introduction of python in data sciencebhavesh lande
 
Introduction_to_Python.pptx
Introduction_to_Python.pptxIntroduction_to_Python.pptx
Introduction_to_Python.pptxVinay Chowdary
 
summer training report on python
summer training report on pythonsummer training report on python
summer training report on pythonShubham Yadav
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of pythonBijuAugustian
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizeIruolagbePius
 
Python Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computerPython Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computersharanyarashmir5
 
Socket programming-in-python
Socket programming-in-pythonSocket programming-in-python
Socket programming-in-pythonYuvaraja Ravi
 

Similaire à Overview of python 2019 (20)

PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
 
FDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonFDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on python
 
Python Course.docx
Python Course.docxPython Course.docx
Python Course.docx
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Chapter - 1.pptx
Chapter - 1.pptxChapter - 1.pptx
Chapter - 1.pptx
 
Python programming
Python programmingPython programming
Python programming
 
Introduction of Python
Introduction of PythonIntroduction of Python
Introduction of Python
 
introduction of python in data science
introduction of python in data scienceintroduction of python in data science
introduction of python in data science
 
Introduction_to_Python.pptx
Introduction_to_Python.pptxIntroduction_to_Python.pptx
Introduction_to_Python.pptx
 
Intro to python
Intro to pythonIntro to python
Intro to python
 
GE3151_PSPP_UNIT_2_Notes
GE3151_PSPP_UNIT_2_NotesGE3151_PSPP_UNIT_2_Notes
GE3151_PSPP_UNIT_2_Notes
 
summer training report on python
summer training report on pythonsummer training report on python
summer training report on python
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of python
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
Python Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computerPython Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computer
 
Cmpe202 01 Research
Cmpe202 01 ResearchCmpe202 01 Research
Cmpe202 01 Research
 
Socket programming-in-python
Socket programming-in-pythonSocket programming-in-python
Socket programming-in-python
 

Plus de Samir Mohanty

Samir's whitepaper5g
Samir's whitepaper5gSamir's whitepaper5g
Samir's whitepaper5gSamir Mohanty
 
5 g communication with ai &amp; iot
5 g  communication with ai &amp; iot5 g  communication with ai &amp; iot
5 g communication with ai &amp; iotSamir Mohanty
 
Lte outbound roaming_session
Lte outbound roaming_sessionLte outbound roaming_session
Lte outbound roaming_sessionSamir Mohanty
 

Plus de Samir Mohanty (6)

Samir's whitepaper5g
Samir's whitepaper5gSamir's whitepaper5g
Samir's whitepaper5g
 
5 g communication with ai &amp; iot
5 g  communication with ai &amp; iot5 g  communication with ai &amp; iot
5 g communication with ai &amp; iot
 
GPRS training
GPRS trainingGPRS training
GPRS training
 
Lte outbound roaming_session
Lte outbound roaming_sessionLte outbound roaming_session
Lte outbound roaming_session
 
5g New Revolution
5g New Revolution 5g New Revolution
5g New Revolution
 
5 gppt
5 gppt5 gppt
5 gppt
 

Dernier

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 

Dernier (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 

Overview of python 2019

  • 1. Overview of Python Programming Samir Mohanty samir2artificialintelligence@gmail.com Mar, 2019
  • 2. Disclaimer  The slides presented here are obtained from several authors of books and from various other contributors. I hereby acknowledge all the contributors for their material and inputs.  I have added and modified a few slides to suit the requirements of the training. 2Overview of Python Programming 2019
  • 3. Agenda Overview of Python Programming 2019 3  Overview of Python basic syntax  Modules in Python  Overview of Data structures  Overview of File I/O  A few Modules
  • 4. PYTHON BASICS 4Overview of Python Programming 2019
  • 5. Introduction to Python  A high-level programming language  Open source and community driven  Standard Distribution includes many modules  Dynamic typed; Automatic Memory management  Basically interpreted, but source can be compiled  Easy to learn, yet powerful  Robust support for OO programming  Can integrate well with other languages  Most popular for Data Analytics 5Overview of Python Programming 2019
  • 6. 6 Compiling and Interpreting  Many languages require you to compile (translate) your program into a form that the machine understands.  Python is instead directly interpreted into machine instructions. compile execute outputsource code Hello.java byte code Hello.class interpret outputsource code Hello.py It generates a machine independent bytecode which is handled by the run time Overview of Python Programming 2019
  • 7. Installing  Python is pre-installed on most Unix systems, including Linux and MAC OS X  Set the path (/usr/bin/python) and ensure you have execute permissions  The pre-installed version may not be the most recent one (2.7.15 and 3.7.2 as of Jan 19)  Download the windows version from http://python.org/download/  Install as a standard windows application  Python comes with a large library of standard modules 9Overview of Python Programming 2019
  • 8. Setup Python PATH  Installing Python  Setting up PATH For Python:  C:UsersMR GOKHLEAppDataLocalProgramsPythonPython37 For IDLE  C:UsersMR GOKHLEAppDataLocalProgramsPythonPython37Libidlelib  Python as a command processor  Launch python at DOS shell prompt  Execute python commands  exit() 10Overview of Python Programming 2019
  • 9. Launch from command prompt 11Overview of Python Programming 2019
  • 10. IDEs  There are several options for an IDE  IDLE – works well with Windows  Emacs with python-mode or your favorite text editor  Eclipse with Pydev (http://pydev.sourceforge.net/) for Windows & Linux  PyCharm Community Edition (open source). Professional Edition is Closed source. Available for Windows & Linux  Spyder ( MIT license). Windows & Linux 12Overview of Python Programming 2019
  • 11. IDLE Development Environment  IDLE is an Integrated DeveLopment Environment for Python, typically used on Windows  Multi-window text editor with syntax highlighting, auto-completion, smart indent and other.  Python shell with syntax highlighting.  Integrated debugger 13Overview of Python Programming 2019
  • 12. IDLE – Development Environment  IDLE helps you program in Python by:  color-coding your program code  debugging  auto-indent  interactive shell Comments are red Strings are green Definitions are blue Keywords are orange Output is blue Shell prompt  IDLE Shell: 15Overview of Python Programming 2019
  • 13. Python Scripts  When you call a python program from the command line, the interpreter evaluates each expression in the file  Familiar mechanisms are used to provide command line arguments and/or redirect input and output  Python also has mechanisms to allow a python program to act both as a script and as a module to be imported and used by another python program 18Overview of Python Programming 2019
  • 14. Python Scripts  Code is interpreted  You see the result immediately on the shell window.  If a statement is in error, execution stops and error message is displayed.  The interpretation does not guarantee display of all errors. Errors are reported only when encountered, it stops on the first error 19Overview of Python Programming 2019
  • 15. A Code Sample (in IDLE) x = 34 - 23 # A comment. y = "Hello" # Another one. z = 3.45 if z == 3.45 or y == "Hello": x = x + 1 y = y + " World “ # String concat print (x) print (y) Variables are used as needed. No specific type of variable. It can hold any type of data. The most recent assignment determines the type 20Overview of Python Programming 2019
  • 16. Data in Python  Everything in Python is an object.  All objects in Python can be either mutable or immutable.  A mutable object can be changed after it is created, and an immutable object can’t.  Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable 22Overview of Python Programming 2019
  • 17. Basic Datatypes  Numbers  Strings  List  Tuple  Set  Dictionary Data structures 23Overview of Python Programming 2019
  • 18. Mutable / Immutable  Mutable: meaning you can change their content without changing their identity  Immutable: are objects that can not be changed 24Overview of Python Programming 2019
  • 19. Modules & Packages  Similar to libraries, called Modules  It can be used (referenced) in standard python code. Include the modules in the code.  Usually a File containing python code  Can define functions, classes, variables  Multiple modules make up a Package, usually a directory of python files.  Official packages are listed in a directory (PyPI) from where one can download / install  pip is the tool to download and install packages 25Overview of Python Programming 2019
  • 20. Basic Datatypes  Integers (default for numbers) z = 5 / 2 # Answer 2.5, real division  Floats x = 3.456  Strings  Can use " " or ' ' to specify with "abc" == 'abc'  Unmatched can occur within the string: "matt's"  Use triple quotes for multi-line strings or strings than contain both ‘ and “ inside of them: """a'b"c""" 26Overview of Python Programming 2019
  • 21. Data Structures  List : An unordered collection of data items  [35,23.15,"Hello", [2,3,'a','b']]  Can be used as a stack, queue  mutable  Tuple : An unordered collection of data items used to capture an entity’s data  (25, 5.5, "My Data", ['a','b'], (1,2,3))  immutable 27Overview of Python Programming 2019
  • 22. Data Structures  Set : An unordered set of values ( no duplicates allowed)  {2,3,"hello"}  Dictionary : unordered collection of data items, each data item is a <key, value> pair  {'name': 'John', 1: [2, 4, 3]} 28Overview of Python Programming 2019
  • 23. Standard Operators  Arithmetic  Relational  Logical Overview of Python Programming 2019 29 Operator Description ** Exponentiation (raise to the power) ~ + - Complement, unary plus and minus (method names for the last two are +@ and -@) * / % // Multiply, divide, modulo and floor division + - Addition and subtraction >> << Right and left bitwise shift & Bitwise 'AND'td> ^ | Bitwise exclusive `OR' and regular `OR' <= < > >= Comparison operators <> == != Equality operators = %= /= //= -= += *= **= Assignment operators is is not Identity operators in not in Membership operators not or and Logical operators
  • 24. Control Structures  if ..  if ..else  if .. elif  while  while .. else  for  break  continue Overview of Python Programming 2019 30
  • 25. Functions & Classes  def fname (abc) : … … return(rval)  class cname(pclass): … Overview of Python Programming 2019 31
  • 26. Example function: fact.py def fact(x): """Returns the factorial of its argument, assumed to be a posint""" if x == 0: return 1 return x * fact(x - 1) print print ("N fact(N)") print ("---------") for n in range(10): print (n, " factorial is : ", fact(n)) 32Overview of Python Programming 2019
  • 27. Class definitions Class ClassName: <statement-1> ... <statement-N>  must be executed  can be executed conditionally  creates new namespace 33Overview of Python Programming 2019
  • 28. Class objects  obj.name references (plus module!): class MyClass: "A simple example class" i = 123 def f(self): return 'hello world' >>> MyClass.i 123  MyClass.f is method object 34Overview of Python Programming 2019
  • 29. Files  Files are manipulated by creating a file object  f = open("points.txt", "r")  The file object then has new methods  print f.readline() # prints line from file  Files can be accessed to read or write  f = open("output.txt", "w")  f.write("Important Output!")  Files are iterable objects, like lists 35Overview of Python Programming 2019
  • 30. Error Capture  Check for type assignment errors, items not in a list, etc.  Try & Except try: a block of code that might have an error except: code to execute if an error occurs in "try"  Allows for graceful failure – important for applications which use system resources 36Overview of Python Programming 2019
  • 31. Popular Modules  Cryptograpgy  M2Crypto, OpenSSL  GUI  PyGTK, TKInter, PyQt  Networking  RPyC, HTTPLib2  Plotting  Matplotlib, plotly, SciPy  Scientific  Numpy, SciPy,  Threading  Threadpool  Web development  Django, web2py Overview of Python Programming 2019 37
  • 33. Comments  Start comments with #, rest of line is ignored  Can include a “documentation string” as the first line of a new function or class you define  Development environments, debugger, and other tools use it: it’s good style to include one def fact(n): “““fact(n) assumes n is a positive integer and returns factorial of n.””” assert(n>0) return 1 if n==1 else n*fact(n-1) 40Overview of Python Programming 2019
  • 34. LETS GO HANDS ON.. 41Overview of Python Programming 2019
  • 35. Assignment  Binding a variable in Python means setting a name to hold a reference to some object  Assignment creates references, not copies  Names in Python do not have an intrinsic type, objects have types  Python determines the type of the reference automatically based on what data is assigned to it  You create a name the first time it appears on the left side of an assignment expression: x = 3  A reference is deleted via garbage collection after any names bound to it have passed out of scope  Python uses reference semantics (more later) 42Overview of Python Programming 2019
  • 36. Naming Rules  Names are case sensitive and cannot start with a number. They can contain letters, numbers, and underscores. bob Bob _bob _2_bob_ bob_2 BoB  There are some reserved words: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while 43Overview of Python Programming 2019
  • 37. Naming Conventions The Python community has these recommend-ed naming conventions joined_lower for functions, methods and, attributes joined_lower or ALL_CAPS for constants StudlyCaps for classes camelCase only to conform to pre-existing conventions Attributes: interface, _internal, __private 44Overview of Python Programming 2019
  • 38. Assignment You can assign to multiple names at the same time >>> x, y = 2, 3 >>> x 2 >>> y 3 This makes it easy to swap values >>> x, y = y, x Assignments can be chained >>> a = b = x = 2 45Overview of Python Programming 2019
  • 39. Accessing Non-Existent Name Accessing a name before it’s been properly created (by placing it on the left side of an assignment), raises an error >>> y Traceback (most recent call last): File "<pyshell#16>", line 1, in -toplevel- y NameError: name ‘y' is not defined >>> y = 3 >>> y 3 46Overview of Python Programming 2019
  • 40. Python Program Structure  modules: Python source files or C extensions  import, top-level via from, reload  statements  control flow  create objects  indentation matters – instead of { }  case sensitive  objects  everything is an object  automatically reclaimed when no longer needed (gc) 47Overview of Python Programming 2019
  • 41. Basic operations  Assignment:  size = 40  a = b = c = 3  Numbers  integer, float  complex numbers: 1j+3, abs(z)  Strings  'hello world', 'it's hot'  "bye world"  continuation via or use """ long text """" 48Overview of Python Programming 2019
  • 42. String operations  concatenate with + or neighbors  word = 'Help' + x  word = 'Help' 'a'  subscripting of strings  'Hello'[2]  'l'  slice: 'Hello'[1:2]  'el'  word[-1]  last character  len(word)  5  immutable: cannot assign to subscript 49Overview of Python Programming 2019
  • 43. Lists  lists can be heterogeneous  a = ['spam', 'eggs', 100, 1234, 2*2]  Lists can be indexed and sliced:  a[0]  spam  a[:2]  ['spam', 'eggs']  Lists can be manipulated  a[2] = a[2] + 23  a[0:2] = [1,12]  a[0:0] = []  len(a)  5 50Overview of Python Programming 2019
  • 44. Basic programming a,b = 0, 1 # non-zero = true while b < 10: # formatted output, without n print b, # multiple assignment a,b = b, a+b 51Overview of Python Programming 2019
  • 45. Control flow: if x = int(raw_input("Please enter #:")) if x < 0: x = 0 print 'Negative changed to zero' elif x == 0: print 'Zero' elif x == 1: print 'Single' else: print 'More'  no case statement 52Overview of Python Programming 2019
  • 46. Control flow: for a = ['cat', 'window', 'defenestrate'] for x in a: print x, len(x)  no arithmetic progression, but  range(10)  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  for i in range(len(a)): print i, a[i]  do not modify the sequence being iterated over 53Overview of Python Programming 2019
  • 47. Loops: break, continue, else  break and continue like C  else after loop exhaustion for n in range(2,10): for x in range(2,n): if n % x == 0: print n, 'equals', x, '*', n/x break else: # loop fell through without finding a factor print n, 'is prime' 54Overview of Python Programming 2019
  • 48. Do nothing  pass does nothing  syntactic filler while 1: pass 55Overview of Python Programming 2019
  • 49. List methods  append(x)  extend(L)  append all items in list (like Tcl lappend)  insert(i,x)  remove(x)  pop([i]), pop()  create stack (FIFO), or queue (LIFO)  pop(0)  index(x)  return the index for value x 60Overview of Python Programming 2019
  • 50. List methods  count(x)  how many times x appears in list  sort()  sort items in place  reverse()  reverse list 61Overview of Python Programming 2019
  • 51. Functional programming tools  filter(function, sequence) def f(x): return x%2 != 0 and x%3 0 filter(f, range(2,25))  map(function, sequence)  call function for each item  return list of return values  reduce(function, sequence)  return a single value  call binary function on the first two items  then on the result and next item  iterate 62Overview of Python Programming 2019
  • 52. List comprehensions (2.0)  Create lists without map(), filter(), lambda  = expression followed by for clause + zero or more for or of clauses >>> vec = [2,4,6] >>> [3*x for x in vec] [6, 12, 18] >>> [{x: x**2} for x in vec} [{2: 4}, {4: 16}, {6: 36}] 63Overview of Python Programming 2019
  • 53. List comprehensions  cross products: >>> vec1 = [2,4,6] >>> vec2 = [4,3,-9] >>> [x*y for x in vec1 for y in vec2] [8,6,-18, 16,12,-36, 24,18,-54] >>> [x+y for x in vec1 and y in vec2] [6,5,-7,8,7,-5,10,9,-3] >>> [vec1[i]*vec2[i] for i in range(len(vec1))] [8,12,-54] 64Overview of Python Programming 2019
  • 54. List comprehensions  can also use if: >>> [3*x for x in vec if x > 3] [12, 18] >>> [3*x for x in vec if x < 2] [] 65Overview of Python Programming 2019
  • 55. del – removing list items  remove by index, not value  remove slices from list (rather than by assigning an empty list) >>> a = [-1,1,66.6,333,333,1234.5] >>> del a[0] >>> a [1,66.6,333,333,1234.5] >>> del a[2:4] >>> a [1,66.6,1234.5] 66Overview of Python Programming 2019
  • 56. Tuples and sequences  lists, strings, tuples: examples of sequence type  tuple = values separated by commas >>> t = 123, 543, 'bar' >>> t[0] 123 >>> t (123, 543, 'bar') 67Overview of Python Programming 2019
  • 57. Tuples  Tuples may be nested >>> u = t, (1,2) >>> u ((123, 542, 'bar'), (1,2))  kind of like structs, but no element names:  (x,y) coordinates  database records  like strings, immutable  can't assign to individual items 68Overview of Python Programming 2019
  • 58. Tuples  Empty tuples: () >>> empty = () >>> len(empty) 0  one item  trailing comma >>> singleton = 'foo', 69Overview of Python Programming 2019
  • 59. Tuples  sequence unpacking  distribute elements across variables >>> t = 123, 543, 'bar' >>> x, y, z = t >>> x 123  packing always creates tuple  unpacking works for any sequence 70Overview of Python Programming 2019
  • 60. Dictionaries  like Tcl or awk associative arrays  indexed by keys  keys are any immutable type: e.g., tuples  but not lists (mutable!)  uses 'key: value' notation >>> tel = {'hgs' : 7042, 'lennox': 7018} >>> tel['cs'] = 7000 >>> tel 71Overview of Python Programming 2019
  • 61. Dictionaries  no particular order  delete elements with del >>> del tel['foo']  keys() method  unsorted list of keys >>> tel.keys() ['cs', 'lennox', 'hgs']  use has_key() to check for existence >>> tel.has_key('foo') 0 72Overview of Python Programming 2019
  • 62. Conditions  can check for sequence membership with is and is not: >>> if (4 in vec): ... print '4 is'  chained comparisons: a less than b AND b equals c: a < b == c  and and or are short-circuit operators:  evaluated from left to right  stop evaluation as soon as outcome clear 73Overview of Python Programming 2019
  • 63. Conditions  Can assign comparison to variable: >>> s1,s2,s3='', 'foo', 'bar' >>> non_null = s1 or s2 or s3 >>> non_null foo  Unlike C, no assignment within expression 74Overview of Python Programming 2019
  • 64. Comparing sequences  unlike C, can compare sequences (lists, tuples, ...)  lexicographical comparison:  compare first; if different  outcome  continue recursively  subsequences are smaller  strings use ASCII comparison  can compare objects of different type, but by type name (list < string < tuple) 75Overview of Python Programming 2019
  • 65. Comparing sequences (1,2,3) < (1,2,4) [1,2,3] < [1,2,4] 'ABC' < 'C' < 'Pascal' < 'Python' (1,2,3) == (1.0,2.0,3.0) (1,2) < (1,2,-1) 76Overview of Python Programming 2019
  • 66. Modules  collection of functions and variables, typically in scripts  definitions can be imported  file name is module name + .py  e.g., create module fibo.py def fib(n): # write Fib. series up to n ... def fib2(n): # return Fib. series up to n 77Overview of Python Programming 2019
  • 67. Modules  import module: import fibo  Use modules via "name space": >>> fibo.fib(1000) >>> fibo.__name__ 'fibo'  can give it a local name: >>> fib = fibo.fib >>> fib(500) 78Overview of Python Programming 2019
  • 68. Modules  function definition + executable statements  executed only when module is imported  modules have private symbol tables  avoids name clash for global variables  accessible as module.globalname  can import into name space: >>> from fibo import fib, fib2 >>> fib(500)  can import all names defined by module: >>> from fibo import * 79Overview of Python Programming 2019
  • 69. Module search path  current directory  list of directories specified in PYTHONPATH environment variable  uses installation-default if not defined, e.g., .:/usr/local/lib/python  uses sys.path >>> import sys >>> sys.path ['', 'C:PROGRA~1Python2.2', 'C:Program FilesPython2.2DLLs', 'C:Program FilesPython2.2lib', 'C:Program FilesPython2.2liblib-tk', 'C:Program FilesPython2.2', 'C:Program FilesPython2.2libsite- packages'] 80Overview of Python Programming 2019
  • 70. Compiled Python files  include byte-compiled version of module if there exists fibo.pyc in same directory as fibo.py  only if creation time of fibo.pyc matches fibo.py  automatically write compiled file, if possible  platform independent  doesn't run any faster, but loads faster  can have only .pyc file  hide source 81Overview of Python Programming 2019
  • 71. Standard modules  system-dependent list  always sys module >>> import sys >>> sys.p1 '>>> ' >>> sys.p2 '... ' >>> sys.path.append('/some/directory') 82Overview of Python Programming 2019
  • 72. Module listing  use dir() for each module >>> dir(fibo) ['___name___', 'fib', 'fib2'] >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__', '__st din__', '__stdout__', '_getframe', 'argv', 'builtin_module_names', 'byteorder', 'copyright', 'displayhook', 'dllhandle', 'exc_info', 'exc_type', 'excepthook', ' exec_prefix', 'executable', 'exit', 'getdefaultencoding', 'getrecursionlimit', ' getrefcount', 'hexversion', 'last_type', 'last_value', 'maxint', 'maxunicode', ' modules', 'path', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setpr ofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'version', 'version_info', 'warnoptions', 'winver'] 83Overview of Python Programming 2019
  • 73. Classes  mixture of C++ and Modula-3  multiple base classes  derived class can override any methods of its base class(es)  method can call the method of a base class with the same name  objects have private data  C++ terms:  all class members are public  all member functions are virtual  no constructors or destructors (not needed) 84Overview of Python Programming 2019
  • 74. Classes  classes (and data types) are objects  built-in types cannot be used as base classes by user  arithmetic operators, subscripting can be redefined for class instances (like C++, unlike Java) 85Overview of Python Programming 2019
  • 75. Class definitions Class ClassName: <statement-1> ... <statement-N>  must be executed  can be executed conditionally (see Tcl)  creates new namespace 86Overview of Python Programming 2019
  • 76. Namespaces  mapping from name to object:  built-in names (abs())  global names in module  local names in function invocation  attributes = any following a dot  z.real, z.imag  attributes read-only or writable  module attributes are writeable 87Overview of Python Programming 2019
  • 77. Namespaces  scope = textual region of Python program where a namespace is directly accessible (without dot)  innermost scope (first) = local names  middle scope = current module's global names  outermost scope (last) = built-in names  assignments always affect innermost scope  don't copy, just create name bindings to objects  global indicates name is in global scope 88Overview of Python Programming 2019
  • 78. Class objects  obj.name references (plus module!): class MyClass: "A simple example class" i = 123 def f(self): return 'hello world' >>> MyClass.i 123  MyClass.f is method object 89Overview of Python Programming 2019
  • 79. Class objects  class instantiation: >>> x = MyClass() >>> x.f() 'hello world'  creates new instance of class  note x = MyClass vs. x = MyClass()  ___init__() special method for initialization of object def __init__(self,realpart,imagpart): self.r = realpart self.i = imagpart 90Overview of Python Programming 2019
  • 80. Instance objects  attribute references  data attributes (C++/Java data members)  created dynamically x.counter = 1 while x.counter < 10: x.counter = x.counter * 2 print x.counter del x.counter 91Overview of Python Programming 2019
  • 81. Method objects  Called immediately: x.f()  can be referenced: xf = x.f while 1: print xf()  object is passed as first argument of function  'self'  x.f() is equivalent to MyClass.f(x) 92Overview of Python Programming 2019
  • 82. Notes on classes  Data attributes override method attributes with the same name  no real hiding  not usable to implement pure abstract data types  clients (users) of an object can add data attributes  first argument of method usually called self  'self' has no special meaning (cf. Java) 93Overview of Python Programming 2019
  • 83. Another example  bag.py class Bag: def __init__(self): self.data = [] def add(self, x): self.data.append(x) def addtwice(self,x): self.add(x) self.add(x) 94Overview of Python Programming 2019
  • 84. Another example, cont'd.  invoke: >>> from bag import * >>> l = Bag() >>> l.add('first') >>> l.add('second') >>> l.data ['first', 'second'] 95Overview of Python Programming 2019
  • 85. Inheritance class DerivedClassName(BaseClassName) <statement-1> ... <statement-N>  search class attribute, descending chain of base classes  may override methods in the base class  call directly via BaseClassName.method 96Overview of Python Programming 2019
  • 86. Multiple inheritance class DerivedClass(Base1,Base2,Base3): <statement>  depth-first, left-to-right  problem: class derived from two classes with a common base class 97Overview of Python Programming 2019
  • 87. Private variables  No real support, but textual replacement (name mangling)  __var is replaced by _classname_var  prevents only accidental modification, not true protection 98Overview of Python Programming 2019
  • 88. ~ C structs  Empty class definition: class Employee: pass john = Employee() john.name = 'John Doe' john.dept = 'CS' john.salary = 1000 99Overview of Python Programming 2019
  • 89. Exceptions  syntax (parsing) errors while 1 print 'Hello World' File "<stdin>", line 1 while 1 print 'Hello World' ^ SyntaxError: invalid syntax  exceptions  run-time errors  e.g., ZeroDivisionError, NameError, TypeError 100Overview of Python Programming 2019
  • 90. Handling exceptions while 1: try: x = int(raw_input("Please enter a number: ")) break except ValueError: print "Not a valid number"  First, execute try clause  if no exception, skip except clause  if exception, skip rest of try clause and use except clause  if no matching exception, attempt outer try statement 101Overview of Python Programming 2019
  • 91. Handling exceptions  try.py import sys for arg in sys.argv[1:]: try: f = open(arg, 'r') except IOError: print 'cannot open', arg else: print arg, 'lines:', len(f.readlines()) f.close  e.g., as python try.py *.py 102Overview of Python Programming 2019
  • 92. Language comparison Tcl Perl Python JavaScript Visual Basic Speed development      regexp    breadth extensible    embeddable   easy GUI   (Tk)  net/web      enterprise cross-platform     I18N     thread-safe    database access      103Overview of Python Programming 2019
  • 93. 104Overview of Python Programming 2019
  • 95. Sequence Types 1. Tuple: (‘john’, 32, [CMSC])  A simple immutable ordered sequence of items  Items can be of mixed types, including collection types 2. Strings: “John Smith”  Immutable  Conceptually very much like a tuple 3. List: [1, 2, ‘john’, (‘up’, ‘down’)]  Mutable ordered sequence of items of mixed types 106Overview of Python Programming 2019
  • 96. Similar Syntax  All three sequence types (tuples, strings, and lists) share much of the same syntax and functionality.  Key difference:  Tuples and strings are immutable  Lists are mutable  The operations shown in this section can be applied to all sequence types  most examples will just show the operation performed on one 107Overview of Python Programming 2019
  • 97. Sequence Types 1  Define tuples using parentheses and commas >>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)  Define lists are using square brackets and commas >>> li = [“abc”, 34, 4.34, 23]  Define strings using quotes (“, ‘, or “““). >>> st = “Hello World” >>> st = ‘Hello World’ >>> st = “““This is a multi-line 108Overview of Python Programming 2019
  • 98. Sequence Types 2  Access individual members of a tuple, list, or string using square bracket “array” notation  Note that all are 0 based… >>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’) >>> tu[1] # Second item in the tuple. ‘abc’ >>> li = [“abc”, 34, 4.34, 23] >>> li[1] # Second item in the list. 34 >>> st = “Hello World” >>> st[1] # Second character in string. ‘e’ 109Overview of Python Programming 2019
  • 99. Positive and negative indices >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Positive index: count from the left, starting with 0 >>> t[1] ‘abc’ Negative index: count from right, starting with –1 >>> t[-3] 4.56 110Overview of Python Programming 2019
  • 100. Slicing: return copy of a subset >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Return a copy of the container with a subset of the original members. Start copying at the first index, and stop copying before second. >>> t[1:4] (‘abc’, 4.56, (2,3)) Negative indices count from end >>> t[1:-1] (‘abc’, 4.56, (2,3)) 111Overview of Python Programming 2019
  • 101. Slicing: return copy of a =subset >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) Omit first index to make copy starting from beginning of the container >>> t[:2] (23, ‘abc’) Omit second index to make copy starting at first index and going to end >>> t[2:] (4.56, (2,3), ‘def’) 112Overview of Python Programming 2019
  • 102. Copying the Whole Sequence  [ : ] makes a copy of an entire sequence >>> t[:] (23, ‘abc’, 4.56, (2,3), ‘def’)  Note the difference between these two lines for mutable sequences >>> l2 = l1 # Both refer to 1 ref, # changing one affects both >>> l2 = l1[:] # Independent 113Overview of Python Programming 2019
  • 103. The ‘in’ Operator  Boolean test whether a value is inside a container: >>> t = [1, 2, 4, 5] >>> 3 in t False >>> 4 in t True >>> 4 not in t False  For strings, tests for substrings >>> a = 'abcde' >>> 'c' in a True >>> 'cd' in a True >>> 'ac' in a False  Be careful: the in keyword is also used in 114Overview of Python Programming 2019
  • 104. The + Operator The + operator produces a new tuple, list, or string whose value is the concatenation of its arguments. >>> (1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) >>> [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] >>> “Hello” + “ ” + “World” 115Overview of Python Programming 2019
  • 105. The * Operator  The * operator produces a new tuple, list, or string that “repeats” the original content. >>> (1, 2, 3) * 3 (1, 2, 3, 1, 2, 3, 1, 2, 3) >>> [1, 2, 3] * 3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> “Hello” * 3 ‘HelloHelloHello’ 116Overview of Python Programming 2019
  • 107. Lists are mutable >>> li = [‘abc’, 23, 4.34, 23] >>> li[1] = 45 >>> li [‘abc’, 45, 4.34, 23]  We can change lists in place.  Name li still points to the same memory reference when we’re done. 118Overview of Python Programming 2019
  • 108. Tuples are immutable >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) >>> t[2] = 3.14 Traceback (most recent call last): File "<pyshell#75>", line 1, in -toplevel- tu[2] = 3.14 TypeError: object doesn't support item assignment You can’t change a tuple. You can make a fresh tuple and assign its reference to a previously used name. >>> t = (23, ‘abc’, 3.14, (2,3), ‘def’) The immutability of tuples means they’re faster than lists. 119Overview of Python Programming 2019
  • 109. Operations on Lists Only >>> li = [1, 11, 3, 4, 5] >>> li.append(‘a’) # Note the method syntax >>> li [1, 11, 3, 4, 5, ‘a’] >>> li.insert(2, ‘i’) >>>li 120Overview of Python Programming 2019
  • 110. The extend method vs +  + creates a fresh list with a new memory ref  extend operates on list li in place. >>> li.extend([9, 8, 7]) >>> li [1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7]  Potentially confusing:  extend takes a list as an argument.  append takes a singleton as an argument. >>> li.append([10, 11, 12]) >>> li [1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7, [10, 11, 12]] 121Overview of Python Programming 2019
  • 111. Operations on Lists Only Lists have many methods, including index, count, remove, reverse, sort >>> li = [‘a’, ‘b’, ‘c’, ‘b’] >>> li.index(‘b’) # index of 1st occurrence 1 >>> li.count(‘b’) # number of occurrences 2 >>> li.remove(‘b’) # remove 1st 122Overview of Python Programming 2019
  • 112. Operations on Lists Only >>> li = [5, 2, 6, 8] >>> li.reverse() # reverse the list *in place* >>> li [8, 6, 2, 5] >>> li.sort() # sort the list *in place* >>> li [2, 5, 6, 8] >>> li.sort(some_function) # sort in place using user-defined comparison 123Overview of Python Programming 2019
  • 113. Tuple details The comma is the tuple creation operator, not parens >>> 1, (1,) Python shows parens for clarity (best practice) >>> (1,) (1,) Don't forget the comma! >>> (1) 1 Trailing comma only required for singletons others 124Overview of Python Programming 2019
  • 114. Summary: Tuples vs. Lists  Lists slower but more powerful than tuples  Lists can be modified, and they have lots of handy operations and mehtods  Tuples are immutable and have fewer features  To convert between tuples and lists use the list() and tuple() functions: li = list(tu) tu = tuple(li) 125Overview of Python Programming 2019
  • 115. 126Overview of Python Programming 2019
  • 116. 127Overview of Python Programming 2019
  • 117. More than just printing  Python is an object oriented language  Practically everything can be treated as an object  “hello world” is a string  Strings, as objects, have methods that return the result of a function on the string 128Overview of Python Programming 2019
  • 118. String Methods  Assign a string to a variable  In this case “hw”  hw.title()  hw.upper()  hw.isdigit()  hw.islower() 129Overview of Python Programming 2019
  • 119. String Methods  The string held in your variable remains the same  The method returns an altered string  Changing the variable requires reassignment  hw = hw.upper()  hw now equals “HELLO WORLD” 130Overview of Python Programming 2019
  • 120. Other Python Objects  Lists (mutable sets of strings)  var = [] # create list  var = [‘one’, 2, ‘three’, ‘banana’]  Tuples (immutable sets)  var = (‘one’, 2, ‘three’, ‘banana’)  Dictionaries (associative arrays or ‘hashes’)  var = {} # create dictionary  var = {‘lat’: 40.20547, ‘lon’: -74.76322}  var[‘lat’] = 40.2054  Each has its own set of methods 131Overview of Python Programming 2019
  • 121. Lists  Think of a list as a stack of cards, on which your information is written  The information stays in the order you place it in until you modify that order  Methods return a string or subset of the list or modify the list to add or remove components  Written as var[index], index refers to order within set (think card number, starting at 0) 132Overview of Python Programming 2019
  • 122. List Methods  Adding to the List  var[n] = object  replaces n with object  var.append(object)  adds object to the end of the list  Removing from the List  var[n] = []  empties contents of card, but preserves order  var.remove(n)  removes card at n  var.pop(n)  removes n and returns its value 133Overview of Python Programming 2019
  • 123. Lists in ArcToolbox You will create lists:  Layers as inputs  Attributes to match  Arrays of objects You will work with lists:  List of field names  List of selected features 134Overview of Python Programming 2019
  • 124. Tuples  Like a list, tuples are iterable arrays of objects  Tuples are immutable – once created, unchangeable  To add or remove items, you must redeclare  Example uses of tuples  County Names  Land Use Codes  Ordered set of functions 135Overview of Python Programming 2019
  • 125. Dictionaries  Dictionaries are sets of key & value pairs  Allows you to identify values by a descriptive name instead of order in a list  Keys are unordered unless explicitly sorted  Keys are unique:  var[‘item’] = “apple”  var[‘item’] = “banana”  print var[‘item’] prints just banana 136Overview of Python Programming 2019
  • 126. Indentation and Blocks  Python uses whitespace and indents to denote blocks of code  Lines of code that begin a block end in a colon:  Lines within the code block are indented at the same level  To end a code block, remove the indentation  You'll want blocks of code that run only when certain conditions are met 137Overview of Python Programming 2019
  • 127. Conditional Branching  if and else if variable == condition: #do something based on v == c else: #do something based on v != c  elif allows for additional branching if condition: elif another condition: … else: #none of the above 138Overview of Python Programming 2019
  • 128. Looping with For  For allows you to loop over a block of code a set number of times  For is great for manipulating lists: a = ['cat', 'window', 'defenestrate'] for x in a: print x, len(x) Results: cat 3 window 6 defenestrate 12 139Overview of Python Programming 2019
  • 129. Looping with For  We could use a for loop to perform geoprocessing tasks on each layer in a list  We could get a list of features in a feature class and loop over each, checking attributes  Anything in a sequence or list can be used in a For loop  Just be sure not to modify the list while looping 140Overview of Python Programming 2019
  • 130. Modules  Modules are additional pieces of code that further extend Python’s functionality  A module typically has a specific function  additional math functions, databases, network…  Python comes with many useful modules  arcgisscripting is the module we will use to load ArcGIS toolbox functions into Python 141Overview of Python Programming 2019
  • 131. Modules  Modules are accessed using import  import sys, os # imports two modules  Modules can have subsets of functions  os.path is a subset within os  Modules are then addressed by modulename.function()  sys.argv # list of arguments  filename = os.path.splitext("points.txt")  filename[1] # equals ".txt" 142Overview of Python Programming 2019
  • 132. Files  Files are manipulated by creating a file object  f = open("points.txt", "r")  The file object then has new methods  print f.readline() # prints line from file  Files can be accessed to read or write  f = open("output.txt", "w")  f.write("Important Output!")  Files are iterable objects, like lists 143Overview of Python Programming 2019
  • 133. Error Capture  Check for type assignment errors, items not in a list, etc.  Try & Except try: a block of code that might have an error except: code to execute if an error occurs in "try"  Allows for graceful failure – important in ArcGIS 144Overview of Python Programming 2019
  • 134. 145Overview of Python Programming 2019
  • 135. 146Overview of Python Programming 2019
  • 136. List A compound data type: [0] [2.3, 4.5] [5, "Hello", "there", 9.8] [] Use len() to get the length of a list >>> names = [“Ben", “Chen", “Yaqin"] >>> len(names) 3 147Overview of Python Programming 2019
  • 137. Use [ ] to index items in the list >>> names[0] ‘Ben' >>> names[1] ‘Chen' >>> names[2] ‘Yaqin' >>> names[3] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> names[-1] ‘Yaqin' >>> names[-2] ‘Chen' >>> names[-3] ‘Ben' [0] is the first item. [1] is the second item ... Out of range values raise an exception Negative values go backwards from the last element. 148Overview of Python Programming 2019
  • 138. Strings share many features with lists >>> smiles = "C(=N)(N)N.C(=O)(O)O" >>> smiles[0] 'C' >>> smiles[1] '(' >>> smiles[-1] 'O' >>> smiles[1:5] '(=N)' >>> smiles[10:-4] 'C(=O)' Use “slice” notation to get a substring 149Overview of Python Programming 2019
  • 139. String Methods: find, split smiles = "C(=N)(N)N.C(=O)(O)O" >>> smiles.find("(O)") 15 >>> smiles.find(".") 9 >>> smiles.find(".", 10) -1 >>> smiles.split(".") ['C(=N)(N)N', 'C(=O)(O)O'] >>> Use “find” to find the start of a substring. Start looking at position 10. Find returns -1 if it couldn’t find a match. Split the string into parts with “.” as the delimiter 150Overview of Python Programming 2019
  • 140. String operators: in, not in if "Br" in “Brother”: print "contains brother“ email_address = “clin” if "@" not in email_address: email_address += "@brandeis.edu“ 151Overview of Python Programming 2019
  • 141. String Method: “strip”, “rstrip”, “lstrip” are ways to remove whitespace or selected characters >>> line = " # This is a comment line n" >>> line.strip() '# This is a comment line' >>> line.rstrip() ' # This is a comment line' >>> line.rstrip("n") ' # This is a comment line ' >>> 152Overview of Python Programming 2019
  • 142. More String methods email.startswith(“c") endswith(“u”) True/False >>> "%s@brandeis.edu" % "clin" 'clin@brandeis.edu' >>> names = [“Ben", “Chen", “Yaqin"] >>> ", ".join(names) ‘Ben, Chen, Yaqin‘ >>> “chen".upper() ‘CHEN' 153Overview of Python Programming 2019
  • 143. Unexpected things about strings >>> s = "andrew" >>> s[0] = "A" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> s = "A" + s[1:] >>> s 'Andrew‘ Strings are read only 154Overview of Python Programming 2019
  • 144. “” is for special characters n -> newline t -> tab -> backslash ... But Windows uses backslash for directories! filename = "M:nickel_projectreactive.smi" # DANGER! filename = "M:nickel_projectreactive.smi" # Better! filename = "M:/nickel_project/reactive.smi" # Usually works 155Overview of Python Programming 2019
  • 145. Lists are mutable - some useful methods >>> ids = ["9pti", "2plv", "1crn"] >>> ids.append("1alm") >>> ids ['9pti', '2plv', '1crn', '1alm'] >>>ids.extend(L) Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L. >>> del ids[0] >>> ids ['2plv', '1crn', '1alm'] >>> ids.sort() >>> ids ['1alm', '1crn', '2plv'] >>> ids.reverse() >>> ids ['2plv', '1crn', '1alm'] >>> ids.insert(0, "9pti") >>> ids ['9pti', '2plv', '1crn', '1alm'] append an element remove an element sort by default order reverse the elements in a list insert an element at some specified position. (Slower than .append()) 156Overview of Python Programming 2019
  • 146. Tuples: sort of an immutable list >>> yellow = (255, 255, 0) # r, g, b >>> one = (1,) >>> yellow[0] >>> yellow[1:] (255, 0) >>> yellow[0] = 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment Very common in string interpolation: >>> "%s lives in %s at latitude %.1f" % ("Andrew", "Sweden", 57.7056) 'Andrew lives in Sweden at latitude 57.7' 157Overview of Python Programming 2019
  • 147. zipping lists together >>> names ['ben', 'chen', 'yaqin'] >>> gender = [0, 0, 1] >>> zip(names, gender) [('ben', 0), ('chen', 0), ('yaqin', 1)] 158Overview of Python Programming 2019
  • 148. Dictionaries  Dictionaries are lookup tables.  They map from a “key” to a “value”. symbol_to_name = { "H": "hydrogen", "He": "helium", "Li": "lithium", "C": "carbon", "O": "oxygen", "N": "nitrogen" }  Duplicate keys are not allowed  Duplicate values are just fine 159Overview of Python Programming 2019
  • 149. Keys can be any immutable value numbers, strings, tuples, frozenset, not list, dictionary, set, ... atomic_number_to_name = { 1: "hydrogen" 6: "carbon", 7: "nitrogen" 8: "oxygen", } nobel_prize_winners = { (1979, "physics"): ["Glashow", "Salam", "Weinberg"], (1962, "chemistry"): ["Hodgkin"], (1984, "biology"): ["McClintock"], } A set is an unordered collection with no duplicate elements. 160Overview of Python Programming 2019
  • 150. Dictionary >>> symbol_to_name["C"] 'carbon' >>> "O" in symbol_to_name, "U" in symbol_to_name (True, False) >>> "oxygen" in symbol_to_name False >>> symbol_to_name["P"] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'P' >>> symbol_to_name.get("P", "unknown") 'unknown' >>> symbol_to_name.get("C", "unknown") 'carbon' Get the value for a given key Test if the key exists (“in” only checks the keys, not the values.) [] lookup failures raise an exception. Use “.get()” if you want to return a default value. 161Overview of Python Programming 2019
  • 151. Some useful dictionary methods >>> symbol_to_name.keys() ['C', 'H', 'O', 'N', 'Li', 'He'] >>> symbol_to_name.values() ['carbon', 'hydrogen', 'oxygen', 'nitrogen', 'lithium', 'helium'] >>> symbol_to_name.update( {"P": "phosphorous", "S": "sulfur"} ) >>> symbol_to_name.items() [('C', 'carbon'), ('H', 'hydrogen'), ('O', 'oxygen'), ('N', 'nitrogen'), ('P', 'phosphorous'), ('S', 'sulfur'), ('Li', 'lithium'), ('He', 'helium')] >>> del symbol_to_name['C'] >>> symbol_to_name {'H': 'hydrogen', 'O': 'oxygen', 'N': 'nitrogen', 'Li': 'lithium', 'He': 'helium'} 162Overview of Python Programming 2019
  • 152.  Background  Data Types/Structure list, string, tuple, dictionary  Control flow  File I/O  Modules  Class  NLTK 163Overview of Python Programming 2019
  • 153. Control Flow Things that are False  The boolean value False  The numbers 0 (integer), 0.0 (float) and 0j (complex).  The empty string "".  The empty list [], empty dictionary {} and empty set set(). Things that are True  The boolean value True  All non-zero numbers.  Any string containing at least one character.  A non-empty data structure. 164Overview of Python Programming 2019
  • 154. If >>> smiles = "BrC1=CC=C(C=C1)NN.Cl" >>> bool(smiles) True >>> not bool(smiles) False >>> if not smiles: ... print "The SMILES string is empty" ...  The “else” case is always optional 165Overview of Python Programming 2019
  • 155. Use “elif” to chain subsequent tests >>> mode = "absolute" >>> if mode == "canonical": ... smiles = "canonical" ... elif mode == "isomeric": ... smiles = "isomeric” ... elif mode == "absolute": ... smiles = "absolute" ... else: ... raise TypeError("unknown mode") ... >>> smiles ' absolute ' >>> “raise” is the Python way to raise exceptions 166Overview of Python Programming 2019
  • 156. Boolean logic Python expressions can have “and”s and “or”s: if (ben <= 5 and chen >= 10 or chen == 500 and ben != 5): print “Ben and Chen“ 167Overview of Python Programming 2019
  • 157. Range Test if (3 <= Time <= 5): print “Office Hour" 168Overview of Python Programming 2019
  • 158. For >>> names = [“Ben", “Chen", “Yaqin"] >>> for name in names: ... print smiles ... Ben Chen Yaqin 169Overview of Python Programming 2019
  • 159. Tuple assignment in for loops data = [ ("C20H20O3", 308.371), ("C22H20O2", 316.393), ("C24H40N4O2", 416.6), ("C14H25N5O3", 311.38), ("C15H20O2", 232.3181)] for (formula, mw) in data: print "The molecular weight of %s is %s" % (formula, mw) The molecular weight of C20H20O3 is 308.371 The molecular weight of C22H20O2 is 316.393 The molecular weight of C24H40N4O2 is 416.6 The molecular weight of C14H25N5O3 is 311.38 The molecular weight of C15H20O2 is 232.3181 170Overview of Python Programming 2019
  • 160. Break, continue >>> for value in [3, 1, 4, 1, 5, 9, 2]: ... print "Checking", value ... if value > 8: ... print "Exiting for loop" ... break ... elif value < 3: ... print "Ignoring" ... continue ... print "The square is", value**2 ... Use “break” to stop the for loop Use “continue” to stop processing the current item Checking 3 The square is 9 Checking 1 Ignoring Checking 4 The square is 16 Checking 1 Ignoring Checking 5 The square is 25 Checking 9 Exiting for loop >>> 171Overview of Python Programming 2019
  • 161. Range()  “range” creates a list of numbers in a specified range  range([start,] stop[, step]) -> list of integers  When step is given, it specifies the increment (or decrement). >>> range(5) [0, 1, 2, 3, 4] >>> range(5, 10) [5, 6, 7, 8, 9] >>> range(0, 10, 2) [0, 2, 4, 6, 8] How to get every second element in a list? for i in range(0, len(data), 2): print data[i] 172Overview of Python Programming 2019
  • 162.  Background  Data Types/Structure  Control flow  File I/O  Modules  Class  NLTK 173Overview of Python Programming 2019
  • 163. Reading files >>> f = open(“names.txt") >>> f.readline() 'Yaqinn' 174Overview of Python Programming 2019
  • 164. Quick Way >>> lst= [ x for x in open("text.txt","r").readlines() ] >>> lst ['Chen Linn', 'clin@brandeis.edun', 'Volen 110n', 'Office Hour: Thurs. 3-5n', 'n', 'Yaqin Yangn', 'yaqin@brandeis.edun', 'Volen 110n', 'Offiche Hour: Tues. 3-5n'] Ignore the header? for (i,line) in enumerate(open(‘text.txt’,"r").readlines()): if i == 0: continue print line 175Overview of Python Programming 2019
  • 165. Using dictionaries to count occurrences >>> for line in open('names.txt'): ... name = line.strip() ... name_count[name] = name_count.get(name,0)+ 1 ... >>> for (name, count) in name_count.items(): ... print name, count ... Chen 3 Ben 3 Yaqin 3 176Overview of Python Programming 2019
  • 166. File Output input_file = open(“in.txt") output_file = open(“out.txt", "w") for line in input_file: output_file.write(line) “w” = “write mode” “a” = “append mode” “wb” = “write in binary” “r” = “read mode” (default) “rb” = “read in binary” “U” = “read files with Unix or Windows line endings” 177Overview of Python Programming 2019
  • 167.  Background  Data Types/Structure  Control flow  File I/O  Modules  Class  NLTK 178Overview of Python Programming 2019
  • 168. Modules  When a Python program starts it only has access to a basic functions and classes. (“int”, “dict”, “len”, “sum”, “range”, ...)  “Modules” contain additional functionality.  Use “import” to tell Python to load a module. >>> import math >>> import nltk 179Overview of Python Programming 2019
  • 169. import the math module >>> import math >>> math.pi 3.1415926535897931 >>> math.cos(0) 1.0 >>> math.cos(math.pi) -1.0 >>> dir(math) ['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] >>> help(math) >>> help(math.cos) 180Overview of Python Programming 2019
  • 170. “import” and “from ... import ...” >>> import math math.cos >>> from math import cos, pi cos >>> from math import * 181Overview of Python Programming 2019
  • 171.  Background  Data Types/Structure  Control flow  File I/O  Modules  Class  NLTK 182Overview of Python Programming 2019
  • 172. Classes class ClassName(object): <statement-1> . . . <statement-N> class MyClass(object): """A simple example class""" i = 12345 def f(self): return self.i class DerivedClassName(BaseClassName): <statement-1> . . . <statement-N> 183Overview of Python Programming 2019
  • 173.  Background  Data Types/Structure  Control flow  File I/O  Modules  Class  NLTK 184Overview of Python Programming 2019
  • 174. 185Overview of Python Programming 2019
  • 175. 186 Expressions  expression: A data value or set of operations to compute a value. Examples: 1 + 4 * 3 42  Arithmetic operators we will use:  + - * / addition, subtraction/negation, multiplication, division  % modulus, a.k.a. remainder  ** exponentiation Overview of Python Programming 2019
  • 176. 187 Integer division  When we divide integers with / , the quotient is also an integer. 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21  More examples:  35 / 5 is 7  84 / 10 is 8  156 / 100 is 1  The % operator computes the remainderOverview of Python Programming 2019
  • 177. 188 Real numbers  Python can also manipulate real numbers.  Examples: 6.022 -15.9997 42.0 2.143e17  The operators + - * / % ** ( ) all work for real numbers.  The / produces an exact answer: 15.0 / 2.0 is 7.5  The same rules of precedence also apply to real numbers: Evaluate ( ) before * / % before + -Overview of Python Programming 2019
  • 178. 189 Math commands  Python has useful commands for performing calculations. Command name Description abs(value) absolute value ceil(value) rounds up cos(value) cosine, in radians floor(value) rounds down log(value) logarithm, base e log10(value) logarithm, base 10 max(value1, value2) larger of two values min(value1, value2) smaller of two values round(value) nearest whole number sin(value) sine, in radians sqrt(value) square root Constant Description e 2.7182818... pi 3.1415926... Overview of Python Programming 2019
  • 179. 190 Variables  variable: A named piece of memory that can store a value.  Usage:  Compute an expression's result,  store that result into a variable,  and use that variable later in the program.  assignment statement: Stores a value into a variable.  Syntax: name = value  Examples: x = 5Overview of Python Programming 2019
  • 180. 191  print : Produces text output on the console.  Syntax: print "Message" print Expression  Prints the given text message or expression value on the console, and moves the cursor down to the next line. print Item1, Item2, ..., ItemN  Prints several messages and/or expressions on the same line. print Overview of Python Programming 2019
  • 181. 192  input : Reads a number from user input.  You can assign (store) the result of input into a variable.  Example: age = input("How old are you? ") print "Your age is", age print "You have", 65 - age, "years until retirement" Output: How old are you? 53 Your age is 53 You have 12 years until retirement input Overview of Python Programming 2019
  • 183. 194 The for loop  for loop: Repeats a set of statements over a group of values.  Syntax: for variableName in groupOfValues: statements  We indent the statements to be repeated with tabs or spaces.  variableName gives a name to each value, so you can refer to it in the statements.  groupOfValues can be a range of integers, specified with the range function.  Example: for x in range(1, 6): print x, "squared is", x * x Output: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25 Overview of Python Programming 2019
  • 184. 195 range  The range function specifies a range of integers:  range(start, stop) - the integers between start (inclusive) and stop (exclusive)  It can also accept a third value specifying the change between values.  range(start, stop, step) - the integers between start (inclusive) and stop (exclusive) by step  Example: for x in range(5, 0, -1): print x Overview of Python Programming 2019
  • 185. 196 Cumulative loops  Some loops incrementally compute a value that is initialized outside the loop. This is sometimes called a cumulative sum. sum = 0 for i in range(1, 11): sum = sum + (i * i) print "sum of first 10 squares is", sum Output: sum of first 10 squares is 385  Exercise: Write a Python program that computes the factorial of an integer. Overview of Python Programming 2019
  • 186. 197 if  if statement: Executes a group of statements only if a certain condition is true. Otherwise, the statements are skipped.  Syntax: if condition: statements  Example: gpa = 3.4 if gpa > 2.0: print "Your application is accepted." Overview of Python Programming 2019
  • 187. 198 if/else  if/else statement: Executes one block of statements if a certain condition is True, and a second block of statements if it is False.  Syntax: if condition: statements else: statements  Example: gpa = 1.4 if gpa > 2.0: print "Welcome to Mars University!" else: print "Your application is denied."  Multiple conditions can be chained with elif ("else if"): if condition: statements elif condition: statements else: statements Overview of Python Programming 2019
  • 188. 199 while  while loop: Executes a group of statements as long as a condition is True.  good for indefinite loops (repeat an unknown number of times)  Syntax: while condition: statements  Example: number = 1 while number < 200: print number, number = number * 2  Output: 1 2 4 8 16 32 64 128 Overview of Python Programming 2019
  • 189. 200 Logic  Many logical expressions use relational operators:  Logical expressions can be combined with logical operators:  Exercise: Write code to display and count the factors of a number. Operator Example Result and 9 != 6 and 2 < 3 True or 2 == 3 or -1 < 5 True not not 7 > 0 False Operator Meaning Example Result == equals 1 + 1 == 2 True != does not equal 3.2 != 2.5 True < less than 10 < 5 False > greater than 10 > 5 True <= less than or equal to 126 <= 100 False >= greater than or equal to 5.0 >= 5.0 True Overview of Python Programming 2019
  • 190. 201 Text and File Processing
  • 191. 202  string: A sequence of text characters in a program.  Strings start and end with quotation mark " or apostrophe ' characters.  Examples: "hello" "This is a string" "This, too, is a string. It can be very long!"  A string may not span across multiple lines or contain a " character. "This is not a legal String." "This is not a "legal" String either."  A string can represent characters by preceding them with a backslash.  t tab character  n new line character  " quotation mark character  backslash character  Example: "HellottherenHow are you?" Strings Overview of Python Programming 2019
  • 192. 203 Indexes  Characters in a string are numbered with indexes starting at 0:  Example: name = "P. Diddy"  Accessing an individual character of a string: variableName [ index ]  Example: print name, "starts with", name[0] Output: P. Diddy starts with P index 0 1 2 3 4 5 6 7 character P . D i d d y Overview of Python Programming 2019
  • 193. 204 String properties  len(string) - number of characters in a string (including spaces)  str.lower(string) - lowercase version of a string  str.upper(string) - uppercase version of a string  Example: name = "Martin Douglas Stepp" length = len(name) big_name = str.upper(name) print big_name, "has", length, "characters" Output: MARTIN DOUGLAS STEPP has 20 characters Overview of Python Programming 2019
  • 194. 205  raw_input : Reads a string of text from user input.  Example: name = raw_input("Howdy, pardner. What's yer name? ") print name, "... what a silly name!" Output: Howdy, pardner. What's yer name? Paris Hilton Paris Hilton ... what a silly name! raw_input Overview of Python Programming 2019
  • 195. 206 Text processing  text processing: Examining, editing, formatting text.  often uses loops that examine the characters of a string one by one  A for loop can examine each character in a string in sequence.  Example: for c in "booyah": print c Output: b o o y a h Overview of Python Programming 2019
  • 196. 207 Strings and numbers  ord(text) - converts a string into a number.  Example: ord("a") is 97, ord("b") is 98, ...  Characters map to numbers using standardized mappings such as ASCII and Unicode.  chr(number) - converts a number into a string.  Example: chr(99) is "c"  Exercise: Write a program that performs a rotation cypher.  e.g. "Attack" when rotated by 1 becomes "buubdl" Overview of Python Programming 2019
  • 197. 208 File processing  Many programs handle data, which often comes from files.  Reading the entire contents of a file: variableName = open("filename").read() Example: file_text = open("bankaccount.txt").read() Overview of Python Programming 2019
  • 198. 209 Line-by-line processing  Reading a file line-by-line: for line in open("filename").readlines(): statements Example: count = 0 for line in open("bankaccount.txt").readlines(): count = count + 1 print "The file contains", count, "lines."  Exercise: Write a program to process a file of DNA text, such as: ATGCAATTGCTCGATTAG  Count the percent of C+G present in the DNA. Overview of Python Programming 2019
  • 199. 210Overview of Python Programming 2019