















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Python for Everybody www.py4e.com
Output: Hello Fun Zip Hello Fun Program: def thing(): print('Hello') print('Fun') thing() print('Zip') thing() def print('Hello') print('Fun') thing() We call these reusable pieces of code “functions” thing(): thing() print('Zip')
big = max('Hello world') print(big) w tiny = min('Hello world') print(tiny)
Argument 'w' Result Assignment
big = max('Hello world') print(big) w def max(inp): blah blah for x in inp: blah blah 'Hello world' (a string) 'w' (a string) A function is some stored code that we use. A function takes some input and produces an output. Guido wrote this code
Functions of Our Own…
x = 5 print('Hello') def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print('Yo') x = x + 2 print(x) Hello Yo 7 print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print_lyrics():
Argument
A parameter is a variable which we use in the function definition. It is a “handle” that allows the code in the function to access the arguments for a particular function invocation.
def greet(lang): ... if lang == 'es': ... print('Hola') ... elif lang == 'fr':
big = max('Hello world') >>> print(big) w def max(inp): blah blah for x in inp: blah blah 'Hello world' (a string) 'w' (a string) A function is some stored code that we use. A function takes some input and produces an output. Guido wrote this code Functions of Our Own… x = 5 print('Hello') def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print('Yo') x = x + 2 print(x) Hello Yo 7 print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print_lyrics(): ## Definitions and Uses - Once we have defined a function, we can call (or invoke) it as many times as we like - This is the store and reuse pattern ## Arguments - An argument is a value we pass into the function as its input when we call the function - We use arguments so we can direct the function to do different kinds of work when we call it at different times - We put the arguments in parentheses after the name of the function ## big = max('Hello world') Argument ## Parameters A parameter is a variable which we use in the function definition. It is a “handle” that allows the code in the function to access the arguments for a particular function invocation. >>> def greet(lang): ... if lang == 'es': ... print('Hola') ... elif lang == 'fr': ... print('Bonjour') ... else: print(big) w def max(inp): blah blah for x in inp: blah blah 'Hello world' (a string) 'w' (a string) A function is some stored code that we use. A function takes some input and produces an output. Guido wrote this code Functions of Our Own… x = 5 print('Hello') def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print('Yo') x = x + 2 print(x) Hello Yo 7 print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print_lyrics(): ## Definitions and Uses - Once we have defined a function, we can call (or invoke) it as many times as we like - This is the store and reuse pattern ## Arguments - An argument is a value we pass into the function as its input when we call the function - We use arguments so we can direct the function to do different kinds of work when we call it at different times - We put the arguments in parentheses after the name of the function ## big = max('Hello world') Argument ## Parameters A parameter is a variable which we use in the function definition. It is a “handle” that allows the code in the function to access the arguments for a particular function invocation. >>> def greet(lang): ... if lang == 'es': ... print('Hola') ... elif lang == 'fr': ... print('Bonjour') ... else: ... print('Hello') ... greet('en') Hello greet('es') Hola greet('fr') Bonjour
Arguments, Parameters, and Results
big = max('Hello world') print(big) w def max(inp): blah blah for x in inp: blah blah return 'w' 'Hello world' 'w' Argument Parameter Result
function