How to print a deck of cards in Python

Using for loops, we can easily print a deck of cards in Python. In this Python tutorial, we will show you how to print all the cards in Python using for loop.

In a deck of cards, there are 52 cards.

  • 4 Signs
  • 13 Different values for each sign

The values of the cards are:

'A','K','Q','J','2','3','4','5','6','7','8','9','10'

The four signs are:

'Heart','CLUB','DIAMOND','SPADE'

Now, these signs and values form 52 number of cards.

Those cards are A of Heart, K of Heart, Q of heart and so on. Then A of Club, K of Club, Q of Club and so on.

In this way, we will get four different sets of a card and in each set, there will be 13 cards. (As there are 13 different values for cards of each sign )




So the total number of cards will be:

13*4 = 52
print deck of cards in Python

deck of cards

Now let’s try to print all these cards one by one using Python Program

Print deck of cards in Python

To print a deck of cards in Python we are going to use two for loops.

Algorithm to print all the cards in Python

  1. Create a list and put 13 different values in that list.
  2. Create another list and put all the four signs of the card.
  3. Use a for loop to iterate the first list.
  4. In that for loop create another for loop to iterate the second list.
  5. Now print the values one by one concatenation with the signs one by one.

Also, learn,

Let’s understand this with a Python program.

Python program to print all the cards using for loop

At first, create a list having all the values in it.

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

Then, create another list to store all the signs of the cards.

card_signs =['Heart','CLUB','DIAMOND','SPADE']

Now finally the for loop which is our main coding portion.

for points in range(len(card_points)):
    for signs in range (len(card_signs)):
        print(card_points[points],card_signs[signs])

If you don’t know how to print items from a list please read this,¬†How to print each item from a Python list?

So our full Python code will be like this:

card_points =['A','K','Q','J','2','3','4','5','6','7','8','9','10']
card_signs =['Heart','CLUB','DIAMOND','SPADE']
for points in range(len(card_points)):
    for signs in range (len(card_signs)):
        print(card_points[points],card_signs[signs])

Output:

$ Python CodeSpeedy.py
A Heart
A CLUB
A DIAMOND
A SPADE
K Heart
K CLUB
K DIAMOND
K SPADE
Q Heart
Q CLUB
Q DIAMOND
Q SPADE
J Heart
J CLUB
J DIAMOND
J SPADE
2 Heart
2 CLUB
2 DIAMOND
2 SPADE
3 Heart
3 CLUB
3 DIAMOND
3 SPADE
4 Heart
4 CLUB
4 DIAMOND
4 SPADE
5 Heart
5 CLUB
5 DIAMOND
5 SPADE
6 Heart
6 CLUB
6 DIAMOND
6 SPADE
7 Heart
7 CLUB
7 DIAMOND
7 SPADE
8 Heart
8 CLUB
8 DIAMOND
8 SPADE
9 Heart
9 CLUB
9 DIAMOND
9 SPADE
10 Heart
10 CLUB
10 DIAMOND
10 SPADE

So you can see all the 52 cards are here.

Let us know if you have a better solution to this problem in the comment section, we will be happy to share that with our learners.


Leave a Reply

Your email address will not be published. Required fields are marked *