Python Dictionary

Python Programming Python Dictionary
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.09
List Price:  €93
You save:  €27.90
£56.02
List Price:  £80.04
You save:  £24.01
CA$96.21
List Price:  CA$137.46
You save:  CA$41.24
A$106.50
List Price:  A$152.16
You save:  A$45.65
S$94.88
List Price:  S$135.55
You save:  S$40.67
HK$547.08
List Price:  HK$781.57
You save:  HK$234.49
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr764.52
List Price:  NOK kr1,092.22
You save:  NOK kr327.70
DKK kr485.54
List Price:  DKK kr693.66
You save:  DKK kr208.12
NZ$116.87
List Price:  NZ$166.97
You save:  NZ$50.09
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,696.46
List Price:  ৳10,995.41
You save:  ৳3,298.95
₹5,845.02
List Price:  ₹8,350.38
You save:  ₹2,505.36
RM331.92
List Price:  RM474.20
You save:  RM142.27
₦98,297.45
List Price:  ₦140,430.95
You save:  ₦42,133.50
₨19,467.44
List Price:  ₨27,811.82
You save:  ₨8,344.38
฿2,586.11
List Price:  ฿3,694.61
You save:  ฿1,108.49
₺2,257.70
List Price:  ₺3,225.42
You save:  ₺967.72
B$355.19
List Price:  B$507.43
You save:  B$152.24
R1,300.44
List Price:  R1,857.85
You save:  R557.41
Лв127.53
List Price:  Лв182.19
You save:  Лв54.66
₩95,343.18
List Price:  ₩136,210.38
You save:  ₩40,867.20
₪259.39
List Price:  ₪370.57
You save:  ₪111.18
₱4,014.38
List Price:  ₱5,735.07
You save:  ₱1,720.69
¥10,876.02
List Price:  ¥15,537.84
You save:  ¥4,661.82
MX$1,183.66
List Price:  MX$1,691.01
You save:  MX$507.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P952.42
List Price:  P1,360.66
You save:  P408.23
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,329.50
List Price:  E£4,756.64
You save:  E£1,427.13
ብር3,992.74
List Price:  ብር5,704.16
You save:  ብር1,711.42
Kz58,583.02
List Price:  Kz83,693.62
You save:  Kz25,110.60
CLP$65,554.03
List Price:  CLP$93,652.63
You save:  CLP$28,098.60
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,071.34
List Price:  RD$5,816.45
You save:  RD$1,745.10
DA9,415.49
List Price:  DA13,451.28
You save:  DA4,035.78
FJ$159.21
List Price:  FJ$227.46
You save:  FJ$68.24
Q544.84
List Price:  Q778.38
You save:  Q233.53
GY$14,671.19
List Price:  GY$20,959.74
You save:  GY$6,288.55
ISK kr9,784.60
List Price:  ISK kr13,978.60
You save:  ISK kr4,194
DH703.12
List Price:  DH1,004.51
You save:  DH301.38
L1,240.96
List Price:  L1,772.88
You save:  L531.91
ден4,012.27
List Price:  ден5,732.06
You save:  ден1,719.79
MOP$564.93
List Price:  MOP$807.08
You save:  MOP$242.14
N$1,294.05
List Price:  N$1,848.73
You save:  N$554.67
C$2,581.34
List Price:  C$3,687.79
You save:  C$1,106.44
रु9,369.68
List Price:  रु13,385.83
You save:  रु4,016.15
S/261.57
List Price:  S/373.68
You save:  S/112.11
K271.48
List Price:  K387.85
You save:  K116.36
SAR262.48
List Price:  SAR374.99
You save:  SAR112.51
ZK1,900.47
List Price:  ZK2,715.07
You save:  ZK814.60
L323.94
List Price:  L462.80
You save:  L138.85
Kč1,630.83
List Price:  Kč2,329.86
You save:  Kč699.02
Ft25,346.53
List Price:  Ft36,210.88
You save:  Ft10,864.35
SEK kr763.26
List Price:  SEK kr1,090.42
You save:  SEK kr327.16
ARS$61,644.55
List Price:  ARS$88,067.42
You save:  ARS$26,422.86
Bs483.68
List Price:  Bs691.01
You save:  Bs207.32
COP$273,058.61
List Price:  COP$390,100.45
You save:  COP$117,041.84
₡35,861.91
List Price:  ₡51,233.50
You save:  ₡15,371.58
L1,732.92
List Price:  L2,475.71
You save:  L742.78
₲523,307.77
List Price:  ₲747,614.58
You save:  ₲224,306.80
$U2,685.15
List Price:  $U3,836.09
You save:  $U1,150.94
zł280.89
List Price:  zł401.29
You save:  zł120.40
Already have an account? Log In

Transcript

Welcome to the Python course. So today I'll be starting with Python dictionary. So what is Python dictionary the dictionary in pythons built in mapping type. So dictionaries map keys to values and these key value pairs provide a useful way to store data in Python. So typically it is used to hold data that are related such as information contained in an ID or a user profile dictionaries are constructed using curly braces on either side. So in least you have used square brackets in tupple, we have used the first bracket single braces and in dictionaries, we'll use the curly braces.

So let us first start creating a dictionary. So first, try and create an empty dictionary. So I'll give it the name D. Then I write just the curly braces and run it software running it I checked the type that is what I created type D what it is showing it is showing dict D ICT dict, which is the aggravation of dictionary. So this curly bracket creates a dictionary. Now I'll create a dictionary in a different way just like list. So in list what we have done is we have created lists with the square brackets.

And also we have created list by using the list function. So here I'll create a dictionary using the date function. So D one suppose equal to dict meter, first bracket now run it and check the type D underscore one. See what it created it created in dictionary. So both way we can create a dictionary by either using these curly braces or by using the function data Now I already told you that dictionaries use keys to store data. So first time here, use a key, or create a key to store a value.

So I'll write d underscore two. And here I am right within this square bracket and give the name of the key. So what name should I give? Let me suppose I'll write one. Okay, so one will have the value, hundred, or you can say, thousand, so I'll give the right thousand. Now, remember one thing before assigning any key or assigning any value to the key, you need to first create a dictionary.

Now I haven't created any dictionary with a name D two. So now if I run this code, I'll get an error. So before assigning a key or before creating a key and assigning a value to that key You first need to define your dictionary, you first need to create your dictionary. So I then create a dictionary with the curly braces or create a dictionary with a dict function. So D two equal to date I'll use a dict function. So here I've created now run this code together, it ran perfectly without an error.

Now print print is d to see what it is showing, it is showing one that is the key which is a value thousand. Now if you want to add a second key with a different value, then what you can do, you can just write d two and now you don't need to define your dictionary again because you have already created it. Now you are just creating the second key This is the second key so a second key should be named as to suppose so two and it should be the value to 500. Okay, now just run it print To now see a second key is created with a value of 2500. So in this way, you can go on adding the elements to the dictionary. Now I'll add a second a third element suppose, so I'll write if you want to add a third element just like right like this.

So I'm give the name obviously three, and it should contain the value 3500. So run it and write, print D two, what it will show it will show 123 c three crease with three different values. Now, we can see a clear similarity with list because we actually separate the elements the same way with the comma. See, we have just separated the element with a comma. So a substantial difference between the two is that if in this case, I would probably perform the action several times it would not order element because the keys have a unique name. So, this keys that I have created have a unique name.

So basically there could never be 2122 or two three there should be only once we can create this case only once with the same name we cannot create a second key with a name one, we cannot create a second key with a name three this names of the keys should be unique. So now suppose I want to change the value of one to suppose 1500. So here I should write d two. One here, just write 1500. Run and print the two See the values is now changed. So one is now changed to 1500.

So in this way you can change the values of dictionary also. Now here I've, what I've done, I've created one key each at a time. Now I create a sequence of keys. So for that, I'll write I'll create with the curly braces. So I'll give an mp3 equal to within these curly braces, I'll write for the keys name. So how to create it for the keys name.

Suppose here I'll write name, okay. And now, I'm right, give a colon over here and give the value of this particular key. So here I will give name suppose I want to give a name called Sam. Give a comma. Now again create a key. Suppose Ah, I want to store so what is the age suppose 26 again, give coma now create one more thing address Eva colon and right suppose downtown Okay.

Now right. So it ran perfectly now printed. So, we have created one more list with a sequence of keys, keys NEMA name, age and address now if you just want to view the name then what you can do so you right here print write your dictionary name d3 and within the square bracket right name, see what it will print, it will print the value of that particular name Sam. Now if you want to see the age, just write print d3 within square bracket within quotes right age what it printed 26 so in this way you can View the values of those keys that you have created in dictionary. Now, we have created the list using these curly braces. Now the same list I'll create with the function.

So default equal to Now using a dig function date within first bracket, again give a curly bracket and then just copy this, I'm just copying this and pasting it over here, copy it, and paste it here. Run it run perfectly without an error print before. See, it again created a dictionary the same way it has created using the curly braces. So in both way, we have created a dictionary one by using curly braces and we're using dict. So next thing that we'll do is so we have called the value of a particular key by using just plain t three and using the name The keyword in the square bracket. Another way of getting a value of a key is by using a get function.

So just write print. And I will use this default dictionary. And then I'll write for dictionary name, dot get. And again, within this first bracket, write the name, so I'm right name of the keys. So I like address and to get that address, so it shows downtown. So in this way, by using a get function, you can call the value of the keys.

And also by just writing the name within a square bracket along with the dictionary name, you can get the values of the key. This is how we can create dictionary how we can call the values of a dictionary. So in the next video, we'll continue with the dictionary part. Till then keep practicing. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.