Dictioaries
Dictioaries
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
Dictionaries are used to store data values in key: value pairs.
A dictionary is a collection which is ordered*, changeable and do not
allow duplicates.
As of Python version 3.7, dictionaries are ordered. Dictionaries are
written with curly brackets, and have keys and values:
Example
Print the number of items in the dictionary
print(len(thisdict))
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(type(thisdict))
Example
String,int, Boolean, and list data types:
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
type()
From Python's perspective, dictionaries are defined as
objects with the data type 'dict':
<class 'dict'>
Python Collections (Arrays)
There are four collection data types in the Python
programming language:
•List is a collection which is ordered and changeable.
Allows duplicate members.
•Tuple is a collection which is ordered and
unchangeable. Allows duplicate members.
•Set is a collection which is unordered, unchangeable*,
and unindexed. No duplicate members.
•Dictionary is a collection which is ordered** and
changeable. No duplicate members.
•When choosing a collection type, it is useful to understand the
properties of that type. Choosing the right type for a particular data set
could mean retention of meaning, and, it could mean an increase in
efficiency or security.
Accessing Items
You can access the items of a dictionary by referring to its key name, inside
square brackets:
Example
Get a list of the keys:
x = thisdict.keys()
The list of the keys is a view of the dictionary, meaning that any changes done to the
dictionary will be reflected in the keys list.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.Keys()
car["color"] = "white"
x = car.values()
car["year"] = 2020
x = car.values()
car["color"] = "red"
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
The update() method will update the dictionary with the items from the given argument.
The argument must be a dictionary, or an iterable object with key:value pairs.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
There are several methods to remove items from a
dictionary:
The pop() method removes the item with the
specified key name
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.
class MyClass:
x = 5
Create an object named p1, and print the value of x:
p1 = MyClass()
print(p1.x)
Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Note: The __init__() function is called automatically every time the class is being used to create a new object.
p1 = Person("John", 36)
print(p1)
The __str__() Function
The __str__() function controls what should be returned when the class object is
represented as a string.
If the __str__() function is not set, the string representation of the object is
returned:
Example
The string representation of an object WITHOUT the __str__() function:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1)
The string representation of an object WITH the __str__()
function:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}({self.age})"
p1 = Person("John", 36)
print(p1)
Object Methods
Objects can also contain methods. Methods in objects are functions that belong to the
object.
Let us create a method in the Person class:
Insert a function that prints a greeting, and execute it on the p1 object:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
The self Parameter
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
Note:
It does not have to be The self parameter
named self , you is acan call itto whatever
reference you like,
the current instance but
of the it has
class, and isto betothe
used first
access parameter
variables of any
that belong class .
to thefunction in the class:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Delete Objects
You can delete objects by using the del keyword:
Example
Delete the p1 object:
del p1