Python Deck of Cards5 Jan 2025 | 6 min read A traditional deck of playing cards has 52 cards total, divided into 4 suits. Each suit has two colours, red and black, and thirteen levels. The four suits are as follows:
Every suit has three face cards: Jack, Queen, and King, in addition to the numbered cards. According to the game's rules, the Ace might be treated as one or eleven. A standard deck is frequently employed for numerous card games, such as poker, blackjack, bridge, etc. Jokers, occasionally used as wild cards or for extra rules in certain games, can also be found in decks. A deck's Jokers can vary in quantity; some have none, while others have one or two. ![]() Let us see the program to print the deck of cards. Program 1:Output: 2 of Hearts 2 of Diamonds 2 of Clubs 2 of Spades 3 of Hearts 3 of Diamonds 3 of Clubs 3 of Spades 4 of Hearts 4 of Diamonds 4 of Clubs 4 of Spades 5 of Hearts 5 of Diamonds 5 of Clubs 5 of Spades 6 of Hearts 6 of Diamonds 6 of Clubs 6 of Spades 7 of Hearts 7 of Diamonds 7 of Clubs 7 of Spades 8 of Hearts 8 of Diamonds 8 of Clubs 8 of Spades 9 of Hearts 9 of Diamonds 9 of Clubs 9 of Spades 10 of Hearts 10 of Diamonds 10 of Clubs 10 of Spades Jack of Hearts Jack of Diamonds Jack of Clubs Jack of Spades Queen of Hearts Queen of Diamonds Queen of Clubs Queen of Spades King of Hearts King of Diamonds King of Clubs King of Spades Ace of Hearts Ace of Diamonds Ace of Clubs Ace of Spades Explanation We have printed the rank with suites using a for loop. Program 2:Output: ![]() Explanation We defined two dictionaries, one for symbols represented with unicode values and the other for numbers. Then, we printed all ranks by iterating through symbols using a for loop. Program 3:Output: ![]() Explanation This program is the same as the previous one, but we defined the suits dictionary using symbols instead of Unicode values. Then, we printed the ranks with suits using a for loop. Program 4:Let us consider a simple program on a deck of cards. Output: [['Club', '1'], ['Club', '2'], ['Club', '3'], ['Club', '4'], ['Club', '5'], ['Club', '6'], ['Club', '7'], ['Club', '8'], ['Club', '9'], ['Club', 'J'], ['Club', 'Q'], ['Club', 'K'], ['Club', 'A'], ['Diamond', '1'], ['Diamond', '2'], ['Diamond', '3'], ['Diamond', '4'], ['Diamond', '5'], ['Diamond', '6'], ['Diamond', '7'], ['Diamond', '8'], ['Diamond', '9'], ['Diamond', 'J'], ['Diamond', 'Q'], ['Diamond', 'K'], ['Diamond', 'A'], ['Heart', '1'], ['Heart', '2'], ['Heart', '3'], ['Heart', '4'], ['Heart', '5'], ['Heart', '6'], ['Heart', '7'], ['Heart', '8'], ['Heart', '9'], ['Heart', 'J'], ['Heart', 'Q'], ['Heart', 'K'], ['Heart', 'A'], ['Spade', '1'], ['Spade', '2'], ['Spade', '3'], ['Spade', '4'], ['Spade', '5'], ['Spade', '6'], ['Spade', '7'], ['Spade', '8'], ['Spade', '9'], ['Spade', 'J'], ['Spade', 'Q'], ['Spade', 'K'], ['Spade', 'A']] Explanation The above program is just 5 lines of code. It is effortless for beginners who want to understand it. Coming to the program code, we just created an empty list with card type and card values, then we paired each card type with card value using a for loop, then extended the empty list called deck and printed the deck of cards. Shuffling the cardsLet us see a program on shuffling cards that creates a deck of cards in mixed order. Program 5:Output: Shuffled Deck: 8 of Diamonds 4 of Spades K of Hearts Q of Spades Q of Hearts 4 of Clubs 10 of Diamonds 9 of Clubs K of Spades 2 of Clubs 4 of Hearts 7 of Hearts J of Spades 2 of Hearts K of Diamonds 6 of Spades 10 of Hearts 5 of Hearts 7 of Clubs 3 of Spades J of Diamonds 3 of Hearts A of Hearts Q of Diamonds A of Diamonds 9 of Spades 8 of Clubs 3 of Clubs 2 of Spades 5 of Spades A of Clubs 9 of Hearts A of Spades K of Clubs 4 of Diamonds J of Clubs 2 of Diamonds 6 of Clubs 10 of Clubs J of Hearts 5 of Diamonds 3 of Diamonds 5 of Clubs Q of Clubs 8 of Spades 9 of Diamonds 8 of Hearts 6 of Diamonds 7 of Spades 10 of Spades 7 of Diamonds 6 of Hearts Explanation Firstly, we have created two 2 lists: one with ranks and the other with suits. We used for loop to pair 13 ranks with 4 suits. Later, we used random.shuffle() method in a way that it takes the input as a list and produces the mixed order of the list. So that we finally get the shuffled deck of cards. ConclusionThis Python program is a basic demonstration of creating, shuffling, and printing a virtual deck of cards. It aims to introduce beginners to Python concepts like lists, dictionaries, functions, and external modules. Python classes allow simulating standard decks of cards and performing various card-related operations. This makes it particularly useful for developing card games or applications that involve manipulating decks of cards. The flexibility of Python enables the creation of custom card types, game rules, and scoring systems, making it a versatile tool for card-based programming. Python's readability and extensive libraries make it an excellent choice for developers of all levels interested in game development. Python has the necessary tools and resources to support your journey into game development. Next TopicHdf5-files-in-python |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India


