Day 9 - Python Dictionaries: Deep Dive, Nesting, and The Secret Auction Program


Introduction

Greetings, tech enthusiasts! Welcome back to my 100-day programming journey, where I'm immersing myself in the world of Python. On this remarkable Day 9, we have an exciting agenda in store for you. We'll take a deeper dive into Python dictionaries, unravel their syntax, explore the art of nesting lists and dictionaries, and cap it off with a unique mini-project, "The Secret Auction Program." So, fasten your seatbelts, and let's embark on today's coding adventure!


Unraveling Python Dictionaries: The Syntax

Python dictionaries are your Swiss Army knife when it comes to storing and retrieving data. They are unordered collections of key-value pairs, which makes them incredibly versatile. Let's take a moment to dissect the syntax with some illuminating code snippets:

Syntax: {key : Value}

python_dictionary = {"Bug" : "an error in a program that prevents it form running as expected"}

dict = {"country" : "India"}  # key = country , #value = India

#can create multiple key-value pair inside single dictionary but must be separted
#by comma as shown in below codes

Creating a Dictionary:

my_dict = {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}

Retrieving Items:

name = my_dict['name']  # Retrieves the value associated with the 'name' key.

Adding Items:

my_dict['job'] = 'Programmer'  # Adds a new key-value pair to the dictionary.

Indexing in Dictionaries: Python dictionaries are unordered, so you access elements by their keys rather than numerical indices.


Nesting Lists and Dictionaries

Nesting, the art of placing one data structure inside another, is a powerful concept. Let's explore it further with additional code snippets:

Nesting Dictionary Inside a Dictionary:

person = {
    'name': 'Bob',
    'address': {
        'street': '123 Main St',
        'city': 'Techville',
    }
}

Nesting Dictionary Inside a List:

people = [
    {'name': 'Alice', 'age': 30},
    {'name': 'Bob', 'age': 25},
]

Nesting List Inside a Dictionary:

movie = {
    'title': 'Inception',
    'cast': ['Leonardo DiCaprio', 'Ellen Page', 'Tom Hardy'],
}

The Secret Auction Program

And now, the pièce de résistance - "The Secret Auction Program." In this intriguing mini-project, we challenge you to create a program for conducting secret auctions. Participants submit their names and bid amounts, and the program determines the highest bidder without revealing others' bids.

Here's a simplified version in Python:

from replit import clear
from art import logo
print(logo)

bids = {}
bidding_finished = False

def find_highest_bidder(bidding_record):
  highest_bid = 0
  winner = ""
  # bidding_record = {"Aftab": 123, "Ben": 321}
  for bidder in bidding_record:
    bid_amount = bidding_record[bidder]
    if bid_amount > highest_bid: 
      highest_bid = bid_amount
      winner = bidder
  print(f"The winner is {winner} with a bid of ${highest_bid}")

while not bidding_finished:
  name = input("What is your name?: ")
  price = int(input("What is your bid?: $"))
  bids[name] = price
  should_continue = input("Are there any other bidders? Type 'yes or 'no'.\n")
  if should_continue == "no":
    bidding_finished = True
    find_highest_bidder(bids)
  elif should_continue == "yes":
    clear()


"""
FAQ: My console doesn't clear()

This will happen if you’re using an IDE other than replit.

In Conclusion

Day 9 of my Python programming journey has been a true voyage of discovery. We've delved into Python dictionaries, uncovered their syntax, dabbled in the art of nesting lists and dictionaries, and even crafted a secret auction program. I hope you found this day as exhilarating as I did. Keep that coding spirit high, and stay tuned for more exciting adventures as we continue our 100-day coding expedition. Until next time, happy coding, fellow enthusiasts! 🚀🐍