-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The first book in 2020 and some exercises.
- Loading branch information
Showing
3 changed files
with
564 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+1.94 MB
programming_languages/python/Handson Python Tutorial/20200117 Hands-onPythonTutorial.pdf
Binary file not shown.
38 changes: 38 additions & 0 deletions
38
programming_languages/python/Handson Python Tutorial/excercise/frenchDeck.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Chapter 1 | ||
""" | ||
|
||
|
||
"""P4 A Pythonic Card Deck""" | ||
import collections | ||
from random import choice | ||
|
||
Card = collections.namedtuple('Card', ['rank', 'suit']) # Construct a simple class to represent individual cards | ||
|
||
class FrenchDeck: | ||
ranks = [str(n) for n in range(2, 11)] + list('JQKA') | ||
suits = 'spades diamonds clubs hearts'.split() | ||
|
||
def __init__(self): | ||
self._cards = [Card(rank, suit) for suit in self.suits | ||
for rank in self.ranks] | ||
|
||
def __len__(self): | ||
return len(self._cards) | ||
|
||
def __getitem__(self, position): | ||
return self._cards[position] | ||
|
||
def main(): | ||
deck = FrenchDeck() | ||
length = len(deck) | ||
print(length) | ||
|
||
random_choice = choice(deck) | ||
print(random_choice) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
||
|
||
|
Oops, something went wrong.