# inserts 5 at index 0 (shift) a.pop() # returns the last element # and deletes it a.pop(0) # returns the element at index i # and deletes it (shift) a.reverse() # reverse a.sort() # sort
5} a[“name”] # returns “john” (lookup) a[“address”] # returns KeyError exception del(a[“name”]) # delete a[“city”]=”doha” # insert a[“city”]=”paris” # modify • Values can be anything (mix and match) • But keys must be immutable and of the same type
classes, functions and variables • Creating a module • Create a file name stack.py (containing the stack definition defined previously) • Using a module import stack # import all definitions in stack x = stack.Stack() from stack import Stack # import the definition of Stack only x = stack.Stack() Or
write Fibonacci series up to n a, b = 0, 1 while b < n: print b, a, b = b, a+b if __name__ == "__main__": import sys fib(int(sys.argv[1])) fibo.py Execute the module $ python fibo.py 4