Mod2_ppt
Mod2_ppt
LIST
DICTIONARIES AND STRUCTURING DATA
LISTS DATA TYPE
Lists can contain multiple values in an ordered sequence
They can also contain other lists
List values are written within [ ]
List values are comma-delimited
List contains different data types
GETTING INDIVIDUAL VALUES IN A LIST WITH
INDEXES
List indexes are used to access values within a list
The first index is a zero
GETTING INDIVIDUAL VALUES IN A LIST WITH
INDEXES
Getting a List’s
Length with len()
CHANGING VALUES IN A LIST WITH
INDEXES
CHANGING VALUES IN A LIST WITH
INDEXES
LIST CONCATENATION AND LIST
REPLICATION
LIST CONCATENATION AND LIST
REPLICATION
REMOVING VALUES FROM LISTS WITH DEL
STATEMENTS
All of the values in the list after the deleted value will
be moved up one index
WORKING WITH LISTS
TO CREATE LIST
catNames = [ ]
while True:
print('Enter the name of cat ' + str(len(catNames) + 1) +
' (Or enter nothing to stop.):')
name = input()
if name == “:
break
catNames = catNames + [name] # list concatenation
print('The cat names are:')
for name in catNames:
print(' ' + name)
USING FOR LOOP WITH LISTS
in and not in
operators determine
whether a value is
or isn’t in a list
The expressions
with in and not will
evaluate to a
Boolean value
THE IN AND NOT IN OPERATORS
THE MULTIPLE ASSIGNMENT TRICK
To assign multiple
variables with the
values in a list in one
line of code
The number of
variables and the
length of the list must
be exactly equal
Using range(len(listname)
Using enumerate(listname)
USING THE RANDOM.CHOICE() AND
RANDOM.SHUFFLE() FUNCTIONS WITH LISTS
append()
insert()
remove()
sort()
reverse()
INDEX() METHOD
Output:
EXAMPLE PROGRAM: MAGIC 8 BALL WITH
A LIST
Lists
Strings
Tuples
Output:
[1, 2, 3, 'Hello']
THE COPY MODULE’S COPY() AND DEEPCOPY() FUNCTIONS
Values
Keys
CREATING AN EMPTY
DICTIONARY
An empty dictionaryUSING DICT
can be created () dict()
using
fucntion
Elements can be added to an empty dictionary
CREATING AN EMPTY DICTIONARY
USING DICT()
ADDING ELEMENTS TO A DICTIONARY
ADDING ELEMENTS TO A
DICTIONARY
Elements can be added to an empty dictionary or
to an existing dictionary
Syntax:
dictionaryName[Key]
Keys and values should = if
be enclosed in quotes
they are non integers value
ADDING ELEMENTS TO AN EMPTY
DICTIONARY
Note: The order of key – value pair will differ from the order in
which these were created
DICTIONARIES VS. LISTS
Unlike lists, items in dictionaries are unordered.
The first item in a listbnamed spam would be
spam[0]. But there is no “first” item in a
dictionary.
While the order of items matters for determining
whether two lists are the same, it does not
matter in what order the key-value pairs are
typed in a dictionary
CONTD ….
CONTD ….
Dictionaries are not
ordered, they can’t be
sliced like lists.
Trying to access a key
that does not exist in
a dictionary will result
in a KeyError error
message, much like a
list’s “out-of-range”
IndexError error
message.
CONTD ….
THE KEYS(), VALUES(), AND ITEMS()
METHODS
>>> for i in
color
spam.items():
age
print(i)
('color', 'red')
('age', 42)
CONTD . . . .
After:
THE SETDEFAULT() METHOD
Output:
Pretty
Printing