Exploring Python Functions Reading - Functions
Exploring Python Functions Reading - Functions
Exploring Python Functions Reading - Functions
Lab Objectives:
By the end of this reading, you should be able to:
Introduction to Functions:
A function is a fundamental building block that encapsulates a specific set of actions or computations. Just as in
mathematics, where functions take inputs and produce outputs, programming functions perform a similar role.
They take inputs, execute a series of predefined actions or calculations, and then return an output.
Purpose of Functions:
The primary purpose of functions is to promote code modularity and reusability. Imagine you have a task that
needs to be performed multiple times within a program. Instead of duplicating the same code at various places,
you can define a function once and call it whenever that task is needed. This not only reduces redundancy but
also makes the code easier to manage and maintain.
Inputs (Parameters):
about:blank 1/9
9/28/23, 1:58 PM about:blank
Functions are designed to operate on data, and they can receive data as input. These inputs are known as
parameters or arguments. Parameters provide functions with the necessary information they need to perform
their tasks. Think of parameters as values that you pass to a function, allowing it to work with specific data.
Performing Tasks:
Once a function receives its input (parameters), it executes a set of predefined actions or computations. These
actions can include calculations, operations on data, or even more complex tasks. The purpose of a function
determines the tasks it performs. For instance, a function could calculate the sum of numbers, sort a list, format
text, or fetch data from a database.
Producing Outputs:
After performing its tasks, a function can produce an output. This output is the result of the operations carried
out within the function. It's the value that the function “returns” to the code that called it. Think of the output as
the end product of the function's work. You can use this output in your code, assign it to variables, pass it to
other functions, or even print it out for display.
Example:
Consider a function named calculate_total that takes two numbers as input (parameters), adds them together,
performs the addition task, and then produces the sum as the output. Here's how it works:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
Copied!
Python comes equipped with a rich set of built-in functions that provide a wide range of functionalities. These
functions are readily available for you to use, and you don't need to be concerned about how they are
implemented internally. Instead, you can focus on understanding what each function does and how to use it
effectively.
To use a built-in function, you simply call the function's name followed by parentheses. Any required arguments
or parameters are passed within these parentheses. The function then performs its predefined task and may return
an output that you can use in your code.
about:blank 2/9
9/28/23, 1:58 PM about:blank
1. 1
2. 2
Copied!
1. 1
Copied!
1. 1
Copied!
1. 1
Copied!
Python's built-in functions offer a wide array of functionalities, from basic operations like len() and sum() to
more specialized tasks.
1. 1
2. 2
1. def function_name():
2. pass
Copied!
A "pass" statement in a programming function is a placeholder or a no-op (no operation) statement. It is used
when you want to define a function or a code block syntactically but do not want to specify any functionality or
implementation at that moment.
Placeholder: "pass" acts as a temporary placeholder for future code that you intend to write within a
function or a code block.
about:blank 3/9
9/28/23, 1:58 PM about:blank
Syntax Requirement: In many programming languages like Python, using "pass" is necessary when you
define a function or a conditional block. It ensures that the code remains syntactically correct, even if it
doesn't do anything yet.
No Operation: "pass" itself doesn't perform any meaningful action. When the interpreter encounters
“pass”, it simply moves on to the next statement without executing any code.
Function Parameters:
Example:
1. 1
2. 2
3. 3
4. 4
5. 5
1. def greet(name):
2. print("Hello, " + name)
3.
4. result = greet("Alice")
5. print(result) # Output: Hello, Alice
Copied!
Example:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
Copied!
Return Statement:
about:blank 4/9
9/28/23, 1:58 PM about:blank
Example:
1. 1
2. 2
3. 3
4. 4
Copied!
Example:
1. 1
Copied!
This line initializes a global variable called global_variable and assigns it the value “I’m global”.
Global variables are accessible throughout the entire program, both inside and outside functions.
1. 1
2. 2
3. 3
4. 4
1. def example_function():
2. local_variable = "I'm local"
3. print(global_variable) # Accessing global variable
4. print(local_variable) # Accessing local variable
Copied!
about:blank 5/9
9/28/23, 1:58 PM about:blank
A local variable named local_variable is declared and initialized with the string value “I'm local.” This
variable is local to the function and can only be accessed within the function's scope.
The function then prints the values of both the global variable (global_variable) and the local variable
(local_variable). It demonstrates that you can access both global and local variables from within a
function.
1. 1
1. example_function()
Copied!
In this part, we call the example_function() by invoking it. This results in the function's code being executed.
As a result of this function call, it will print the values of the global and local variables within the function.
1. 1
Copied!
After calling the function, we print the value of the global variable global_variable outside of the function. This
demonstrates that global variables are accessible both inside and outside of functions.
1. 1
Copied!
In this part, we are attempting to print the value of the local variable local_variable outside of the function.
However, this line would result in an error.
Local variables are only visible and accessible within the scope of the function where they are
defined.
Example:
about:blank 6/9
9/28/23, 1:58 PM about:blank
1. 1
2. 2
3. 3
4. 4
5. 5
1. def print_numbers(limit):
2. for i in range(1, limit+1):
3. print(i)
4.
5. print_numbers(5) # Output: 1 2 3 4 5
Copied!
Example
1. 1
2. 2
3. 3
4. 4
5. 5
1. def greet(name):
2. return "Hello, " + name
3.
4. for _ in range(3):
5. print(greet("Alice"))
Copied!
We'll use Python and a list as the data structure for this illustration. In this example, we will create functions to
add and remove elements from a list.
1. 1
2. 2
Copied!
In this part, we start by creating an empty list named my_list. This empty list serves as the data structure that we
will modify throughout the code.
1. 1
2. 2
3. 3
about:blank 7/9
9/28/23, 1:58 PM about:blank
1. # Function to add an element to the list
2. def add_element(data_structure, element):
3. data_structure.append(element)
Copied!
Here, we define a function called add_element. This function takes two parameters:
data_structure: This parameter represents the list to which we want to add an element.
element: This parameter represents the element we want to add to the list.
Inside the function, we use the append method to add the provided element to the data_structure, which is
assumed to be a list.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
Copied!
In this part, we define another function called remove_element. It also takes two parameters:
Inside the function, we use conditional statements to check if element is present in the data_structure. If it is, we
use the remove method to remove the first occurrence of element. If it's not found, we print a message indicating
that the element was not found in the list.
1. 1
2. 2
3. 3
4. 4
Copied!
Here, we use the add_element function to add three elements (42, 17, and 99) to the my_list. These are added
one at a time using function calls.
Copied!
This part simply prints the current state of the my_list to the console, allowing us to see the elements that have
been added so far.
1. 1
2. 2
3. 3
Copied!
In this part, we use the remove_element function to remove elements from the my_list. First, we attempt to
remove 17 (which is in the list), and then we try to remove 55 (which is not in the list). The second call to
remove_element will print a message indicating that 55 was not found.
1. 1
2. 2
Copied!
Finally, we print the updated my_list to the console. This allows us to observe the modifications made to the list
by adding and removing elements using the defined functions.
Conclusion:
Congratulations! You've completed the Reading Instruction Lab on Python functions. You've gained a solid
understanding of functions, their significance, and how to create and use them effectively. These skills will
empower you to write more organized, modular, and powerful code in your Python projects.
Author
Akansha Yadav
Changelog
Date Version Changed by Change Description
2023-08-24 1.0 Akansha Yadav Created reading
about:blank 9/9