How to Make A Deck of Cards With Python Using Object Oriented Programming (OOP)

Python is a fantastic programming language platform that includes OOP benefits. This language mainly uses attributes and methods to define a class that you’ll call later.  In this article, we will be learning about “how to make a deck of cards” with the help of OOP in Python. We will also learn some other cool things you can do with the same programming language. Let’s get right into it.

Table of Contents

  • What are the Building Blocks of OOP?
  • How to make a deck of cards with Python using OOP?
  • Conclusion

What are the Building Blocks of OOP?

Before we move to creating deck of cards, let us have a quick look at the building blocks of object oriented programming:

  • Classes: Classes are data types that the user specifies.
  • Objects: Objects are representations of classes that have been generated with unique data.
  • Attributes: The data that is recorded is referred to as attributes. The Class prototype defines the attributes. 
  • Methods: Methods are used to describe different types of behaviors.

How to make a deck of cards with Python using OOP?

Python certification has a range of advantages over some other programming languages such as Java, C++, and R. It’s a powerful language with various high-level data types. As a result, programming is much quicker than it is for Java or C++. There is no need to declare the variables and arguments used by the coder; thus, Python presents far too many applications in the real world. One of the most interesting of them is to make a deck of cards. It is very easy and fun to make. For making a deck of cards with Python using OOP, follow the given steps:

Step 1: Get your Classes Ready: 

There will be three groups in all. A class Card, a class Player, and a class Deck are all appropriate. These will all be inherited from the object. Each class gets its input method. You can use the code below to do the same.

print (“Hello World”)

Class Card :



Class Deck :



Class Player :



Step 2: Make Your Class Card: 

The card will contain a value self and suit. Now create the attributes suit. Set this value to whatever is sent while making a card. Create a new method for displaying the card. Make a string that will print out the suit and value in the show method. You can use the code below to do the same.

print (“Hello World”)

Class Card:

def_init_(self, suit, val):

self.suit = suit

self.value = val

def show(self) :

print (“{} of {}”.format(slef.value , self.suit))

Creation of card class is done; by creating an instance of a class, we can test this.

card = Card(“Card”,6)

Step 3: Create the Class Deck:

Start by making a 52-card deck including four suits ranging from Ace to King. We begin with an init method that creates a cards attribute with just an empty array that we will add to and a construction method to generate our deck. You can use the code below to do the same.

 class Deck:

      def_init_(self) : = []

We make a build method that includes in self, and also we want to make 52 cards with four suits. We make a for loop, which loops via “suit.” Now inside the 1st  loop, we construct a second for loop that loops through values ranging (1,14).

 class Deck :

def_init_(self) : = [] ()

def build(self):

for s in [“Spades”, “Clubs”, “Diamonds”, “Hearts”] :

for v in range (1 , 14) : , v))

Now we have to show the cards.

def show(self):

 for c in

deck = Deck() 

Step 4: Design a Shuffle Method:

To accomplish this, use the Fisher-Yates shuffle by importing random. After that, we will design a method for shuffling the cards. A loop will be created, which will help us to go from the end of the beginning of the list. You can use the code below to do the same.

import random

def shuffle(self) :

 for i in range (len(,0,-1)

r = random.randint(0 , i)[i]  , self.crads[r] =[r] ,[i]

Call shuffle and display our deck.

deck = Deck()


Step 5: Make a Draw Card Method:

From the top of the deck, the last card will be removed and returned. You can use the code below to do the same.

def drawCard(self) :


deck = Deck()


card = deck.drawCard()

Step 6: Create Class Players:

Make a class to set name and empty list with a name attribute and hand attribute, respectively. You can use the code below to do the same.

class player 

def __init_ (self, name): = name

self.hand = []

def draw(self , deck) :

self. hand. append(deck.drawCard())

return self

def showHand(self) :

for card in self.hand:

card. show()

deck = Deck()


bob = Player(“Bob”)




That was all; by following the above-given steps, you can design and make a deck of cards. Nowadays, coding is in high demand, and we all know how hard it is to learn it. But, with the right companion, anyone can learn how to code and use Python like a pro.

Python programming is much more understandable and straightforward.