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…