PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  1. Python For Data Analytics
  2. 1.Python
  3. 3.Python Projects

Distribution of Cards(List & Nested for)

We are going to create a program that will distribute 52 cards of a deck amongst 4 players "Randomly".

Before creating a program we have to create those cards in Python language. In a deck of cards, there is a collection of 52 cards categorized into 2 types (numbers and suits(houses)). numbers= ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] suits= ['Hearts', 'Diamonds', 'Clubs', 'Spades'] The cards we are going to create will be in the form of tuple ex:: ('Ace','club'),('king','spades'),('10',' hearts),('5','diamonds')

  1. In this program, we are going to import a random library so that we can use the shuffle() function to generate randomness in the cards.co

import random
  1. Now we are going to create a deck of 52 cards. For that, we have to take 2 lists. 1st list includes number cards. 2nd list includes suits

suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
  1. Now that we have created the lists. It's time to combine them, make cards, and then form a deck.

deck=[]
for suit in suits:
    for rank in ranks:
      deck.append((rank,suit))
  1. Now is the time to shuffle the deck before the distribution of cards. For that, we will use the shuffle() function of the random library.

random.shuffle(deck)
  1. Now we have to code to create a game that consists of 4 players and we will divide the cards amongst them to start the game.

game=[]
for i in range(4):
    l=[]
    player=input(f'enter the name of {i} player:')
    for card in deck[i::4]:
        l.append(card)
    l.insert(0,player)
    game.append(l)
  1. Now the distribution is completed, random, and equal. we can check the cards of the player (optional) and start the game.

for i in game:        # OPTIONAL
    print(i)          # OPTIONAL
print('LET THE GAME BEGINS')

PreviousAtm-functionalities(nested if)NextGuess the Number Game

Last updated 11 months ago