# Programming Languages (F28PL) lecture transcript - 19th October at 15:15 # Transcript starts @ 23:00. # Demonstrating iteration over other types: x = "Hello World" for l in x: print(l) # Will print each character in "Hello World" sequentially, as strings are # iteratable types x[2] # will return l # Example exam question: reverse a string using python using python indexing # functions def reversestring(s): return s[-1::-1] # s[::-1] also works reversestring(x) # will return "dlroW olleH" # to demonstrate the use of break primelist = [] for n in range(2, 100): for x in range(2, n): # n/2 will also work if n % x == 0: break else: # BEWARE! Indentation is important! This else belongs to the for loop primelist.append(n) print(primelist) pass # does nothing superman_todo = {1:"Get out of bed", 2:"Save world", 3:"Eat breakfast"} # dictionary: key-value data structure type(superman_todo) # superman_todo[1] # "Get out of bed" superman_todo[2] # "Save world" superman_todo[3] # "Breakfast" superman_todo[4] # Error! # demonstrating .get() function superman_todo.get(1) # "Get out of bed" superman_todo.get(2) # "Save world" superman_todo.get(3) # "Breakfast" superman_todo.get(4) # Returns NoneType. Note difference to the previous example print(superman_todo.get(4)) # prints 'None' to console # procedure as an object reversestring # returns the location in memory 5**100 is 5**100 # returns false, as there exists two 5*100's in memory 5**2 is 5**2 # returns true, as python caches numbers between -5 and 256 such #that when you create an int in that range you actually just get back a reference to #the existing object. https://docs.python.org/2/c-api/int.html https://docs.python.org/3/c-api/long.html