Python Programming for Hackers (Part 4) – Dictionaries Concept

April 12, 2016 | Views: 6440

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Python Programming for Hackers (Part 4) – Dictionaries Concept

Well, Welcome All to Part 4. Here, I’ll give you the basic concepts on Dictionaries when it comes to programming in Python.

[For the Video Lecture on this part, check out this link: https://youtu.be/6LFot8r4fCY ]

 

– The Python dictionary data structure provides a hash table that can store any number of Python objects. The dictionary consists of pairs of items that contain a KEY and VALUE. 

– When constructing a dictionary, each KEY is separated from a value by a colon (:), and we separate items by Commas (,).

The complete Code looks like this [Note: Explanation of each line of code is provided below the code]:

>>> services = {‘ftp’:21, ‘ssh’:22, ‘smtp’:25, ‘http’:80}

>>> services.keys()

[‘ftp’, ‘http’, ‘smtp’, ‘ssh’]

>>> services.items()

[(‘ftp’, 21), (‘http’, 80), (‘smtp’, 25), (‘ssh’, 22)]

>>> services.has_key(‘ftp’)

True

>>> services[‘ftp’]

21

>>> print “[+] Found vuln with FTP on port “+str(services[‘ftp’])

[+] Found vuln with FTP on port 21

 

EXPLANATION:

Let’s see first line of code:

services = {‘ftp’:21, ‘ssh’:22, ‘smtp’:25, ‘http’:80}  – Here, we’re creating a dictionary named ‘services‘, and the KEYs are ftp,ssh,smtp,http . . . which are separated from VALUEs by colons. [Note: values are 21,22,25,80] . . . And, these four items are separated by commas.

The second line of code:

services.keys()  – .keys() is method that will return all the keys in dictionary.

The third line of code:

services.items()  – .items() is method that will return the entire list of items in dictionary.

The fourth line of code:

services.has_key(‘ftp’)  – it’s a kind of question like Has Key FTP ? . . . If yes, then the value returns TRUE.

 


Well, this was about creating a Dictionary in Python. References: Violent Python PDF, Cybrary Study Materials and Python.Org

By:  Bijay Acharya

Twitter: @acharya_bijay

Share with Friends
FacebookTwitterGoogle+LinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterGoogle+LinkedInEmail
Ready to share your knowledge and expertise?
10 Comments
  1. can you send me the link of part 2 and 3…..

  2. Thank you for the explanation..it’s very useful

Page 2 of 2«12
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play
 

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel