FUNCTIONS
FUNCTIONS
We have been using functions in Python since the first chapter. These functions
include print, input, int, float, str, and type. The Python standard library includes
many other functions useful for common programming tasks.
In mathematics, a function computes a result from a given value. In Python, a
function is a named block of code that performs a specific task. If an executing
program needs to perform such a task, it calls upon the function to do the work.
FUNCTIONS
FUNCTIONS
[Link]
FUNCTION BASICS
There are two aspects to every Python function:
• Function definition. The definition of a function contains the code that
determines the function’s behavior.
• Function invocation. A function is used within a program via a
function invocation. Every function has exactly one definition but may have
many invocations.
FUNCTION BASICS
An ordinary function definition consists of four parts:
• def
The def keyword introduces a function definition.
• Name
The name is an identifier. As with variable names, the name
chosen for a function should accurately portray its intended
purpose or describe its functionality.
FUNCTION BASICS
• Parameters
every function definition specifies the parameters that it accepts from
callers. The parameters appear in a parenthesized comma-separated list.
The list of parameters is empty if the function requires no information
from code that calls the function. A colon follows the parameter list.
• Body
every function definition has a block of indented statements that
constitute the function’s body. The body contains the code to execute
when callers invoke the function. The code within the body is responsible
for producing the result, if any, to return to the caller.
FUNCTION DEFINITION
FUNCTION
In Python a function is defined using the def keyword:
Creating a
def my_function(): function
print("Hello from a function")
Calling a
my_function() function
Parameter List or Arguments
The terms parameter and argument can be used for the same thing:
information that are passed into a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the
function definition.
An argument is the value that is sent to the function when it is called.
Parameter List or Arguments?
def my_function(fname):
print(" Hello " + fname)
my_function("MJ")
my_function("JM")
my_function("MJS")
Arguments are often shortened to args in Python documentations.
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can
add as many arguments as you want, just separate them with a comma.
Parameter List or Arguments
def my_function(fname, lname):
print(fname + " " + lname)
my_function("MJ", "Sarmiento")
Function
def double(n):
return 2 * n
x = double(3)
print(x)
Function
Function
Function
Function
Python Collection Arrays
There are four collection data types in the Python programming language:
● List is a collection which is ordered and changeable. Allows duplicate
members.
● Tuple is a collection which is ordered and unchangeable. Allows duplicate
members.
● Set is a collection which is unordered, unchangeable*, and unindexed. No
duplicate members.
● Dictionary is a collection which is ordered** and changeable. No duplicate
members.
LIST
List
The list is one of the most flexible data types available in
Python and can be expressed as a list of values (items) that
are separated by commas enclosed in square brackets.
A list can contain values of mixed data types. Meaning, we can
create a list that contains a string, an int, and a float
altogether. Generally though, it’s
recommended to store values in a list of the same data type.
Creating List - Elements are added to a list in Python by enclosing them in
square brackets [] and separating them with commas.
empty_list = []
list_of_integers = [1, 2, 3]
list_with_mixed_data_types = [1, "Hello", 3.4]
nested_list = [[1, 2, 3], [30, 10, 20], [-7, -5]]
print("empty list : ", empty_list)
print("list of integers : ", list_of_integers)
print("list with mixed data types : ", list_with_mixed_data_types)
print("nested list : ", nested_list)
Accessing the List
There are various ways in which we can access the elements of a list.
Any other index attempts will result in an IndexError being raised. An index should
be an integer. We are not allowed to utilize floats or other kinds; doing so will cause
a TypeError.
Nested indexing is used to retrieve nested listings.
Python lists are also mutable; once they are declared, a variety of list functions can
quickly alter them. The fact that a list’s components do not have to be of the same
type is crucial.
Accessing the List
my_list = ['p', 'r', 'o', 'b', 'e']
print("my_list[0] : ", my_list[0])
print("my_list[2] : ", my_list[2])
print("my_list[4] : ", my_list[4])
n_list = ["Happy", [2, 0, 1, 5]]
print("n_list[0] : ", n_list[0])
print("n_list[1] : ", n_list[1])
print("n_list[1][0] : ", n_list[1][0])
my_list = ['p', 'r', 'o', 'b', 'e']
print("my_list[-1]: ", my_list[-1])