COMP1005 Lecture 2.pdf
Document Details
![LuckiestIambicPentameter](https://quizgecko.com/images/avatars/avatar-5.webp)
Uploaded by LuckiestIambicPentameter
Curtin University
Tags
Full Transcript
Lecture 2 Strings and Lists COMP1005/COMP5005 Fundamentals of Programming Discipline of Computing, Curtin University Strings Style Indexing Strings Building and operating on strings and Lists Lists Slicing ...
Lecture 2 Strings and Lists COMP1005/COMP5005 Fundamentals of Programming Discipline of Computing, Curtin University Strings Style Indexing Strings Building and operating on strings and Lists Lists Slicing (Pseudo) random numbers Monte Carlo techniques Copyright warning COMMONWEALTH OF AUSTRALIA Copyright Regulation 1969 WARNING This material has been copied and communicated to you by or on behalf of Curtin University of Technology pursuant to Part VB of the Copyright Act 1968 (the Act) The material in this communication may be subject to copyright under the Act. Any further copying or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice Learning outcomes Define and use more complex datatypes (strings and lists) and variations on control structures Use slicing and indexing to access elements in a list Use a supplied Python package to provide random number options Understand and implement simple Monte Carlo algorithms Strings Defining strings Escaping characters Strings Special characters Long strings Very long strings Length of strings Strings We have already seen strings in many of the previous examples: print(‘TICKET MACHINE’) Strings are a sequence of characters Some characters are special - \n is a new line print(‘\nTICKET MACHINE\n’) Characters are alphabetical (upper and lower), numbers, symbols and spaces The order of the characters matters, so a string is referred to as a sequence Defining strings Quotes indicate 42 is a string, not a number '42' - '42' is the character '4', followed by '2' int('42') is a number (1010102) A string is defined using matching quotation marks "String 1" 'String 2' If we mix the quotations marks, we get a syntax error $ python stringex.py File "stringex.py", line 3 string3 = 'String 3" ^ SyntaxError: EOL while scanning string literal Escaping characters If we need our string to contain a quotation mark or an apostrophe, we can “escape” it with a “\” grail = 'It\'s just a flesh wound' brian = 'Now you listen here! He\'s not the Messiah. He\'s a very naughty boy!' Or we can use double quotes outside and singles inside (plus continuation char "\" to wrap lines): walks = "I'm sorry to have kept you waiting, " \ "but I'm afraid my walk has become " \ "rather sillier lately.” print(grail + "\n" + brian + '\n' + walks) Note: MS Powerpoint and Word will change the quote characters to “” ‘’ instead of ""'' – these will not be recognised by Python Special characters A few times we've used \n to insert a new line into a string Newlines are not the only special characters we might want to use Tabs \t can be useful for formatting, backspaces If we want to print "\n", we need to escape the escape character: print('Use \\n for newline:\\n...') print('Use \\t for tab:\\t!') Long strings Python style suggests limiting each line of code to 79 characters This lets you have multiple windows open at the same time with every line fully visible To have a long string across multiple lines, split the string and add a \ walks = "I'm sorry to have kept you waiting, " \ "but I'm afraid my walk has become " \ "rather sillier lately.” Style guide advises to line up the opening quotes on each line If you're inside brackets, no need for \ print("Now you listen here! He's not the Messiah. " "He\'s a very naughty boy!") Very long strings Use triple quotes to create a very long string, wrapping across multiple lines: parrot = """This parrot is no more. It has ceased to be. It's expired and gone to meet its maker. \nThis is a late parrot. \nIt's a stiff. Bereft of life, it rests in peace. If you hadn't nailed it to the perch, it would be pushing up the daisies. It's rung down the curtain and joined the choir invisible. \nThis is an ex-parrot.""" print(parrot) There is no need to escape the apostrophies within triple quotes – much easier to maintain Often used for documentation – e.g. docstrings Length of strings Every string has a function len() to get the string length The len() function counts all the characters, including spaces, newlines and tabs, to get the length ni = 'Ni!' print('Length of string is: ', len(ni)) Length of string is: 3 print('Length of string is: ', len(parrot)) Length of string is: 315 Python style Style guide Style Style in this unit Style beyond this unit Python style Python is a community development, with "Python Enhancement Proposals" or "PEP"s used to define and pitch for changes/standards PEP-8 provides a style guide, which we will be using in this unit https://www.python.org/dev/peps/pep-0008/ These are not rules, but guidelines to help with consistency and readability Guido says: "Code is read much more often than it is written". PEP-20 (Zen of Python) says: "Readability counts." Style in this unit You will need to read and modify your code, and we will need to read and assess your code Readability counts Follow PEP-8 throughout this unit We will write a README file for each practical, test and for the assignment We will also require comments at the start of each program, e.g. - or use triple quotes/docstrings # # Author : Michael Palin # ID : 12345678 # # numbers2.py - Read in a list of numbers (negative to # exit) and give the sum of the numbers # # Revisions: 8/3/2017 – fixed style to comply with PEP-8 # : 2/3/2017 – created # Style beyond this unit After this unit, you may be part of a project that uses a different style. When in Rome, do as the Romans do…...follow the project style. PEP-8: "A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important." When writing your own code, you may have to define your own style – PEP-8 is an excellent starting point Indexing Accessing individual characters Indexing For loops & range() function Indexing with negative numbers Indexing As a sequence, we can assign a number to each element in a string: witches = 'Now, why do witches burn?’ N o w , w h y d o w i t c h e s b u r n ? 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Counting starts at zero Escaped characters only count as one char blackknight = 'It\'s just a flesh wound.' I t ' s j u s t a f l e s h w o u n d. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Accessing individual characters Once we have numbers assigned to each character position, we can pick out individual characters Element zero is the first letter "I" blackknight is "I" blackknight is "'" blackknight is " " blackknight is "." I t ' s j u s t a f l e s h w o u n d. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Accessing individual characters Using the indexes, we can access characters within a string print(blackknight) w Putting this together with the string length, a for loop and a range() function: for index in range(len(blackknight)): print(blackknight[index]) I t ' s j u s t For loops & range() function - subrange Range can give a sequence of numbers in a range going forward, backward or skipping… range([start] ,stop, [step]) (start and step are [optional], defaulting to 0 and 1 respectively) for index in range(12, len(blackknight)): print(blackknight[index]) f l e s h w o u n d ! For loops & range() function - reverse Range can give a sequence of numbers in a range going forward, backward or skipping… range([start] ,stop, [step]) for index in range(len(blackknight)-1, -1, -1): print(blackknight[index]) ! d n u o w h s For loops & range() function - skip Range can give a sequence of numbers in a range going forward, backward or skipping… range([start] ,stop, [step]) for index in range(0, len(blackknight), 2): print(blackknight[index]) I ' u t a f e For loops & range() function – reverse & skip Range can give a sequence of numbers in a range going forward, backward or skipping… range([start] ,stop, [step]) for index in range(len(blackknight)-1, -1, -3): print(blackknight[index]) ! u e t j ' Using negative numbers Sometimes it's useful to work back from the end of the string This is done using negative numbers Element 10 is 'e' and is also element -1 johncleese[-1] is "e" johncleese[-7] is " " johncleese[-11] is "J" J o h n C l e e s e 0 1 2 3 4 5 6 7 8 9 10 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 Another example In blackknight, element 23 is '.' and is also element -1 blackknight[-1] is "." blackknight[-5] is "o" blackknight is " " blackknight is "." I t ' s j u s t a f l e s h w o u n d. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 Building strings Building and Printing strings operating on Working with strings strings String methods Building strings The main operator for string expressions is "+" or concatenate Concatenate adds the strings together one after the other – no spaces name = 'John' + 'Cleese' …will give us 'JohnCleese' name = 'John' + ' ' + 'Cleese' …will give 'John Cleese' Printing strings - separators When printing multiple strings we usually want a space between them Thus the default separator is ' ‘ If we want a different character printed between each string, we can use sep= print(eric, graham, terry) Eric Idle Graham Chapman Terry Gilliam print(eric, graham, terry, sep='*') Eric Idle*Graham Chapman*Terry Gilliam print(eric, graham, terry, sep='') Eric IdleGraham ChapmanTerry Gilliam print(eric, graham, terry, sep=',') Eric Idle,Graham Chapman,Terry Gilliam Printing strings - ends If we want a character printed at the end of each line, we use end= Default separator is '\n' Handy for keeping lines together when printing in loops for index in range(len(blackknight)-1, -1, -1): print(blackknight[index], end=' ') ! d n u o w h s e l f a t s u j s ' t I for index in range(len(blackknight)-1, -1, -1): print(blackknight[index], end='') !dnuow hself a tsuj s'tI Working with strings Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal to x, else True s+t the concatenation of s and t s * n or n * s equivalent to adding s to itself n times len(s) length of s min(s) smallest item of s max(s) largest item of s index of the first occurrence of x in s s.index(x[, i[, j]]) (at or after index i and before index j) s.count(x) total number of occurrences of x in s Working with strings - examples menuitem1 = 'Spam, egg, spam, spam, bacon and spam' print(min(menuitem1)) print(max(menuitem1)) Output: Min value: Max value: s Working with strings - examples spam = 'spam, ' menuitem2 = 'Spam, ' + spam*6 + 'baked beans, ' + spam*2 + 'spam and spam' print(menuitem2) print(menuitem2.count('spam')) print(menuitem2.count(',')) print(menuitem2.index('spam')) print(menuitem2.index('spam',10, 20) Output: Spam, spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam and spam Spam count: 10 Comma count: 10 Spam at: 6 Spam at: 12 String methods Method Result s.upper() Returns a copy of s with all elements converted to uppercase s.lower() Returns a copy of s with all elements converted to lowercase s.startswith(pre) Returns True if s starts with pre s.endswith(post) Returns True if s ends with post Returns a copy of the string with [the first count] occurrences of old s.replace(old, new[, count]) replaced with new Return a copy of the string with leading and trailing whitespace removed s.strip() (spaces, tabs etc) s.isnumeric() Return True if string has only numeric chars Working with strings - examples spam = 'Spam' print(spam.upper()) print(spam.lower()) if spam.startswith('Sp'): print(spam + ' Starts with: ' + 'Sp') print(menuitem2.replace('spam','egg')) Output: SPAM spam Spam Starts with: Sp Spam, egg, egg, egg, egg, egg, egg, baked beans, egg, egg, egg and egg Lists Using lists Lists Lists within lists From strings to lists (of strings) Lists If you need to keep a collection of data in one place, one option is to put it into a list Lists can contain numbers, strings, other lists, or a combination Items in a list are kept in order –> a sequence You can access elements in a list with an index (like we saw with strings) You can change, delete, or add to the items in a list at any point Lists are flexible and dynamic – helping to make it easy to handle data in Python Lists python = ['John', 'Michael', 'Terry', 'Graham', 'Eric', 'Terry'] # duplicates are ok + wrapping inside brackets print(python) # is Michael python = 'Terry Jones' # update the value in pos #2 python = 'Terry Gilliam' # update the value in pos #5 del python # deletes 'Graham', Eric is now #3 del python # deletes 'Terry Jones', Eric is now #2 print(len(python)) # length is now 4 python.append('Graham') # adds Graham at end python.append('Terry Jones') # pining for the fjords Using lists for monty in python: # another for loop, the for-each print('Legend: ', monty) # gives us an element at a time Legend: John Legend: Michael Legend: Eric Legend: Terry Gilliam Legend: Graham Legend: Terry Jones x = [1, 2, 3, 4] # can hold numbers, or a mix y = [5, 6, 7, 8] z = x + y # concatenate two lists print(z) [1, 2, 3, 4, 5, 6, 7, 8] Lists within lists menu = [['egg', 'bacon'], ['egg', 'sausage', 'bacon'], ['egg', 'spam'], ['egg', 'bacon', 'spam'], ['egg', 'bacon', 'sausage', 'spam'], ['spam', 'bacon', 'sausage', 'spam'], ['spam', 'egg', 'spam', 'spam', 'bacon', 'spam'], ['spam', 'sausage', 'spam', 'spam', 'bacon', \ 'spam', 'tomato', 'spam']] print(menu) ['egg', 'bacon', 'sausage', 'spam'] Lists within lists menu = [['egg', 'bacon'], ['egg', 'sausage', 'bacon'], ['egg', 'spam'], ['egg', 'bacon', 'spam'], ['egg', 'bacon', 'sausage', 'spam'], ['spam', 'bacon', 'sausage', 'spam'], ['spam', 'egg', 'spam', 'spam', 'bacon', 'spam'], ['spam', 'sausage', 'spam', 'spam', 'bacon', \ 'spam', 'tomato', 'spam']] print(menu) sausage From strings to lists (of strings) A common and regular task is to split a strings into pieces, based on a delimiter. The split method returns a list of strings Default delimiter is " " (space) Very useful when working with comma separated files ingredients = menuitem2.split(',') print(ingredients) ['Spam', ' spam', ' spam', ' spam', ' spam', ' spam', ' spam', ' baked beans', ' spam', ' spam', ' spam and spam'] ingredients = menuitem2.split(', ') print(ingredients) ['Spam', 'spam', 'spam', 'spam', 'spam', 'spam', 'spam', 'baked beans', 'spam', 'spam', 'spam and spam'] Slicing Slicing Using step and negative indexing Indices Slicing We can slice strings and lists to access parts of them. Similar to how we could use start, stop and step with the range function… mystring[[start]: [stop]: [step]] name = 'John' + ' ' + 'Cleese' name[5:10] => 'Clees’ If any of start/stop/step are omitted, they default to 0, size and 1 respectively name[:4] => 'John ' name[4:] => ' Cleese' J o h n C l e e s e 0 1 2 3 4 5 6 7 8 9 10 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 Step and negative indexing name = 'John' + ' ' + 'Cleese' name[:-2] => 'John Clee' name[-6:-2] => 'Clee' name[0:-1:2] => 'Jh le' name[-1:0:-2] => 'eel h' name[:4:3] => 'Jn' name[4::3] => ' ee' J o h n C l e e s e 0 1 2 3 4 5 6 7 8 9 10 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 Indices One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. +---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1. The first row of numbers gives the position of the indices 0...6 in the string; the second row gives the negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively. For non-negative indices, the length of a slice is the difference of the indices Generating random floating point numbers Seeding random number sequences Generating random integers (Pseudo) Selecting random items random About random number generation numbers Pseudorandom number generator Good PRNGs random.org Generating random floating point numbers The random module provides random number generation for python Packages and modules extend Python, letting us use code written by others To use a package/module, we need to import it The random() function returns the next random floating point value from the generated sequence All of the returned values fall within the range 0