๐ ๐๏ธ Python Dictionaries#
๐ What Are Dictionaries?#
Definition: A dictionary in Python is a collection of key-value pairs, defined using curly braces {}
. Keys are unique, and each key maps to a specific value.
Applications in Civil Engineering:
Storing material properties (e.g., density, strength)
Managing site data (e.g., location, project status)
Organizing project information (e.g., budgets, timelines)
๐งฑ Example: Material Properties#
Letโs create a dictionary to store properties of concrete:
# Material properties for concrete
concrete_properties = {
"density": 2400, # in kg/mยณ
"compressive_strength": 30, # in MPa
"tensile_strength": 3.5, # in MPa
}
print(concrete_properties)
{'density': 2400, 'compressive_strength': 30, 'tensile_strength': 3.5}
โ๏ธ Key Features of Dictionaries#
Key-Value Pairs: Organize data for easy access.
Dynamic: Add, modify, or remove key-value pairs as needed.
Efficient: Quick lookups based on keys.
Example: Accessing and Modifying Data#
# Accessing a value
strength = concrete_properties["compressive_strength"]
print(f"Compressive Strength: {strength} MPa")
# Adding a new property
concrete_properties["modulus_of_elasticity"] = 25 # in GPa
print(concrete_properties)
Compressive Strength: 30 MPa
{'density': 2400, 'compressive_strength': 30, 'tensile_strength': 3.5, 'modulus_of_elasticity': 25}
๐ Dictionary Operations#
Common operations for managing dictionaries:
Adding/Updating:
dictionary[key] = value
Removing:
del dictionary[key]
Keys and Values: Use
.keys()
and.values()
to access them.
Example:#
# Removing a property
del concrete_properties["tensile_strength"]
print(concrete_properties)
# Listing all keys
print(concrete_properties.keys())
{'density': 2400, 'compressive_strength': 30, 'modulus_of_elasticity': 25}
dict_keys(['density', 'compressive_strength', 'modulus_of_elasticity'])
๐๏ธ Real-World Application#
Store project data for a construction site:
# Project data
project_data = {
"site_name": "Downtown Tower",
"location": "City Center",
"budget": 5000000, # in USD
"status": "In Progress",
}
print(project_data)
{'site_name': 'Downtown Tower', 'location': 'City Center', 'budget': 5000000, 'status': 'In Progress'}
Example Use Cases:#
Update the status as the project progresses.
Calculate remaining budget by adding new costs.
๐ง Common Dictionary Methods#
Here are a few important methods for working with dictionaries:
.keys()
#
Returns all the keys in the dictionary.
concrete_properties = {"density": 2400, "compressive_strength": 30}
print(
concrete_properties.keys()
) # Output: dict_keys(['density', 'compressive_strength'])
dict_keys(['density', 'compressive_strength'])
.get()
#
Retrieves the value for a specified key, returning a default value if the key is not found.
# Retrieve value with default fallback
modulus = concrete_properties.get("modulus_of_elasticity", "Not Specified")
print(modulus) # Output: Not Specified
Not Specified
โ ๏ธ Limitations of Dictionary Keys#
Keys Must Be Immutable#
Dictionary keys must be immutable types like strings, numbers, or tuples. Mutable types like lists cannot be used.
# This will raise an error
# invalid_dict = {["key"]: "value"} # TypeError
Keys Must Be Unique#
If a key is repeated, the latest value overwrites the previous one.
# Example of overwriting
example = {"a": 1, "a": 2}
print(example) # Output: {'a': 2}
{'a': 2}