I HAVE NO IDEA OF WHAT THIS IS…

--Originally published at Coding The Future

Picture by Syd Wachs.

Yup... I really don't. Thankfully I was able to find it on my dictionary, and I can now explain to you what a dictionary in Python is.

Remember lists and tuples? Dictionaries are very similar, only that this time, indexes have names, and can hold several types of information within one variable. Little confused? So was I... Here's an example.

Declaring Dictionaries

Let's say I wanted to create a dictionary with all of my personal information, instead of having a single variable for each thing. This is what it would look like:

myInfo = {'Name': 'Emanuel', 'Age': 18, 'University': 'Tec de Monterrey'}

As you can see, declaring a dictionary is quite easy. All I did was to begin with the dictionary's name, and then I assigned values inside of curly brackets. Inside of the brackets, I start by naming the first index Name, and declared the value after the colon. The second index comes after the coma, and if you pay close attention, you'll notice it is not a string, like the first and last index. That's because dictionaries can have several types, unlike lists and tuples.

Therefore, if I wanted to print my name from the dictionary, here's what I would do:

print(myInfo[Name])

Notice how I use the index name and not its number. That's what makes dictionaries different from lists and tuples.

Dictionaries can come in handy when you are dealing with information that can be identified with a name easier than with a number. If you want to learn more ways of how you can use them, be sure to check this tutorial by TutorialsPoint and this one Learn Python The Hard Way –they both really helped me!

That's it for today! Stay tuned for exiting content about bots coming up next !

@emamex98

comments powered by Disqus