

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 show the data.
Below is a demonstration for the same −
Example
import itertools, random my_deck = list(itertools.product(range(1,11),['Spade','Heart','Diamond','Club'])) print("The cards are being shuffled") random.shuffle(my_deck) print("Cards are drawn at random") print("They are : ") for i in range(5): print(my_deck[i][0], "of", my_deck[i][1])
Output
The cards are being shuffled Cards are drawn at random They are : 1 of Diamond 5 of Diamond 4 of Club 2 of Spade 4 of Heart
Explanation
- The required packages are imported.
- The 'itertools' package is used, and the 'product' method is used to get the deck of cards in a list format.
- This list is shuffled using the 'shuffle' method present in the 'random' library.
- Then, relevant message is displayed.
- The above shuffled data is iterated over.
- This is displayed on the console.
- Related Questions & Answers
- X of a Kind in a Deck of Cards in C++
- Create a grid of cards using Bootstrap 4 .card-deck class
- Program to shuffle string with given indices in Python
- Java Program to Shuffle the Elements of a Collection
- How to shuffle a list of objects in Python?
- Shuffle an Array in Python
- Program to find expected number of shuffle required to sort the elements of an array in Python
- Java Program to shuffle an array using list
- Write a Python program to shuffle all the elements in a given series
- Java Program to display 5 different cards in a CardLayout
- Program to arrange cards so that they can be revealed in ascending order in Python
- Container to create a grid of Bootstrap 4 cards
- Shuffle Array Contents
- C++ code to get minimum sum of cards after discarding
- Shuffle elements of ArrayList with Java Collections
Advertisements