Python Dictionaries

 Python Dictionaries

What is a Dictionary?

A dictionary in Python is an unordered collection of key-value pairs. It's like a real-world dictionary where you look up a word (key) to find its definition (value).

Python Dictionaries

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

Accessing Values:

You can access values using their corresponding keys:

print(my_dict['name'])  # Output: Alice

Adding or Modifying Values:

my_dict['country'] = 'USA'  # Add a new key-value pair

my_dict['age'] = 31           # Modify an existing value

Common Dictionary Methods:

  • keys(): Returns a list of all keys.
  • values(): Returns a list of all values.
  • items(): Returns a list of key-value 1 pairs as tuples.
  • get(key, default): Returns the value for the key. If the key is not found, returns the default value.
  • pop(key): Removes the key-value pair and returns the value.
  • Comments

    Popular posts from this blog

    Python to automate What's App messages

    Redirecting to another page with button click in Python-flask

    Install WAMP server to run python