12/3/24, 3:17 PM Learn Python 3: Functions Cheatsheet | Codecademy
Cheatsheets / Learn Python 3
Functions
Lambda Functions
Lambda Functions
A lambda function in Python is a simple, anonymous
function that is defined without a name. Lambda
functions are useful when we want to write a quick
function in one line that can be combined with other
built-in functions such as map() , filter() , and
apply() . This is the syntax to define lambda functions:
lambda argument(s): expression
Function Parameters
Sometimes functions require input to provide data for def write_a_book(character, setting,
their code. This input is defined using parameters.
special_skill):
Parameters are variables that are defined in the function
definition. They are assigned the values which were print(character + " is in " +
passed as arguments when the function was called, setting + " practicing her " +
elsewhere in the code.
special_skill)
For example, the function definition defines parameters
for a character, a setting, and a skill, which are used as
inputs to write the first sentence of a book.
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 1/6
12/3/24, 3:17 PM Learn Python 3: Functions Cheatsheet | Codecademy
Multiple Parameters
Python functions can have multiple parameters. Just as def ready_for_school(backpack,
you wouldn’t go to school without both a backpack and a
pencil_case):
pencil case, functions may also need more than one input
to carry out their operations. if (backpack == 'full' and pencil_case
To define a function with multiple parameters, parameter == 'full'):
names are placed one after another, separated by
print ("I'm ready for school!")
commas, within the parentheses of the function
definition.
Functions
Some tasks need to be performed multiple times within a # Define a function my_function() with
program. Rather than rewrite the same code in multiple
parameter x
places, a function may be defined using the def
keyword. Function definitions may include parameters,
providing data input to the function. def my_function(x):
Functions may return a value using the return keyword
return x + 1
followed by the value to return.
# Invoke the function
print(my_function(2)) # Output: 3
print(my_function(3 + 5)) # Output: 9
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 2/6
12/3/24, 3:17 PM Learn Python 3: Functions Cheatsheet | Codecademy
Function Indentation
Python uses indentation to identify blocks of code. Code # Indentation is used to identify code
within the same block should be indented at the same
blocks
level. A Python function is one type of code block. All
code under a function declaration should be indented to
identify it as part of the function. There can be additional def testfunction(number):
indentation within a function to handle other statements
# This code is part of testfunction
such as for and if so long as the lines are not indented
less than the first line of the function code. print("Inside the testfunction")
sum = 0
for x in range(number):
# More indentation because 'for' has a
code block
# but still part of he function
sum += x
return sum
print("This is not part of testfunction")
Calling Functions
Python uses simple syntax to use, invoke, or call a doHomework()
preexisting function. A function can be called by writing
the name of it, followed by parentheses.
For example, the code provided would call the
doHomework() method.
Function Arguments
Parameters in python are variables — placeholders for def sales(grocery_store, item_on_sale,
the actual values the function needs. When the function
cost):
is called, these values are passed in as arguments.
For example, the arguments passed into the function print(grocery_store + " is selling " +
.sales() are the “The Farmer’s Market”, “toothpaste”, item_on_sale + " for " + cost)
and “$1” which correspond to the parameters
grocery_store , item_on_sale , and cost .
sales("The Farmer’s Market", "toothpaste",
"$1")
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 3/6
12/3/24, 3:17 PM Learn Python 3: Functions Cheatsheet | Codecademy
Function Keyword Arguments
Python functions can be defined with named arguments def findvolume(length=1, width=1,
which may have default values provided. When function
depth=1):
arguments are passed using their names, they are
referred to as keyword arguments. The use of keyword print("Length = " + str(length))
arguments when calling a function allows the arguments print("Width = " + str(width))
to be passed in any order — not just the order that they
print("Depth = " + str(depth))
were defined in the function. If the function is invoked
without a value for a specific argument, the default value return length * width * depth;
will be used.
findvolume(1, 2, 3)
findvolume(length=5, depth=2, width=4)
findvolume(2, depth=3, width=4)
Returning Multiple Values
Python functions are able to return multiple values using def square_point(x, y, z):
one return statement. All values that should be returned
x_squared = x * x
are listed after the return keyword and are separated by
commas. y_squared = y * y
In the example, the function square_point() returns z_squared = z * z
x_squared , y_squared , and z_squared . # Return all three values:
return x_squared, y_squared, z_squared
three_squared, four_squared, five_squared
= square_point(3, 4, 5)
The Scope of Variables
In Python, a variable defined inside a function is called a a = 5
local variable. It cannot be used outside of the scope of
the function, and attempting to do so without defining the
variable outside of the function will cause an error. def f1():
In the example, the variable a is defined both inside and a = 2
outside of the function. When the function f1() is
print(a)
implemented, a is printed as 2 because it is locally
defined to be so. However, when printing a outside of
the function, a is printed as 5 because it is print(a) # Will print 5
implemented outside of the scope of the function. f1() # Will print 2
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 4/6
12/3/24, 3:17 PM Learn Python 3: Functions Cheatsheet | Codecademy
Returning Value from Function
A return keyword is used to return a value from a def check_leap_year(year):
Python function. The value returned from a function can
if year % 4 == 0:
be assigned to a variable which can then be used in the
program. return str(year) + " is a leap year."
In the example, the function check_leap_year returns a else:
string which indicates if the passed parameter is a leap
return str(year) + " is not a leap
year or not.
year."
year_to_check = 2018
returned_value =
check_leap_year(year_to_check)
print(returned_value) # 2018 is not a leap
year.
Global Variables
A variable that is defined outside of a function is called a a = "Hello"
global variable. It can be accessed inside the body of a
function.
In the example, the variable a is a global variable def prints_a():
because it is defined outside of the function prints_a . It print(a)
is therefore accessible to prints_a , which will print the
value of a .
# will print "Hello"
prints_a()
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 5/6
12/3/24, 3:17 PM Learn Python 3: Functions Cheatsheet | Codecademy
Parameters as Local Variables
Function parameters behave identically to a function’s def my_function(value):
local variables. They are initialized with the values passed
print(value)
into the function when it was called.
Like local variables, parameters cannot be referenced
from outside the scope of the function. # Pass the value 7 into the function
In the example, the parameter value is defined as part
my_function(7)
of the definition of my_function , and therefore can
only be accessed within my_function . Attempting to
print the contents of value from outside the function # Causes an error as `value` no longer
causes an error. exists
print(value)
Print Share
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 6/6