Creating a Dictionary and accessing elements

Lesson Details:
September 17, 2020

Video Transcription: After learning the basics of dictionaries now we people are going to write the program this video is brought to you by Appy Pie’s Academy, so we will come to know how to make a dictionary and access its elements press the I have already created a project with the name my dictionary press the right mouse button go to new choose Python file and give it any name suppose I am going to give the name nicht 1 okay so this is the name of the Python file so full name will be dick 1 dot py as you know so now let me give the comment what I am going to do in this particular lecture is I am going to show you how to create a dictionary and how to access its elements right so this is what we are going to see in this program so I'm going to take a dictionary the syntax goes like this my dick 1 is equal to within curly braces I am going to give the pairs as you know the pair a pair consists of two things key and its value so 1 colon passes past I intend to write past 1 colon past means key is 1 and value is past so this is the pair key is 1 value is passed okay after coma I have given the second pair the first is key - and second is its value field so key and values they are separated with : okay so here I will show you how to access a particular element so what I will do is I will write print my dick 1 within square bracket I have written 2 so key is 2 and the value against it it's field so when I execute the program we should get the output field I have pressed the right mouse button on the program then click on run oops instead of writing print I have mistakenly written printf again we execute the program okay and this is the output field why because I have chosen the key to my dick one within square bracket two so what is the corresponding value field so field was printed so now I have changed to one so the key is one and it's corresponding value is passed so I will reacts acute my program and let me see the result yeah it's passed because the value of the key one is passed that has been printed so far so good okay now next we will see we can have mixed data types of keys mixed data types of keys so I'm going to give an example I'm going to take another dictionary my dick 2 is equal to within curly braces I'm going to write suppose 10 colon and 10 is the key and after colon I am going to give the corresponding value that is India then comma other pair is going to be given the first thing is key of the second pair first thing key is you : and any corresponding value suppose I am going to write United States of America now as you can see what I want is if users wants to print India user will be supplying the key 10 otherwise user will be supplying the key you so you can see there is a combination of integer and string keys so run this program okay let me use print statement print and here I am going to write my dick to within square brackets let me give you so you key is in the form of string and corresponding values United States of America as you can see the output is quite okay United States of America now let me try it with the integer key that is 10 so for integer key 10 the corresponding value happens to be India output should be India yes the output is right it is India okay so these are the things that you have learnt mixed data types are allowed in dictionaries and always remember dictionaries are immutable and there is no indexing system because dictionaries are lists of unordered elements now I will show you that we can also have a list as a value so it's very easy to prove now what I am going to do is I have taken the key a Colin all World Conference suppose I want to write all World Conference so what I want is when the key a is supplied by the user at the time of using print that is at the time of accessing the message all world conference should be there after comma I have given the key B then Colin after Colin we are supposed to give a value and as you can see this time over the value is a is not a normal value rather it is a list so again see it a for a all world conference for B this whole list hundred 200 300 400 the elements off the items of this list will be printed in the event of choosing B so let me show it to you I am going to write print my dick 3 is the name of the dictionary and within square brackets I have written a so what should be the output it should be all world conference in order to execute the program you can also press control shift f10 ctrl + Shift + f10 as you can see all all world conference the output is correct now I am going to give B the key is B so the whole list hundred 200 300 400 it should be printed I have really cute to the program by pressing ctrl shift f10 and as you can see 100 200 300 400 the value of this list is getting printed.

Course content