Quick intro to splitting and joining:
Suppose you have a string of words, where each word is separated from its neighbors with a space. You want to create a list of those words (without the spaces). Then you want to take that list of words, capitalize each word, and then create a string with all of those capitalized words, separated by commas. Here's how using the split() and join() string methods. The dialog shows a sequence of commands and their results in IDLE:
>>> A = 'this is easier than you think' >>> a_list = A.split() >>> print a_list ['this', 'is', 'easier', 'than', 'you', 'think'] >>> for i in range(len(a_list)): a_list[i] = a_list[i].capitalize() >>> print a_list ['This', 'Is', 'Easier', 'Than', 'You', 'Think'] >>> B = ','.join(a_list) >>> print B This,Is,Easier,Than,You,Think
Quick intro to detecting datatypes:
Sometimes you want to know what type of data a particular variable is holding. For instance, does the variable A hold a string, or an integer, or a list? There's a function called type() that returns that answer, but in a weird form. Luckily you don't have to know what that weird thing is, all you need to do is test whether two weird things are the same. For instance, to detect whether A contains an integer, test: type(A) == type(0). Here you're just testing whether the type of data in A is the same as the type of the integer 0. You could have compared it not only to 0, but to the type of any integer. So:
if type(A)==type(12): return 'An integer' if type(A)==type(45.298): return 'A floating point number' if type(A)==type(''): return 'A string' if type(A)==type([]): return 'A list'
Quick intro to random numbers:
If you want the Python to give you random numbers, you have to provide it with the range within which those numbers should be located. For instance, if you want a random number between 1 and 10 (with 1 and 10 included), you'd call the function random.randint(1,10), which will give you a random number between 1 and 10, each with a probability of 0.1. You'd have to include the random library first. So, here is how to create a list of 20 random numbers between 1 and 10:
import random lst=[] for i in range(20): lst.append(random.randint(1,10)) print lst # here is the result: [6, 2, 4, 8, 8, 5, 6, 2, 9, 9, 10, 1, 6, 6, 6, 4, 10, 8, 1, 5]
You can also ask for random floating point numbers: random.random() which will give you a random number between 0 and 1.0 (though never 1.0 itself). If you want a random floating-point number between 0 and 5.0, just multiply random.random() by 5. For example, let's generate a list of 3 random floating-point numbers between 1.0 and 10.0:
import random lst=[] for i in range(3): x = 1.0 + random.random() * 9 lst.append(x) print lst # here is the result: [3.876029673012756, 6.402562406173115, 1.6079897300262929]
Exercises:
1. Create the function Esrever(L) which will be given a list of strings, and will reverse each string (rearranging the characters backwards) and will return a list containing each of them. Example: Esrever(['abcd','derF']) # returns ['dcba', 'Fred']
2. Let's test how good Python's random number generator is. Notice, in the "Quick intro to random numbers" above, we asked for 20 numbers between 1 and 10, and Python actually gave us the number 6 four times (20% of the time), when the average that each number in the range 1 to 10 should have come up was 2 times (and 3 never even appeared in the list). That's not bad. Clusters like that are very likely to happen, by chance. In fact, if we had gotten each different number exactly twice in the list, then we'd be suspicious about the randomness of the function. But, on the other hand, if we were to ask Python to give us 100,000 random numbers between 1 and 10, we would expect the number 6 to come up pretty close to 10% of the time. So, let's test Python's random number generator by creating the function RandTest(low,high,numTrials) which will call Python's random number generator numTrials number of times to provide numbers between low and high, and then will return a list of the fraction of times each number came up. For instance, let's test it 10,000 timesfor numbers between 11 and 20:
print RandTest(11,20,10000) # prints: [0.0939, 0.1058, 0.1025, 0.1014, 0.0953, 0.098, 0.1004, 0.0937, 0.105, 0.104]
This means that the number 11 came up 9.39% of the time, and 12 came up 10.58% of the time, etc.
3. Create the function Av(L) which will be given a list, and will return the average (mean) of all the numbers in the list. The list can be any length, and can contain numbers and other types of data as well. If there are no numbers, it should return 0. Examples:
Av([3.5, 'Fred', 4.5, 1]) # returns 3.0 Av(range(1,5)) # returns 2.5 Av(["Nope, nothin' here"]) # returns 0
4. Create the function SmallestPos(L) which will be given a non-empty list of numbers and will return the position of the smallest number. If there are duplicates of the smallest number, it will return the position of the first one. Examples:
SmallestPos([3, 1, -3, 10, 2, 45.04]) # returns 2, because -3 is the smallest number SmallestPos([12, 3.8, 13, 3.8]) # returns 1
5. Create the function SortNums(L) which will be given a non-empty list of numbers, and will return a list of those numbers in non-decreasing order. You cannot use any built-in sorting function already in Python, though, perhaps, you might use SmallestPos(). BTW, there are several ways that you can remove an element from a list; you might want to do some research on this. Example:
SortNums([7, -3.4, 12, 7, 1.3]) # returns [-3.4, 1.3, 7, 7, 12]
6. Challenge problem: Create the function NumInWords(n) which will be given a number in the range 0 <= n < 1000, and will return a string with the number stated in English words. Try to make your code as short as you can. Examples:
NumInWords(0) # returns 'zero' NumInWords(13) # returns 'thirteen' NumInWords(27) # returns 'twenty-seven' NumInWords(427) # returns 'four hundred and twenty-seven'