Chapter 6.ABSTRACTION. from 'Beginning Python From Novice to Professional' by Magnus Lie Hetland
fibs = [0, 1] for i in range(8): fibs.append(fibs[-2] + fibs[-1]) >>> fibs [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] __________________________ To print fibs for numbers set by user fibs = [0, 1] num = input('How many numbers do you want? ') for i in range(num-2): fibs.append(fibs[-2] + fibs[-1]) print fibs ______________________________________ Making your own function def hello(name): return 'Hello, '+ name +'!' >>> print hello('John') Hello, John! _______________________________________ def fibs(num): result = [0, 1] for i in range(num-2): result.append(result[-2] + result[-1]) return result >>> fibs(8) [0, 1, 1, 2, 3, 5, 8, 13] _______________________________________ 'Calculates the square of number x' return x*x >>> square(2) 4 >>> square.__doc__ 'Calculates the square of number x' >>> help(square) Help on