To create a dictionary with initial key-value pairs in Python, you can use curly braces `{}` and specify the key-value pairs within the braces.
We can create a dictionary in Python with initial key-value pairs where the keys are integers and the values are strings. This example demonstrates how to initialize a dictionary with key-value pairs and print its contents.
For example,
my_dict
with integer keys and string values.my_dict = {
1: 'One',
2: 'Two',
3: 'Three'
}
# Print the dictionary contents
print('Dictionary contents:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
Dictionary contents: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three
We can create a dictionary in Python with initial key-value pairs where the keys are strings and the values are doubles (floats). This example demonstrates how to initialize a dictionary with key-value pairs and print its contents.
For example,
my_dict
with string keys and double values.my_dict = {
'A': 1.1,
'B': 2.2,
'C': 3.3
}
# Print the dictionary contents
print('Dictionary contents:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
Dictionary contents: Key: A, Value: 1.1 Key: B, Value: 2.2 Key: C, Value: 3.3
In this tutorial, we learned How to create a Dictionary with Initial Key-Value Pairs in Python language with well detailed examples.