0% found this document useful (0 votes)
3 views

day 7 python

This document covers Python dictionaries, including their creation, retrieval, and manipulation. It also discusses nesting lists and dictionaries, providing examples and exercises related to these concepts. Additionally, it outlines a project for building a blind auction program, detailing its functionality and flow.

Uploaded by

Tushar Chandel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

day 7 python

This document covers Python dictionaries, including their creation, retrieval, and manipulation. It also discusses nesting lists and dictionaries, providing examples and exercises related to these concepts. Additionally, it outlines a project for building a blind auction program, detailing its functionality and flow.

Uploaded by

Tushar Chandel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PYTHON

DAY 7

TOPIC – 67
day 9 Goals : what we will make by the end of the day

dicitionary
Demo

________________________________________________________________________________
_
Topic – 68
the python Dicitionary : Deep Dive
A dictionary in Python functions similarly to a dictionary in real life. It's a data structure that allows us to associate a key
to a value and pair the two pieces of data together.
This is how you create a dictionary in Python:

# An example dictionary
colours = {
"apple": "red",
"pear": "green",
"banana": "yellow"
}

This is how you retrieve items from a dictionary:

print(colours["pear"])
#Will print "green"

This is how to create an empty dictionary:

my_empty_dictionary = {}

This is how you can add new items to an existing dictionary:

colours["peach"] = "pink"

This is also how you can edit an existing value in a dictionary:

colours["apple"] = "green"

This is how to loop through a dictionary and print all the keys:

for key in colours:


print(key)

This is how to loop through a dictionary and print all the values:

for key in colours:


print(colours[key])

example

# Creating a dictionary
programming_dictionary = {
"Bug": "An error in a program that prevents the program from running as
expected.",
"Function": "A piece of code that you can easily call over and over again.",
}
# Retrieving a value from a dictionary
print(programming_dictionary["Function"])
# Adding more items to a dictionary
programming_dictionary["Loop"] = "The action of doing something over and over
again."
# Creating an empty dictionary
empty_dictionary = {}
# Wipe an existing dictionary
# programming_dictionary = {}
# print(programming_dictionary)
# Edit an item in a dictionary
programming_dictionary["Bug"] = "A moth in your computer."
# print(programming_dictionary)
# Loop through a dictionary
for key in programming_dictionary:
print(key)
print(programming_dictionary[key])

coading exercise
Grading Program

You have access to a database of student_scores in the format of a dictionary.


The keys in student_scores are the names of the students and the values are
their exam scores.

Write a program that converts their scores to grades.

By the end of your program, you should have a new dictionary


called student_grades that should contain student names as keys and their
assessed grades for values.

The final version of the student_grades dictionary will be checked.

**DO NOT** modify lines 1-7 to change the existing student_scores dictionary.

This is the scoring criteria:


- Scores 91 - 100: Grade = "Outstanding"
- Scores 81 - 90: Grade = "Exceeds Expectations"
- Scores 71 - 80: Grade = "Acceptable"
- Scores 70 or lower: Grade = "Fail"
_________________________________________________________________

Topic -69
nesting Lists and Dicitionaries

You can mix and match various data types to achieve your desired structure.

Nesting a List inside a Dictionary


Instead of a String value assigned to a key, we can replace it with a List. You can nest a List in a Dictionary like this:

my_dictionary = {
key1: [List],
key2: Value,
}

PAUSE 1
See if you can figure out how to print out "Lille" from the nested List called travel_log.

travel_log = {
"France": ["Paris", "Lille", "Dijon"],
"Germany": ["Stuttgart", "Berlin"],
}

Hint 1

To get this part: ["Paris", "Lille", "Dijon"] You would need: travel_log["France"]

Therefore to get Lille, you need: travel_log["France"][1]

Nesting Lists inside other Lists


We've previously seen Nested Lists:

nested_list = ["A", "B", ["C", "D"]]

PAUSE 2
Do you remember how to get items that are nested deeply in a list? Try to print "D" from the list nested_list.
Hint 2

To get this list: ["C", "D"] we need the code:

nested_list[2]

Therefore, to get "D" we need:

nested_list[2][1]

Nesting a Dictionary inside a Dictionary


You can also nest a dictionary in a dictionary:

my_dictionary = {
key1: Value,
key2: {Key: Value, Key: Value},
}

PAUSE 3
Figure out how to print out "Stuttgart" from the following list:

travel_log = {
"France": {
"cities_visited": ["Paris", "Lille", "Dijon"],
"total_visits": 12
},
"Germany": {
"cities_visited": ["Berlin", "Hamburg", "Stuttgart"],
"total_visits": 5
},
}

capitals = {
"France": "Paris",
"Germany": "Berlin",
}
# Nested List in Dictionary
# travel_log = {
# "France": ["Paris", "Lille", "Dijon"],
# "Germany": ["Stuttgart", "Berlin"],
# }
# print Lille
# print(travel_log["France"][1])
nested_list = ["A", "B", ["C", "D"]]
# print(nested_list[2][1])
# Nested dictionary in a dictionary
travel_log = {
"France": {
"cities_visited": ["Paris", "Lille", "Dijon"],
"total_visits": 12
},
"Germany": {
"cities_visited": ["Berlin", "Hamburg", "Stuttgart"],
"total_visits": 5
},
}
print(travel_log["Germany"]["cities_visited"][2])

Topic – 70
The goal is to build a blind auction program.

Demo
https://appbrewery.github.io/python-day9-demo/

Clearing the Output


There are several ways of clearing the output. The easiest is to simply print "\n" many times so that the output scrolls
down many lines.
e.g.

# This will add 20 new lines to the output


print("\n" * 20)

Functionality
•Each person writes their name and bid.
•The program asks if there are others who need to bid. If so, then the computer clears the output (prints several
blank lines) then loops back to asking name and bid.
•Each person's name and bid are saved to a dictionary.
•Once all participants have placed their bid, the program works out who has the highest bid and prints it.

Hint 1

Try writing out a flowchart to plan your program.

Hint 2

The values that come from the input() function are Strings, you'll need to use the int() function to convert it to a number.

Flowchart
If you want to see my flowchart, you can download it here.
START
Show logo from art.py
Ask for Name input
Ask for Bid Price
Add Name and Bid into a dictionary as the
key and value.
Ask if there are other
users who want to bid
Yes
Clear the screen
Find the highest bid
in the dictionary and
declare them as the
winner
No

Copy as Image

Clear Default
Style

from art import logo


print(logo)
def find_highest_bidder(bidding_record):
highest_bid = 0
winner = ""
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}")
bids = {}
continue_bidding = True
while continue_bidding:
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":
continue_bidding = False
find_highest_bidder(bids)
elif should_continue == "yes":
print("\n" * 20)

You might also like