Super Simple Python: Generate a Deck of Cards

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to generate a standard deck of cards in about 30 lines of code.

For a video version see:

Many of the Super Simple Python projects have revolved around random number generation or around creating simple functions. This one is actually going to take a step up and also include Classes. If you haven’t already got a solid grasp on classes, I would suggest learning Python Classes first.

deck of cards spread on the floor
deck of cards spread on the floor

Defining the Cards

A standard deck of 52 cards has 13 values from Two to Ace and four suits: clubs, diamonds, hearts, and spades. Some games run Ace to King, but you can make that adjustment yourself. Let’s begin by defining the card values and the suits. We’ll also make a dictionary to convert from the face cards – Jack, Queen, King, and Ace – to their respective values and back. We may need to use this dictionary later when using the cards in playing a game, like Texas Hold Em. Next we’ll create a card class that holds the two properties of each card, the suit and the value.

# 11 = J, 12 = Q, 13 = K, 14 = A
card_values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
suits = ["clubs", "diamonds", "hearts", "spades"]
 
face_cards = {
    "J": 11,
    "Q": 12,
    "K": 13,
    "A": 14,
    11: "J",
    12: "Q",
    13: "K",
    14: "A"
}
 
class Card:
    def __init__(self, value, suit):
        self.value = value
        self.suit = suit

Generating the Card Deck

Now that we have the card values and suits set up, we can generate the deck of cards. Let’s create a generate_cards() function. This function won’t need any parameters, it will simply use the list of values and suits we created earlier to generate a standard deck of 52 cards. We loop through each of the values and each of the suits, you can do this in either order, I chose to loop through the suits values first and the suits inside of that. If the value is one of the face cards, we’ll substitute it with the right letter. Then we append the generated card to the deck. At the end of our nested for loop, we’ll return the list of cards.

def generate_cards():
    cards = []
    for value in card_values:
        for suit in suits:
            if value in face_cards:
                _card = Card(face_cards[value], suit)
            else:
                _card = Card(value, suit)
            cards.append(_card)
    return cards

Viewing the Deck

We can’t just print out the cards because they are objects so we wouldn’t see the value and suit inside of each card. So, after we generate the cards, we’ll need to loop through them to actually see the representations.

cards = generate_cards()
for card in cards:
    print(card.value, card.suit)

When we run our program, we should see something like this, but going all the way through King and Ace instead of just up to 9.

printout from generating a deck of cards in python
printout from generating a deck of cards in python

Further Reading

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

One thought on “Super Simple Python: Generate a Deck of Cards

Leave a Reply

Discover more from PythonAlgos

Subscribe now to keep reading and get access to the full archive.

Continue reading