Python Program to Shuffle Deck of Cards

By | April 12, 2021
Python Program to Shuffle Deck of Cards

Here in this article, we have provided a python program which can shuffle a deck of card.

Prerequisite topics to create this program.

  • Python Loop
  • Python Modules
  • Python Random
  • Python Built-in functions

Steps

  • import the random and itertools in the program.
  • using the itertool module create a list of tuples where each tuple contains the card number and its suits.
  • use the random module to shuffle the list.
  • print the first 5 tuples of the list.

Python Program to Shuffle Deck of Cards:

Code:

Vamware
import itertools, random
# it create a list of tuples
deck = list(itertools.product(range(1,14),['SPADE','HEART','DIAMOND','CLUB']))
random.shuffle(deck) #shuffel
print("Your Cards are:")
for i in range(5):
    print(deck[i][0], "of", deck[i][1])

Output 1:

Your Cards are:
4 of HEART
3 of HEART
5 of CLUB
7 of DIAMOND
12 of CLUB

Output 2:

Your Cards are:
10 of CLUB
6 of HEART
3 of CLUB
4 of CLUB
7 of SPADE

One thought on “Python Program to Shuffle Deck of Cards

Leave a Reply

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