PYTHON PROGRAMMING (Arguments)
PYTHON PROGRAMMING (Arguments)
A function is a block of code which only The terms parameter and argument can
runs when it is called. be used for the same thing: information
that are passed into a function.
You can pass data, known as
parameters, into a function. From a function's perspective:
A function can return data as a result. o A parameter is the variable listed
inside the parentheses in the function
definition.
Creating a Function o An argument is the value that is sent to
the function when it is called.
In Python a function is defined using the
def keyword:
Example Number of Arguments
By default, a function must be called with
the correct number of arguments.
Meaning that if your function expects 2
Calling a Function arguments, you have to call the function
with 2 arguments, not more, and not less.
To call a function, use the function name
followed by parenthesis:
Example:
Arguments
Arbitrary Arguments, *args
Information can be passed into functions
as arguments. If you do not know how many arguments
that will be passed into your function,
Arguments are specified after the
add a * before the parameter name in
function name, inside the parentheses.
the function definition.
You can add as many arguments as you
want, just separate them with a comma. This way the function will receive a tuple
of arguments, and can access the items
The following example has a function with
accordingly:
one argument (fname). When the
function is called, we pass along a first
name, which is used inside the function to
print the full name:
Passing a List as an Argument
Keyword Arguments E.g. if you send a List as an argument, it
will still be a List when it reaches the
You can also send arguments with the
function:
key = value syntax.
This way the order of the arguments does
not matter.
Return Values
The phrase Keyword Arguments are often To let a function return a value, use the
shortened to kwargs in Python return statement:
documentations.
-------------------------------------------------------
Python Function Declaration
Here,
o def - keyword used to declare a
function.
o function_name - any name given to
the function.
o arguments - any value passed to
function.
o return (optional) - returns value from
a function.
Here,
o When the function is called, the
control of the program goes to the
function definition.
o All codes inside the function are
executed.
o The control of the program jumps to
the next statement after the function
call.
LECTURE
o L.count(x) – Returns the number of
items of L that are equal to x
o L.index(x) – Returns the index of the
first occurrence of an item in L that is
In the above example, we have used equal to c, or raises an exception if L
has no such item
o math.sqrt(4) - to compute the square
o L.append(x) – Appends item x to the
root of 4
end of L; e.g. L[len(L):]=[x]
o pow(2, 3) - computes the power of a
o L.extend(s) – Appends all the items
number i.e. 23
iterable s to the end of L
o L.insert(i, x) – Insert item x in L before
Here, notice the statement,
the item at index I, moving following
items of L(if any) “rightward” to make
space(increases len(L) by one, does
Since sqrt() is defined inside the math not replace any item, does not raise
module, we need to include it in our exceptions; acts just like L[i:i]=[x])
program. o L.pop([i]) – Returns the value of the
Benefits of Using Functions item at index I and removes in from L,
if i is omitted, removes and returns the
1. Code Reusable - We can use the last item; raises an exception on if L is
same function multiple times in our empty or i is an invalid index in L
program which makes our code o L.reverse( ) – Reverses, in place, items
reusable. For example, of L
# function definition
o L.remove(x) – Removes from L the first
def get_square(num): occurrence of an item in L that is
return num * num equal to x, or raises an exception if L
has no such item.
for i in [1,2,3]: o variable[start:stop:step] – obtains a
# function call portion of sequence from index start
result = get_square(i) up to but not including index stop
print('Square of',i, '=',result) o min() and max() – take one argument,
Output a nonempty iterable whose items are
comparable, and return the smallest
Square of 1 = 1 and largest items, respectively.
Square of 2 = 4
Square of 3 = 9