Programming Languages Transcript – Lecture of 13th October at 13:15 (* Is the value 2 intentionally equal to 2? *) >>> 2 is 2 (* Does this also work for strings? *) >>> “Hello” == “Hello” >>> “Hello” is “Hello” (* Does it also work for empty lists? *) >>> [] == [] >>> [] is [] (* A different method of variable instantiation, does b point to an empty list and then a to the same empty list? *) >>> a = b = [] >>> a is b (* Usage of the reverse method on lists *) >>> a = [1,2,3] >>> type(a) >>> a.reverse >>> a.reverse() >>> a >>> a.reverse() >>> a >>> reversed(a) >>> b = reversed(a) >>> b (* Creating an iterator over the list a, going backwards *) >>> b = reversed(a) (* The iterator is consumable, it only returns this result once *) >>> list(b) >>> list(b) (* Normally, lists aren’t consumable *) >>> b = [1,2,3] >>> list(b) >>> list(b) (* Showing the type difference between lists and iterators *) >>> b = [1,2,3] >>> type(b) >>> b = reversed([1,2,3]) >>> type(b) (* Returning an item from a list iterator one by one *) >>> b = reversed([1,2,3]) >>> next(b) >>> next(b) >>> next(b) (* Iterating over a list normally *) >>> b = iter([1,2,3]) >>> next(b) (* You can change the list that an iterator is pointing to *) >>> a = [1,2,3] >>> b = reversed(a) (* Does using append work? *) >>> a.append(42) >>> list(b) (* Does using a direct assignment on the index work? *) >>> a[0] = 42 >>> list(b) (* Lists can hold multiple types at once, including a list within a list. What is the length of this list? *) >>> a = [99, "bottles of beer", ["on","the","wall"]] >>> len(a) (* Using the join method, we can add more characters to the end of a string. What will this return? *) >>> " " >>> " ".join([a[1]]+a[2]) >>> "@".join([a[1]]+a[2]) (* Casting to the string type. What will these method calls return? *) >>> str(9) >>> str(0b11111111) >>> str(“2”) >>> str([“2”,”3”]) (* Casting to the integer type. What will these method calls return? *) >>> int(“400”) >>> int(None) >>> int([“3”]) >>> int(4.0) >>> int(4.7) >>> int(-4.7) (* Example of instantiating a range, and its type *) >>> range(10) >>> type(range(10)) (* You can create a range of any size, it does not actually calculate the value until it’s called. May crash your system. *) >>> type(range(2**100000000)) >>> list(range(2**100000000)) (* Instantiating a list to a range 0-9 *) >>> list(range(10)) [0,1,2,3,4,5,6,7,8,9]