#defining a function #the keyword 'def' introduces a function definition #the function name follows #parenthesis follow the name and include within these are the parameters #the following statements must be indented def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 while a < n: print a, a, b = b, a+b #call the function we just defined: fib(2000) print'' #renaming a function f = fib f(10) print'' #function example 2 def fib2(n): # return Fibonacci series up to n """Return a list containing the Fibonacci series up to n.""" result = [] a, b = 0, 1 while a < n: result.append(a) # adds the value of a to the end of the result[] List a, b = b, a+b return result # return outputs the value of a function. without an expression after return, the ouput = None fib_res = fib2(100) print fib_res
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question