Card Module

An simple API for creating and using playing cards

We will be using number to represent a playing card suit and rank. These are the suits:

suits
['♠️', '♥️', '♣️', '♦️']

For example the suit at index 0:

suits[0]
'♠️'

The ranks are:

ranks
[None, 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

For instance the rank at index 1 is (Note that the rank at index 0 is none, as we want to ranks to match the indices):

ranks[1]
'A'

source

Card

 Card (suit:int, rank:int)

A playing card

Type Details
suit int An index into suits
rank int An index into ranks

Here is an example of creating and displaying a card

c = Card(suit=1, rank=3)
c
3♥️

Comparison Operators

Equality, greater than, and less than

test_eq(Card(suit=1, rank=3), Card(suit=1, rank=3))
test_ne(Card(suit=2, rank=3), Card(suit=1, rank=3))
test_ne(Card(suit=1, rank=2), Card(suit=1, rank=3))
assert Card(suit=1, rank=3) > Card(suit=1, rank=2)
assert Card(suit=2, rank=3) > Card(suit=1, rank=3)
assert not Card(suit=1, rank=2) > Card(suit=1, rank=3)
assert Card(suit=1, rank=2) < Card(suit=1, rank=3)
assert Card(suit=1, rank=2) < Card(suit=2, rank=2)
assert not Card(suit=1, rank=3) < Card(suit=1, rank=2)