Play with python lecture 2
-
Upload
iloveallahsomuch -
Category
Technology
-
view
344 -
download
3
Transcript of Play with python lecture 2
Play with PythonLecture 2
Agenda
Revision
Object Oriented Programming
Demo 1
Demo 2
Demo 3 (GUI)
Assignment
Next Week !
Revision
Lists
empty list:
lst = []
list of lists:
lofl = [[1,2],[3,4]]
list of different types: lst2 = [1, "hi"]
Dictionary:
empty dictionary:
d = {}
Object Oriented Programming
Application is a set of objects
Object Oriented Programming
Application is a set of living objects
Object Oriented Programming
Application is a set of living interacting objects
Object Oriented Programming
Application is a set of living interacting objects
Data + Behaviour
Application is a set of living interacting objects
Demo 1 (Animals)
c = Cat()
c.talk()
c.walk()
Demo 1 (Animals)
c = Cat()
c.talk()
c.walk()
d = Duck()
d.talk()
d.fly()
Demo 1 (Animals)
c = Cat()
c.talk()
c.walk()
d = Duck()
d.talk()
d.fly()
m = Mouse()
m.die()
Demo 1 (Animals)
c = Cat()
c.talk()
c.walk()
d = Duck()
d.talk()
d.fly()
m = Mouse()
m.die()
Demo 1 (Animals eating)
c = Cat()
c.talk()
c.walk()
c.eat(m)
c2 = Cat()
c2.eat(b)
Demo 1 (Animals)
c = Cat()
c.talk()
c.walk()
c.eat(m)
c2 = Cat()
c2.eat(b)
m = Mouse()
b = Bread()
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 “” كراش”كراش
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" !
Demo 2 (Computer Device)
Demo 2 (Computer Device)
class Computer:def __init__(self, computer_type, basePrice):
self.computerType = computer_type
Constructor
Member
GUI with PyQt4
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()
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()