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

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
- 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.
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.
Hi Ahmad ,
Firstly Ide Mubarik – bring peace in the world.
I am very new to python, though I have experience in writing codes. During my class we had a project where the purpose was to print pack of card. That’s how I got to this place.
I saw your codes but instead of printing them in long list my aim was to print them in a orderly fashion with each set of cards.
I used “in Range” mixed with ‘while’ and ‘if conditions – these function make a very powerful decision making codes. Have a look.
# DrawTxtInRange.py
# Notes : This Py demonstrate power of ‘in range’ used with ‘while’ & ‘if’ condition.
# In this example I have used three ‘in range loops’, one ‘while’ & one ‘if’ condition.
# Create a list of cards and signs
# print them in a window for eact Card_Sign
from graphics import *
DKwin= GraphWin(“Project CS 138-Mira Coasta College.CA, USA : Student D K Dutta”,610,630) # Header
card_point = [ “Ace”,”King”,”Queen”, “Jack”,”2″,”3″, “4”, “5”,
“6”,”7″,”8″,”9″,”10″]
card_sign = [“Club”, “Diamond”, “Heart”, “Spade”]
j =0
k =0
x =70 # Value of X Cord
for y in range(530):
y = j +35 # y is Value of Y cord
while k 500 : # This is the max the loop can go.
x = x + 140 # at this point move X & Y Cords back up
y =35
j =0
# Save and Run & have Fun
can you please answer this?
write a program to print the maximum points of you cards:
Input:
2
3
CA D9 H8
3
SJ SQ S8
Output:
11
28
bro did u get the answer????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????