Embed presentation
Download to read offline










![Guide to Programming with Python 11Creating the Card Classclass Card(object):""" A playing card. """RANKS = ["A", "2", "3", "4", "5", "6", "7","8", "9", "10", "J", "Q", "K"]SUITS = ["c", "d", "h", "s"]def __init__(self, rank, suit):self.rank = rankself.suit = suitdef __str__(self):reply = self.rank + self.suitreturn reply](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-11-2048.jpg&f=jpg&w=240)

![Guide to Programming with Python 13Creating the Hand Classclass Hand(object):""" A hand of playing cards. """def __init__(self):self.cards = []def __str__(self):if self.cards:reply = ""for card in self.cards:reply += str(card) + " "else:reply = "<empty>"return reply](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-13-2048.jpg&f=jpg&w=240)
![Guide to Programming with Python 14Creating the Hand Class (continued)def clear(self):self.cards = []def add(self, card):self.cards.append(card)def give(self, card, other_hand):self.cards.remove(card)other_hand.add(card)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-14-2048.jpg&f=jpg&w=240)







![Guide to Programming with Python 22Creating a Base Class(Same as before)class Hand(object):""" A hand of playing cards. """def __init__(self):self.cards = []def __str__(self):if self.cards:reply = ""for card in self.cards:reply += str(card) + " "else:reply = "<empty>"return rep](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-22-2048.jpg&f=jpg&w=240)
![Guide to Programming with Python 23Creating a Base Class (continued)(Same as before)def clear(self):self.cards = []def add(self, card):self.cards.append(card)def give(self, card, other_hand):self.cards.remove(card)other_hand.add(card)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-23-2048.jpg&f=jpg&w=240)



![Guide to Programming with Python 27Extending a Derived Class (continued)def deal(self, hands, per_hand = 1):for rounds in range(per_hand):for hand in hands:if self.cards: # if len(self.cards) > 0top_card = self.cards[0]self.give(top_card, hand)else:print "Out of cards!"](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-27-2048.jpg&f=jpg&w=240)


![Guide to Programming with Python 30Using the Derived Class (continued)my_hand = Hand()your_hand = Hand()hands = [my_hand, your_hand]deck1.deal(hands, per_hand = 5)print my_hand # 5 alternating cards from deckprint your_hand # 5 alternating cards from deckprint deck1 # deck minus first 10 cardsdeck1.clear()print deck1 # <empty>playing_cards2.py](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-30-2048.jpg&f=jpg&w=240)



![Guide to Programming with Python 34Creating a Base Class(Same as before)class Card(object):""" A playing card. """RANKS = ["A", "2", "3", "4", "5", "6", "7","8", "9", "10", "J", "Q", "K"]SUITS = ["c", "d", "h", "s"]def __init__(self, rank, suit):self.rank = rankself.suit = suitdef __str__(self):reply = self.rank + self.suitreturn reply](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-34-2048.jpg&f=jpg&w=240)























This document is a guide on object-oriented programming in Python, covering topics such as class creation, object interaction, inheritance, and method overriding. It uses examples like a Blackjack game and card classes to illustrate concepts such as object composition, polymorphism, and module creation. The document emphasizes how objects can communicate through methods, enabling the development of complex applications.










![Guide to Programming with Python 11Creating the Card Classclass Card(object):""" A playing card. """RANKS = ["A", "2", "3", "4", "5", "6", "7","8", "9", "10", "J", "Q", "K"]SUITS = ["c", "d", "h", "s"]def __init__(self, rank, suit):self.rank = rankself.suit = suitdef __str__(self):reply = self.rank + self.suitreturn reply](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-11-2048.jpg&f=jpg&w=240)

![Guide to Programming with Python 13Creating the Hand Classclass Hand(object):""" A hand of playing cards. """def __init__(self):self.cards = []def __str__(self):if self.cards:reply = ""for card in self.cards:reply += str(card) + " "else:reply = "<empty>"return reply](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-13-2048.jpg&f=jpg&w=240)
![Guide to Programming with Python 14Creating the Hand Class (continued)def clear(self):self.cards = []def add(self, card):self.cards.append(card)def give(self, card, other_hand):self.cards.remove(card)other_hand.add(card)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-14-2048.jpg&f=jpg&w=240)







![Guide to Programming with Python 22Creating a Base Class(Same as before)class Hand(object):""" A hand of playing cards. """def __init__(self):self.cards = []def __str__(self):if self.cards:reply = ""for card in self.cards:reply += str(card) + " "else:reply = "<empty>"return rep](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-22-2048.jpg&f=jpg&w=240)
![Guide to Programming with Python 23Creating a Base Class (continued)(Same as before)def clear(self):self.cards = []def add(self, card):self.cards.append(card)def give(self, card, other_hand):self.cards.remove(card)other_hand.add(card)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-23-2048.jpg&f=jpg&w=240)



![Guide to Programming with Python 27Extending a Derived Class (continued)def deal(self, hands, per_hand = 1):for rounds in range(per_hand):for hand in hands:if self.cards: # if len(self.cards) > 0top_card = self.cards[0]self.give(top_card, hand)else:print "Out of cards!"](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-27-2048.jpg&f=jpg&w=240)


![Guide to Programming with Python 30Using the Derived Class (continued)my_hand = Hand()your_hand = Hand()hands = [my_hand, your_hand]deck1.deal(hands, per_hand = 5)print my_hand # 5 alternating cards from deckprint your_hand # 5 alternating cards from deckprint deck1 # deck minus first 10 cardsdeck1.clear()print deck1 # <empty>playing_cards2.py](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-30-2048.jpg&f=jpg&w=240)



![Guide to Programming with Python 34Creating a Base Class(Same as before)class Card(object):""" A playing card. """RANKS = ["A", "2", "3", "4", "5", "6", "7","8", "9", "10", "J", "Q", "K"]SUITS = ["c", "d", "h", "s"]def __init__(self, rank, suit):self.rank = rankself.suit = suitdef __str__(self):reply = self.rank + self.suitreturn reply](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fch09-241018043703-f8fb98a9%2f75%2fProgramming-in-python-and-introduction-ppt-34-2048.jpg&f=jpg&w=240)






















