Saving variables with shelve module.

6. Briefly explain saving variables with shelve module.

Answer:

Saving Variables with shelve Module

The shelve module allows storing Python objects (like lists, dictionaries, etc.) in a file.
It works like a dictionary that remembers data even after the program ends.


Example: Saving and Retrieving Data

import shelve

# Save data
with shelve.open('mydata') as shelf:
    shelf['info'] = ['Python', 'Programming', 101]

# Retrieve data
with shelve.open('mydata') as shelf:
    print(shelf['info'])  # Output: ['Python', 'Programming', 101]

This creates a file named mydata.db (and some helper files), where the list is saved and can be retrieved later.

Leave a Reply

Your email address will not be published. Required fields are marked *