Download presentation
Presentation is loading. Please wait.
Published byOctavia Lucas Modified over 9 years ago
1
COMPUTATION WITH STRINGS 4 DAY 5 - 9/05/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University
2
Course organization 05-Sept-2014NLP, Prof. Howard, Tulane University 2 http://www.tulane.edu/~howard/LING3820/ http://www.tulane.edu/~howard/LING3820/ The syllabus is under construction. http://www.tulane.edu/~howard/CompCultEN/ http://www.tulane.edu/~howard/CompCultEN/ Is there anyone here that wasn't here on Wednesday?
4
Open Spyder 05-Sept-2014 4 NLP, Prof. Howard, Tulane University
5
Go over homework Review 05-Sept-2014 5 NLP, Prof. Howard, Tulane University
6
3.5. Practice, cont 4. What is the longest sequence of operators that you can make? 5. Show which of len(), sort(), and set() takes the most time to process. 6. Line 2 from the previous subsection has the expression L[2:6].capitalize().upper(). Show whether it is faster for Python to process this as it is, or broken into its parts as in: >>> A = L[2:6] >>> B = A.capitalize() >>> C = B.upper() 7. Recall the discussion of the default precedence of * and + in §3.1.1. Perhaps * comes first because it is quicker to process than +. Test this hypothesis by writing a function for either combination of * and + and time them to see which runs faster. 05-Sept-2014NLP, Prof. Howard, Tulane University 6
7
A string is a sequence of characters delimited between single or double quotes.string §3. Computation with strings 05-Sept-2014 7 NLP, Prof. Howard, Tulane University
8
3.6. Dealing with strings that are longer than a line >>> longslash = 'A wonderful bird is the pelican.'\... 'His bill can hold more than his belican.' >>> print longslash 'A wonderful bird is the pelican.His bill can hold more than his belican.' >>> longparen = ('A wonderful bird is the pelican.'... 'His bill can hold more than his belican.') >>> print longparen 'A wonderful bird is the pelican.His bill can hold more than his belican.' 05-Sept-2014NLP, Prof. Howard, Tulane University 8
9
Strings longer than a line, cont. >>> longsingle = '''A wonderful bird is the pelican.... His bill can hold more than his belican.''' >>> print longsingle A wonderful bird is the pelican. His bill can hold more than his belican. >>> longdouble = """A wonderful bird is the pelican.... His bill can hold more than his belican.""" >>> print longdouble A wonderful bird is the pelican. His bill can hold more than his belican. >>> longdouble 'A wonderful bird is the pelican.\nHis bill can hold more than his belican.' 05-Sept-2014NLP, Prof. Howard, Tulane University 9
10
3.7.1. Assignment of variable names You have by now performed an assignment of a variable to an expression several times and hopefully have internalized something like: variable = expression This can be read as “variable is assigned to expression”. For convenience, we often refer to the variable as the name for the expression, though the Python documentation actually prefers identifier to name. 05-Sept-2014NLP, Prof. Howard, Tulane University 10
11
What's in a name There are several limitations on what can be a name. The main one is that it cannot be one of the words that Python reserves for its own uses, such as: >>> print = 'print' A trickier case is that of the methods built into Python, such as the string methods reviewed in this chapter: >>> len = 'len' Using one of them as a name is not prohibited, but is considered bad form, since it could be confused with the method len() and lead to misunderstanding. Your really do not want to create the possibility of expressions like len(len) in your code. 05-Sept-2014NLP, Prof. Howard, Tulane University 11
12
What's in a name, cont. There are also limits on what characters can be part of a name. A name must start with a letter or underscore, but not a digit: >>> name = 'zzz' >>> _name = 'zzz' >>> 0name = 'zzz' Once a name has been started correctly, it can contain any combination of letters, digits, or underscores, but nothing else: >>> my_name = 'zzz' >>> my_name1 = 'zzz' >>> my name = 'zzz' >>> my-name = 'zzz' Upper and lower case are different: >>> name = 'zzz' >>> Name = 'zzz2' >>> name == Name 05-Sept-2014NLP, Prof. Howard, Tulane University 12
13
3.7.2. Mutability Try these: >>> name[0] = 'b' >>> del name[0] Strings are immutable, which is to say, once a string has been created, it cannot be changed by adding or deleting items from it. The only way that it can be changed is by means of the methods that were reviewed above. 05-Sept-2014NLP, Prof. Howard, Tulane University 13
14
Q1 as take home to turn in in class on Monday §4 Lists Next time 05-Sept-2014NLP, Prof. Howard, Tulane University 14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.