Skip to content
Home » Blog » Python Program to Shuffle Deck of Cards

Python Program to Shuffle Deck of Cards

Program to Shuffle Deck of Cards in Python

In this program, you’ll learn how to Shuffle Deck of Cards in Python.

To properly understand this example to shuffle a deck of cards in Python, you should have the knowledge of following Python programming topics:

  • Python for Loop
  • Python Operators
  • Python I/O and Import

Program to shuffle deck of cards

# import modules
import itertools, random

# make a deck of cards
deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))

# shuffle the cards

# draw five cards
print("You got:")
for i in range(5):
   print(deck[i][0], "of", deck[i][1])


You got:
5 of Heart
1 of Heart
8 of Spade
12 of Spade
4 of Spade
shuffle deck of cards
Python Program to shuffle a deck of cards

Note: To get different shuffle cards, run this Program again.

In the above program, we used product() function in itertools module to create a deck of cards. This function performs the Cartesian product of the two sequences.

The two sequences are, numbers from 1 to 13 as we have 13 cards for each phase and the four suits. So, total we have 13 * 4 = 52 items in the deck of cards with each card as a tuple.

For e.g. deck[0] = (1, 'Spade').

Our deck is ordered, so we shuffle it using the function shuffle() in random module.

now after the above steps, we draw the 1st five cards and display it to the user.

Here we have used the standard modules itertools and random that comes with Python.

Related Program:

  1. Program to Find LCM in Python.
  2. Program to Find HCF or GCD in Python.
  3. Program to find the area of Triangle in Python.
  4. Program to Solve Quadratic Equation in Python.
  5. Python Program to insert a new node at the beginning of the Circular Linked List.
  6. Python program to create and display a doubly linked list.
  7. Python Program to remove duplicate elements from a Circular Linked List.
  8. Python Program to find the Square Roots.
  9. Python Program to Shuffle Deck of Cards.
  10. Python program converts decimal number to binary number using recursion.

Ask your questions and clarify your doubts on the Program by commenting. Python Documentation