Linguist 278: Programming for Linguists (Stanford Linguistics, Fall 2021)

Class 2: The basics of lists, built-ins, for-loops, conditionals, and function definitions

list

  1. Lists have many shared properties with strings. For many purposes in Python, a string is a list of characters.

  2. Lists are done with square brackets, with the elements separated by commas: [1, 2, 3] is a list, as is [1].

  3. Lists can contain any Python object, including other lists: [[1, 2], 4].

  4. List can contain mixtures of different kinds of objects: [[1, 2], 4, False, 7, 2.0].

  5. To add something to the end of a list, use the append method -- given x = [1, 2, 3], x.append(0) adds 0 to the end.

  6. Using append actually changes x! Lists are mutable!

  7. If you want to avoid changing x, you can use +, as in x + [0]. This returns a new list, leaving x unchanged.

  8. Notice the intuitive use of + there. For numerical types, it means addition. For str and list, it means concatenation.

  9. What does [1] * 5 do, then? What about [1, 3] * 5?

  10. The method insert can be used to add things to any place in a list. The pattern is x.insert(index, value), where index is the position you want the new element to occupy:

  11. x.index(2) is just like x.find(2), but it raises an exception of there is no 2 in x.

  12. x.sort() will sort x in place, using whatever logic Python has for sorting objects inside x. This will fail if the objects are of different types.

  13. There are many other list methods: https://docs.python.org/3.7/tutorial/datastructures.html#more-on-lists

Some built-in functions

  1. len is different than the methods above: it is a built-in function that can operate on many expressions. Thus, to get the length in characters of a string s, use len(s)

  2. Similarly, to convert a string to an int, use int("1"). For a float, use float("1").

  3. Concatenation: use +. Thus s + s returns the concatenation of s with itself. This is a common pattern in Python: the + operator is addition if the arguments are numerical, it is concatenation if the arguments are str, and it creates a new list if given lists as arguments.

  4. You can also do "x" * 5, which return "xxxxx".

for-loops

  1. Iterating over a list:

    for x in [1, 2, 3]:
       print(x)
    
  2. Iterating over a str:

    for x in "abc":
       print(x)
    

Conditionals

  1. Lone if clause:

    x = 3
    if x > 2:
       print("{} is greater than 2".format(x))
    
  2. Adding an else clause:

    if x > 2:
       print("{} is greater than 2".format(x))
    else:
       print("{} is not greater than 2".format(x))
    
  3. Adding an elif clause:

    if x > 2:
       print("{} is greater than 2".format(x))
    elif x == 2:
       print("{} equals 2".format(x))
    else:
       print("{} is not greater than 2".format(x))
    
  4. There can be any number of elif statements.

  5. The final else is optional, but it must occur at the end of the conditional block.

Function definitions

  1. Positivity testing:

    def is_positive(x):
       if x > 0:
           return True
       else:
           return False
    

    Note: because x > 0 returns True or False according to the same logic, the entire body of this can be simplified to x > 0. The above shows off more of the possibilities for function formatting, though.

  2. An answer to assignment 1, question 1; feel free to use it, but consider rewriting it as a one-liner using sum.

    def mean(vals):
       total = 0
       length = len(vals)
       for x in vals:
            total = total + x
       mu = total / length
       return mu