DOC PREVIEW
TAMU CSCE 110 - Introduction to manipulating comma-separated values
Type Lecture Note
Pages 4

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionLecture 10Outline of Last Lecture:I. Review of Basic ProgramsA. Basic OperationsB. If/elseC. WhileD. User-defined functionII. Additional NotesOutline of Current Lecture:I. Comma-Separated ValuesII. Immutable vs. MutableIII. IterablesA. For loopsB. MembershipCurrent Lecture:I. Comma-Separated Values (csv)We can import data lists from spreadsheets or create our own. Within these lists will be comma-separated variables that we can manipulate. For example, we can use the split function, which gives discrete values from a list of different items that are compressed that we want to break apart. We can use the strip function, which strips off unwanted characters from the data selection, or we can use the pop function, which "pops" the last element of a list off. The functions we will look at are strictly related to strings and lists. Let's experiment within a PythonShell:>>> x = [1, 'a', 'b', 2, 7]>>> x.pop()>>> 7>>> print x>>> [1, 'a', 'b', 2]Notice how we have a smaller list. When we use pop, Python will show us the element it will pop off the list. The next time we called the list, it was missing this popped element. We can continue to pop off as many elements from the list as we'd like.>>> x = 'hEliUm'These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.>>> x.upper()>>> 'HELIUM'>>> x.lower()>>> 'helium'To change a string to upper or lower case, we can put the variable name, followed by ".upper()" or ".lower()".>>> x = ' asdfjkl; qwerty poiu '>>> x.strip()>>> 'asdfjkl; qwert poiu'This function strips the empty white spaces from the sides. However, it should be noted that once a significant character is entered, spaces in between the characters will not be stripped. >>> x = ' asdfjkl; qwerty poiu '>>> x.lstrip()>>> 'asdfjkl; qwert poiu 'Adding the "l" before the "strip" only strips the spaces to the left. If we had added an "r" insteadof the "l", only the spaces the right would have been stripped. >>> y = 'abc,def,ghi,l,k,o,p,zldl'>>> y.split(',')>>> ['abc', 'def', 'ghi', 'l', 'k', 'o', 'p', 'zldl']Whenever the character in the parentheses/quotation marks is encountered, a new element is formed and placed into the list that is formed. You can split on any character, not just a comma.>>> y = 'abc,def,ghi,l,k,o,p,zldl'>>> y.split('l')>>> ['abc,def,ghi,', ',k,o,p,z', 'd', '']The last set of quotation marks here is just an empty string because the last character in the original string is the one we are splitting on.II. Immutable versus MutableLists are mutable. However, strings and tuples are not (tuples are the sets of elements inside parentheses). >>> element = 'HeliUm'>>> print element.upper()>>> print element.lower()>>> print element.replace('el', 'afn')>>> print 'element after manipulation:', element>>> HELIUM>>> helium>>> HafniUm>>> element after manipulation: HeliUmWe altered copies of the string (copies of the string are mutable) but never changed the originalstring itself. This is why the very last line has "HeliUm" still in it. Tuples are lists, but are read-only. However, lists within tuples are mutable:>>> z = (90, 'hello', [1, 2, 3], 'yes')>>> print z[-1]>>> yes>>> z[2][0] = 4>>> z>>> (90, 'hello', [4, 2, 3], 'yes')We can change the contents of the list, but we cannot change the list itself into another value. III. IterablesStrings, lists, an tuples are iterables. This means we can separate them into separate characters or elements that are ascribed a different index. To receive each of those values, it is said that weiterate over the " ". First, we need to learn another type of loop.A. For loopThe for loop is used whenever we have a specific amount of times we want an action performed. In these situations, the for loop is much more efficient than the while loop. The amount of times the loop will be executed will be the size of the iterable, noted by "i", or whatever is after the "for," getting its value from the list. Here's an example of a for loop:>>> for i in [1, 2, 3, 4, 5]:>>> print i>>> 1>>> 2>>> 3>>> 4>>> 5Here's another example, where we want to insert an "X" after every letter :>>> x = 'apple'>>> new_string = ''>>> y = 'apple'>>> for character in y:>>> new_string += character + 'X' >>> print new_string>>> aXpXpXlXeXB. MembershipWe can check to see whether or not a certain character is in a string or whether a certain element is in a list or tuple. We use Booleans.>>> 'a' in 'apple'>>> True>>> 'b' in 'apple'>>> False>>> 1 in [1, 2, 3]>>> TrueThis is fairly simple! It is similar to typing in comparisons and expecting a True or False


View Full Document

TAMU CSCE 110 - Introduction to manipulating comma-separated values

Type: Lecture Note
Pages: 4
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Introduction to manipulating comma-separated values
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to manipulating comma-separated values and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to manipulating comma-separated values 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?