Tutorial Python 2.1: Dictionaries - Printable Version +- Red Security (https://redsecurity.info/cc) +-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5) +--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9) +--- Thread: Tutorial Python 2.1: Dictionaries (/showthread.php?tid=7903) |
Python 2.1: Dictionaries - Covid-19 - 04-27-2022 Dictionaries: So, now let’s talk about a new concept , similar to lists but a slight difference i.e dictionaries. Dictionaries are data structures like lists. Elements in list have index but in dictionaries elements are accessed by its key not by index. For revise let’s code a list Quote:numbers = [12,4,2,31,4,6,7] Now let’s see how can we make a dictionary in python Code: d = {'name' : 'Covid', 'age' : 13, 'location' : 'china'} The key is on the left side of the colon and value is on the right side. Only immutable objects (Which can’t be changed) can be used as keys i.e integer, boolean, float and strings. Mutable objects can not be used as keys i.e lists. For Example, Look at the below code it will generate an error. Code: d = {['name'] : 'Covid', 'age' : 3, 'location' : 'china'} Now let’s see how can we add more element to our dictionary, Let’s code it Code: d = { 'name' : 'Covid','age' : 13, 'location' : 'china'} Changing the value of element in this code we will change name from Covid to Kurd Code: d = { 'name' : 'Covid','age' : 13, 'location' : 'china'} So till now we have covered the syntax of the dictionary adding elements and more we covered changing values of the dictionary. As in our previous threads we covered some built-in functions for strings and lists. Now we will add some built-in functions for dictionaries. First method is ‘len()’ . Similar to a list it returns the size of the dictionary. get() method Returns the value of the specified key. values() This method return the all values present in a dictionary but not keys popitem() removes the last pair of dictionary Check out the link below for more method on Python Now let’s write a code a apply all those method mentioned above in this code Code: # Code to demonstrate dictionaries methods That’s it for this thread. I'll see you in next thread... "So, Keep Good care of your Health Allah Hafiz and Good Bye Till the next post" "Keep Learning and Keep exploring" RE: Python 2.1: Dictionaries - Mr.Kurd - 04-27-2022 Amazing thread, Working with dicts are all fun especially with dynamic languages like Javascript. RE: Python 2.1: Dictionaries - Covid-19 - 04-28-2022 (04-27-2022, 07:53 PM)Mr.Kurd Wrote: Amazing thread, Working with dicts are all fun especially with dynamic languages like Javascript. Thanks Kurd |