Learn Python 3_ Functions Cheatsheet _ Codecademy
Learn Python 3_ Functions Cheatsheet _ Codecademy
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:
Function Parameters
Sometimes functions require input to provide data for def write_a_book(character, setting,
their code. This input is defined using parameters.
Parameters are variables that are defined in the
special_skill):
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
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
findvolume(1, 2, 3)
findvolume(length=5, depth=2, width=4)
findvolume(2, depth=3, width=4)
Python functions are able to return multiple values def square_point(x, y, z):
using one return statement. All values that should
x_squared = x * x
be returned are listed after the return keyword
and are separated by commas. y_squared = y * y
In the example, the function square_point() z_squared = z * z
returns x_squared , y_squared , and # Return all three values:
z_squared .
return x_squared, y_squared, z_squared
year_to_check = 2018
returned_value =
check_leap_year(year_to_check)
print(returned_value) # 2018 is not a
leap year.
Global Variables
Print Share