Solved: Creating a Dictionary using built-in function dict%28%29

The main problem with using the built-in function dict%28%29 is that it does not allow for user-defined keys. This means that if you want to create a dictionary that includes custom keys, you will need to use another function or library to do so.


dictionary = dict() 
  
# Add values one by one to the dictionary 
dictionary[0] = 'Geeks'
dictionary[2] = 'For'
dictionary[3] = 1 
  
print(dictionary)

This code creates an empty dictionary and then adds three key-value pairs to it. The first key is 0 and the value is ‘Geeks’. The second key is 2 and the value is ‘For’. The third key is 3 and the value is 1. Finally, the dictionary is printed.

Dictionaries

Python has a standard library that includes dictionaries. A dictionary is a data structure that stores key-value pairs. The keys in a dictionary are the unique identifiers of the items, and the values are the corresponding items.

To create a dictionary in Python, you use the dict() function. The dict() function takes two arguments: the type of data structure you want to create (in this case, a dictionary), and an array of strings representing the keys of your data structure.

Here’s an example that creates a dictionary named mydict with two keys, “name” and “age”:

mydict = dict(name=’John’, age=24)

Built-in functions

Python has a number of built-in functions that you can use in your code. These functions are available in the Python interpreter and can be accessed by typing import functionname at the top of your code.

Some common built-in functions include:

def() – This function defines a new function.

print() – This function prints text to the screen.

range() – This function returns an integer range object.

Related posts:

Leave a Comment