Module 3 Dic Strings
Module 3 Dic Strings
Module 3
Module 3
When you use the keys(), values(), and items() methods, a for loop can iterate over the keys, values, or
key-value pairs in a dictionary, respectively. Notice that the values in the dict_items value returned by the
items() method are tuples of the key and value.
The keys(), values(), and items() Methods
➢ If you want a true list from one of these methods, pass its list-like return
value to the list() function.
➢ The list(spam.keys()) line takes the dict_keys value returned from keys()
and passes it to list(), which then returns a list value of ['color', 'age'].
Access Dictionary Keys :Value one by one through Loop
➢ You can also use the multiple assignment trick in a for loop to assign the
key and value to separate variables.
➢ We loop through the key-value pairs in a dictionary using *two* iteration
variables
➢ Each iteration, the first variable is the k for key and
the second variable v is the corresponding value for the key
Checking Whether a Key or Value Exists in a Dictionary
➢ The in and not in operators can check whether a value exists in a list.
➢ You can also use these operators to see whether a certain key or value exists
in a dictionary.
➢ Because there is no 'eggs' key in the picnicItems dictionary, the default value 0
is returned by the get() method.
➢ Without using get(), the code would have caused an error message, such as in
the following example:
The setdefault() Method
➢ You’ll often have to set a value in a dictionary for a certain key only if that key
does not already have a value.
➢ The method returns the value 'black' because this is now the value set
for the key 'color’.
>>> girl = {'name': 'Rose', 'age': 33, 'has_hair': True, 'hair_color': 'brown',
'height': 1.6, 'eye_color': 'brown'}
>>> pprint.pprint(girl)
Output:
{'age': 33,
'eye_color': 'brown',
'hair_color': 'brown',
'has_hair': True,
'height': 1.6,
'name': 'Rose'}
Nested Dictionary in Python
• print(people)
Access elements of a Nested Dictionary
• print(people[1]['name'])
• print(people[1]['age'])
• print(people[1]['sex'])
Add element to a Nested Dictionary
people[3] = {}
people[3]['name'] = 'Luna'
people[3]['age'] = '24'
people[3]['sex'] = 'Female'
people[3]['married'] = 'No'
print(people[3])
Delete elements from a Nested Dictionary
del people[3]['married']
del people[4]['married']
print(people[3])
print(people[4])
Delete dictionary from a nested dictionary
\t Tab
>>> 'That is Carol\'s cat.’)
‘That is Carol's cat.’
Raw String
• A raw string completely ignores all escape characters and
prints any backslash that appears in the string.
• Raw strings treat the backslash character (\) as a literal
character.
• place an r before the beginning quotation mark of a string to
make it a raw string.
• Ex 1:
>>> print(r'That is Carol\'s cat.’)
That is Carol\'s cat.
Raw String:
• Raw strings don't treat the backslash as a special character at all. Every
character you put into a raw string stays the way you wrote it:
>>> print 'C:\\nowhere'
This would print following result:
C:\nowhere
Ex 2:
Now let's make use of raw string. We would put expression in
r'expression' as follows:
>>> print r'C:\\nowhere'
This would print following result:
C:\\nowhere
Working with Strings
➢ While you can use the \n escape character to put a newline into a string, it is
often easier to use multiline strings.
➢ A multiline string in Python begins and ends with either three single quotes or
three double quotes.
➢ Any quotes, tabs, or newlines in between the “triple quotes” are considered
part of the string.
➢ Python’s indentation rules for blocks do not apply to lines inside a multiline
string.
Working with Strings
Multiline Comments
While the hash character (#) marks the beginning of a comment for the rest of the
line, a multiline string is often used for comments that span multiple lines.
Working with Strings
➢ Remember to include the f prefix; otherwise, the braces and their contents will
be a part of the string value:
Useful String Methods
upper() Return a new string where all the letters in the original string
have been converted to uppercase
lower() Return a new string where all the letters in the original string
have been converted to lowercase
isupper() Return a Boolean True value if the string has at least one letter
and all the letters are uppercase otherwise returns Boolean False
islower() Return a Boolean True value if the string has at least one letter
and all the letters are lowercase otherwise returns Boolean False
Useful String Methods
The upper(), lower() Methods
➢ The upper() and lower() string methods return a new string where all the
letters in the original string have been converted to uppercase or lowercase,
respectively.
➢ Nonletter characters in the string remain unchanged.
Useful String Methods
Note:
➢ These methods do not change the string itself but return new string values.
➢ If you want to change the original string, you have to call upper() or lower()
on the string and then assign the new string to the variable where the original
was stored.
➢ This is why you must use
spam = spam.upper() to change the string in spam instead of simply
spam.upper().
(This is just like if a variable eggs contains the value 10. Writing eggs + 3 does
not change the value of eggs, but eggs = eggs + 3 does.)
Useful String Methods
➢ Since the upper() and lower() string methods themselves return strings, you
can call string methods on those returned string values as well.
➢ Expressions that do this will look like a chain of method calls.
Useful String Methods