Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.1: Dictionaries

2 Replies, 948 Views

[Image: BFMMlbcQvml9HSqXcvNp]

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'}
print(d)

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'}
print(d)

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'}
print(d)

d['Course'] = 'Python'
print(d)

Changing the value of element in this code we will change name from Covid to Kurd

Code:
d = { 'name' : 'Covid','age' : 13, 'location' : 'china'}
print(d)

d['Course'] = 'Python'
print(d)
d['name'] = 'Kurd'
print(d)


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

d = { 'name' : 'Covid','age' : 13, 'location' : 'china'}
print(d)
print(len(d))
print(d.get('age'))
print(d.values())
d.popitem()
print(d)

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"



Love me like you do Heart
Amazing thread, Working with dicts are all fun especially with dynamic languages like Javascript.
Rs
* Thankful to Allah *
Kurdy
(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  Heart
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,607 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,646 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,826 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,591 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,825 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,775 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,805 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,807 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,801 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)