Learn Python Basics Together - Day 4 πŸš€ - Dictionaries

Learn Python Basics Together - Day 4 πŸš€ - Dictionaries by @techcoderlabz on 3Speak

▢️ Watch on 3Speak --- Welcome back to our Python learning series! In today's video, we learn about one of the most useful data structures in Python: βœ… Dictionaries in Python This video is beginner...

Tags: #python #learn #free #forall #basics #coaching #training #students #school #hivesuite

Transcript

Hello, welcome back to my YouTube channel. So let's continue our journey for learning Python on our own. So far, we've covered a couple of basic topics up to over here. Basically, in the last video, we learned about classes and objects. Now, in this video, we're going to talk about dictionaries. modules and packages and input and output so yeah let's get started so first of all let's go ahead and click on this dictionaries which I've already opened for you all in a new tab so dictionaries are a key value pair so over here they have given the example of phone book and you can see that if you want to let's say declare a list you can simply declare a list like this so those are like rectangular brackets but for the for the dictionary, you just have to use curly braces. And once it's done, what you can do is use the rectangular brackets and select a key and then equals set the value that you want to set. For example, over here for the key, I want to say, let's say, let's say, 1, 3, 4, 5, 6, 7, 8, 9. So with this, basically, I'm assigning this number to key saga. Okay, right now in this example, the keys are string, but as stated over here, you can also assign an integer value as a key, and the value can be string also. Dictionary is like, you know, holding a key and a value key can be of any type, and value also can be of any type, any type. So in this example, the other example which I have written over here is the student's example over here, you can see the roll number, and then the value over here, and then the other role number over here, and the value. So if I click on print over here, you will be able to see the output. Okay, so that's one of the example that I've covered, so let me go ahead and add this over here as file number 13, so I'm going to click on file number 13 and save dictionary. DotPI, and then let's just copy paste this fast one over here, and then put a separator over here because we have covered the first topic, we can also run the same program right over here as well, right click, and then over here we have run Python and Terminal. With that, you can see the same output that we saw over here. Okay, now let's go to the next item. Alternatively, a dictionary can be initialized with the same set of values I see. Okay, so over here you can say that dictionary was initialized as an empty dictionary and then in that we basically added number of values in it one by one. Okay, so like declared as empty dictionary and then one by one, first we added John, then Jack and then Jive and then Sager. So let's say if I put the print statement over here, then it would only print two values, and after that it would print four values over here. But at the time of initialization, let's say if you want to assign the bulk value, that also you can do, which I already did over here, you can see for the student's variable, we have assigned the roll number as well as the value. So over here it was empty at first, and then values were added to the dictionary. Over here, we started off with a bunch of values in students. Okay, now iterating over dictionaries. Let's just go through this one. So in over here, let me write it properly so that we can read it. So, yeah, this is one of the things that you should always consider whenever you're writing a code. Code formatting is necessary in order to make the code readable. So whenever you see unformatted code, make sure that you formatted well. In my case, I do that with the help of extension cord. AutoPath 8. This is the extension that I have installed, format or extension for VS4 using AutoPPP8. So now let's say in case of over here, let's say if I want to format this, so command shift P, in case of Mac OS and in case of Windows, I believe it would be ControlSift P. Or let's say if you don't want to remember the shortcut, you just go over here, hit the arrow and then start typing Format Document. and then over here you just have to click on it and then it would format the code. So in case your code, if it is not formatted properly, let's say, for example, I'm going to hit Command Z over here and copy this and put it right over here. So as far as I understand, let's say this one doesn't look like a formatted code. So, Command, see, P, form a document, and you can see that it added spaces and all. So let's say I'm trying to put it like this, and then I'm going to format the document. Okay, one minute, it is still formatted, but I believe not as good as usually the compiler would expect. So I'm intentionally not formatting it well. And let's see how it goes. Command 6C form a document. So you can see that the indentation, it updated automatically. So this is the job of the extension that we have installed, the code formator. So make sure that you have the code formator installed. So no matter what mistakes you do in terms of code formatting, the code formator will take care of that so that no matter who is working on the code base will have the same code format across the team, across the project, and with that, everyone would be able to read the code. Give me a second. Alright, so let's, now that we have clarity on code formatting, let's talk about the iteration. So, for the list, it's very straightforward, you just have to basically use for each look over here, it's more or less same. Let's try this. So over here for the first value, you get the key, for the second value, you get the value. So let's try this on our own. Over here, let's not try the phone book. How about we try printing the students? So let's say number or enrolment number. Okay, and then the next one is, uh, good on. Let me just go ahead and copy this and then keep this one as is. And over here, we add a separator. and we do it for the other one. So, enrollment number, and yeah, the name, all right, in students. So I'm going to remove this and say students. So for each item in student, so for all the items in the students, dictionary, we iterate, and the key would be assigned to endowment number, and the value would be assigned to name. Okay, and over here I'm going to do a special formatting, percentage, or maybe, let's just say, and percentage D or percentage I, so I'm going to use percentage D, percentage as for the string, and over here, let's just go ahead and use an element number, and name over here, and let's try running this one. So now I'm going to hit this button says run Python file in terminal, and with that you can see that an element number, and the name is shown, and with this, we basically understood how to iterate through each item in a dictionary. Let's go to the next point that we have. how do we remove a value from a dictionary? It's straightforward, you provide the key, and if at all, let's say value with that key exists, it's going to delete it. So over here, we've added one value saga, so let's try removing this. So I'm going to go ahead and delete a value over here, delete, and then the name of the dictionary is phone book. From that, I want to remove saga. Okay, now after that let's say I want to print, to make sure that it is deleted or not, or just to confirm, I'm printing the phone book right over here. Now let's try running this one more time and, okay, one minute, the phone book and sour, did I do any mistake? Looks like no, delete phone book and the key, and it says over here at line number 35, there's a key error, okay. Oh yeah, I see the phone book was redefined over here, so this is one mistake, which I did because that key doesn't exist. So now if I run this one more time, then it would work just fine, and then you can see that the Saga key and the value was removed over here. Okay, nice. Next. Oh, the pop also would work fine. So there are two syntax to do that. One of them is the pop syntax. and one of them is delete syntax. I prefer this syntax because this is something similar to JavaScript, which I have used. So, I would stick with this, I'm not going to write this, but yes, I can definitely write a comment over here for your knowledge that you can also try this. Does the same thing. Alright, let's go to the next point, exercise. So this exercise is for you all, go ahead and give it a try. Over here, in the phone book you have three values and then you have to add the Jake and then remove the Jake as well. And of course, this is how you test Jake in the phone book. So this is important thing. I think they added this as a part of exercise, but how do you check if a key is present or not in the dictionary? So let's say I want to check if, let's say, roll number three exists in the list or not. So for that, I have to say if three in students. In that case, oh, I'm so used to the curly brackets over here. If, okay, friend over here, enrollment number three exists, all right? Else, all right, over here, does not exist. All right, let's try running this. If I run this over here, it says it doesn't exist. But at number three, let's say if I put something over here, let's say Anaya over here, and if I try running this now, it would print something else, enrollmentally exists. So this is something you have to understand that if you want to check, because a couple of seconds ago, we tried running this and we encountered an error over here. So whenever you're trying to access a key or a value with the key, make sure that you put it conditionally. So over here, this code is not conditional. So whenever you're trying to access a key, always make sure that you put code in such a way that it is fail-safe. Alright, so over here I'm going to say if Sager in phone book. So if this key is present only in that case, delete it, otherwise don't delete or else, like no. If you try to put it directly, the code may crash as well, so which it did because the key wasn't present in the dictionary. So this is how you make your code fail safe. That's what I am advising you how you should write code. Okay, so with this we've covered dictionaries. I think we have reached good amount of length in this video. So the other things which we wanted to cover, like modules and packages and input and output, we would cover in the next video. So stay tuned. Hit the subscribe button, hit the like button, and follow for more. See you in the next one. Bye-bye.

Comments

@hivesuite.app: Hi @techcoderlabz, sorry β€” your video encoding on HiveSuite failed. File: 02 classes and objects.mp4 Reason: Hive publish exhausted retries: Server selection timed out after 10000 ms What to do Re export the file as MP4 (H.264 + AAC) using a tool like HandBrake (free, all platforms), then upload it again from the HiveSuite composer. Open the composer here β†’ Failed videos and tap Try again on this entry to re publish without re typing the title, tags, beneficiaries or community. Automated message from <a href="

@hivesuite.app: Hi @techcoderlabz, sorry β€” your video encoding on HiveSuite failed. File: 03 dictionaries.mp4 Reason: Hive publish exhausted retries: Server selection timed out after 10000 ms What to do Re export the file as MP4 (H.264 + AAC) using a tool like HandBrake (free, all platforms), then upload it again from the HiveSuite composer. Open the composer here β†’ Failed videos and tap Try again on this entry to re publish without re typing the title, tags, beneficiaries or community. Automated message from <a href="

More videos on 3Speak