Learn Python 3 - Functions Cheatsheet - Codecademy
Learn Python 3 - Functions Cheatsheet - Codecademy
Functions
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 print(character + " is in " +
were passed as arguments when the function was setting + " practicing her " +
called, 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.
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
pencil_case):
a 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, == 'full'):
parameter names are placed one after another,
print ("I'm ready for school!")
separated by commas, within the parentheses of the
function definition.
Functions
Some tasks need to be performed multiple times within # Define a function my_function() with
a program. Rather than rewrite the same code in
parameter x
multiple 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
return x + 1
keyword followed by the value to return.
print(my_function(2)) # Output: 3
print(my_function(3 + 5)) # Output: 9
Function Indentation
Python uses indentation to identify blocks of code. # Indentation is used to identify code
Code within the same block should be indented at the
blocks
same 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 def testfunction(number):
be additional indentation within a function to handle
# This code is part of testfunction
other statements such as for and if so long as the
lines are not indented less than the first line of the print("Inside the testfunction")
function code. 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
cost):
function is called, these values are passed in as
arguments. print(grocery_store + " is selling " +
For example, the arguments passed into the function item_on_sale + " for " + cost)
.sales() are the “The Farmer’s Market”, “toothpaste”,
and “$1” which correspond to the parameters
grocery_store , item_on_sale , and cost . sales("The Farmer’s Market",
"toothpaste", "$1")
Function Keyword Arguments
Python functions can be defined with named def findvolume(length=1, width=1,
arguments which may have default values provided.
depth=1):
When function arguments are passed using their
names, they are referred to as keyword arguments. The print("Length = " + str(length))
use of keyword arguments when calling a function print("Width = " + str(width))
allows the arguments to be passed in any order — not
print("Depth = " + str(depth))
just the order that they were defined in the function. If
the function is invoked without a value for a specific return length * width * depth;
argument, the default value will be used.
findvolume(1, 2, 3)
findvolume(length=5, depth=2, width=4)
findvolume(2, depth=3, width=4)
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 = "Hello"
a 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 . print(a)
It is therefore accessible to prints_a , which will print
the value of a .
# will print "Hello"
prints_a()
Print Share