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:
The four signs are:
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
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
- Create a list and put 13 different values in that list.
- Create another list and put all the four signs of the card.
- Use a for loop to iterate the first list.
- In that for loop create another for loop to iterate the second list.
- Now print the values one by one concatenation with the signs one by one.
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.
Then, create another list to store all the signs of the cards.
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])
$ 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.