Python Program to Shuffle Deck of Cards – 2024

Hey guys, in this blog we will see a Python Program to Shuffle Deck of Cards.

Code

# Python Program to Shuffle Deck of Cards

# importing modules
import itertools, random

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

# shuffle the cards
random.shuffle(deck)

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

Output

You got:
8 of Diamond
3 of Spade
12 of Spade
1 of Heart
6 of Diamond
You got:
9 of Spade
12 of Diamond
4 of Diamond
5 of Spade
10 of Club
You got:
6 of Diamond
10 of Spade
7 of Diamond
11 of Diamond
4 of Club

We have simply created all the combinations of cards possible using the following command.

deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))

And then randomly picking out 5 from them.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 517

Leave a Reply

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