Lecture 1 - DePaul University



CSC 401 – Lecture #6Yosef MendelsohnThe first part of this lecture reviews several topics that have been covered previously. Some don't need their own separate discussion, but are included here for completeness. Review: Another use for the 'in' operator: searchingWe have used this operator many times such as when looping through a list or the range() function:for n in lstNumbers:for i in range(10):However, this very convenient operator also allows you to check if a value is present in some collection (e.g. list, or string, or other):For example, you might want to see if the number 3 is present in a list.Or you might want to see if the letter 'q' is present in a string.>>> lstNums = [1, 2, 3, 5, 7, 11, 13] #a list with 7 integers>>> lstPets = ['rabbit','goldfish','emu']>>> s = "Doh!"The ‘in’ command returns True if the first argument is present inside the second argument. Otherwise, it returns False. >>> 2 in lstNumsTrue>>> 4 in lstNumsFalse>>> 'emu' in lstPetsTrue>>> 'Emu' in lstPetsFalse>>> '!' in sTrue>> 'oh' in sTrueExample:numbers = [3,4,27,96, 18, 43]searchNumber = input("Enter a number to search for: ")searchNumber = int(searchNumber)if searchNumber in numbers: print("It's there!")Loop patterns: Accumulator loopA common pattern in loops is to accumulate some value in every iteration of the loop. This value can be a numeric value such as a count of something, or it might refer to a list that we keep appending to, etc.For example, given a list of numbers we might want to determine the sum of those numbers.To do this we need to introduce a variable that will contain the sum. This variable will need to be initialized to 0, and then a for loop is used to add every number in the list to the variable.>>> lst = [3, 2, 7, -1, 9]>>> total = 0>>> for number in lst:total = total + number>>> print(total)20In this example, total is the accumulator. The assignment total = total + n increments the value of total by n.This type of operation, i.e. one in which we add a value to an existing variable is so common that there is a shortcut for it: total += nNote: This shortcut also works for various other common mathematical operations such as subtraction, multiplication, division. For example, let's say you had a variable called total. If each time through the loop you wanted to double the value of this variable you could use:total = total * 2OR total *= 2 Many – perhaps most – programmers tend to use this version.ASIDE: The accumulator ‘shortcut’Incrementing one number by some other number is so common that there is a popular shortcut for it: total += nIn fact, it works for all of the usual mathematical operators:x = x + 5 could be written as x += 5x = x * 3 could be written as x *= 3x = x * y can be written as x *= yx = x /z can be written as x /= zLearn this shortcut – it is very, very widely used!ASIDE: The accumulator ‘shortcut’Incrementing one number by some other number is so common that there is a popular shortcut for it: total += nIn fact, it works for all of the usual mathematical operators:x = x + 5 could be written as x += 5x = x * 3 could be written as x *= 3x = x * y can be written as x *= yx = x /z can be written as x /= zLearn this shortcut – it is very, very widely used!Example: Suppose we want to multiply all the numbers in the list.>>> lst = [3, 2, 7, -1, 9]>>> product = 0>>> for i in lst:product = product * i #Or: product*=i>>> print(product)0Clearly something went wrong. What is the problem?Note that we initialized our variable product to 0. Since anything times 0 equals 0, that's what our calculation will end up being.Instead we need to initialize product to 1, which is neutral for multiplication.>>> lst = [3, 2, 7, -1, 9]>>> product = 1>>> for i in lst:product *= i>>> product-378In the previous two examples the accumulators were of a numeric type.There can be other types of accumulators. In the following examples, the accumulators are strings and lists.Practice problem 1: Write a function called upAbbrev that takes a string as an argument and returns a string consisting of all upper-case characters in the string.It would be used as follows:>>> upAbbrev(“Cubs Win the Pennant!”)‘CWP’def upAbbrev(string): strCaps = "" for letter in string: if letter.isupper(): strCaps+=letter return strCaps##string = "Cubs Win the Pennant!"##print(upAbbrev(string))Practice problem 2: Write a function divisors that takes a positive integer n as an argument and returns the list of all positive divisors of n. That is, all numbers in which if you divide one number by another, the modulus is 0. For example, positive divisors of 10 are: 10 (since 10%10 is 0), 5 (since 10%5 is 0), 2, and 1. It would be used as follows:>>> divisors(6)[1, 2, 3, 6]>>> divisors(11)[1, 11]>>> divisors(12)[1, 2, 3, 4, 6, 12]def divisors(number): lstDivisors = [] for i in range(1,number+1): if number%i==0: lstDivisors.append(i) return lstDivisors##print( divisors(12))Practice problem 3: An acronym is a word formed by taking the first letters of the words in a phrase and making a word from them. For example, RAM is an acronym for “random access memory”. Write a function acronym that takes a phrase as a parameter and returns the acronym for that phrase. Note: The acronym should be all uppercase, even if the words in the phrase are not capitalized.It would be used as follows:>>> makeAcronym('random access memory')'RAM'>>> makeAcronym('internal revenue service')'IRS'Hint: You'll need to break your phrase up into separate words. There is a function you definitely want to become familiar with that does this. Answer: The split() function.def makeAcronym(phrase): lstWords = phrase.split() strFirstLetters='' for word in lstWords: strFirstLetters = strFirstLetters + word[0].upper() return strFirstLetters##p = "Random Access Memory"##print( makeAcronym(p) )New & Important Loop pattern: 'while' loopThis loop consists of the keyword 'while' followed by a conditional. As long as the conditional evaluates to True, the loop continues. repeat = ''while repeat != 'No': print("Hello!") repeat = input("Would you like another greeting? Type Yes or No: ")print("Goodbye!")Loop pattern: Infinite loopIt is also possible to create a so called “infinite” loop, i.e. a loop that runs forever. Most of the time, infinite loops are the result of sloppy programming, but they can also be used effectively. It is fairly common to accidentally create infinite loops when first learning how to program. When this happens, the most common way to exit the loop is to press Control-C (Windows), or Command-C (Mac). There are occasionally situations where infinite loops are useful. For example, consider a service that is needed to be executed indefinitely. A web server, i.e. a program that serves web pages, is an example of a program that provides a service that has no known end point.Perhaps the easiest way to create an infinite loop intentionally is:while True:<body>Practice problem: Write a function greeting() that, repeatedly without end, requests that the user input their name and then greets the user using that name.Recall that an infinite loop happens when the condition after the ‘while’ term always evaluates to True. I.e. It never becomes false. The following is an example of how the function would be used:>>> greeting()What’s your name? ClarkHello Clark.What’s your name? LoisHello Lois.What’s your name?def greeting(): while True: name = input("What is your name? ") print('Hello, {}.'.format(name))Loop pattern: Interactive loopA while loop can be the right choice when a program needs to repeatedly request input from the user, until the user enters a "flag" which is a value indicating the end of the input.This use of a flag (i.e. a boolean indicator) is a very common technique in programming, particularly with loops.Example: Write a function called ‘betterGreeting()’ which fixes the function above, so that if the user types ‘stop’ (in lower case), the loop ends.def betterGreeting(): flag=True while (flag==True): name = input("What is your name? ") if (name=='stop'): flag=False print('Hello, {}'.format(name))Example: Write a function interact1() that repeatedly requests positive integers from the user and appends them to a list. The user will indicate the end of the input with the value 0 or a negative number, at which point the function should return the list of all positive integers entered by the user.The following is an example of how the function would be used:>>> interact1()Enter value: 3Enter value: 1Enter value: 8Enter value: 0[3, 1, 8]>>> interact1()Enter value: 0[]def interact1(): lstNumbers=[] flag=True while flag==True: num = int( input("Enter a number: ")) if num>0: lstNumbers.append(num) else: flag=False return lstNumbersSpecialized loop statements: break, continue, passFollowing are some statements that can be used to construct more elegant loops. These are: break, continue, and pass.The break statement and the loop-and-a-half patternA break statement immediately terminates the entire loop (not simply the current iteration). A break statement can be placed anywhere inside the body of the loop. As with any situation in which a loop has completed, execution then resumes at the first statement after the body of the loop.To see how it works, let's write a variation of the interact1() function, called interact2() in which we use a break statement to exit the loop when the user gives us our 'flag' value. >>> interact2()Enter value: 3Enter value: 1Enter value: 8Enter value: 0[3, 1, 8]>>> interact2()Enter value: 0[]def interact2(): lstNumbers=[] while True: num = int( input("Enter a number: ")) if num>0: lstNumbers.append(num) else: break return lstNumbersThe continue statementThe continue statement terminates only the current iteration of the loop. That is, whereas the break statement completely exits the loop – and does not even try to check the conditional—the continue statement merely terminates the current iteration through the loop. However, the loop is not over! The interpreter now returns to the conditional to evaluate for True vs False. Restated: Unlike the break statement, execution is NOT resumed with the first statement after the loop body. Instead, execution resumes at the loop's conditional. If the loop is a while loop, execution is resumed at the condition checking statement. If the condition is still true, then execution moves to the next iteration of the while loop.If the loop is a for loop, then execution resumes with the next iteration (provided, of course, that there are additional elements to iterate over).For example, consider the startsWithVowel() function:def startsWithVowel(): while True: word = input("Enter a word: ") if word[0] not in 'aeiouAEIOU': continue else: print("Starts with a vowel.")The print statement is only executed when the condition in the if statement evaluates to False.This is because the continue statement in the body of the if statement makes the execution skip to the next iteration of the while loop.Also note that this code has an infinite loop inside. You'll have to press control-C to end the loop. I hope you recognize that in the real world, infinite loops should not be used this way – the user should never have to cancel out of your program! The pass statementIn Python the body of a function, if statement, for loop, while loop, except clauses must contain at least one statement.Sometimes, particularly during testing, we may not want or need to do anything inside the body. When that’s the case, we can use the pass statement.For example:if n % 2 == 0:passelse:print(n)In the case where n is even, we don’t do anything. When n is odd, we print the number.The pass statement is especially useful when a body for a particular construct has not yet been written. Basically, this means that you should pretty much only use it during writing/testing of code. For example, for functions that we haven’t implemented. In such a case, it is there as a temporary placeholder until we are ready to implement that section.In other words, “production” code doesn't commonly include ‘pass’ in it. EXAMPLE: Putting some key things together…def printContents(): while True: #Infinite loop try: filename = input("Enter a file to be opened: ") fin = open(filename,'r') break except (IOError, FileNotFoundError): print('The file "{}" could not be opened.\nPlease try again.' .format(filename)) continue text = fin.read() print(text)Think of the situation in which we ask the user to enter a file name and then try to open a file. Let's write a function that prompts the user for a file name. If a FileNotFoundError or IOError exception occurs, the function will ask the user to try again. This will continue until the user enters a valid file name. Once the open() function completes without generating any exception, the function will return the file stream object (e.g. 'infiile'). def safeOpen(): while True: #Infinite loop try: filename = input("Enter a file to be opened: ") fin = open(filename,'r') return fin #Only gets executed if no exception except (IOError, FileNotFoundError): print('{} could not be opened.\nPlease try again.' .format(filename))We can now do something like:infile = safeOpen()contents = infile.read()Loop patterns: Nested loopsSome problems can only be solved using multiple loops together.To see how to use multiple loops, consider the following (somewhat artificial) problem: Suppose we want to write a function nested() that takes a positive integer n and prints to the screen the following n lines:0 1 2 3 … n-10 1 2 3 … n-1…0 1 2 3 … n-1For example:>>> nested(5)0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4We’ve already seen that in order to print one line we can write the following:>>> for i in range(n):print(i, end=" ")0 1 2 3 4In order to get n such lines (5 lines in this case), we need to repeat the above loop n times (5 times in this case). To do this, we take the above loop and put it INSIDE another loop! In other words, we will repeat (i.e. loop) the entire loop! >>> for j in range(n):for i in range(n): #the original loopprint(i, end = " ")0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4Great – we're close!!Still, this doesn’t produce exactly we wanted. Setting end to a space forces all of the numbers onto a single line, which is not what we want.We would like to have a new line printed between each line printed by the inner for loop. We can do that as follows:>>> for j in range(n):for i in range(n): #the original loopprint(i, end = " ")print() #add a new line at the end0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4def nested(num): for i in range(0,num): for j in range(0,num): print(j,end=' ') print()Practice problem: Write a function multmult that takes two lists of integers as parameters and returns a list containing the products of integers from the first list with the integers from the second list.For example, it would be used as follows:>>> multmult([3, 4, 1], [2, 0])[6, 0, 8, 0, 2, 0]Important: This is a classic example of a situation where you should not, not, not start by typing out code! Rather, you should spend a few minutes with a pen and paper and think (yuck) through the problem and come up with a strategy for solving it.def multmult(lst1, lst2): lstProducts = [] for i in lst1: for j in lst2: lstProducts.append(i*j) return lstProducts ##l1 = [3,4,1]##l2 = [2,0]##result = multmult(l1,l2)##print(result)Another Example: Imagine a function lines() that takes one positive integer n as a parameter and prints on the screen the following n lines:00 10 1 20 1 2 3 …0 1 2 3 … n-1For example:>>> lines(5)0 0 1 0 1 2 0 1 2 3 0 1 2 3 4This is a situation where a nested loop can do the job nicely with minimal code.def lines(n): for j in range(n+1): for i in range(j): print (i, end = " ") print()More on lists: Multi-dimensional listsA 2-dimensional table has many real-world representations. For example, think of just about any table or spreadsheet you may have seen:Sales of 5 people in your department over 12 monthsStatistics of 10 basketball players (shooting percentage, free throw percentage, fouls committed, etc)Here is an example of a table showing the scores (out of 40) on 5 homework assignments for a group of students:Tables such as these are frequently analyzed in data science and other fields using programming code. In Python, 2-dimensional tables can be easily stored as a list of lists. Prior to our current discussion, the lists we’ve seen have been one-dimensional. We might think of them as a one-dimensional table.For example, a list such as: lst1 = [3, 5, 7] can be viewed as the table:357A two-dimensional table like the following:472519836can be viewed as a list of three rows, with each row being a one-dimensional list:472519836Can you see what I'm getting at? We can use Python to represent such a two-dimensional table by making it a list of lists:table = [ [4, 7, 2] , [5, 1, 9] , [8, 3, 6] ]Note: In the above line of code I added several (unnecessary) extra spaces to help visualize what is happening. In the real world, we shouldn't space things out this much.In this example:table[0] is holding the list [4,7,2]table[1] is holding the list [5,1,9]table[2] is holding the list [8,3,6]To see how to use nested loops to process a two-dimensional list, we need to think about how to access elements in a two-dimensional list.#Recall our original list:>>> lst = [[4, 7, 2], [5, 1, 9], [8, 3, 6]]If you are working with a list of lists, then accessing that object with a single index gives you an entire sub-list:>>> lst[0][4, 7, 2]>>> lst[1][5, 1, 9]Accessing a list of lists with two indices gives you a specific element in the specified row.Here are some examples using the object 'table' we created above:>>> table[0][0] #first row, first element4>>> table[1][2] #second row, third element9>>> table[2][1] #third row, second element3To summarize: To access an element in row i and column j, we would write: table[i][j]Example: If we wanted to get the average of the first number in each row we could do something like:avg = (table[0][0]+table[1][0]+table[2][0]) / 3.0But imagine if instead of 3 rows, we had 30, or perhaps 3,000,000? Clearly writing out this code would be impossible. However, now that we are more familiar with nested loops, we can use this technique to analyze this data. Aside: For those of you who have been hearing all the buzz about the field of "data science", this is one of the bread-and-butter techniques used by data scientists to analysze tabular data – which can easily and commonly reach into millions and millions of records. Practice problem: Write a function add2D that takes a 2-dimensional list of integers, adds 1 to each entry in the list, and returns the resulting modified list.For example, it would be used as follows:>>> lst = [[4, 7, 2], [5, 1, 9], [8, 3, 6]]>>> add2D(lst)>>> print(lst)[[5, 8, 3], [6, 2, 10], [9, 4, 7]]Hint: The indices for the row will be 0 to len(lst), and the indices for the columns will be 0 to len(lst[i]).def add2D(table): '''Note how we use identifiers 'row' and 'item' (or sometimes 'column') when iterating through a table in a nested loop. This is very common, and, I hope you'll agree, makes for much clearer and easier to follow code than say 'i' or 'x'. ''' for row in range(len(table)): #len(table) is the number of rows for item in range( len(table[row]) ): # len( table[row] ) tells us how many items are in the current row table[row][item] = table[row][item]+1 return tablesome_table = [[4,7,2],[5,1,9],[8,3,6],[-3,7,-5]]add2D(some_table)DictionariesSuppose you want to store employee records with information such as their social security number and name. You'd like to store these in a collection and access the employee records (e.g. name, birthdate, salary, etc, etc) using the employees’ social security numbers.The list collection is not really useful for this problem because list items must be accessed using an index, i.e. by their position in the collection.But what if we want a way to access an item in a collection by, say, some kind of key? For example, say you wanted to access a bunch of information about a student based on their Student ID number? Dictionaries are a type of collection will allow us to do that. For this problem we need a collection type that allows access to stored items, but instead of using a position as our index, we use a "key". With dictionaries, we must decide what the key will be. The one requirement is that the key we choose must be unique for every record. For an employee, social security number would be a good choice since no two people can have the same value.Recall that to create a list we specify an identifier and square brackets:myList = []Dictionaries are created very similarly, but use braces:myDict = {}Note: There are quite a few different ways of creating new Dictionaries. You can find out more about these using help() or any Python text, google, etc. We add items to a dictionary by typing <key> a colon <value(s)>Here is an example using employees. In this collection, we have created 4 items. Note how each item has a key and a value. >>> employees = {'022-83-8795':'Allison Andover', '987-65-4321':'Benjamin Benton', '123-45-6789':'Cassidy Cantor', '874-74-1532':'Daniel Danton'}Notes on Terminology: KEY: In the employees example, ‘022-83-8795’ is a key. The key is also known as the ‘index’ of the item. VALUE: The information associated with a given key. This may be a single thing such as a name. However, a key can have a whole pile of information associated with it.ITEM: Each key and its related values are referred to as an ‘item’.Notes on Syntax:Note how each key is separated from its value by a colonNote how each key:value item is separated from the next item by a commaRecall that with a list, we would refer to an item using its position. For example, to output the first item in a list we might say:>>> myList[0]When working with a dictionary, we refer to an item using its key. We still use the familiar square brackets, but instead of an index number like we use with list indexes, we must instead type out the key. >>> employees['123-45-6789']'Cassidy Cantor'Note that since this particular key is a string, we have to put this value in quotes. Here is a short summary of some important facts about dictionaries:A dictionary, also called a map, is a collection type that stores key-value pairs.In the employees dictionary above, ‘022-83-8795’: ‘Allison Andover’ is known as a "key-value pair".The key ‘022-83-8795’ is the “index” of the stored object. We use this index to access the object. The key MUST be an object of an immutable data type (like a string or number or tuple).We’ll learn about the ‘tuple’ data type shortly….The value for the key 022-83-8795 is the string 'Allison Andover'.Items in a Dictionary are NOT ordered.Dictionaries are mutable, like lists, meaning they can be modified to contain different items by adding items, removing items, modifying items (though not their keys!), etc.Like lists, dictionaries have dynamic size, meaning they can grow or shrink (i.e. you can add and remove items). Here is another example in which we have a dictionary of employees. Each key in the dictionary is a string representing a 4 character string of digits. The value of each key is a list. The list contains three items: The employee's name, age, and title.dctEmployees = { '4444':['Lisa Jackson',33,'VP Sales'], '2222':['Nasia Motungo',44,'CEO'], '6666':['William Diaz',58,'CIO'] }Dictionary methodsBecause the dict (dictionary) data type is different from the list data type it has different methods. You can, of course, see these by looking them up in the Python API. For a very quick look, you can type help(dict) at the console.Study and experiment with the following examples:>>> d = {'Mo': 'Monday', 'Tu': 'Tuesday', 'We': 'Wednesday', 'Th': 'Thursday', 'Fr': 'Friday', 'Sa': 'Saturday', 'Su': 'Sunday'}>>> print(d) {'Fr': 'Friday', 'Mo': 'Monday', 'Tu': 'Tuesday', 'We': 'Wednesday', 'Su': 'Sunday', 'Th': 'Thursday', 'Sa': 'Saturday'}#Note that the ordering is different...#Never assume that a dictonary will keep any kind of ordering>>> d['We'] 'Wednesday'>>> 'Su' in dTrue>>> d.keys()dict_keys(['Fr', 'Mo', 'Tu', 'We', 'Su', 'Th', 'Sa'])>>> d.values()dict_values(['Friday', 'Monday', 'Tuesday', 'Wednesday', 'Sunday', 'Thursday', 'Saturday'])>>> d.items()dict_items([('Fr', 'Friday'), ('Mo', 'Monday'), ('Tu', 'Tuesday'), ('We', 'Wednesday'), ('Su', 'Sunday'), ('Th', 'Thursday'), ('Sa', 'Saturday')])>>> del(d['Sa'])>>> print(d){'Fr': 'Friday', 'Mo': 'Monday', 'Tu': 'Tuesday', 'We': 'Wednesday', 'Su': 'Sunday', 'Th': 'Thursday'}>>> d.clear()>>> print(d){}To ADD a new key value pair:>>> d['new_key'] = some valueIn other words, if you type a new key, then you will add the new key to the dictionary and assign it the value as shown above. However, if you type the name of an existing key, the value of that key will be overwritten. >>> dictMonths = {'Jan':'January','Feb':'Febuary','Mar':'March'}>>> dictMonths[‘Jan’] = ‘Brr its cold'#Will overwrite the original ‘January’ valueAs shown above, among the many functions that can be used on dictionaries are: keys(), values(), and items()Here is an example where we use the keys() function. Let's output all of the keys in a dictionary, one on each line:for key in dictMonths.keys():print(key)Repating with values: for value in dictMonths.values():print(value)Here is the API for the Dict data type. This data type is extremely important and useful in Python. It is something with which you should certainly practice and become familiar. Problem: Write a function wordFreq() that takes as a parameter a file name (i.e. a string object). The function should compute the frequency of each word in the file and then output the list of words and their frequencies. Note that in order to ensure accurate results, we must first remove all punctuation and capitalization. Here the function has been invoked on the text file sample.txt: >>> wordFreq('sample.txt')out 1are 2figure 1do 1length 2of 2should 1file 1this 2it 1we 1can 1one 1three 1function 1many 2is 1test 1a 2words 2how 2You can find this function in this week’s sample PY file.The random moduleRandom numbers are useful in a variety of contexts:Computer games (e.g. poker, blackjack, dice games, action games, etc)Simulations for science, engineering, and financeStatistical analysisComputer security protocolsUnfortunately the machines we use are deterministic, which means (among other things) they have no access to 'perfect' randomness.Instead we have to make do with pseudo random numbers, i.e. “almost” random numbers.A pseudo random number generator produces a sequence of numbers that “look” random. These generators are typically good enough for most applications that need random numbers. For certain very subtle or high-performance situations where "true" randomness is needed, other techniques must b eemployed. Python does have a useful (pseudo) random number gneerator. To use it, we must import the random module:>>> import randomFunctions in the random moduleThere are several useful functions in the random module. We’ll work through a number of them.Choosing a random integerThe function randrange() takes a pair of integers and returns some number in the range [a, b) -- the brackets notation here indicates that it includes ‘a’ but but is not inclusive of ‘b’.>>> random.randrange(1,7)3>>> random.randrange(1,7)4>>> random.randrange(1,7)6>>> random.randrange(1,7)6>>> random.randrange(1,7)3>>> random.randrange(1,7)2randrange() also provides an additional parameter 'step' – we won't go into it here, but feel free to read up on it.There is a very similar function called randint() takes a pair of integers and returns some number in the range [a, b]. That is, whereas randrange() returns a value up to but not including b, randint() can include b. Choosing a random decimalSometimes we need a decimal random number rather than a decimal integer. In that case we need to use the uniform() function which takes two numbers a and b and returns a float number x such that a ≤ x ≤ b (assuming that a ≤ b), with each float in the range equally likely.The following obtains several random numbers between 0 and 1:>>> random.uniform(0,1)0.7378384739678002>>> random.uniform(0,1)0.09911455205729514>>> random.uniform(0,1)0.18295866524385507>>> random.uniform(0,1)0.3319868023085931Shuffling, choosing and samplingThe function shuffle() shuffles, or permutes, the objects in a Python sequence (e.g. a list, set, etc) – much like the way a deck of cards is shuffled before playing a card game. In fact, if we were to simulate, say a card game, we would probably use this function. >>> lst = [1, 2, 3, 4, 5]>>> print(lst)[1, 2, 3, 4, 5]>>> random.shuffle(lst)>>> print(lst)[1, 2, 4, 5, 3]>>> random.shuffle(lst)>>> print(lst)[3, 5, 2, 1, 4]>>> words = ['cat', 'bat', 'at', 'fat']>>> random.shuffle(words)>>> words['at', 'cat', 'bat', 'fat']The function choice() allows us to choose an item from a container at random:>>> random.choice(lst)5>>> random.choice(lst)4>>> random.choice(lst)1>>> random.choice(words)'fat'>>> random.choice(words)'cat'>>> random.choice(words)'cat'>>> random.choice(words)'fat'We use the sample() function if instead of choosing a single item from a sequence we wanted to choose a sample of size k, with every sample equally likely. The function takes the container and the number k as parameters:>>> random.sample(lst, 2)[1, 2]>>> random.sample(lst, 2)[4, 2]>>> random.sample(lst, 3)[3, 4, 1]>>> random.sample(words, 3)['at', 'cat', 'bat']>>> random.sample(words, 3)['fat', 'at', 'cat']>>> random.sample(words, 2)['fat', 'bat'] ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download