Python Program to Print a Deck of Cards in Python

In the previous article, we have discussed Python Program to Calculate Age in Days from Date of Birth
A deck of cards contains 52 cards.

  • There are 4 sign cards –  ‘Heart’, ‘CLUB’, ‘DIAMOND’, ‘SPADE’
  • There are 13 value of cards – ‘A’,’K’,’Q’,’J’,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’10’

These are the cards A of Heart, K of Heart, Q of Heart, and so forth. Then there’s A of Club, K of Club, Q of Club, and so on.

As a result, we will have four different sets of a card, with 13 cards in each set. (Because there are 13 different values for each sign’s card)

As a result, the total number of cards = 13*4 = 52

Given two lists of cards and the task is to print a deck of cards.

For Loop:

A for loop is used to iterate through a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

This works more like an iterator method in other object-oriented programming languages than for the keyword in other programming languages.

The for loop allows us to execute a set of statements once for each item in a list, tuple, set, and so on.

In General:

A deck of cards contains 52 cards.

Each card is divided into four suits, each of which contains 13 cards.

A deck of cards can also be classified as follows:

  1. Face cards
  2. Numbers (2 -10)
  3. Aces

These cards are also referred to as court cards.

In all four suits, they are Kings, Queens, and Jacks.

Program to Print a Deck of Cards in Python

Below are the ways to print a deck of cards.

Method: Using For Loop

Approach:

  • Give the list of value cards as static input and store it in a variable.
  • Give the list of signs cards as static input and store it in another variable.
  • Loop in the above list of value cards using the for loop and len() function.
  • Inside the loop, loop again in the above list of sign cards using the for loop and len() function.
  • Print the corresponding iterator values.
  • The Exit of the program.

Below is the implementation:

# Give the list of value cards as static input and store it in a variable.
card_values = ['A', 'K', 'Q', 'J', '2',
               '3', '4', '5', '6', '7', '8', '9', '10']
# Give the list of signs cards as static input and store it in another variable.
crd_signs = ['Heart', 'CLUB', 'DIAMOND', 'SPADE']
# Loop in the above list of value cards using the for loop and len() function.
print("The Deck of cards are:")
for val in range(len(card_values)):
    # Inside the loop, loop again in the above list of sign cards using the for loop
    # and len() function.
    for signval in range(len(crd_signs)):
     # Print the corresponding iterator values .
        print(card_values[val], crd_signs[signval])

Output:

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

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.