0% found this document useful (0 votes)
7 views34 pages

Lecture 7 Strings, Dictionaries and Sets

This lecture covers programming principles related to strings, dictionaries, and sets in Python. It includes a recap of string methods for testing, searching, and manipulating, as well as an introduction to dictionaries and sets, highlighting their creation and modification. The required reading includes chapters on strings and dictionaries from the textbook 'Starting Out with Python' by Tony Gaddis.

Uploaded by

ashwin68732007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views34 pages

Lecture 7 Strings, Dictionaries and Sets

This lecture covers programming principles related to strings, dictionaries, and sets in Python. It includes a recap of string methods for testing, searching, and manipulating, as well as an introduction to dictionaries and sets, highlighting their creation and modification. The required reading includes chapters on strings and dictionaries from the textbook 'Starting Out with Python' by Tony Gaddis.

Uploaded by

ashwin68732007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CSP1150/CSP5110:

Programming Principles
Lecture 7: Strings, Dictionaries and Sets
This Lecture

• Strings
– Recap of strings
– String methods for testing, searching and manipulating
– Chaining methods
– Strings in other languages

• Dictionaries and sets


– Creating, modifying and working with dictionaries and sets
– Dictionaries and sets in other languages
Textbook

• Starting Out with Python, 3rd Edition


– Tony Gaddis

• Reading the chapter(s) is required


– Read the indicated chapter(s) before class

• This week covers the following textbook chapter(s):


– Chapter 8 – More About Strings
• Entire chapter
– Chapter 9 – Dictionaries and Sets
• Entire chapter
– Note: You may also wish to revise parts of Chapter 7
Strings
Recap of Strings

• Strings are a data type that we’ve used all semester


– A string is a sequence of characters, which can include
letters, numbers, punctuation, symbols, whitespace and
special characters such as “\n”

• We haven’t focused on them in any particular module, but


you should have learnt how to use them quite well by now:
– You can concatenate strings with a “+” to join them together,
e.g. “fullName = firstName + ' ' + lastName”
– You can multiply strings, e.g. “'z' * 5” gives “zzzzz”
– Other types can be converted to strings, e.g. “str(3.14)”
– Input (reading from files or “input()”) is always a string
– Strings can be converted to other types, as long as they
contain an appropriate value, e.g. “float('3.14')”
Recap of Strings

• Strings are sequences (like lists and tuples) so you can…


– Refer to a character by its index, e.g. “title[0]” refers to
the first character in a string variable named “title”
– Slice them, e.g. “title[1:3]” to get 2nd and 3rd character
– Iterate through each character of a string using a “for” loop
– See if a string is in another string, e.g. “'Python' in title”
– Determine the length of a string, e.g. “len(title)”

• Strings are immutable – you cannot actually change them


– Trying to change a character (“name[0] = 'B'”) or delete
(“del name[0]”) a character will raise a TypeError exception
– Concatenation (and functions that appear to modify a string)
actually create a new string and return/use that
– Most of the time, you won’t notice that strings are immutable!
String Methods - Testing

• Numerous string methods exist – i.e. functions that can


be performed on strings or string variables (“string objects”)
– The method call goes after the string – “string.method()”,
e.g. “title.rstrip('\n')” (removes line breaks at end)
– We have covered some of them at various points in the unit

• Some string methods are used to test strings, returning


True or False depending on what the string contains
Method Usage
isupper() Returns True if all letters in the string are in uppercase.
islower() Returns True if all letters in the string are in lowercase.
isspace() Returns True if all characters in the string are whitespace.
isalpha() Returns True if all characters in the string are letters.
isdigit() Returns True if all characters in the string are numbers.
isalnum() Returns True if all characters in the string are letters or numbers.
String Method Examples - Testing
str1 = 'HELLO!' Python

str1.isupper() # are all letters uppercase?


True

str1.isalpha() # is the string all letters?


False

str1.isalnum() # is the string all alphanumeric?


False

# you can use string methods directly on strings, not just variables

'3.14'.isdigit() # is the string all digits?


False

'LotR'.isalpha() # is the string all letters?


True

'CSP1150'.isalnum() # is the string all alphanumeric?


True
String Methods - Searching

• Some string methods are used to search strings, returning


True/False or the index number of where a match is found
Method Usage
startswith(sub) Returns True if the string starts with sub.
endswith(sub) Returns True if the string ends with sub.
count(sub) Returns the number of times that sub appears in string.
find(sub) Returns index num of first instance of sub, or -1 if not found.
index(sub) Works like “find(sub)”, but raises ValueError if sub not found.
replace(old, new) Returns copy of string with all instances of old replaced by new.

– Use “in” if you just want to know if something exists in a string


– Some of these methods have optional start and end
parameters allowing you to search within part of the string
– The “rfind(sub)” and “rindex(sub)” methods also exist,
finding the last instance of sub rather than the first one
String Method Examples - Searching
str1 = 'Testing, testing. One! Two! Three!' Python

str1.startswith('Test') # check if the string starts with 'Test'


True

str1.count('e') # count the number of times that 'e' appears


5

str1.find('o') # return the index of the first 'o' (lowercase O)


27

str1.find('x') # return the index of the first 'x' (there is none)


-1

str1.index('x') # return the index of the first 'x' (there is none)


ValueError: substring not found

str1.replace('!', '!?') # replace all '!' with '!?'


'Testing, testing. One!? Two!? Three!?'

– Remember, strings are case-sensitive - We will examine


how to work around this elegantly in the next few slides!
String Methods – Manipulation

• Some string methods are used to manipulate strings,


performing some operation and returning the result
Method Usage
lower() / upper() Returns the string with all letters in lowercase / uppercase.
Returns the string with any whitespace at the start or end
lstrip() / rstrip()
removed. Can also specify specific characters to remove.
ljust(w) / rjust(w) Returns the string with spaces used to align it to the left, right
center(w) or centre of a line that is w characters wide.
Splits the string up wherever there is a newline character,
splitlines()
and returns all of the bits as a list.
Splits the string up wherever sep appears, and returns all of
split(sep)
the bits as a list. Splits on whitespace if sep not specified.
Returns a string which concatenates the string between
join(iterable)
every item in iterable (e.g. a list).

– “strip()” removes whitespace/characters from both ends


– Many languages have a “substring” function to get part of a
string, but being able to slice strings makes this unnecessary
String Method Examples - Manipulation
str1 = 'Testing, testing. One! Two! Three!' Python

str1.upper() # produce an uppercase version of the string


'TESTING, TESTING. ONE! TWO! THREE!'

str1.center(60) # centre the string in a string of 60 characters


' Testing, testing. One! Two! Three! '

str1.split() # split the string into a list using whitespace


['Testing,', 'testing.', 'One!', 'Two!', 'Three!']

str2 = 'Leonardo\nDonatello\nRaphael\nMichelangelo'

lst = str2.splitlines() # split the string into a list using newline


['Leonardo', 'Donatello', 'Raphael', 'Michelangelo']

str3 = ', '.join(lst) # put ', ' between every item in the list
'Leonardo, Donatello, Raphael, Michelangelo'

str3.split(', ') # split the string into a list using ', '
['Leonardo', 'Donatello', 'Raphael', 'Michelangelo']
“Chaining” Methods

• You can join methods directly to one another, without


storing the result of one in a variable before doing the next

• This simple program prompts for a list of unit codes and


then uses the “split()” method to turn them into a list:
val = input('Enter unit codes with commas between: ') Python
units = val.split(', ')

– Ideally, the user enters the data how you expect it:
Enter unit codes with commas between: CSP1150, CSG1132, CSI1241
units = val.split(', ') # result: ['CSP1150', 'CSG1132', 'CSI1241']

– Realistically, the user enters the data in all sorts of ways:


Enter unit codes with commas between: CSP 1150, CSG 1132 , csi1241,
units = val.split(', ') # result: [' CSP 1150', ' CSG 1132 ', 'csi1241', '']

• (note that there is a space at the start and end of the input)
“Chaining” Methods

Enter unit codes with commas between: CSP 1150, CSG 1132 , csi1241,
units = val.split(', ') # result: [' CSP 1150', ' CSG 1132 ', 'csi1241', '']

– This results in a list with lowercase letters and an extra item

• We’ve covered the methods we need to “sanitise” the data:


– “upper()” to make it all uppercase
– “replace(' ', '')” to get rid of all the spaces
– “strip(',')” to remove any extra commas at the start/end

• Here it is as one statement, with “split(',')” at the end:


val = input('Enter unit codes with commas between: ') Python
units = val.upper().replace(' ', '').strip(',').split(',')
① ② ③ ④ ⑤

① ' CSP 1150, CSG 1132 , csi1241, ' ② ' CSP 1150, CSG 1132 , CSI1241, '
③ 'CSP1150,CSG1132,CSI1241,' ④ 'CSP1150,CSG1132,CSI1241'
⑤ ['CSP1150', 'CSG1132', 'CSI1241']
“Chaining” Methods

• “Chaining” methods in this way works as long as what you


are doing makes sense based on what the methods return:
str1 = ' a b c ' Python

lst = str1.split().upper() # tries to turn a list into uppercase


AttributeError: 'list' object has no attribute 'upper'
lst = str1.upper().split() # uppercase string, then split
['A', 'B', 'C']

• In situations/languages where you are using standalone


functions rather than methods attached to data types, you
“nest” the functions inside each other instead of chaining
– The functions are executed from the innermost one first
$str1 = ' a b c '; PHP
$lst = explode(' ', trim(strtoupper($str1)));
④ ③ ② ①

① ' a b c ' ② ' A B C ' ③ 'A B C' ④ ['A', 'B', 'C']


Strings in Other Languages

• Some languages don’t have many functions and methods


to conveniently manipulate strings
– You can either write code that achieves things “manually”, or
find and import a module that adds the functionality you want

• C++ has a function to capitalise a character, but not a string:


for (int i = 0; i < str.length(); i++) C#
{
str[i] = toupper(str[i]);
}

– Loops through the string and capitalises each character

• Java has no function to count the occurrences of a substring:


String str = "This cat catapult is a catastrophe!"; Java
String subStr = "cat";
int difference = str.length() - str.replace(subStr, "").length();
int occurences = difference / subStr.length();
– Count length of string minus length of it with substring removed
Strings Summary

• Being able to conveniently test, search and manipulate


strings is very important in every programming language
– Some languages have more string manipulation functions /
methods than others, but most have similar capabilities
– Some languages will require a more “manual” approach

• String processing is important even if you are coding a


GUI-driven program or a game – everything involves text!

• Most languages also support “regular expressions”, which


are a way of searching/matching text using a pattern
– “Find 3 letters followed by 4 digits in this string” (a unit code)
– “Does this string contain 1-2 digits, dash or slash, 1-2 digits,
dash or slash, 2-4 digits?” (a date)
Dictionaries
Dictionaries

• A dictionary is another data structure available in Python


– Like lists, they are mutable (you can change the content)
– Dictionaries are often known as “associative arrays”

• The difference between lists and dictionaries is the index


used to identify and refer to the items they contain:
– Items in a list are always identified by a ordered integers
• myList[0], myList[1], myList[2]…

– Items in a dictionary are identified by “keys” that you specify


• A key is usually a string; A “name” or “label” for the value
• Keys are not ordered. The items in a dictionary have no order
– There is no “first” item, “second” item – just different keys
• Items in a dictionary are often referred to as “key-value pairs”
Creating Dictionaries and Referencing Items

• Dictionaries are created using curly braces – “{” and “}”


phones = {'Greg':6282, 'Justin':6174, 'Phil':6427} Python
{'Justin': 6174, 'Phil': 6427, 'Greg': 6282}

faxes = {} # creates empty dictionary (that you can add items to)

– Note that since dictionaries are not ordered, the order that the
items appear in is random/arbitrary – it is not a “sequence”

• Use the key to refer to item in a dictionary (like a list index):


phones['Phil'] Python
6427

phones[0]
KeyError: 0

phones['Brett']
KeyError: 'Brett'

– KeyError is raised if you try to refer to a key that does not exist
Adding, Changing and Removing Dictionary Items

• Dictionaries are mutable, so you can add, change and


remove items from them after they have been created

• Adding and changing dictionary items simply involves


assigning a value to a key – if it exists, value is overwritten:
phones = {'Greg':6282, 'Justin':6174, 'Phil':6427} Python
{'Justin': 6174, 'Phil': 6427, 'Greg': 6282}

phones['Brett'] = 6734 # add a new item


{'Justin': 6174, 'Phil': 6427, 'Greg': 6282, 'Brett': 6734}

phones['Greg'] = 6283 # change an existing item


{'Justin': 6174, 'Phil': 6427, 'Greg': 6283, 'Brett': 6734}

• Use “del” to delete an item from a dictionary, using its key:


del phones['Greg'] # delete an item from the dictionary Python
{'Justin': 6174, 'Phil': 6427, 'Brett': 6734}
Working with Dictionaries

• The “in” comparison operator looks for a key, not a value:


phones = {'Greg':6283, 'Justin':6174, 'Phil':6427} Python
{'Justin': 6174, 'Phil': 6427, 'Greg': 6283}

6174 in phones # there is no key of 6174 in phones


False

'Justin' in phones # there is a key of 'Justin' in phones


True

• “len” tells you how many items are in a dictionary:


len(phones) Python
3

• Dictionaries can have items and keys of different data types:


things = {'pi':3.14, 2:'two', 'lotto':[8, 12, 43, 16]} Python
{2: 'two', 'lotto': [8, 12, 43, 16], 'pi': 3.14}

things[2] # integer key with a string value


'two'

things['lotto'] # string key with a list value


[8, 12, 43, 16]
Loops and Dictionaries

• Looping through a dictionary using “for” returns the keys:


– You can use the key to refer to the value
phones = {'Greg':6283, 'Justin':6174, 'Phil':6427} Python
{'Justin': 6174, 'Phil': 6427, 'Greg': 6283}

for key in phones: for key in phones:


print(key) print(key, '-', phones[key])
Justin Justin - 6174
Phil Phil - 6427
Greg Greg - 6283

• Or use the “items()” method to get both at once:


phones = {'Greg':6283, 'Justin':6174, 'Phil':6427} Python
{'Justin': 6174, 'Phil': 6427, 'Greg': 6283}

for key, value in phones.items():


print(key, '-', value)
Justin - 6174
Phil - 6427
Greg - 6283
Dictionary Methods

• Dictionaries have some useful methods you can use:


Method Usage

keys() Returns a live list of all keys in the dictionary.

values() Returns a live list of all values in the dictionary.

items() Returns a live list of tuples of all key-value pairs in the dictionary.

Returns the value of the specified key, or default if the key


get(key, default)
does not exist (instead of raising a KeyError).

pop(key, default) Like “get()”, but also deletes the item after returning the value.

Returns a list containing a randomly selected key-value pair and


popitem()
then deletes that item from the dictionary.

clear() Removes all items from the dictionary, leaving it empty.


Dictionary Method Examples
phones = {'Greg':6283, 'Justin':6174, 'Phil':6427} Python
{'Justin': 6174, 'Phil': 6427, 'Greg': 6283}

phones.keys() # returns a live list of the keys


dict_keys(['Justin', 'Phil', 'Greg'])

phones.values() # returns a live list of the values


dict_values([6174, 6427, 6283])

phones.items() # returns a live list of tuples of key-value pairs


dict_items([('Justin', 6174), ('Phil', 6427), ('Greg', 6283)])

phones.get('Leisa', 'No such key.') # key not found; returns default


'No such key.'

phones.pop('Greg', 'No such key.') # 'Greg' item removed from list


6283

phones.popitem() # 'Justin' item removed from list (chosen randomly)


('Justin', 6174)

phones.clear() # all items deleted from dictionary


Common Dictionary Usage

• Dictionaries are commonly used to store multiple details


about a thing – the keys make it easy to keep track of things
person = {'fname': 'John', 'sname': 'Smith', 'age': 68} Python

print(person['fname'] + ' ' + person['sname'])


John Smith

if person['age'] >= 65:


print('Retired.')
Retired.

• A list of dictionaries can easily store details about multiple


instances of a thing, which can be looped through, etc:
people = [{'fname': 'John', 'sname': 'Smith', 'age': 68}, Python
{'fname': 'Sue', 'sname': 'Woods', 'age': 29},
{'fname': 'Cory', 'sname': 'Jones', 'age': 47}]

for person in people: # loop through each item in the list


print(person['fname'] + ' ' + person['sname'])
Dictionaries in Other Languages

• Dictionaries are often known as “associative arrays”,


but some languages call them “hash tables” or “maps”
– The syntax used to create and interact with them varies too

• PHP uses “=>” instead of “:” between key and value, and
has a “foreach” loop to iterate through the items:
$phones = array('Greg' => 6283, 'Justin' => 6174, 'Phil' => 6427); PHP
foreach($phones as $key => $value)
{
echo "$key - $value\n";
}

• Java uses Maps / HashMaps, and is quite… inelegant:


Map<String, Integer> phones = new HashMap<String, Integer>(); Java
phones.put("Greg", 6283); // other "put" lines omitted for space

for (String key: phones.keySet())


{
System.out.println(key + " - " + phones.get(key));
}
Dictionaries Summary

• Dictionaries implement a data structure known as an


“associative array” or “hashes” in many languages
– Dictionaries are mutable (they can be changed) and they are
unordered (the items have no “position” in the dictionary)

• Instead of the items being ordered and having an index


number, the items are key-value pairs
– Keys are usually strings that serve to name or label the item
– The keys in a dictionary are unique – trying to add another
item with the same key will just overwrite that item
– The key is what is searched with “in” and returned with “for”

• Dictionary methods exist that provide convenient ways to


access keys and values, as well as manipulating items
– KeyError is raised if you refer to a key that does not exist
Sets
Sets

• A set is another data structure available in Python


– The values in a set must all be unique (different)
– Sets are unordered and the items have no keys or indexes
– Sets are mutable – you can add and remove items
– The values in a set can be of different data types

• Sets are a fundamental concept of mathematics, e.g.


– If set A is {1, 2, 3, 4} and Set B is {2, 4, 6, 8}, then…

• The union of A and B (A∪B) is {1, 2, 3, 4, 6, 8} A B

• The intersection of A and B (A∩B) is {2, 4} A B


Creating and Manipulating Sets

• Sets can be created in two ways – curly braces or “set()”:


set1 = {'Rock', 'Scissors', 'Paper'} # can specify multiple items Python
{'Paper', 'Rock', 'Scissors'}

set2 = set('abracadabra') # can only pass one item to turn into a set
{'b', 'r', 'c', 'd', 'a'}

– Curly braces will create a set if you don’t specify keys, and a
dictionary if you do
– If you use “set()”, you can only pass one value and it will be
broken down into a set of unique values

• “add()”, “remove()” and “update()” manipulate the set:


set1.update(['Lizard', 'Spock']) # add this list to the set Python
{'Paper', 'Rock', 'Scissors', 'Lizard', 'Spock'}

set1.add('Nuke') # add one item to the set


{'Lizard', 'Rock', 'Scissors', 'Nuke', 'Spock', 'Paper'}

set1.remove('Nuke') # remove one item from the set


{'Paper', 'Rock', 'Scissors', 'Lizard', 'Spock'}
Working with Sets & Set Methods

• “len()”, “in” and “for” all work as you would expect

• Set methods exist for common mathematical comparisons:


set1 = {1, 2, 3, 4} Python
set2 = {2, 4, 6, 8}

set1.union(set2) set1 | set2 # combine the sets


{1, 2, 3, 4, 6, 8} {1, 2, 3, 4, 6, 8}

set1.intersection(set2) set1 & set2 # items in both sets


{2, 4} {2, 4}

set1.difference(set2) set1 - set2 # items in set1 not set2


{1, 3} {1, 3}

set1.symmetric_difference(set2) set1 ^ set2 # items not in both sets


{1, 3, 6, 8} {1, 3, 6, 8}

– You can use special operators instead of the methods


– There are also methods/operators to find out
if a set is a subset or superset of another set
Sets Summary

• Sets are unordered collections of unique values


– The items have no indexes or keys
– Based upon the mathematical concept of a set
– Items can be added and removed from sets, and sets can be
compared to each other and individual values in various ways

• Sets are a data structure that many languages do not offer


– They are only useful in fairly specific situations, and most of
their functionality can be achieved using other data structures
– When you need to keep track of a unique collection of values,
sets are a very convenient way of doing so
Conclusion

• String testing, manipulating and searching is important,


and Python gives you quite a few convenient ways to do it

• Dictionaries store items as unordered key-value pairs, and


are often known as “associative arrays” in other languages

• Sets contain an unordered and unindexed collection of


unique values, similar to the mathematical concept of a set

• Multiple variables can reference an object in memory,


which can be confusing when modifying mutable types

You might also like