In this tutorial you will learn about the Python Program to Shuffle Deck of Cards and its application with practical example.
In this tutorial, we will learn to create a Python Program to Shuffle Deck of Cards using python Programming.
Before starting with this tutorial we assume that you are best aware of the following Python programming topics:
- Python Operator.
- Basic Input and Output
- Basic Python programming.
- Python Data type.
- Python in-built Functions.
- Loops in Python.
- Python Modules.
- Python Random Module.
Shuffling of Deck
Shuffling is a Method used to shuffle deck of playing cards to provide an element of chance in card games.
Python Program to Shuffle Deck of Cards.
In this program we will Shuffle deck of cards Using Random Module . We would first declared and initialized the required variables. Next, we will shuffle deck of cards. Let have a look at the program.
# Python program to shuffle a deck of card
# importing random module in the program
import itertools, random
# Making a deck of cards
Deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))
# shuffling the cards
# draw random five cards
print("Your's Deck is :")
for i in range(5):
print(Deck[i], "of", Deck[i])
Here we will shows Two output that will Display random values.
In the above program, we have to first declared and initialized required set of variables and initiate in the program.
- Deck = it will hold Different decks.
Explanation of The Above Program Logic.
First of all import all required packages in our program.
Here we use ‘itertools’ package , and also the ‘product()’ method is used to get the deck of cards in form of random list.
This will shuffle list using the ‘shuffle’ method present in the ‘random’ library or in Python library.
The Deck data Shuffled to range 5.
This will be displayed on the screen as shown in above image.
In this our program, we used the “Product()” Method which is present in ‘itertools’ module to create a Deck of cards. Function performs the Cross product of the different sequences of Deck of Cards.