Creation and use of dictionaries in Python

--Originally published at Py(t)hon

This is the last post of the mastery topics, so let’s give everything to it. Dictionaries, they are just another type of data just like lists and tuples. They are indexed by keys, which can be any immutable type, or tuples if they don’t contain any mutable object.

You can create them with: {}, there is also a function dict() inside you put all the elements that you want inside the dictionary.Here is an example of a dictionary:

tel = {'jack': 4098, 'sape': 4139}

Here is a cool video:

That’s all of the semester, i have completed my mastery topics #PUG#Tec#TC101#TheEnd#Dictionaries

 


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 Continue reading "I HAVE NO IDEA OF WHAT THIS IS…"

Dictionaries in Python

--Originally published at Programming

Today we’ll be talking about the Dictionaries in Python. A Dictionary and a List share a lot of things in common, the only difference is that you can get the data out of a Dictionary using almost anything, on the other hand, when using a list, the only way to get the data was by using the value’s position. In other words, a Dictionary is like a database for storing and organizing data.

A Dictionary let’s you use anything, not just numbers, to get the elements of the Dictionary and to modify them. Basically a Dictionary associates one thing to another. Let’s see how a Dictionary is created by associating values to strings.1.png

2.png

As you can see, unlike the list, here we are using strings to get the values we want from the dictionaryExample. But keep in mind that you are allowed to use also numbers just like in a list. But what if we want to delete elements from the dictionary? You need to use the del keyword like this:

3.png

4.png