1 / 23

Introduction to Computational Linguistics

Introduction to Computational Linguistics. Programming II. Resum é. calculator mode arithmetic operators, simple and complex arithmetic expressions saving, checking and running programs. Exercise 2.1. Write a program that gets 2 string variables and 2 integer variables from the user,

dakota
Download Presentation

Introduction to Computational Linguistics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computational Linguistics Programming II

  2. Resumé • calculator mode • arithmetic operators, simple and complex arithmetic expressions • saving, checking and running programs

  3. Exercise 2.1 Write a program that • gets 2 string variables and 2 integer variables from the user, • concatenates (joins them together with no spaces) and displays the strings, • then multiplies the two numbers on a new line.

  4. Answer s1 = raw_input("give me string 1 ") s2 = raw_input("give me string 2 ") n1 = input("give me number 1 ") n2 = input("give me number 2 ") print s1+ " " + s2, n1+n2

  5. while loop • while <condition> : <statements> i=0 while (i<10) : print i i = i+1 • Notice that indentation is used to group items together

  6. Exercise 2.2 • Modify the last program so that it prints the sum of all the numbers.

  7. Exercise 2.2 sum=0 i=0 while (i<10) : print i i = i+1 sum = sum+i print sum

  8. range([start,] stop[, step]) • Creates lists containing arithmetic progressions • most often used in for loops. • If the step argument is omitted, it defaults to 1. • If the start argument is omitted, it defaults to 0. • Observe the behaviour of the range function

  9. Use of range function >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(1, 11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> range(0, 30, 5) [0, 5, 10, 15, 20, 25] >>> range(0, 10, 3) [0, 3, 6, 9] >>> range(0, -10, -1)[0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> range(0) [] >>> range(1, 0) []

  10. for loop • Basic shape of for statement is this for <variable> in <list>: do something • Examplesfor i in [1,2,3] : print ifor i in range(1,4): print ifor i in ["comp", "ling"]: print ifor i in ['comp', 'ling']: print i

  11. Exercise 2.3 • calculate and print the sum of numbers in range(5,20).

  12. Strings • Besides numbers, Python can also manipulate strings, which can be expressed in several ways. • They can be enclosed in single quotes or double quotes: >>> 'spam eggs‘'spam eggs' >>> 'doesn\'t‘"doesn't" >>> "doesn't“"doesn't" >>> ‘ "Yes," he said.' ‘ "Yes," he said.'

  13. Strings • We can get at individual characters of a string using subscript notation>>> s = 'dog'>>>s[0]'d'>>>s[1]'o'>>>s[2]'g'

  14. Strings • Strings can be concatenated (glued together) with the + operator, and repeated with *: • >>> word = 'Help' + 'A' • >>> word 'HelpA' • >>> '<' + word*5 + '>' '<HelpAHelpAHelpAHelpAHelpA>'

  15. Slice notation I • Slice notation is two indices separated by a colon, and selects that part of a string which begins with the first index and which finishes just before the second>>> s = 'dog'>>> s[0:1]'d'>>> s[0:2]'do'

  16. Slice notation II • Slice indices have useful defaults • an omitted first index defaults to zero • an omitted second index defaults to the size of the string being sliced. >>> s[:2] 'do' >>> word[2:] 'g' >>> word[0:] 'dog'

  17. More Data Types • We have seen numbers and strings. • These are different types of data. • Each kind of data has characteristic operations. • Now we look at lists. • A list is a compound data type which is used to group other data types together. • example: range(3) = [0,1,2]

  18. Lists • The list can be written as a list of comma-separated values (items) between square brackets. • List items need not all have the same type. >>> a = ['spam', 'eggs', 100, 1234]>>> a ['spam', 'eggs', 100, 1234] • Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:

  19. Lists • Lists can be sliced, concatenated and so on: >>> a[0] 'spam' >>> a[3] 1234>>> a[-2] 100>>> a[1:-1] ['eggs', 100]>>> a[:2] + ['bacon', 2*2] ['spam', 'eggs', 'bacon', 4] >>> 3*a[:3] + ['Boe!']['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']

  20. Lists can be changed • Unlike strings, which are immutable, it is possible to change individual elements of a list>>> a[2] = a[2] + 23 • Assignment to slices is also possible, and this can even change the size of the list

  21. Changing Lists # Replace some items: >>> a[0:2] = [1, 12] >>> a [1, 12, 123, 1234] # Remove some: >>>a[0:2] = [] >>> a[123, 1234] # Insert some: >>>a[1:1] = ['bletch', 'xyzzy'] >>> a [123, 'bletch', 'xyzzy', 1234]

  22. The built-in function len( ) This function computes the number of elements in a list >>> a = [1,2,3]>>> len(a)8>>>len([1,1,1,1,1,1])6

  23. Nesting Lists It is possible to nest lists (create lists containing other lists), for example: >>> q = [2, 3]>>> p = [1, q, 4]>>> p[1,[2,3],4]

More Related