More Related Content Similar to Play with python lecture 2 Similar to Play with python lecture 2 (10) Play with python lecture 23. Revision
Lists
empty list:
lst = []
list of lists:
lofl = [[1,2],[3,4]]
list of different types: lst2 = [1, "hi"]
Dictionary:
empty dictionary:
d = {}
11. Demo 1 (Animals)
c = Cat() m = Mouse()
c.talk() m.die()
c.walk()
d = Duck()
d.talk()
d.fly()
12. Demo 1 (Animals)
c = Cat() m = Mouse()
c.talk() m.die()
c.walk()
d = Duck()
d.talk()
d.fly()
13. Demo 1 (Animals eating)
c = Cat()
c.talk()
c.walk()
c.eat(m)
c2 = Cat()
c2.eat(b)
14. Demo 1 (Animals)
c = Cat() m = Mouse()
c.talk()
c.walk()
c.eat(m) b = Bread()
c2 = Cat()
c2.eat(b)
15. Demo 1 Classes (Animals)
class Cat: class Mouse:
def talk(self): def die(self):
print " !! "
print " "
def walk(self): class Bread:
print " " def die(self):
print “” ”
def eat(self, something):
something.die()
print " "
16. Demo 1: List of Animals
animals = [] animals list is also an
animals.append(Cat()) object of class "List" !
animals.append(Duck())
for a in animals:
a.talk()
18. Demo 2 (Computer Device)
class Computer: Constructor
def __init__(self, computer_type, basePrice):
self.computerType = computer_type Member
20. GUI Example
dlg = 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()
21. GUI Example (Added an image)
dlg = QDialog() dlg.show()
dlg.resize(500, 500)
def onButtonClicked():
btn = QPushButton("Click Me !", dlg) print "Hello World"
btn.clicked.connect(onButtonClicked)
def onButton2Clicked():
btn.close()
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)