Python Dictionaries #
In Python, a dictionary is a collection of key-value pairs. Each key in a dictionary must be unique and can be of any hashable type (strings, numbers, tuples, etc.). The values can be of any type (strings, numbers, lists, dictionaries, etc.).
You can create a dictionary using curly braces {} and separating key-value pairs using a colon : and separate each pair by a comma ,. Here's an example:
my_dict = {
"name": "John",
"age": 25,
"city": "New York",
}
In the example above, we created a variable my_dict with key-values as per the table below:
| key | value |
|---|---|
| name | "John" |
| age | 25 |
| city | "New York" |
Exercise #
- Create a variable
studentas a dictionary with:- name as a key with a string value
- age as a key with an integer value
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules