SlideShare a Scribd company logo
Sep 7, 2015 Advanced Programming
Spring 2002
Python
Henning Schulzrinne
Department of Computer Science
Columbia University
(based on tutorial by Guido van Rossum)
Sep 7, 2015 Advanced
Introduction
 Most recent popular
(scripting/extension) language
 although origin ~1991
 heritage: teaching language (ABC)
 Tcl: shell
 perl: string (regex) processing
 object-oriented
 rather than add-on (OOTcl)
Sep 7, 2015 Advanced
Python philosophy
 Coherence
 not hard to read, write and maintain
 power
 scope
 rapid development + large systems
 objects
 integration
 hybrid systems
Sep 7, 2015 Advanced
Python features
no compiling or linking rapid development cycle
no type declarations simpler, shorter, more flexible
automatic memory management garbage collection
high-level data types and
operations
fast development
object-oriented programming code structuring and reuse, C++
embedding and extending in C mixed language systems
classes, modules, exceptions "programming-in-the-large" support
dynamic loading of C modules simplified extensions, smaller
binaries
dynamic reloading of C modules programs can be modified without
stopping
Lutz, Programming Python
Sep 7, 2015 Advanced
Python features
universal "first-class" object model fewer restrictions and rules
run-time program construction handles unforeseen needs, end-
user coding
interactive, dynamic nature incremental development and
testing
access to interpreter information metaprogramming, introspective
objects
wide portability cross-platform programming
without ports
compilation to portable byte-code execution speed, protecting source
code
built-in interfaces to external
services
system tools, GUIs, persistence,
databases, etc.
Lutz, Programming Python
Sep 7, 2015 Advanced
Python
 elements from C++, Modula-3
(modules), ABC, Icon (slicing)
 same family as Perl, Tcl, Scheme,
REXX, BASIC dialects
Sep 7, 2015 Advanced
Uses of Python
 shell tools
 system admin tools, command line programs
 extension-language work
 rapid prototyping and development
 language-based modules
 instead of special-purpose parsers
 graphical user interfaces
 database access
 distributed programming
 Internet scripting
Sep 7, 2015 Advanced
What not to use Python (and
kin) for
 most scripting languages share these
 not as efficient as C
 but sometimes better built-in algorithms
(e.g., hashing and sorting)
 delayed error notification
 lack of profiling tools
Sep 7, 2015 Advanced
Using python
 /usr/local/bin/python
 #! /usr/bin/env python
 interactive use
Python 1.6 (#1, Sep 24 2000, 20:40:45) [GCC 2.95.1 19990816 (release)] on sunos5
Copyright (c) 1995-2000 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
>>>
 python –c command [arg] ...
 python –i script
 read script first, then interactive
Sep 7, 2015 Advanced
Python structure
 modules: Python source files or C extensions
 import, top-level via from, reload
 statements
 control flow
 create objects
 indentation matters – instead of {}
 objects
 everything is an object
 automatically reclaimed when no longer needed
Sep 7, 2015 Advanced
First example
#!/usr/local/bin/python
# import systems module
import sys
marker = '::::::'
for name in sys.argv[1:]:
input = open(name, 'r')
print marker + name
print input.read()
Sep 7, 2015 Advanced
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 """"
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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'
Sep 7, 2015 Advanced
Do nothing
 pass does nothing
 syntactic filler
while 1:
pass
Sep 7, 2015 Advanced
Defining functions
def fib(n):
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
>>> fib(2000)
 First line is docstring
 first look for variables in local, then global
 need global to assign global variables
Sep 7, 2015 Advanced
Functions: default argument
values
def ask_ok(prompt, retries=4,
complaint='Yes or no, please!'):
while 1:
ok = raw_input(prompt)
if ok in ('y', 'ye', 'yes'): return 1
if ok in ('n', 'no'): return 0
retries = retries - 1
if retries < 0: raise IOError,
'refusenik error'
print complaint
>>> ask_ok('Really?')
Sep 7, 2015 Advanced
Keyword arguments
 last arguments can be given as keywords
def parrot(voltage, state='a stiff', action='voom',
type='Norwegian blue'):
print "-- This parrot wouldn't", action,
print "if you put", voltage, "Volts through it."
print "Lovely plumage, the ", type
print "-- It's", state, "!"
parrot(1000)
parrot(action='VOOOM', voltage=100000)
Sep 7, 2015 Advanced
Lambda forms
 anonymous functions
 may not work in older versions
def make_incrementor(n):
return lambda x: x + n
f = make_incrementor(42)
f(0)
f(1)
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
List methods
 count(x)
 how many times x appears in list
 sort()
 sort items in place
 reverse()
 reverse list
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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}]
Sep 7, 2015 Advanced
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]
Sep 7, 2015 Advanced
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]
[]
Sep 7, 2015 Advanced
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]
Sep 7, 2015 Advanced
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')
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
Tuples
 Empty tuples: ()
>>> empty = ()
>>> len(empty)
0
 one item  trailing comma
>>> singleton = 'foo',
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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)
Sep 7, 2015 Advanced
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)
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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)
Sep 7, 2015 Advanced
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 *
Sep 7, 2015 Advanced
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']
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
Standard modules
 system-dependent list
 always sys module
>>> import sys
>>> sys.p1
'>>> '
>>> sys.p2
'... '
>>> sys.path.append('/some/directory')
Sep 7, 2015 Advanced
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']
Sep 7, 2015 Advanced
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)
Sep 7, 2015 Advanced
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)
Sep 7, 2015 Advanced
Class definitions
Class ClassName:
<statement-1>
...
<statement-N>
 must be executed
 can be executed conditionally (see Tcl)
 creates new namespace
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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)
Sep 7, 2015 Advanced
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)
Sep 7, 2015 Advanced
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)
Sep 7, 2015 Advanced
Another example, cont'd.
 invoke:
>>> from bag import *
>>> l = Bag()
>>> l.add('first')
>>> l.add('second')
>>> l.data
['first', 'second']
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
Multiple inheritance
class DerivedClass(Base1,Base2,Base3):
<statement>
 depth-first, left-to-right
 problem: class derived from two classes
with a common base class
Sep 7, 2015 Advanced
Private variables
 No real support, but textual replacement
(name mangling)
 __var is replaced by
_classname_var
 prevents only accidental modification,
not true protection
Sep 7, 2015 Advanced
~ C structs
 Empty class definition:
class Employee:
pass
john = Employee()
john.name = 'John Doe'
john.dept = 'CS'
john.salary = 1000
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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
Sep 7, 2015 Advanced
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     

More Related Content

What's hot

Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
Edureka!
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
RTS Tech
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Namespaces
NamespacesNamespaces
Namespaces
Sangeetha S
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Python : Data Types
Python : Data TypesPython : Data Types
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
Sajid Hasan
 
Simple c program
Simple c programSimple c program
Simple c program
Ravi Singh
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
Umamaheshwariv1
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 
python Function
python Function python Function
python Function
Ronak Rathi
 
Types of function call
Types of function callTypes of function call
Types of function call
ArijitDhali
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
C programming
C programmingC programming

What's hot (20)

Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Namespaces
NamespacesNamespaces
Namespaces
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
Simple c program
Simple c programSimple c program
Simple c program
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
python Function
python Function python Function
python Function
 
Types of function call
Types of function callTypes of function call
Types of function call
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C programming
C programmingC programming
C programming
 

Viewers also liked

Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
Matt Harrison
 
An Introduction To Python - Modules & Solving Real World Problems
An Introduction To Python - Modules & Solving Real World ProblemsAn Introduction To Python - Modules & Solving Real World Problems
An Introduction To Python - Modules & Solving Real World Problems
Blue Elephant Consulting
 
Python的module机制与最佳实践
Python的module机制与最佳实践Python的module机制与最佳实践
Python的module机制与最佳实践
Leo Zhou
 
ZLM-Cython Build you first module
ZLM-Cython Build you first moduleZLM-Cython Build you first module
ZLM-Cython Build you first module
Vladimir Ulogov
 
Python typing module
Python typing modulePython typing module
Python typing module
Ryan Blunden
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
Chui-Wen Chiu
 
Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]
Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]
Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]
Python Meetup
 
Ppt java
Ppt javaPpt java
Introdução à linguagem de programação Python
Introdução à linguagem de programação PythonIntrodução à linguagem de programação Python
Introdução à linguagem de programação Python
Carlos Schults
 
O CartolaFC e o processo de desenvolvimento na globo.com
O CartolaFC e o processo de desenvolvimento na globo.comO CartolaFC e o processo de desenvolvimento na globo.com
O CartolaFC e o processo de desenvolvimento na globo.com
Alexandre Saboia Fuziyama
 
Pivotal Data Labs - Technology and Tools in our Data Scientist's Arsenal
Pivotal Data Labs - Technology and Tools in our Data Scientist's Arsenal Pivotal Data Labs - Technology and Tools in our Data Scientist's Arsenal
Pivotal Data Labs - Technology and Tools in our Data Scientist's Arsenal
Srivatsan Ramanujam
 
Palestra Apresentando Python e Suas Aplicações
Palestra Apresentando Python e Suas AplicaçõesPalestra Apresentando Python e Suas Aplicações
Palestra Apresentando Python e Suas Aplicações
Marcos Thomaz
 
Conhencendo Python e por que ela importa?
Conhencendo Python e por que ela importa?Conhencendo Python e por que ela importa?
Conhencendo Python e por que ela importa?
Marcel Caraciolo
 
Python e Django na Globo.com
Python e Django na Globo.comPython e Django na Globo.com
Python e Django na Globo.com
ricobl
 
OO em Python sem sotaque
OO em Python sem sotaqueOO em Python sem sotaque
OO em Python sem sotaque
Luciano Ramalho
 
Presentacion Python
Presentacion  Python Presentacion  Python
Presentacion Python
Fco Javier Lucena
 
Python PPT
Python PPTPython PPT
Python PPT
Edureka!
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
Karin Lagesen
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
Laxman Puri
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
Sujith Kumar
 

Viewers also liked (20)

Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
An Introduction To Python - Modules & Solving Real World Problems
An Introduction To Python - Modules & Solving Real World ProblemsAn Introduction To Python - Modules & Solving Real World Problems
An Introduction To Python - Modules & Solving Real World Problems
 
Python的module机制与最佳实践
Python的module机制与最佳实践Python的module机制与最佳实践
Python的module机制与最佳实践
 
ZLM-Cython Build you first module
ZLM-Cython Build you first moduleZLM-Cython Build you first module
ZLM-Cython Build you first module
 
Python typing module
Python typing modulePython typing module
Python typing module
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]
Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]
Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]
 
Ppt java
Ppt javaPpt java
Ppt java
 
Introdução à linguagem de programação Python
Introdução à linguagem de programação PythonIntrodução à linguagem de programação Python
Introdução à linguagem de programação Python
 
O CartolaFC e o processo de desenvolvimento na globo.com
O CartolaFC e o processo de desenvolvimento na globo.comO CartolaFC e o processo de desenvolvimento na globo.com
O CartolaFC e o processo de desenvolvimento na globo.com
 
Pivotal Data Labs - Technology and Tools in our Data Scientist's Arsenal
Pivotal Data Labs - Technology and Tools in our Data Scientist's Arsenal Pivotal Data Labs - Technology and Tools in our Data Scientist's Arsenal
Pivotal Data Labs - Technology and Tools in our Data Scientist's Arsenal
 
Palestra Apresentando Python e Suas Aplicações
Palestra Apresentando Python e Suas AplicaçõesPalestra Apresentando Python e Suas Aplicações
Palestra Apresentando Python e Suas Aplicações
 
Conhencendo Python e por que ela importa?
Conhencendo Python e por que ela importa?Conhencendo Python e por que ela importa?
Conhencendo Python e por que ela importa?
 
Python e Django na Globo.com
Python e Django na Globo.comPython e Django na Globo.com
Python e Django na Globo.com
 
OO em Python sem sotaque
OO em Python sem sotaqueOO em Python sem sotaque
OO em Python sem sotaque
 
Presentacion Python
Presentacion  Python Presentacion  Python
Presentacion Python
 
Python PPT
Python PPTPython PPT
Python PPT
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 

Similar to python.ppt

Python classes in mumbai
Python classes in mumbaiPython classes in mumbai
Python classes in mumbai
Vibrant Technologies & Computers
 
Python basics - for bigginers
Python basics - for bigginersPython basics - for bigginers
Python basics - for bigginers
Abdulaziz M. Ghaleb
 
Python basics
Python basicsPython basics
Python basics
PrafullaKamble3
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
ssuserd64918
 
python.ppt
python.pptpython.ppt
python.ppt
ramamoorthi24
 
python.ppt
python.pptpython.ppt
python.ppt
MukundSharma74
 
python.ppt
python.pptpython.ppt
python.ppt
Arun471829
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .ppt
sushil155005
 
python.ppt
python.pptpython.ppt
python.ppt
MayankRai689616
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
Colin Su
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
Henry Schreiner
 
Python in 90mins
Python in 90minsPython in 90mins
Python in 90mins
Larry Cai
 
python.ppt
python.pptpython.ppt
python.ppt
NikhilSoni177492
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
Introduction to python
Introduction to pythonIntroduction to python
python.ppt
python.pptpython.ppt
python.ppt
ssuser696419
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 

Similar to python.ppt (20)

Python classes in mumbai
Python classes in mumbaiPython classes in mumbai
Python classes in mumbai
 
Python basics - for bigginers
Python basics - for bigginersPython basics - for bigginers
Python basics - for bigginers
 
Python basics
Python basicsPython basics
Python basics
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
 
python.ppt
python.pptpython.ppt
python.ppt
 
python.ppt
python.pptpython.ppt
python.ppt
 
python.ppt
python.pptpython.ppt
python.ppt
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .ppt
 
python.ppt
python.pptpython.ppt
python.ppt
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
 
Python in 90mins
Python in 90minsPython in 90mins
Python in 90mins
 
python.ppt
python.pptpython.ppt
python.ppt
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
python.ppt
python.pptpython.ppt
python.ppt
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 

More from shreyas_test_1234

Pdf sample
Pdf samplePdf sample
Pdf sample
shreyas_test_1234
 
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
shreyas_test_1234
 
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
shreyas_test_1234
 
sample.pdf
sample.pdfsample.pdf
sample.pdf
shreyas_test_1234
 
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
shreyas_test_1234
 
Presentation1.PPTX
Presentation1.PPTXPresentation1.PPTX
Presentation1.PPTX
shreyas_test_1234
 
Presentations-Tips.ppt
Presentations-Tips.pptPresentations-Tips.ppt
Presentations-Tips.ppt
shreyas_test_1234
 
example.pps
example.ppsexample.pps
example.pps
shreyas_test_1234
 
example.pps
example.ppsexample.pps
example.pps
shreyas_test_1234
 
doc
docdoc
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
shreyas_test_1234
 
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
shreyas_test_1234
 

More from shreyas_test_1234 (13)

Pdf sample
Pdf samplePdf sample
Pdf sample
 
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
 
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
 
11
1111
11
 
sample.pdf
sample.pdfsample.pdf
sample.pdf
 
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
http---cdata.nqa.sprinklr.com-DAM-5-2010blast-100530124747-Blast-cc20d154-5e0...
 
Presentation1.PPTX
Presentation1.PPTXPresentation1.PPTX
Presentation1.PPTX
 
Presentations-Tips.ppt
Presentations-Tips.pptPresentations-Tips.ppt
Presentations-Tips.ppt
 
example.pps
example.ppsexample.pps
example.pps
 
example.pps
example.ppsexample.pps
example.pps
 
doc
docdoc
doc
 
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
 
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
Http --cdata.nqa.sprinklr.com-dam-5-sample-9c9db3c1-d4df-4724-b055-45c31cb0e3...
 

python.ppt

  • 1. Sep 7, 2015 Advanced Programming Spring 2002 Python Henning Schulzrinne Department of Computer Science Columbia University (based on tutorial by Guido van Rossum)
  • 2. Sep 7, 2015 Advanced Introduction  Most recent popular (scripting/extension) language  although origin ~1991  heritage: teaching language (ABC)  Tcl: shell  perl: string (regex) processing  object-oriented  rather than add-on (OOTcl)
  • 3. Sep 7, 2015 Advanced Python philosophy  Coherence  not hard to read, write and maintain  power  scope  rapid development + large systems  objects  integration  hybrid systems
  • 4. Sep 7, 2015 Advanced Python features no compiling or linking rapid development cycle no type declarations simpler, shorter, more flexible automatic memory management garbage collection high-level data types and operations fast development object-oriented programming code structuring and reuse, C++ embedding and extending in C mixed language systems classes, modules, exceptions "programming-in-the-large" support dynamic loading of C modules simplified extensions, smaller binaries dynamic reloading of C modules programs can be modified without stopping Lutz, Programming Python
  • 5. Sep 7, 2015 Advanced Python features universal "first-class" object model fewer restrictions and rules run-time program construction handles unforeseen needs, end- user coding interactive, dynamic nature incremental development and testing access to interpreter information metaprogramming, introspective objects wide portability cross-platform programming without ports compilation to portable byte-code execution speed, protecting source code built-in interfaces to external services system tools, GUIs, persistence, databases, etc. Lutz, Programming Python
  • 6. Sep 7, 2015 Advanced Python  elements from C++, Modula-3 (modules), ABC, Icon (slicing)  same family as Perl, Tcl, Scheme, REXX, BASIC dialects
  • 7. Sep 7, 2015 Advanced Uses of Python  shell tools  system admin tools, command line programs  extension-language work  rapid prototyping and development  language-based modules  instead of special-purpose parsers  graphical user interfaces  database access  distributed programming  Internet scripting
  • 8. Sep 7, 2015 Advanced What not to use Python (and kin) for  most scripting languages share these  not as efficient as C  but sometimes better built-in algorithms (e.g., hashing and sorting)  delayed error notification  lack of profiling tools
  • 9. Sep 7, 2015 Advanced Using python  /usr/local/bin/python  #! /usr/bin/env python  interactive use Python 1.6 (#1, Sep 24 2000, 20:40:45) [GCC 2.95.1 19990816 (release)] on sunos5 Copyright (c) 1995-2000 Corporation for National Research Initiatives. All Rights Reserved. Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam. All Rights Reserved. >>>  python –c command [arg] ...  python –i script  read script first, then interactive
  • 10. Sep 7, 2015 Advanced Python structure  modules: Python source files or C extensions  import, top-level via from, reload  statements  control flow  create objects  indentation matters – instead of {}  objects  everything is an object  automatically reclaimed when no longer needed
  • 11. Sep 7, 2015 Advanced First example #!/usr/local/bin/python # import systems module import sys marker = '::::::' for name in sys.argv[1:]: input = open(name, 'r') print marker + name print input.read()
  • 12. Sep 7, 2015 Advanced 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 """"
  • 13. Sep 7, 2015 Advanced 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
  • 14. Sep 7, 2015 Advanced 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
  • 15. Sep 7, 2015 Advanced 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
  • 16. Sep 7, 2015 Advanced 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
  • 17. Sep 7, 2015 Advanced 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
  • 18. Sep 7, 2015 Advanced 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'
  • 19. Sep 7, 2015 Advanced Do nothing  pass does nothing  syntactic filler while 1: pass
  • 20. Sep 7, 2015 Advanced Defining functions def fib(n): """Print a Fibonacci series up to n.""" a, b = 0, 1 while b < n: print b, a, b = b, a+b >>> fib(2000)  First line is docstring  first look for variables in local, then global  need global to assign global variables
  • 21. Sep 7, 2015 Advanced Functions: default argument values def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while 1: ok = raw_input(prompt) if ok in ('y', 'ye', 'yes'): return 1 if ok in ('n', 'no'): return 0 retries = retries - 1 if retries < 0: raise IOError, 'refusenik error' print complaint >>> ask_ok('Really?')
  • 22. Sep 7, 2015 Advanced Keyword arguments  last arguments can be given as keywords def parrot(voltage, state='a stiff', action='voom', type='Norwegian blue'): print "-- This parrot wouldn't", action, print "if you put", voltage, "Volts through it." print "Lovely plumage, the ", type print "-- It's", state, "!" parrot(1000) parrot(action='VOOOM', voltage=100000)
  • 23. Sep 7, 2015 Advanced Lambda forms  anonymous functions  may not work in older versions def make_incrementor(n): return lambda x: x + n f = make_incrementor(42) f(0) f(1)
  • 24. Sep 7, 2015 Advanced 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
  • 25. Sep 7, 2015 Advanced List methods  count(x)  how many times x appears in list  sort()  sort items in place  reverse()  reverse list
  • 26. Sep 7, 2015 Advanced 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
  • 27. Sep 7, 2015 Advanced 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}]
  • 28. Sep 7, 2015 Advanced 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]
  • 29. Sep 7, 2015 Advanced 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] []
  • 30. Sep 7, 2015 Advanced 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]
  • 31. Sep 7, 2015 Advanced 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')
  • 32. Sep 7, 2015 Advanced 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
  • 33. Sep 7, 2015 Advanced Tuples  Empty tuples: () >>> empty = () >>> len(empty) 0  one item  trailing comma >>> singleton = 'foo',
  • 34. Sep 7, 2015 Advanced 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
  • 35. Sep 7, 2015 Advanced 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
  • 36. Sep 7, 2015 Advanced 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
  • 37. Sep 7, 2015 Advanced 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
  • 38. Sep 7, 2015 Advanced 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
  • 39. Sep 7, 2015 Advanced 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)
  • 40. Sep 7, 2015 Advanced 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)
  • 41. Sep 7, 2015 Advanced 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
  • 42. Sep 7, 2015 Advanced 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)
  • 43. Sep 7, 2015 Advanced 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 *
  • 44. Sep 7, 2015 Advanced 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']
  • 45. Sep 7, 2015 Advanced 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
  • 46. Sep 7, 2015 Advanced Standard modules  system-dependent list  always sys module >>> import sys >>> sys.p1 '>>> ' >>> sys.p2 '... ' >>> sys.path.append('/some/directory')
  • 47. Sep 7, 2015 Advanced 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']
  • 48. Sep 7, 2015 Advanced 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)
  • 49. Sep 7, 2015 Advanced 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)
  • 50. Sep 7, 2015 Advanced Class definitions Class ClassName: <statement-1> ... <statement-N>  must be executed  can be executed conditionally (see Tcl)  creates new namespace
  • 51. Sep 7, 2015 Advanced 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
  • 52. Sep 7, 2015 Advanced 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
  • 53. Sep 7, 2015 Advanced 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
  • 54. Sep 7, 2015 Advanced 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
  • 55. Sep 7, 2015 Advanced 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
  • 56. Sep 7, 2015 Advanced 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)
  • 57. Sep 7, 2015 Advanced 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)
  • 58. Sep 7, 2015 Advanced 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)
  • 59. Sep 7, 2015 Advanced Another example, cont'd.  invoke: >>> from bag import * >>> l = Bag() >>> l.add('first') >>> l.add('second') >>> l.data ['first', 'second']
  • 60. Sep 7, 2015 Advanced 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
  • 61. Sep 7, 2015 Advanced Multiple inheritance class DerivedClass(Base1,Base2,Base3): <statement>  depth-first, left-to-right  problem: class derived from two classes with a common base class
  • 62. Sep 7, 2015 Advanced Private variables  No real support, but textual replacement (name mangling)  __var is replaced by _classname_var  prevents only accidental modification, not true protection
  • 63. Sep 7, 2015 Advanced ~ C structs  Empty class definition: class Employee: pass john = Employee() john.name = 'John Doe' john.dept = 'CS' john.salary = 1000
  • 64. Sep 7, 2015 Advanced 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
  • 65. Sep 7, 2015 Advanced 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
  • 66. Sep 7, 2015 Advanced 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
  • 67. Sep 7, 2015 Advanced 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     