Python
Functions
Session IV
Norms
Take Notes! Do It With Me! Participate!
Agenda
1. Functions!
2. What are methods?
3. Lambda functions!
What are Functions?
Functions!
● Formally, a function is a useful device that
groups together a set of statements so
they can be run more than once.
● They can also let us specify parameters
that can serve as inputs to the functions.
Functions!
● On a more fundamental level, functions
allow us to not have to repeatedly write
the same code again and again.
● Functions will be one of most basic
levels of reusing code in Python, and it
will also allow us to start thinking of
program design
When to use Functions?
Functions!
● You plan on using a block of code multiple
times.
● You are supposed to call the same block of
code without having to write it multiple
times.
● You to create more complex Python
scripts.
How to create Functions?
Functions!
Creating a function: In Python a function is defined using the def keyword.
Calling a function: To call a function, use the function name followed by
parenthesis.
Functions!
Have you understood
how to create a function?
What are Arguments?
Arguments!
● Information can be passed into
functions as arguments.
● Specified after the function name,
inside the parentheses. You can add
as many arguments as you want, just
separate them with a comma.
Arguments!
What are Parameters?
Parameters!
● Variables or data that we want our
function to work on, e.g. numbers to
be added.
● Can have any number of parameters
be accepted in your function or no
parameters if your function does not
need it.
What is the difference between
Parameters & Arguments?
Parameters & Arguments!
● The terms parameter and argument can
be used for the same thing: information
that are passed into a function.
● But, 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.
Activity!
● Create a new function with all the rules
getting satisfied!!!
What are the types of Arguments?
Types of Arguments!
1. Default Arguments
2. Arbitrary Arguments
3. Keyword Arguments
Default Arguments!
● Those arguments that take default
values if no explicit values are passed
to these arguments from the function
call. Let's define a function with one
default argument.
Default Arguments!
Arbitrary Arguments!
● As the name suggests, sometimes the
programmer does not know the number of
arguments to be passed into the function.
In such cases, Python arbitrary arguments
are used.
● In this, we use the asterisk (*) to denote
this method before the parameter in the
function. This is also called as Python *args.
Arbitrary Arguments!
● Python *args allows a function to accept
any number of positional arguments i.e.
arguments which are non-keyword
arguments, variable-length argument list.
Arbitrary Arguments!
Keyword Arguments!
● Keyword arguments (or named arguments)
are values that, when passed into a function,
are identifiable by specific parameter names.
A keyword argument is preceded by a
parameter and the assignment operator, = .
● Keyword arguments can be likened to
dictionaries in that they map a value to a
keyword.
Keyword Arguments!
● You can also send arguments with the key = value syntax.
● This way the order of the arguments does not matter.
Activity!
● Create a default argument that gives cubes
of given integers.
○ 12, 15, 18, 9
● Add all the cube values by using arbitrary
arguments.
What are methods?
Methods!
● A method in python is somewhat similar to a
function, except it is associated with
object/classes.
● Here object can be list, set,tuple or
dictionary.
○ List Methods
○ Set Methods
○ Tuple Methods
○ Dictionary Methods
List Methods!
Python has a set of built-in methods that you can use on lists.
List Methods!
Set Methods!
Python has a set of built-in methods that you can use on sets.
Set Methods!
Tuple Methods!
Python has two built-in methods that you can use on tuples.
Dictionary Methods!
Python has a set of built-in methods that you can use on dictionaries.
Activity!
● Evaluate the difference between two sets
given below.
○ set A = {10, 20, 30, 40, 80}
○ set B = {100, 30, 80, 40, 60}
● Add an element from set A.
● Remove all the elements from set B.
What is Lambda Function?
Lambda Function!
● A lambda function is a small anonymous
function.
● It can take any number of arguments, but
can only have one expression.
● A lambda function evaluates an expression
for a given argument.
Lambda Function!
Activity!
● Return the values which are less than 6
from the list given below, by using lambda
function.
○ [10, 2, 8, 7, 5, 4, 3, 11, 0, 1]
What we learnt today!
● Functions
● Parameters
● Arguments
● Methods
● Lambda function
Time for Feedback!
1. Click on the feedback form link.
2. Enter your personal details.
3. Rate the session based on
a. Overall Experience
b. Trainer
c. Content
4. Then click on Submit…!
Doubts?
Key Takeaways
What is your one key takeaway from the session?