Python: a modern hybrid A language for scripting and prototyping Balance between extensibility and powerful built-in data structures genealogy: –Setl (NYU,

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Introduction to Python Week 15. Try It Out! Download Python from Any version will do for this class – By and large they are all mutually.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Object-Oriented Programming
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Names and Bindings.
Types in Scripting Languages CS 351 – Programming Paradigms.
What is a scripting language? What is Python?
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Python By Steve Wright. What is Python? Simple, powerful, GP scripting language Simple, powerful, GP scripting language Object oriented Object oriented.
Scripting languages Typically a language used for short programs to manage other programs. Interpreted, dynamically typed, permissive semantics Usually.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Type Checking  Legality checks  Operator determination  Overload resolution.
Introduction to Python Programming Languages Fall 2003 Adapted from Tutorial by Mark Hammond Skippi-Net, Melbourne, Australia
Object Oriented Databases - Overview
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object and Object- Relational Databases.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Comparison of OO Programming Languages © Jason Voegele, 2003.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
High-Level Programming Languages: C++
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
The Python Programming Language Jeff Myers Programming Language Concepts, 01/14/2002
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
If statements while loop for loop
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Basic Semantics Associating meaning with language entities.
CSC 508 – Theory of Programming Languages, Spring, 2009 Week 3: Data Abstraction and Object Orientation.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Built-in Data Structures in Python An Introduction.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Chapter 14 Advanced Function Topics CSC1310 Fall 2009.
Chapter 9 Dictionaries and Sets.
Prof. Hilfinger CS164 Lecture 51 The Pyth Language Lecture 5.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
Object-Oriented Programming Chapter Chapter
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
ISBN Object-Oriented Programming Chapter Chapter
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
PHP vs. Python. Similarities are interpreted, high level languages with dynamic typing are Open Source are supported by large developer communities are.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Scripting languages Originally, a scripting language, but more and more a high level programming language. Interpreted, dynamically typed, permissive semantics.
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Introduction to Python
Introduction to Python
CSE 3302 Programming Languages
Introduction to Python
Learning Python 5th Edition
Presentation transcript:

Python: a modern hybrid A language for scripting and prototyping Balance between extensibility and powerful built-in data structures genealogy: –Setl (NYU, J.Schwartz et al ) –ABC (Amsterdam, Meertens et al ) –Python (Van Rossum et all ) Very active open-source community

Prototyping Emphasis on experimental programming: interactive (like LISP, ML, etc). minimal translation to bytecode (like Java) dynamic typing (like LISP, SETL, APL) higher-order functions (LISP) garbage-collected, no pointers (LISP, etc.) Uniform treatment of indexable structures (like SETL) Built-in associative structures (like SETL) Light syntax, indentation is significant (from ABC)

No one will look at it without OOP Simple model of modules and classes inheritance of implementation multiple inheritance No information-hiding simple visibility model clumsy mechanism for operator overloading limited nesting: – built-in scope, global scope, local scope

What is looks like rulers = { ‘france’: [‘chirac’, 1995, 7], # general mapping ‘us’ : [‘clinton’, 1996, 4], ‘peru’ : [‘fujimori’, 1998, 0], “romania” : [‘illiescu’, 2000, 5]} for country in rulers.keys(): # built-in iterators [pres, elected, term] = rulers[country] # assignment if elected < term: print country, “:”, pres “has %I years to go” % (term - ( elected)) else: print country, “:”, pres, “is out of office”

Simple interactive model $python pres.py # load and execute france: chirac has 2 years to go us: clinton is out of office romania: illiescu has 6 years to go peru: fujimori is out of office can also write $python >>> import pres # load, execute, continue

Uniform treatment of indexable data Strings, lists and arrays have common operations characters are strings of length 1 name = “Python”; courses = [“languages”, “compilers”] + [“databases”, “basketry”]; coordinates = (0.0, 1.5, ); indexing from 0 negative index: indexing from end name [-2] is “o”, courses [-3] is “compilers” if ix is negative, lis [ix] is lis [len (lis) + ix]

Slicing (every which way) and iterating slicing: s[m:n] – from mth component, up to but excluding nth s [m:] to end, s[:n] from beginning, s[:] all components s * 4 repetition built-in iterators: for c in name: # c bound to each char for course in courses:

Dictionaries General-purpose associative maps domain (keys) of arbitrary types retrieval by key: rulers [‘peru’] yields [‘fujimori’, 1998, 0] assignment / modification >>>rulers [‘peru’][2] = 10 # coup: another 8 years to go! >>>rulers [‘mexico’] = [‘fox’, 2000, 6] >>>rulers [‘israel’] = [ ] # no type consistency required

Set theory as a model of computation Alternative to lists + recursion: sets + membership + iterators set constructors in SETL: S2 := { f (x) : x in S | P (x) }; in Python: S2 = [ ]; for x in S: if P(x): S2.append (f(x));

Loops Iterators over collections: for x in L: Iterators over dictionaries for k in mydict.keys( ) … Explicit iterators: for x in [1, 1, 2, 3, 5, 8, 13]: Numeric iterators for x in range (1,100):

Functions def intersect (seq1, seq2): # no type info res = [ ] # initialize list for x in seq1: # iterate over list if x in seq2: # built-in membership res.append (x) # in-place modification return res assigned names are local unless declared global no possible hiding

Modules Modules are namespaces: unit of encapsulation Modules are objects: components can be accessed Modules can be inspected dynamically: – __dict__ provides dictionary for module: – keys are strings for entities for attr in module.__dict__keys ( ): # look at all entities print attr, # comma prevents LF if attr [0:2] == “__”: # naming convention print atrr, “built-in name”

Classes and inheritance Standard notions: superclasses, derived classes, self (for this), dynamic dispatching Each class and each object is a namespace with a dictionary To locate an operation, lookup in dictionary of object (dispatch table). If not found, examine superclasses. Operator overloading through predefined names: –__init__ constructor –__del__ destructor –_add__ operator “+” –__repr__ printing, external representation

Data members are created implicitly Class Number: def __init__ (self, start): self.data = start; # data is defined def __add__ (self, other): # number + number return Number (self.data + other.data) def __repr__ (self) return `self.data` # convert to string note: no way to overload (Number + integer) etc.

Any class can be a collection class collection: def __getitem__ (self, i): return self.data[I] # attribute data is indexable …. X = collection (); X.data = [1, 2, 3] # member exists, assignment ok for item in X: # for calls __getitem print item << item # equivalent to item * 2 ^ item

Exceptions, etc. internally, iterator implemented as i = 0; try: while 1: # no boolean type item = getitem (self, i)... # body of loop i = i +1 except IndexError: # eventually i too large pass # null statement except: # all other exceptions print “unexpected chaos!”