Log log10 modf pi pow radians sin sinh sqrt tan tanh

Info icon This preview shows pages 4–7. Sign up to view the full content.

View Full Document Right Arrow Icon
’log’, ’log10’, ’modf’, ’pi’, ’pow’, ’radians’, ’sin’, ’sinh’, ’sqrt’, ’tan’, ’tanh’] 7.15 One useful method: split The split string method produces a list of strings: It divides the original string at whitespace breaks. Example 1 . >> s = "My dog has no fleas." >> s . split () [’My’, ’dog’, ’has’, ’no’, ’fleas.’] Notice that the last word ends with the period at the end of the sentence. We might want to get rid of that, depending on our application. We just made a list (and so did the dir command). It’s time to talk about them, now. 7.16 3 Lists in Python Lists in Python Lists in Python are used very much like how they are in Scheme: They store things that don’t have to be of the same type. We identify a list by enclosing it in square brackets (which you can’t use interchangeably with parentheses, as you did in Scheme). Put commas between the elements. The list [ ’this is’ , 1, ’list’ ] has three elements. The first element is a string, the second is an integer, and the third is a string. 7.17 4
Image of page 4

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The length of a list The function len computes the length of a list: >> s = [ ’a’ , ’b’ , ’c’ ] >> len ( s ) 3 We saw earlier that it can also be used for strings: len ( ’Aardvark’ ) is 8. A second contract for len is: len : ( listof any ) int 7.18 3.1 Ranges Ranges A useful kind of list is a range of numbers. The function range creates a list based on its two arguments. It counts: from the first argument to just under the second argument. >> range (1,3) [1, 2] You can call range with only one argument, and it counts from zero. >> range (3) [0, 1, 2] 7.19 Ranges with different steps With three arguments, range counts up from the first argument to the second argument, but the step size is the third argument, instead of always being 1: >> start = 12 >> end = 24 >> range ( start , end , 3) [12, 15, 18, 21] And you can count down. >> range (4,0) [] >> range (4,0,-1) [4, 3, 2, 1] 7.20 Indexing lists and slicing lists We saw with strings that we could select out substrings and individual positions of the string. We can do the same thing with lists, using the same notation: >> Names = [ ’John’ , ’Li’ , ’Marjorie’ , ’Tran’ ] >> Names [3] == ’Tran’ True >> Names [2:4] [’Marjorie’,’Tran’] Extracting a single position from a list as in Names [3] is called indexing the list. Note: this is much easier than in Scheme. We don’t have an easy way in Scheme to look at the 24th entry of a list L , but in Python, it’s L [23] . (And not L [24] ! The list starts with entry #0.) 7.21 5
Image of page 5
Differences around lists Python lists are different from what is in other languages in a number of ways. Python lists can shrink and grow, as we’ll see. In some languages, the structure like lists has a fixed size when it’s created, and can’t change that size. Python lists can be of mixed type. In most languages, that’s not allowed: a list will contain only integers, or only floating-point numbers.
Image of page 6

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern