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

PYTHON PROGRAMMING (Arguments)

Python functions allow for code reuse through defining blocks of code that can be executed by calling the function. Functions may take in parameters and return values. Parameters are variables passed into a function that can be used within the function body, while arguments are the specific values passed to a function when calling it. Functions improve code readability and reusability. Common built-in Python functions include print(), sqrt(), and pow() that are used by including the module name before the function.

Uploaded by

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

PYTHON PROGRAMMING (Arguments)

Python functions allow for code reuse through defining blocks of code that can be executed by calling the function. Functions may take in parameters and return values. Parameters are variables passed into a function that can be used within the function body, while arguments are the specific values passed to a function when calling it. Functions improve code readability and reusability. Common built-in Python functions include print(), sqrt(), and pow() that are used by including the module name before the function.

Uploaded by

Gail Agad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Functions Parameters or 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.

Arbitrary Keyword Arguments, **kwargs


This way the function will receive a
dictionary of arguments, and can access
the items accordingly:
The pass Statement
function definitions cannot be empty, but
if you for some reason have a function
definition with no content, put in the pass
statement to avoid getting an error.

Default Parameter Value


If we call the function without argument,
it uses the default value:

-------------------------------------------------------
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.

Python Function Arguments


As mentioned earlier, a function can also
have arguments. An argument is a value
that is accepted by a function. For
example, Python Library Functions
In Python, standard library functions are
the built-in functions that can be used
directly in our program. For example,
o print() - prints the string inside the
quotation marks
If we create a function with arguments, o sqrt() - returns the square root of a
we need to pass the corresponding number
values while calling them. For example, o pow() - returns the power of a number
Hence, the same method is used again
and again.
2. Code Readability - Functions help us
break our code into chunks to make
our program readable and easy to
understand.

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

You might also like