0% found this document useful (0 votes)
28 views

Learn Python 3 - Functions Cheatsheet - Codecademy

Functions allow code to be reused by defining blocks of code that can be "called" or invoked multiple times from different places in a program. Functions take optional input parameters and can return values. Parameters are placeholders that get assigned argument values when the function is invoked. Functions improve code organization and reuse.

Uploaded by

Riaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Learn Python 3 - Functions Cheatsheet - Codecademy

Functions allow code to be reused by defining blocks of code that can be "called" or invoked multiple times from different places in a program. Functions take optional input parameters and can return values. Parameters are placeholders that get assigned argument values when the function is invoked. Functions improve code organization and reuse.

Uploaded by

Riaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

09/12/2023, 08:09 Learn Python 3: Functions Cheatsheet | Codecademy

Cheatsheets / Learn Python 3

Functions

Function Parameters

Sometimes functions require input to provide data for their code. This input is defined def write_a_book(character, setting, special_skill):
using parameters.
print(character + " is in " +
Parameters are variables that are defined in the function definition. They are assigned
the values which were passed as arguments when the function was called, elsewhere in setting + " practicing her " +
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 you wouldn’t go to school def ready_for_school(backpack, pencil_case):
without both a backpack and a pencil case, functions may also need more than one
if (backpack == 'full' and pencil_case == 'full'):
input to carry out their operations.
To define a function with multiple parameters, parameter names are placed one after print ("I'm ready for school!")
another, separated by commas, within the parentheses of the function definition.

https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 1/6
09/12/2023, 08:09 Learn Python 3: Functions Cheatsheet | Codecademy

Functions

Some tasks need to be performed multiple times within a program. Rather than rewrite # Define a function my_function() with parameter x
the same code in multiple places, a function may be defined using the def keyword.
Function definitions may include parameters, providing data input to the function.
Functions may return a value using the return keyword followed by the value to def my_function(x):
return. return x + 1

# Invoke the function

print(my_function(2)) # Output: 3
print(my_function(3 + 5)) # Output: 9

Function Indentation

Python uses indentation to identify blocks of code. Code within the same block should # Indentation is used to identify code blocks
be indented at the 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 be additional indentation within a function to handle other statements such def testfunction(number):
as for and if so long as the lines are not indented less than the first line of the # This code is part of testfunction
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")

https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 2/6
09/12/2023, 08:09 Learn Python 3: Functions Cheatsheet | Codecademy

Calling Functions

Python uses simple syntax to use, invoke, or call a preexisting function. A function can doHomework()
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 the actual values the function def sales(grocery_store, item_on_sale, cost):
needs. When the function is called, these values are passed in as arguments.
print(grocery_store + " is selling " + item_on_sale + " for
For example, the arguments passed into the function .sales() are the “The Farmer’s
Market”, “toothpaste”, and “$1” which correspond to the parameters grocery_store , " + cost)
item_on_sale , and cost .
sales("The Farmer’s Market", "toothpaste", "$1")

Function Keyword Arguments

Python functions can be defined with named arguments which may have default values def findvolume(length=1, width=1, depth=1):
provided. When function arguments are passed using their names, they are referred to
print("Length = " + str(length))
as keyword arguments. The use of keyword arguments when calling a function allows
the arguments to be passed in any order — not just the order that they were defined in print("Width = " + str(width))
the function. If the function is invoked without a value for a specific argument, the print("Depth = " + str(depth))
default value will be used.
return length * width * depth;

findvolume(1, 2, 3)
findvolume(length=5, depth=2, width=4)
findvolume(2, depth=3, width=4)

https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 3/6
09/12/2023, 08:09 Learn Python 3: Functions Cheatsheet | Codecademy

Returning Multiple Values

Python functions are able to return multiple values using one return statement. All def square_point(x, y, z):
values that should be returned are listed after the return keyword and are separated
x_squared = x * x
by commas.
In the example, the function square_point() returns x_squared , y_squared , and y_squared = y * y
z_squared . z_squared = z * z
# 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 local variable. It cannot be a = 5


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.
In the example, the variable a is defined both inside and outside of the function. def f1():
When the function f1() is implemented, a is printed as 2 because it is locally a = 2
defined to be so. However, when printing a outside of the function, a is printed as
print(a)
5 because it is implemented outside of the scope of the function.

print(a) # Will print 5


f1() # Will print 2

https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 4/6
09/12/2023, 08:09 Learn Python 3: Functions Cheatsheet | Codecademy

Returning Value from Function

A return keyword is used to return a value from a Python function. The value returned def check_leap_year(year):
from a function can be assigned to a variable which can then be used in the program.
if year % 4 == 0:
In the example, the function check_leap_year returns a string which indicates if the
passed parameter is a leap year or not. return str(year) + " is a leap year."
else:
return str(year) + " is not a leap 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 global variable. It can be a = "Hello"
accessed inside the body of a function.
In the example, the variable a is a global variable because it is defined outside of the
function prints_a . It is therefore accessible to prints_a , which will print the value of def prints_a():
a. print(a)

# will print "Hello"


prints_a()

https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 5/6
09/12/2023, 08:09 Learn Python 3: Functions Cheatsheet | Codecademy

Parameters as Local Variables

Function parameters behave identically to a function’s local variables. They are def my_function(value):
initialized with the values passed into the function when it was called.
print(value)
Like local variables, parameters cannot be referenced from outside the scope of the
function.
In the example, the parameter value is defined as part of the definition of # Pass the value 7 into the function
my_function , and therefore can only be accessed within my_function . Attempting
my_function(7)
to print the contents of value from outside the function causes an error.

# Causes an error as `value` no longer exists


print(value)

Print Share

https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 6/6

You might also like