1# Dictionaries in Python are used to store set of data like Key: Value pair
2
3# the syntax of a dictionary in Python is very simple we use {} inside that
4 # we define {Key: Value}, to separate multiple values we use','
5programming_dictionary = {
6 "Bug": "An error in a program that prevents the program from running as expected.",
7
8 "Function": "A piece of code that you can easily call over and over again.",
9
10 "Loop": "The action of doing sommething again and again",
11}
12# to retrieve the values from a dictionary we use the Key name as an Index
13# retrieving the Function's definition
14print(programming_dictionary["Function"]) # this will print the definition of Function
15
16# if you wanna print all the entries in the dictionary you can do that by for loop
17for key in programming_dictionary:
18 print(programming_dictionary[key]) # prints all entries
19
20# adding items to a dictionary
21# the following code will add another entry to the dictionary called Variable
22programming_dictionary["Variable"] = "The label to store some sort of data"
23print(programming_dictionary["Variable"])
24
25# editing the values of a key
26# editing the value of variable
27programming_dictionary["Variable"] = "Variables are nothing but reserved memory locations to store values. This means that when you create a variableyou reserve some space in memory"
28
29# if you learnt something from this please upvote it
30
1tel = {'jack': 4098, 'sape': 4139}
2tel['guido'] = 4127
3print(tel)
4# OUTPUT {'jack': 4098, 'sape': 4139, 'guido': 4127}
5print(tel['jack'])
6# OUTPUT 4098
7del tel['sape']
8tel['irv'] = 4127
9print(tel)
10# OUTPUT {'jack': 4098, 'guido': 4127, 'irv': 4127}
11print(list(tel))
12# OUTPUT ['jack', 'guido', 'irv']
13print(sorted(tel))
14# OUTPUT ['guido', 'irv', 'jack']
15print('guido' in tel)
16# OUTPUT True
17print('jack' not in tel)
18# OUTPUT False
1d = {'key1':'value1','key2':'value2'}
2print(d) # to print full dictionary
3l=d.keys
4print(l) # to print keys
5b=d.values
6print(b)#to print values in dictionary
1my_dict = {"key": "value", "a": 1, 2: "b"}
2print(my_dict["key"])
3# Output: value
4print(my_dict["a"])
5# Output: 1
6print(my_dict[2])
7# Output: b
1#A dictionary has key-value pairs. Here 1,2,3 are the keys and Item1,Item2,Item3
2#are their values respectively.
3dictionaryName = { 1: "Item1", 2: "Item2", 3: "Item3"}
4
5#retrieving value of a particular key
6dictionaryName[1]
7
8#retrieving all the keys in a dictionary
9dictionaryName.keys()
10
11#retrieving all the values in a dictionary
12dictionaryName.values()
1# Dictionaries in Python
2
3ages = {"John": 43, "Bob": 24, "Ruth": 76} # Marked by { at beginning and a } at end
4
5# ^^^ Has sets of keys and values, like the 'John' and 43 set. These two values must be seperated by a colon
6
7# ^^^ Sets of values seperated by commas.
8
9
1my_dict = {"Key": "Value"}
2# Dictionary format
3
4print(my_dict["Key"])
5# Prints out "Value"
6
7my_animals = {"Dog": "Percy",
8 "Cat": "Fluffy",
9 "Turtle": "Bennedict"}
10# Multiple items in a dictionary, all separated by a comma
11
12for animal in my_animals:
13 print(my_animals[animal])
14# Prints the names of each of the pets
1# Changing and adding Dictionary Elements
2my_dict = {'name': 'Jack', 'age': 26}
3
4# update value
5my_dict['age'] = 27
6
7#Output: {'age': 27, 'name': 'Jack'}
8print(my_dict)
9
10# add item
11my_dict['address'] = 'Downtown'
12
13# Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}
14print(my_dict)