DEV Community

Nguyễn Đức Tài
Nguyễn Đức Tài

Posted on

Need a clearly explanation!!!

I have start learning a higher level Python's course in sololearn, but somehow it very confusing me a lot because I'm a selfstudy, especially it was about Dictionary Functions. I expected that someone kind with help me overcome all of this complex code. "smile...!"

Top comments (1)

Collapse
 
claudiogi profile image
ClaudioGi

In Python, "dictionary functions" typically refer to the built-in functions that can be used to manipulate and work with dictionaries. These functions provide various operations and functionalities to perform common tasks on dictionaries. Here are some of the commonly used dictionary functions in Python:

len(): Returns the number of key-value pairs in a dictionary.
keys(): Returns a view object of all the keys in a dictionary.
values(): Returns a view object of all the values in a dictionary.
items(): Returns a view object of all key-value pairs in a dictionary.
get(): Returns the value associated with a specified key. If the key is not found, it can return a default value.
pop(): Removes and returns the value associated with a specified key.
popitem(): Removes and returns a random key-value pair from the dictionary.
clear(): Removes all key-value pairs from the dictionary.
update(): Updates a dictionary with the key-value pairs from another dictionary or an iterable.
copy(): Returns a shallow copy of the dictionary.
Enter fullscreen mode Exit fullscreen mode

NOTICE that in Python an identifier representing a value often comes with the feature of being able to do something with the value or even something fully different but related using functions/methods which a part of what the identifier represents.

Not knowing that an identifier representing a value can be used to run functions/methods can be a huge source of confusion which resolves with getting clarity that in Python all identifier/names/symbols relate to objects and objects can have values, properties and functions/methods not only values.

See following code:

i=6
print(i.bit_length())        # gives 3
print(i.bit_count())         # gives 2
print(i.to_bytes())          # gives b'\x06'
print(i.from_bytes(b'\x16')) # gives 22 
#     ^-- same as int.from_bytes(b'\x16')
assert int.from_bytes(b'\x16') == i.from_bytes(b'\x16')
Enter fullscreen mode Exit fullscreen mode

I have configured the SciTE highlighting for Python so that it changes the text style for common methods which are used in Python like these available for the integer type. Below how the code looks like opened in my SciTE editor:

Image description