Python Dictionaries.ppt
Python Dictionaries.ppt
Ms P Mhlanga
University of Zimbabwe
Department of Computer Science
LISTS
◻ A list in Python is used to store the sequence of
various types of data. Python lists are mutable type
its mean we can modify its element after it created.
However, Python consists of six data-types that are
capable to store the sequences, but the most
common and reliable type is the list.
◻ A list can be defined as a collection of values or
items of different types. The items in the list are
separated with the comma (,) and enclosed with the
square brackets [].
…
◻ A list can be define as below
◻ L1 = ["John", 102, "USA"]
◻ L2 = [1, 2, 3, 4, 5, 6]
…
◻ print(type(L1))
◻ print(type(L2))
Characteristics of lists
◻ The list has the following characteristics:
◻ The lists are ordered.
◻ The element of the list can access by index.
◻ The lists are the mutable type.
◻ The lists are mutable types.
◻ A list can store the number of various elements.
List indexing and splitting
◻ list.(index,object)
◻ Print(list)
Insert() before an element
◻ ist1 = [ 1, 2, 3, 4, 5, 6 ]
◻ # Element to be inserted
◻ element = 13
◻ # Element to be inserted before 3
◻ beforeElement = 3
◻ # Find index
◻ index = list1.index(beforeElement)
◻ # Insert element at beforeElement
◻ list1.insert(index, element)
◻ print(list1)
…
◻ list = [1, 2, 3, 4, 5, 6]
◻ print(list)
◻ # It will assign value to the value to the second inde
x
◻ list[2] = 10
◻ print(list)
◻ # Adding multiple-element
◻ list[1:3] = [89, 78]
..
◻ print(list)
◻ # It will add value at the end of the list
◻ list[-1] = 25
◻ print(list)
…
◻ The list elements can also be deleted by using the
del keyword. Python also provides us the remove()
method if we do not know which element is to be
deleted from the list.
Python lists operations
◻ The concatenation (+) and repetition (*) operators
work in the same way as they were working with the
strings.
◻ Let's see how the list responds to various operators.
Iterating a list
◻ list can be iterated by using a for - in loop. A simple
list containing four strings, which can be iterated as
follows.
◻ list = ["John", "David", "James", "Jonathan"]
◻ for i in list:
Union of sets
◻ The union operation on two sets produces a new set
containing all the distinct elements from both the
sets. In the below example the element “Wed” is
present in both the sets.
◻ Example
◻ DaysA = set(["Mon","Tue","Wed"])
◻ DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
◻ AllDays = DaysA|DaysB
◻ print(AllDays)
Intersection of sets
◻ Intersection of Sets
◻ The intersection operation on two sets produces a
new set containing only the common elements from
both the sets. In the below example the element
“Wed” is present in both the sets.
◻ DaysA = set(["Mon","Tue","Wed"])
◻ DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
◻ AllDays = DaysA & DaysB
◻ print(AllDays)
Python dictionary
◻ Python Dictionary is used to store the data in a key-value pair
format. The dictionary is the data type in Python, which can
simulate the real-life data arrangement where some specific
value exists for some particular key. It is the mutable
data-structure. The dictionary is defined into element Keys and
values.
◻ Keys must be a single element
◻ Value can be any type such as list, tuple, integer, etc.
◻ In other words, we can say that a dictionary is the collection of
key-value pairs where the value can be any Python object. In
contrast, the keys are the immutable Python object, i.e., Numbers,
string, or tuple.
Creating a dictionary
◻ The dictionary can be created by using multiple
key-value pairs enclosed with the curly brackets {}, and
each key is separated from its value by the colon
(:).The syntax to define the dictionary is given below.