Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Program to Shuffle Deck of Cards
When it is required to shuffle a deck of cards using Python, the itertools and the random packages need to be used. Random library has a method named shuffle() that can be used to mix up and randomize the data.
Creating and Shuffling a Deck
Below is a demonstration of how to create a standard deck and shuffle it ?
import itertools
import random
# Create a deck of cards using itertools.product
deck = list(itertools.product(range(1, 14), ['Spade', 'Heart', 'Diamond', 'Club']))
print("Original deck created with", len(deck), "cards")
print("The cards are being shuffled...")
# Shuffle the deck in place
random.shuffle(deck)
print("Cards are drawn at random")
print("First 5 cards drawn:")
for i in range(5):
card_value, suit = deck[i]
print(f"{card_value} of {suit}")
Original deck created with 52 cards The cards are being shuffled... Cards are drawn at random First 5 cards drawn: 8 of Heart 12 of Club 3 of Diamond 1 of Spade 7 of Heart
Creating a More Realistic Deck
We can create a more realistic deck with face cards and proper card names ?
import itertools
import random
# Define card values including face cards
values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
suits = ['?', '?', '?', '?']
# Create the deck
deck = list(itertools.product(values, suits))
print("Full deck created:")
print(f"Total cards: {len(deck)}")
# Shuffle the deck
random.shuffle(deck)
print("\nDealing 5 random cards:")
for i in range(5):
value, suit = deck[i]
print(f"{value}{suit}")
Full deck created: Total cards: 52 Dealing 5 random cards: 7? K? 3? Q? A?
How It Works
-
itertools.product()creates all possible combinations of card values and suits -
random.shuffle()shuffles the deck in place, modifying the original list - Each card is represented as a tuple containing (value, suit)
- The shuffled deck can be used to deal cards randomly
Alternative Approach Using Classes
For a more object-oriented approach, you can create a Card class ?
import random
class Card:
def __init__(self, value, suit):
self.value = value
self.suit = suit
def __str__(self):
return f"{self.value} of {self.suit}"
class Deck:
def __init__(self):
values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
suits = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
self.cards = [Card(value, suit) for value in values for suit in suits]
def shuffle(self):
random.shuffle(self.cards)
def deal_card(self):
return self.cards.pop() if self.cards else None
# Create and use the deck
deck = Deck()
deck.shuffle()
print("Dealing 3 cards:")
for _ in range(3):
card = deck.deal_card()
print(card)
Dealing 3 cards: 5 of Hearts K of Diamonds 9 of Clubs
Conclusion
Use itertools.product() to create card combinations and random.shuffle() to randomize the deck. For more complex card games, consider using classes to represent cards and deck operations.
Advertisements
