Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

eval() evaluates a string as a Python expression and returns the result.

abs() returns the absolute value of a number.


type() returns the type of an object.
len() returns the length of a string, list, or any other iterable.
range() generates a sequence of numbers.
input() reads input from the user.

Q10. Discuss the advantages of using dictionaries over other data structures in Python.

Dictionaries offer advantages such as fast lookup, easy association between related data, and
flexible structure. They provide efficient retrieval of values based on keys, making them suitable for tasks
like data indexing and mapping.

Q15. Write a Python program that creates an empty dictionary and adds three key-value pairs to it.

my_dict = { }
my_dict['key1'] = 'value1'
my_dict['key2'] = 'value2'
my_dict['key3'] = 'value3'

Q16. Write a Python program that takes a dictionary as input and prints all the keys and their
corresponding values.

my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}


# Iterate over the dictionary
for key in my_dict:
value = my_dict[key]
print("Key:", key, "Value:", value)

Q17. Show how you can change the value associated with a specific key in a dictionary?

To change the value associated with a specific key in a dictionary, you can assign a new value to
that key. For example, my_dict['key'] = new_value will update the value for the key 'key' in the
dictionary my_dict.

Q18. Explain the difference between the pop() and popitem() methods in Python dictionaries.

The pop() method is used to remove a key-value pair from a dictionary based on the specified key. It
returns the value associated with the key. On the other hand, the popitem() method removes the last
key-value pair from the dictionary and returns it as a tuple. This method is useful when the order of
removal doesn't matter.

You might also like