1 00:00:00,510 --> 00:00:03,280 Hello and welcome to this video. 2 00:00:03,330 --> 00:00:11,550 In this video I'm going to introduce you to the Python data type code dictionary. 3 00:00:11,670 --> 00:00:16,960 What is a Python dictionary. 4 00:00:17,100 --> 00:00:23,090 A real life dictionary holes words and their meanings. 5 00:00:23,100 --> 00:00:34,180 A Python dictionary is similar but it holds key value pairs key value pairs. 6 00:00:34,180 --> 00:00:44,670 Basically mean all the values inside the dictionary will have a unique key and a corresponding value. 7 00:00:44,710 --> 00:00:54,000 For example you could have a key that is called fruit and the value would be in name of a fruit. 8 00:00:54,010 --> 00:00:55,480 For example Apple. 9 00:00:56,020 --> 00:00:59,400 So that's what it means by key value pairs. 10 00:00:59,410 --> 00:01:00,670 So they come in pairs. 11 00:01:00,670 --> 00:01:04,810 You have the key and then you have the values. 12 00:01:04,960 --> 00:01:06,880 The values can be changed. 13 00:01:06,880 --> 00:01:09,700 That means they are mutable. 14 00:01:09,700 --> 00:01:19,330 Also the values must have unique keys so you cannot have duplicate keys for the same values. 15 00:01:19,330 --> 00:01:22,510 The keys are unique. 16 00:01:22,510 --> 00:01:31,000 You can also store mixed data types so you can store Strings and you can also store numbers inside a 17 00:01:31,000 --> 00:01:32,140 dictionary. 18 00:01:32,140 --> 00:01:38,320 You can mix the data types or you can have a specific data type. 19 00:01:38,350 --> 00:01:42,330 There are different ways you can create a dictionary. 20 00:01:42,400 --> 00:01:46,030 So let me walk you through a couple of ways. 21 00:01:46,570 --> 00:01:54,970 I'm going to be using the text editor called atm and I've created an empty file on my desktop called 22 00:01:55,000 --> 00:01:58,840 My dict and I'll call it my dict dot P Y. 23 00:01:59,260 --> 00:02:03,010 So any code right here will be a python file. 24 00:02:03,040 --> 00:02:06,250 First you need to do is give your dictionary a name. 25 00:02:06,280 --> 00:02:10,990 Try to give it a name that reflects what it is you are storing. 26 00:02:10,990 --> 00:02:21,670 So I'm going to call my dictionary my car and then R equals 2 and then I'll add my curly braces on our 27 00:02:21,700 --> 00:02:28,590 top in between to separate the curly braces and I will start adding the key. 28 00:02:28,600 --> 00:02:30,790 So I stuck with the key first. 29 00:02:30,790 --> 00:02:41,770 So the key are typing brand and after the key do a column and I then specify the value. 30 00:02:42,400 --> 00:02:52,970 So my value I'll say Range Rover sports OK. 31 00:02:53,040 --> 00:02:56,370 That is one key value. 32 00:02:56,370 --> 00:02:58,220 This is the key which is unique. 33 00:02:58,410 --> 00:03:01,260 And this is a value the value can change. 34 00:03:01,380 --> 00:03:09,180 But the key has to be unique for each value someone to tap and add another. 35 00:03:09,180 --> 00:03:13,110 So you need to separate each of the key value pairs with a comma. 36 00:03:13,110 --> 00:03:17,880 So had a comma come down here and do a different one. 37 00:03:17,970 --> 00:03:28,920 So add another key this time I'm going to give this a model make this model and I will add the colon 38 00:03:29,760 --> 00:03:42,720 and give it a value the fairly how say h s e the comma now had one more key value this time. 39 00:03:42,710 --> 00:03:46,280 Who's going to make this one the year. 40 00:03:46,490 --> 00:03:48,850 Make it in lower case. 41 00:03:49,020 --> 00:03:57,760 Yeah and then the colon and I'll add the year I'll say 2017. 42 00:03:57,800 --> 00:04:03,720 Notice this is a numerical data type so I don't need to put quotes around it and I don't need to add 43 00:04:03,720 --> 00:04:06,900 a comma because that is the last value. 44 00:04:06,900 --> 00:04:13,340 So no need I'm adding more than I use a comma to separate each key value pair. 45 00:04:13,860 --> 00:04:24,030 So that is basically it for creatine addiction or I've just created a basic dictionary so if I wanted 46 00:04:24,030 --> 00:04:34,560 to print this dictionary all I have to do is type in print and in parentheses I specify the name of 47 00:04:34,560 --> 00:04:43,230 my dictionary which is called my car and I'll save my document to control s to save it and then I'll 48 00:04:43,230 --> 00:04:52,590 run it inside my terminal or command prompt so I've got my command prompt here and I've navigated to 49 00:04:52,590 --> 00:04:57,360 my desktop which is where my Python far less and that's the name of the file. 50 00:04:57,690 --> 00:05:01,140 So if I just press enter it should execute the code. 51 00:05:01,140 --> 00:05:05,480 You can see here is printed out my dictionary. 52 00:05:05,480 --> 00:05:07,080 That is the key. 53 00:05:07,140 --> 00:05:08,270 That's the value. 54 00:05:08,400 --> 00:05:10,870 That's another key that's value. 55 00:05:10,880 --> 00:05:12,000 That's another key. 56 00:05:12,030 --> 00:05:15,330 That is the value. 57 00:05:15,330 --> 00:05:23,220 Another way you can create a dictionary is by using a dictionary constructor function the constructor 58 00:05:23,220 --> 00:05:29,080 is basically a special function that you can use to create an object. 59 00:05:29,190 --> 00:05:36,510 So to create a dictionary using the constructor method you first of all have to give the dictionary 60 00:05:36,510 --> 00:05:36,960 names. 61 00:05:36,960 --> 00:05:46,630 I'm going to call mine my my greens and you do the equals to type in the word dict. 62 00:05:46,780 --> 00:05:54,960 Okay and then in parentheses you then specify the key and then the values. 63 00:05:54,960 --> 00:05:56,580 So this is how you do it. 64 00:05:56,610 --> 00:06:01,730 If that's for typing the key is a bit different when you use a constructor method. 65 00:06:01,980 --> 00:06:09,720 So you use the key and then once you specify the key you are the equals two and then you enclose the 66 00:06:09,810 --> 00:06:12,120 values in quotes. 67 00:06:12,150 --> 00:06:19,020 If it's a string values I'm going to say green apples. 68 00:06:19,480 --> 00:06:24,030 Okay so that is one of the comma. 69 00:06:24,030 --> 00:06:27,140 Now add another key. 70 00:06:27,180 --> 00:06:30,290 The key this time is going to be virtually tables. 71 00:06:31,950 --> 00:06:32,950 Okay. 72 00:06:33,060 --> 00:06:34,910 That's the key. 73 00:06:34,920 --> 00:06:37,200 And our two equals two. 74 00:06:38,250 --> 00:06:40,390 And then I'll add value. 75 00:06:40,470 --> 00:06:42,770 I'm going to enclose that in quotes as well. 76 00:06:43,230 --> 00:06:55,260 And the value will be kale I can add a print function and then print out what's inside this new dictionary 77 00:06:55,260 --> 00:06:56,130 are created. 78 00:06:56,390 --> 00:07:05,540 All I need to do is just pass in the name of the dictionary and I'll save the file and open up my command 79 00:07:05,540 --> 00:07:07,950 prompt. 80 00:07:08,010 --> 00:07:11,960 That's my command prompt so I just type in the name. 81 00:07:12,200 --> 00:07:17,330 You can see here that is my first dictionary. 82 00:07:17,390 --> 00:07:22,860 This is the second dictionary I created using the constructor function. 83 00:07:22,970 --> 00:07:27,770 So that's basically how you create dictionaries. 84 00:07:27,770 --> 00:07:29,540 That's it for this video. 85 00:07:29,540 --> 00:07:30,440 Thanks for watching.