Play with python lecture 2

21
Play with Python Lecture 2

Transcript of Play with python lecture 2

Page 1: Play with python lecture 2

Play with PythonLecture 2

Page 2: Play with python lecture 2

Agenda

Revision

Object Oriented Programming

Demo 1

Demo 2

Demo 3 (GUI)

Assignment

Next Week !

Page 3: Play with python lecture 2

Revision

Lists

empty list:

lst = []

list of lists:

lofl = [[1,2],[3,4]]

list of different types: lst2 = [1, "hi"]

Dictionary:

empty dictionary:

d = {}

Page 4: Play with python lecture 2

Object Oriented Programming

Application is a set of objects

Page 5: Play with python lecture 2

Object Oriented Programming

Application is a set of living objects

Page 6: Play with python lecture 2

Object Oriented Programming

Application is a set of living interacting objects

Page 7: Play with python lecture 2

Object Oriented Programming

Application is a set of living interacting objects

Page 8: Play with python lecture 2

Data + Behaviour

Application is a set of living interacting objects

Page 9: Play with python lecture 2

Demo 1 (Animals)

c = Cat()

c.talk()

c.walk()

Page 10: Play with python lecture 2

Demo 1 (Animals)

c = Cat()

c.talk()

c.walk()

d = Duck()

d.talk()

d.fly()

Page 11: Play with python lecture 2

Demo 1 (Animals)

c = Cat()

c.talk()

c.walk()

d = Duck()

d.talk()

d.fly()

m = Mouse()

m.die()

Page 12: Play with python lecture 2

Demo 1 (Animals)

c = Cat()

c.talk()

c.walk()

d = Duck()

d.talk()

d.fly()

m = Mouse()

m.die()

Page 13: Play with python lecture 2

Demo 1 (Animals eating)

c = Cat()

c.talk()

c.walk()

c.eat(m)

c2 = Cat()

c2.eat(b)

Page 14: Play with python lecture 2

Demo 1 (Animals)

c = Cat()

c.talk()

c.walk()

c.eat(m)

c2 = Cat()

c2.eat(b)

m = Mouse()

b = Bread()

Page 15: Play with python lecture 2

Demo 1 Classes (Animals)

class Cat:

def talk(self):

print "ميااو"

def walk(self):

print " تك تك " تك

def eat(self, something):

something.die()

print " لذييذ ، ايم " يا

class Mouse:

def die(self):

print " !! ،آآآآآه"النجدة

class Bread:

def die(self):

print “” كراش”كراش

Page 16: Play with python lecture 2

Demo 1: ِ�List of Animals

animals = []

animals.append(Cat())

animals.append(Duck())

for a in animals:

a.talk()

animals list is also an object of class "List" !

Page 17: Play with python lecture 2

Demo 2 (Computer Device)

Page 18: Play with python lecture 2

Demo 2 (Computer Device)

class Computer:def __init__(self, computer_type, basePrice):

self.computerType = computer_type

Constructor

Member

Page 19: Play with python lecture 2

GUI with PyQt4

Page 20: Play with python lecture 2

GUI Exampledlg = QDialog()

btn = QPushButton("Click Me !", dlg)

btn.clicked.connect(onButtonClicked)

btn2 = QPushButton("Click Me Too !!", dlg)

btn2.move(100,0)

btn2.clicked.connect(onButton2Clicked)

dlg.show()

def onButtonClicked():

print "Hello World"

def onButton2Clicked():

btn.close()

Page 21: Play with python lecture 2

GUI Example (Added an image)dlg = QDialog()

dlg.resize(500, 500)

btn = QPushButton("Click Me !", dlg)

btn.clicked.connect(onButtonClicked)

btn2 = QPushButton("Click Me Too !!", dlg)

btn2.move(100,0)

btn2.clicked.connect(onButton2Clicked)

label = QLabel("My First GUI program", dlg)

pix = QPixmap("Water lilies.jpg")

label.setPixmap(pix)

dlg.show()

def onButtonClicked():

print "Hello World"

def onButton2Clicked():

btn.close()