- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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 −
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], "of", my_deck[i])
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
- 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 Articles
- 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
- Golang program to shuffle the elements of an array
- How to shuffle a list of objects in Python?
- Program to find expected number of shuffle required to sort the elements of an array in Python
- Write a Python program to shuffle all the elements in a given series
- One card is drawn from a well shuffled deck of 52 cards. Find the probability of getting a spade.
- Java Program to shuffle an array using list
- Shuffle an Array in Python
- One card is drawn from a well shuffled deck of 52 cards. Find the probability of getting a face card.
- One card is drawn from a well shuffled deck of 52 cards. Find the probability of getting a jack of hearts.
- One card is drawn from a well-shuffled deck of 52 cards. Find the probability of getting the jack of hearts.
- One card is drawn from a well-shuffled deck of 52 cards. Find the probability of getting the queen of diamonds.