Deck - A deck of cards

A deck of playing cards

source

Deck

 Deck ()

A deck of 52 cards, not including jokers.

When we initially create a deck, all of the cards will be present:

deck = Deck()
deck
A♠️; 2♠️; 3♠️; 4♠️; 5♠️; 6♠️; 7♠️; 8♠️; 9♠️; 10♠️; J♠️; Q♠️; K♠️; A♥️; 2♥️; 3♥️; 4♥️; 5♥️; 6♥️; 7♥️; 8♥️; 9♥️; 10♥️; J♥️; Q♥️; K♥️; A♦️; 2♦️; 3♦️; 4♦️; 5♦️; 6♦️; 7♦️; 8♦️; 9♦️; 10♦️; J♦️; Q♦️; K♦️; A♣️; 2♣️; 3♣️; 4♣️; 5♣️; 6♣️; 7♣️; 8♣️; 9♣️; 10♣️; J♣️; Q♣️; K♣️

That should be 52 cards.

len(deck)
52

We can check if a card is on the deck:

Card(1,1) in deck
True

source

Deck.pop

 Deck.pop (idx:int=-1)

Remove and return one card.

Type Default Details
idx int -1 The index of the card to remove (default: last card)

Let’s pop a card

deck.pop()
K♣️

And now the dack should have only 51 cards

len(deck)
51

source

Deck.remove

 Deck.remove (card:nbdev_tutorial.card.Card)

Remove a specific card.

card23 = Card(2, 3)
deck.remove(card23)

assert card23 not in deck

source

Deck.shuffle

 Deck.shuffle ()

Shuffle the cards.


source

draw_n

 draw_n (n:int, replace:bool=True)

Draw n cards from the deck.

Type Default Details
n int The number of cards to draw
replace bool True Whether to replace the cards after drawing
draw_n(5)
[8♥️, 4♣️, 3♣️, 5♥️, 5♠️]