This project is an implementation of a deck of cards in Python. It is a standard 52 card deck, with 4 suits, and 13 cards in each suit. The cards can be shuffled, and drawn from the deck.
The project demonstrates many Python features introduced from Python 3.6 to 3.12:
- PEP 634: Structural Pattern Matching: match case
- PEP 572: Assignment Expressions: walrus operator
- New annotations, such as: str | int, list[str] | None, etc.
- PEP 616: String methods to remove prefixes and suffixes: str.removeprefix(), str.removesuffix()
- f-string: f"{variable=}" to print variable name and value, f"{variable!r}" to print variable name and repr(variable), f"{variable:<8}" to align left, f"{variable:>8}" to align right, f"{variable:^8}" to align center
- dataclasses: dataclass, field, ClassVar, _post_init_
- str.center(width, fill_char)
- dict new features
- etc.
The project includes the following classes:
SUIT
: An enumeration representing the four suits of a deck of cards.Card
: A class representing a playing card, with a rank and a suit.Deck
: A class representing a deck of cards.FrenchDeck
: A subclass ofDeck
that generates a standard 52 card deck.
To use the deck of cards, create an instance of FrenchDeck
, shuffle it, and draw cards:
deck = FrenchDeck()
deck.shuffle()
hand: list[Card] = deck.make_hand(num_cards=5)
┌─────────┐┌─────────┐┌─────────┐┌─────────┐┌─────────┐
│ 7 ││ 8 ││ J ││ Q ││ K │
│ ♣ ││ ♣ ││ ♠ ││ ♠ ││ ♣ │
│ ││ ││ ││ ││ │
│ ♣ ││ ♣ ││ ♠ ││ ♠ ││ ♣ │
│ 7 ││ 8 ││ J ││ Q ││ K │
└─────────┘└─────────┘└─────────┘└─────────┘└─────────┘
This script requires Python 3.12 or later.