How to Pick a Random Card in Python
Last Updated :
28 Apr, 2025
The Python Random module is a built-in Python module, used to create random integers. Because these are pseudo-random numbers, they are not actually random. This module may be used to generate random numbers, print a random value from a list or string, and so on.
To choose a random card from a deck of cards in Python, you must first store all of the cards. Then select a card at random. However, there are 52 cards. it's not a good idea to keep all of the cards in a list one by one. In Python, you must first save all of the cards in a data structure before selecting a random card. So, before we place a card in a data structure, let's look at the various types of cards in a deck of cards.
To Pick a Random Card in Python follow the steps below:
Step 1: Import the random library
import random
Step 2: Store the signs and value of the cards
cards = ["Diamonds", "Spades", "Hearts", "Clubs"]
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
Step 3: Using the random module, choose a random value from both the list and return the value
def pick_a_card():
card = random.choices(cards)
rank = random.choices(ranks)
return(f"The {rank} of {card}")
Step 4: Return and print the value
print(pick_a_card())
Example 1:
Python3
# importing library
import random
# storing the signs and the rank value
cards = ["Diamonds", "Spades", "Hearts", "Clubs"]
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
# random value from both the list
def pick_a_card():
card = random.choices(cards)
rank = random.choices(ranks)
# returning the selected card
return(f"The {rank} of {card}")
# printing the selected card
print(pick_a_card())
Output:
The [3] of ['Hearts']
Example 2:
In this method, instead of printing the suit names, we will print their symbols using Unicode.
Python3
# importing library
import random
def pick_a_card():
# picking random value for suit and rank
suit = random.randint(1, 4)
value = random.randint(1, 13)
# if-elif for selecting suit
if suit == 1:
random_suit = '\u2660'
elif suit == 2:
random_suit = '\u2665'
elif suit == 3:
random_suit = '\u2666'
else:
random_suit = '\u2663'
# if-elif for selecting rank
if value == 1:
random_value = 'Ace'
elif value == 11:
random_value = 'Jack'
elif value == 12:
random_value = 'Queen'
elif value == 13:
random_value = 'King'
else:
random_value = value
# returning the selected card
return(f"The {random_value} of {random_suit}")
# printing the selected card
print(pick_a_card())
Output:
The 4 of ♥
Example 3:
Python3
#importing library
import random
#storing the signs and the rank value
cards = ["♦️", "♠️", "♥️", "♣️"]
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
#random value from both the list and return the value
def pick_a_card():
card = random.choices(cards)
rank = random.choices(ranks)
# returning the selected card
return(f"The {rank} of {card}")
# printing the selected card
print(pick_a_card())
Output:
The ['Queen'] of ['♠️']
Similar Reads
How to make random colors in Python - Turtle? The turtle is a built-in module from the Python library. The turtle module is used to draw interesting shapes or drawings. We can use the turtle module by calling import turtle. The random module is used to generate random numbers. Methods Usedrandint(0,255): It is used to generate numbers between
2 min read
Show Random Picture from a Folder in Python In this article, we are going to share with you the steps to create a simple script that selects and displays a random image from a specified folder using Python. By using the combination of os, random, and PIL libraries, you can easily do this, So, let's get started. Showing Random Picture from a F
3 min read
How to Play Random mp3 from a Folder in Python Playing a random MP3 file from a folder using Python is an exciting and practical task that combines file handling, randomness, and multimedia libraries. In the tutorial, we'll create a simple script that randomly selects and plays an MP3 file from a specified folder. Play Random MP3 from a Folder i
2 min read
How to Print a Deck of Cards in Python This article teaches how to print a deck of cards using Python. The most commonly used deck is a standard 52-card French-suited pack, which is:Widely used in English-speaking nations.The only regularly available pattern in countries like the UK and the US.Composed of 4 suits and 13 ranks, making a t
4 min read
How to create a matrix of random integers in Python ? Prerequisites: numpy To create a matrix of random integers in Python, randint() function of the numpy module is used. This function is used for random sampling i.e. all the numbers generated will be at random and cannot be predicted at hand. Syntax : Â numpy.random.randint(low, high=None, size=None,
2 min read
numpy.random.choice() in Python With the help of choice() method, we can get the random samples of one dimensional array and return the random samples of numpy array. Syntax : numpy.random.choice(a, size=None, replace=True, p=None) Parameters: 1) a - 1-D array of numpy having random samples. 2) size - Output shape of random sample
1 min read
numpy.random.gamma() in Python With the help of numpy.random.gamma() method, we can get the random samples of gamma distribution and return the random samples of numpy array by using this method. gamma distribution Syntax : numpy.random.gamma(shape, scale=1.0, size=None) Return : Return the random samples of numpy array. Example
1 min read
How to generate a random phone number using Python? In this article, we will learn how to generate a random phone number using Python. In general, Indian phone numbers are of 10 digits and start with 9, 8, 7, or 6. Approach: We will use the random library to generate random numbers.The number should contain 10 digits.The first digit should start with
1 min read
How to Select a Random Element from a Tuple in Python Selecting a random element consists of retrieving one element from a collection in an unpredictable manner. In Python, this can be done easily using different methods provided by the random module. Below are the three different approaches to select a random element from a tuple. Select a Random Elem
2 min read
How to get weighted random choice in Python? Weighted random choices mean selecting random elements from a list or an array by the probability of that element. We can assign a probability to each element and according to that element(s) will be selected. By this, we can select one or more than one element from the list, And it can be achieved
3 min read