Skip to main content

HandsDecksCards

Source code: GitHub

HandsDecksCards is a Python package that provides classes representing playing cards, decks of cards, and hands of cards. It can be used to help create card games coded in Python.

Usage

Note that all the usage examples below assume that the HandsDecksCards package has been installed from PyPi, and is available for import. If this is not the case, and you are just working with the source code, adjust the import statements by dropping the leading 'HandsDecksCards.' from the module paths.

Using Card class

from HandsDecksCards.card import Card

# Create cards: King of Hearts and 10 of Diamonds
c_kh = Card('H', 'K')
c_10d = Card('D', '10')
# Card implements __str__()
print(f"Created cards: {c_kh}, {c_10d}")
# You can get the suit of a card
print(f"Suit of {c_10d}: {c_10d.suit}")
# You can get the pips of a card
print(f"Pips of {c_10d}: {c_10d.pips}")
# You can get the count of a card
print(f"Count of {c_kh}: {c_kh.count_card()}")
# Card implements __lt__(), so a list of Card objects, can be sorted (A high)
cards = [c_kh, c_10d]
cards.sort()
print(f"Sorted cards (low to high): {cards}")
# Or Card objects an be directly compared:
print(f"10 is less than king?: {c_10d < c_kh}")
# Card provides a utility function that can create a list of Card objects from a string representation
card_list = Card().make_card_list_from_str('AS KH QD JC 10H 2S')
print(f"Created card list from string: {card_list}")

Using Deck class

from HandsDecksCards.deck import Deck

# Create a deck of cards
d = Deck()
# Draw 5 cards from the deck
drawn = d.draw(5)
print(f"Drew 5 cards from deck: {drawn}")
# How many cards remain in the deck? Deck implements __len__().
print(f"Cards remaining in deck: {len(d)}")
# What cards remain in the deck? Deck implements __str__().
print(f"Cards remaining in deck: {d}")

Note that the deck module also provided a ShoeDeck class, which represents multiple decks of cards shuffled together. It also provides a StackedDeck class, which represents a deck of cards with a predefined draw order, useful for testing.

Using Hand class

from HandsDecksCards.card import Card
from HandsDecksCards.hand import Hand

# Create an empty hand of cards
h = Hand()
# Create a list of cards
card_list = Card().make_card_list_from_str('AS KH QD JC 10H 2S AC')
# Add cards to the hand. Typically this would be done by drawing from a deck, but here we will just add a list of chosen cards.
h.add_cards(card_list)
print(f"Hand after adding cards: {h}")
# Count the cards in the hand
print(f"Count of hand (ace low): {h.count_hand()}")
# Get information about the hand
hand_info = h.hand_info()
print(f"Hand info: {hand_info}")
# How many cards are in the hand?
print(f"Number of cards in hand: {hand_info.Num_Aces + hand_info.Num_Other}")
print(f"Number of cards in hand: {h.get_num_cards()}")
# How many aces are in the hand?
print(f"Number of aces in hand: {h.get_num_aces()}")
# What aces are in the hand?
aces = h.get_aces()
# Can iterate a Hand object to get the cards in the hand
for a in aces:
    print(f"Ace in hand: {a}")
# How many non-ace cards are in the hand?
print(f"Number of non-ace cards in hand: {h.get_num_non_aces()}")
# Which non-ace cards are in the hand?
non_aces = h.get_non_aces()
# Hand defines __repr__(), so can use eval() to recreate a Hand object
print(f"Non-ace cards in hand: {eval(repr(non_aces))}")
# Remove the last card from the hand
removed_card = h.remove_card()
print(f"Removed card: {removed_card}")
print(f"Remaining cards in hand: {h}")
# Remove the 3rd card (index 2) from the hand
removed_card = h.remove_card(2)
print(f"Removed card at index 2: {removed_card}")
print(f"Remaining cards in hand: {h}")

Demonsration

A demonstration of Card, Deck, and Hand functionality, based on the usage examples above, is provided in the main.py file in the src\HandsDecksCards directory. To run this demonstration, type python -m HandsDecksCards.main in a terminal window.

Unittests

Unittests for the HandsDecksCards are in the tests directory, with filenames starting with test_. To run the unittests, type python -m unittest discover -s ..\..\tests -v in a terminal window in the src\HandsDecksCards directory.

License

MIT License. See the LICENSE file for details

Metadata

Release files for HandsDecksCards 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for HandsDecksCards 1.0.0
File Size Uploaded
handsdeckscards-1.0.0.tar.gz 14.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for HandsDecksCards 1.0.0
File Interpreter ABI Platform
handsdeckscards-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.6 kB

Release files / handsdeckscards-1.0.0.tar.gz

Download URL handsdeckscards-1.0.0.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
358f5334f11149d41d29669fe42547117d29108fc6f66b883f4416f2a820f494
BLAKE2b-256 checksum
How to use checksums
faa42b6cf0e934df8688c18092755076661feeffe41c9992919c9575a0083867
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 9, 2025.

Transparency log

Release files / handsdeckscards-1.0.0-py3-none-any.whl

Download URL handsdeckscards-1.0.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5bff43e0b69f3ab2bdea8b46f075102c9e3007d4c4e31edeeabbfee25269e2b2
BLAKE2b-256 checksum
How to use checksums
885f3c1395df5fd4e71d9aef24ff28573cf13a160a126ff58dfc35ec4384169a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 9, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page