Card – A basic playing card

A simple API for creating and using playing cards

We will be using numbers to represent playing card clubs and ranks.

These are the suits:

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

For instance, the suit at index 0:

suits[0]
'♠️'

These are the ranks:

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

For instance, the rank at index 1:

(Note that there isn’t a rank at index 0, since we want the rank to match the indices where possible.)

ranks[1]
'A'

source

Card

 Card (suit:int, rank:int)

A playing card, created by passing in rank and suit

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=3, rank=4)
c
4♣️

Comparison Operators

Equality, less than and greater than work on the rank and suit indices:

A test for equality:

test_eq(Card(suit=3, rank=4), Card(suit=3, rank=4))

And <:

assert Card(suit=1, rank=3) < Card(suit=2, rank=3)

And >:

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