PYTHON Volkan Tüfekçi – Gülcan Aydın 26.12.2007
History Philosphy Usage Guido  van Rossum  1989 Hobby Language ABC – Modula 3 – Unix/C – Common Lisp - Haskell Dislikes: D ifficulty of adding new "primitive" operations M onolithic, "closed system“ , basic I/O operations I deosyncratic syntax (all uppercase keywords!) Aims: Readability Rely on the  Unix infrastructure and conventions, without being Unix-bound I mportance of programmer effort over computer effort 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
History Philosphy Usage(cont.) 1991 S yntax and semantics are  minimalist , while the  standard library  is large and comprehensive multi-paradigm programming language :  object orientation  and  structured programming   -> fully functional programming  and  aspect-oriented programming  -> by language features pyDBC  and  Contracts for Python  which allow  Design by Contract dynamic  name resolution  (late binding) New built-in modules are easily written in  C  or  C++ Python philosophy rejects exuberant syntax, such as in  Perl PyQt, PyGTK, PyCon, PyPy 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
History Philosphy Usage(cont.) Nasa, Google, YouTube, BitTorrent, Air Canada’s Reservation Management TurboGears, Django(NewYork Times’ web site) Maya, Blender Fedora, Gentoo, Pardus Jython, IronPython 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Introduction to O-O Python Python is an object-oriented programming language.   Supports procedural programming with modules and functions.   Generally, the O-O paradigm is suitable when you want to group state (data) and behavior (code) together in handy packets of functionality.   The procedural paradigm, based on modules and functions, tends to be simpler and is more suitable when you don't need any of the benefits of object-oriented programming. 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Syntax All values in Python can be used as logic values. Some of the more “empty” ones, like [], 0, “” and None represent logical falsity, while most other values (like [0], 1 or “Hello, world”) represent logical truth. if  0 < month <= 12: doSomething() 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Dictionaries  1 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN A dictionary in Python is like an instance of the Hashtable class in Java No duplicate member No order Mutable
Dictionaries  2 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
List s  1 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
List s  2 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
List s  3 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
List & Dictionary Examples L=[1,2,3,4,5] IMPORTANT : The value will be stored into a range of memory blocks, and what if we do this?  L2=L  It make L2 refers to the same memory blocks where L points to. Example;  29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
List C omprehension s 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Tuples 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN A tuple is an immutable list. A tuple can not be changed in any way once it is created Tuples are faster than lists It makes your code safer if you “write-protect” data Tuples can be converted into lists, and vice-versa.
Assigning Variables 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
String Formatting & Concatenating 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN # 1 membered Tuple!!! # Strong typed
Importing Modules & Getting Help 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
String Operation Examples 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Modules from UserDict import UserDict
Constructor A constructor is a method that initializes a newly created object. The overridden derived-class constructor usually calls the base-class constructor, to initialize base-class attributes before initializing derived-class attributes.   If a derived class does not define a constructor, the class’s base-class constructor executes when the client creates a new object of the class.   29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Destructor & Static Variable 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN D oes not actually destroy the object # Static variable # 0 # 2 # 0
Static Methods 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Property Descriptor 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Slots Attribute 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN >>>8
Inheritance Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors. The new class is referred to as a  derived class,  inherit from the class referred to as  base class. With  single inheritance, a class is derived from one base class. With  multiple inheritance,  a derived class inherits from several base classes. 29/05/09 CSE 494  Volkan TÜFEKÇİ -  Gülcan  AYDIN
(cont.) We distinguish between  “is-a” relationships  and  “has-a” relationships: “ Is a” is inheritance. In an “is a” relationship, an object of a derived-class type may also be treated as an object of the base-class type. “ Has a” is composition. In a “has a” relationship, an object  has  references to one or more objects of other classes as members. 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
“ is a” Inheritance hierarchy for university community members : 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Multiple Inheritance and Name Clash Problem   class Base1:  def amethod(self):   print &quot;Base1“ class Base2(Base1): pass    # pass!!! class Base3:  def amethod(self):  print &quot;Base3&quot;  class Derived(Base2, Base3): pass  aninstance = Derived( )  aninstance.amethod( )  # prints: &quot;Base1&quot;   29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Data Abstraction One of the fundamental principles of good software engineering is that a client should not need to know how a class is implemented to use that class.  Python’s use of modules facilitates this data abstraction—a program can import a class definition and use the class without knowing how the class is implemented.   29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Encapsulation No methods or member variables are protected (or private or the like) in Python.   Encapsulation is pretty much a matter of programming style.  If you really need it, there are naming-conventions that will allow some privacy. such as;  _ _ident   Python compiler implicitly changes the identifier into  _classname_ _ident , where  classname  is the name of the class.   29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Example of Private Data Access 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Abstract Classes 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN Traceback (most recent call last): b.earnings() raise NotImplementedError, &quot;Cannot call abstract method&quot; NotImplementedError: Cannot call abstract method
Polymorphism 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Polymorphism 2 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Polymorphism 3 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Polymorphism 4 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Polymorphism 5 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
Composition vs. Inheritance composition of existing classes Employee  is a BirthDate or that an Employee is a TelephoneNumber Employee  has a BirthDate and that an Employee has a  TelephoneNumber. 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN
“ Uses A” and “Knows A” Relationships a person object is not a car and a person object does not contain a car, a   person object certainly  uses a car A program uses an object simply by calling a method of   that object through a reference. one object is said to have a  knows a relationship with the other object;   this is sometimes called an  association. 29/05/09 CSE 494  Volkan TÜFEKÇİ - Gülcan AYDIN

Python

  • 1.
    PYTHON Volkan Tüfekçi– Gülcan Aydın 26.12.2007
  • 2.
    History Philosphy UsageGuido van Rossum 1989 Hobby Language ABC – Modula 3 – Unix/C – Common Lisp - Haskell Dislikes: D ifficulty of adding new &quot;primitive&quot; operations M onolithic, &quot;closed system“ , basic I/O operations I deosyncratic syntax (all uppercase keywords!) Aims: Readability Rely on the Unix infrastructure and conventions, without being Unix-bound I mportance of programmer effort over computer effort 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 3.
    History Philosphy Usage(cont.)1991 S yntax and semantics are minimalist , while the standard library is large and comprehensive multi-paradigm programming language : object orientation and structured programming -> fully functional programming and aspect-oriented programming -> by language features pyDBC and Contracts for Python which allow Design by Contract dynamic name resolution (late binding) New built-in modules are easily written in C or C++ Python philosophy rejects exuberant syntax, such as in Perl PyQt, PyGTK, PyCon, PyPy 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 4.
    History Philosphy Usage(cont.)Nasa, Google, YouTube, BitTorrent, Air Canada’s Reservation Management TurboGears, Django(NewYork Times’ web site) Maya, Blender Fedora, Gentoo, Pardus Jython, IronPython 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 5.
    Introduction to O-OPython Python is an object-oriented programming language. Supports procedural programming with modules and functions. Generally, the O-O paradigm is suitable when you want to group state (data) and behavior (code) together in handy packets of functionality. The procedural paradigm, based on modules and functions, tends to be simpler and is more suitable when you don't need any of the benefits of object-oriented programming. 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 6.
    Syntax All valuesin Python can be used as logic values. Some of the more “empty” ones, like [], 0, “” and None represent logical falsity, while most other values (like [0], 1 or “Hello, world”) represent logical truth. if 0 < month <= 12: doSomething() 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 7.
    Dictionaries 129/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN A dictionary in Python is like an instance of the Hashtable class in Java No duplicate member No order Mutable
  • 8.
    Dictionaries 229/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 9.
    List s 1 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 10.
    List s 2 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 11.
    List s 3 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 12.
    List & DictionaryExamples L=[1,2,3,4,5] IMPORTANT : The value will be stored into a range of memory blocks, and what if we do this? L2=L It make L2 refers to the same memory blocks where L points to. Example; 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 13.
    List C omprehensions 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 14.
    Tuples 29/05/09 CSE494 Volkan TÜFEKÇİ - Gülcan AYDIN A tuple is an immutable list. A tuple can not be changed in any way once it is created Tuples are faster than lists It makes your code safer if you “write-protect” data Tuples can be converted into lists, and vice-versa.
  • 15.
    Assigning Variables 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 16.
    String Formatting &Concatenating 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN # 1 membered Tuple!!! # Strong typed
  • 17.
    Importing Modules &Getting Help 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 18.
    String Operation Examples29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 19.
    Modules from UserDictimport UserDict
  • 20.
    Constructor A constructoris a method that initializes a newly created object. The overridden derived-class constructor usually calls the base-class constructor, to initialize base-class attributes before initializing derived-class attributes. If a derived class does not define a constructor, the class’s base-class constructor executes when the client creates a new object of the class. 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 21.
    Destructor & StaticVariable 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN D oes not actually destroy the object # Static variable # 0 # 2 # 0
  • 22.
    Static Methods 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 23.
    Property Descriptor 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 24.
    Slots Attribute 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN >>>8
  • 25.
    Inheritance Inheritance isa form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors. The new class is referred to as a derived class, inherit from the class referred to as base class. With single inheritance, a class is derived from one base class. With multiple inheritance, a derived class inherits from several base classes. 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 26.
    (cont.) We distinguishbetween “is-a” relationships and “has-a” relationships: “ Is a” is inheritance. In an “is a” relationship, an object of a derived-class type may also be treated as an object of the base-class type. “ Has a” is composition. In a “has a” relationship, an object has references to one or more objects of other classes as members. 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 27.
    “ is a”Inheritance hierarchy for university community members : 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 28.
    Multiple Inheritance andName Clash Problem class Base1: def amethod(self): print &quot;Base1“ class Base2(Base1): pass # pass!!! class Base3: def amethod(self): print &quot;Base3&quot; class Derived(Base2, Base3): pass aninstance = Derived( ) aninstance.amethod( ) # prints: &quot;Base1&quot; 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 29.
    Data Abstraction Oneof the fundamental principles of good software engineering is that a client should not need to know how a class is implemented to use that class. Python’s use of modules facilitates this data abstraction—a program can import a class definition and use the class without knowing how the class is implemented. 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 30.
    Encapsulation No methodsor member variables are protected (or private or the like) in Python. Encapsulation is pretty much a matter of programming style. If you really need it, there are naming-conventions that will allow some privacy. such as; _ _ident Python compiler implicitly changes the identifier into _classname_ _ident , where classname is the name of the class. 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 31.
    Example of PrivateData Access 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 32.
    Abstract Classes 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN Traceback (most recent call last): b.earnings() raise NotImplementedError, &quot;Cannot call abstract method&quot; NotImplementedError: Cannot call abstract method
  • 33.
    Polymorphism 29/05/09 CSE494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 34.
    Polymorphism 2 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 35.
    Polymorphism 3 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 36.
    Polymorphism 4 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 37.
    Polymorphism 5 29/05/09CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 38.
    Composition vs. Inheritancecomposition of existing classes Employee is a BirthDate or that an Employee is a TelephoneNumber Employee has a BirthDate and that an Employee has a TelephoneNumber. 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN
  • 39.
    “ Uses A”and “Knows A” Relationships a person object is not a car and a person object does not contain a car, a person object certainly uses a car A program uses an object simply by calling a method of that object through a reference. one object is said to have a knows a relationship with the other object; this is sometimes called an association. 29/05/09 CSE 494 Volkan TÜFEKÇİ - Gülcan AYDIN