Dictionary and its function with output in the python

technical talkiess
0

  Dictionary in the python



 Disctionary is nothing but Key and Value pair


dic = {}

 print(type(dic))


The following is the dictionary  in the python

age = {"vishal":"27","usrate":"28","lakhan":"21","polictics":"32"}


 you can also create the mixxed dictionary in the python


age1 = {"vishal":"27","usrate":"28","lakhan":"21","polictics":{"a":"b"}}


now you can access the dictionary value in the python


 print(age1["vishal"])


 If you want to delete the any element to the dictionary you can use the following command in the python


 del age1["vishal"]


 print(age1)

 now you can save all the data in the new variable but one drawback in this code is if you delete the dl element its automatic delete the age1 element in the python

dl = age1


 del dl["vishal"]


 print(age1)



 now prevent the above mistake you can use the copy function to not delete the current dictionary in the python

dl1 = age1.copy()

 print(dl1)


 now you can delete any element but its not delete in the current list in the python


del dl1["vishal"]

 print(age1)


 now you can access the any keys using the get function in the python


 print(dl1.get("usrate"))


 if you want to the add some value in the dictionary in the python 


dl1["test"] = "test"

 print(dl1)


 another way to add the values is  in the python


dl1.update({"ha1":"hi2"})


 print(dl1)


 keys function return the all the keys of the dictionary in the python

 print(dl1.keys())


 the items function return the all the items of the dictionary in the python

 print(dl1.items())


 if you want to clear the all element in the dictionary in the python

 dl1.clear()

 print(dl1)


 forkeys in the python you can assign some anothe key in the list in the python

dl.fromkeys(age1,dl)

 print(dl)


 now you can delete any element in the list  in the python


dl.pop("vishal")

 print(dl)


 if you want to remove the last inserted keys and value pair you can use the popitems function  in the python


dl.popitem()


print(dl)


PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{}

<class 'dict'>

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

  File "c:\python code\dictionaryandfunction.py", line 11

    age = {"vishal":"27","usrate":"28","lakhan":"21","polictics":{"a":"b"}"}

SyntaxError: unterminated string literal (detected at line 11)

  File "c:\python code\dictionaryandfunction.py", line 11

    age1 = {"vishal":"27","usrate":"28","lakhan":"21","polictics":["a":"b"],"}

SyntaxError: unterminated string literal (detected at line 11)

27

27

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

    del dl1("vishal")

        ^^^^^^^^^^^^^

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

{'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

None

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

Traceback (most recent call last):

  File "c:\python code\dictionaryandfunction.py", line 41, in <module>

    print(dl1.get["vishal"])

          ~~~~~~~^^^^^^^^^^

TypeError: 'builtin_function_or_method' object is not subscriptable

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

Traceback (most recent call last):

  File "c:\python code\dictionaryandfunction.py", line 41, in <module>

    dl1.get["vishal"]

    ~~~~~~~^^^^^^^^^^

TypeError: 'builtin_function_or_method' object is not subscriptable

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

Traceback (most recent call last):

  File "c:\python code\dictionaryandfunction.py", line 41, in <module>

          ~~~~~~~^^^^^^^^^^

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'vishal': '27', 'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'usrate': '28', 'lakhan': '21', 'polictics': {'a': 'b'}}

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'usrate': '28', 'lakhan': '21'}

PS C:\python code> & "C:/python 3.11/python.exe" "c:/python code/dictionaryandfunction.py"

{'usrate': '28', 'lakhan': '21'}

PS C:\python code> 

Tags

Post a Comment

0Comments

Thanks you for commenting your questions. I will see question and respond you.

Post a Comment (0)