This document discusses Python functions. It defines a function as a reusable block of code that performs a specific task. Functions help break programs into smaller, modular chunks. The key components of a function definition are the def keyword, the function name, parameters in parentheses, and a colon. Functions can take different types of arguments, including positional, default, keyword, and variable length arguments. Objects like lists, dictionaries, and sets are mutable and can change, while numbers, strings, tuples are immutable and cannot change. The document provides examples of passing list, tuples, and dictionaries to functions using techniques like tuples, asterisk operators, and double asterisk operators.
2. TOPIC COVERED
1. FUNCTION
2. CONCEPT
3. PARAMETER PASSING
4. TYPES OF FUNCTION
5. PROPERTIES OF FUNCTION
6. TYPES OF PARAMETERS
7. PASSING LIST TUPLES,
DICTIONERIES
8. INBUILT FUNCTIONS
3. FUNCTION IS A GROUP OF RELATED
STATEMENTS THAT PERFORM A SPECIFIC
TASK.
A FUNCTION CAN BE DEFINED AS THE
ORGANIZED BLOCK OF REUSABLE CODE
WHICH CAN BE CALLED WHENEVER
REQUIRED
FUNCTIONS HELP BREAK OUR PROGRAM INTO
SMALLER AND MODULAR CHUCKS
FUNCTION
4. DEFINING FUNCTION
def marks the start of function
function name to uniquely
identifya function.
def function_name (parameter) :
Argument to pass avalue in function
colon(:) to mark end of
function header
7. BRIEF COMPONENTS REQUIRED
A FUNCTION IS DEFINED WITH KEYWORD DEF WHICH IS FOLLWED BY
NAME(UNIQUE) OF FUNCTION, THEN PARENTHESIS AND AFTER THAT :
EX. DEF SAMPLE() :
A FUNCTION IS CALLED/INVOKED BY WRITING NAME OF FUNCTION
EXAMPLE SAMPLE()
A FUNCTION CAN BE CALLED NUMBER OF TIMES.
THE ORDER IN WHICH FUNCTION IS CALLED AND THE ORDER IN WHICH
FUNCTION IS DEFINED NEED NOT NECESSARILY BE SAME.
8. BRIEF COMPONENTS REQUIRED
WHEN A FUNCTION IS CALLED THE CONTROL IS TRANSFERRED TO THE PLACE
WHERE FUNCTION IS DEFINED AND WHEN EXECUTION/CALCULATIONC IS
OVER THEN CONTROL IS RETURN BACK TO MAIN.
A FUNCTION IS CALLED IN ANY OTHER FUNCTION BUT A FUNCTION CAN NOT
BE DEFIND IN ANY OTHER FUNCTION.
AN OPTIONAL RETURN STATEMENT TO RETURN A VALUE FROM THE
FUNCTION.
9. TYPES OF FUNCTION
(PASSING & RETURNING)
BASICALLY THERE ARE 4 TYPES OF FUNCTION IN USE
ON THE BASIS OF PASSING ARGUMENTS AND
ACCEPTING IN PARAMETER :-
NOT PASSING AND NOT RETURNING
PASSING AND NOT RETURNING
PASSING AND RETURNING
NOT PASSING AND RETURNING *
10. PARAMETER & ARGUMENT
PARAMENTERS ARE THE VALUE(S)
PROVIDED IN THE PARENTHESIS WHEN WE
WRITE FUNCTION HEADER. THESE ARE THE
VALUES REQUIRED BY FUNCTION TO
WORK.
FOR EXAMPLE :--- def simp_int(p, r, t):
ARGUMENTS ARE THE VALUE THAT IS
PASSED TO THE FUNCTION WHEN IT IS
CALLED.
FOR EXAMPLE :--- simp_int(4000,5,10)
PARAMENTERS
ARGUMENTS
16. POSITIONAL ARGUMENTS ARE THE ARGUMENTS PASSED TO A FUNCTION
IN CORRECT POSITIONAL ORDER.
IF WE CHANGE THE ORDER THEN THE RESULT IS CHANGED.
POSITIONAL ARGUMENTS
17. Default value to function argument is passed using
assignment operator = .
Example:
def sample(name, msg = "Good morning!"):
print("Hello, " name + ', ' + msg)
Non-default argument cannot follow default argument
Example: def greet(msg = "Good morning!", name):
SyntaxError:
non-defaultargument followsdefaultargument
DEFAULT ARGUMETNS
18. IF THERE IS A FUNCTION WITH MANY
PARAMETER AND WE WANT TO
SPECIFY ONLY SOME OF THEM IN
FUNCTION CALL, THEN VALUE FOR
SUCH PARAMETERS CAN BE PROVIDED
BY THEIR NAME INSTEAD OF THE
POSITION.
SO IN KEYWORD ARGUMENTS WE CAN
PASS ARGUMENTS VALUES BY
KEYWORD I.E BY PARAMETER NAME
KEYWORD ARGUMENTS
19. greet(name = "Bruce",msg = "How do you do?")
2 keyword arguments
greet(msg = "How do you do?",name = "Bruce")
2 keyword arguments (out of order)
greet("Bruce",msg = "How do you do?")
1 positional, 1 keyword argument
greet(name="Bruce","How doyou do?")
SyntaxError:
non-keyword arg after keyword arg
EXAMPLE OF KEYWORD ARGUMENTS
20. WHEN WE WRITE FUNCTION SUM TO ADD TWO NUMBER AND
RETURN ADDITIION BACK THEN IT ADDS ALWAYS TWO VALUES.
NOW IMAGINE WE WANT TO CREATE A SUM FUNCTION THAT
CAN TAKE ANY NUMBER OF ARGUMENTS (LIKE 2,3,4,5.) SO
IN THIS SITUATION THE ABOVE SUM FUNCTION WE CAN PASS
TWO ARGUMENTS.
PYTHON ALLOWS US TO CREATE FUNCTION THAT CAN TAKE
MULTIPLE ARGUMENTS. SUCH TYPE OF ARGUMENTS ARE
CALLED VARIABLE LENGTH ARGUMENTS/PARAMETERS.
VARIABLE LENGTH ARGUMENTS ARE DECLARED WITH * SYMBOL
IN PYTHON
VARIABLE LENGTH ARGUMENTS
22. MUTABLE/IMMUTABLE PROPERTIES OF DATA
OBJECTS
A MUTABLE OBJECTS CAN CHANGE ITS STATE OR
CONTENTS AND IMMUATABLE OBJECTS CANNOT.
MUTABLE OBJECTS ARE :
LIST,DICT,SET, BYTE ARRAY
IMMUATABLE OBJECTS ARE:
INT,FLOAT,COMPLEX,STRING,TUPLE,BYTES
THE EXAMPLE SHOWING CONCEPT IS :-
23. MUTABLE/IMMUTABLE PROPERTIES OF DATA
OBJECTS
THE OBJECT IN WHICH X WAS TAGGED IS CHANGED. OBJECT 10 WAS
NEVER MODIFIED. IMMUTABLE OBJECTS DOESNT ALLOW MODIFICATION
AFTER CREATING
24. MUTABLE/IMMUTABLE PROPERTIES OF DATA
OBJECTS
SO IT MEANS FROM ABOVE EXAMPLES:
PYTHON HANDLES MUTABLE AND IMMUATABLE OBJECTS
DIFFERENTLY.
IMMUTABLE ARE QUICKER TO ACCESS THAN MUTABLE
OBJECTS.
MUTABLE OBJECTS ARE GREAT TO USE WHEN YOU NEED TO
CHANGE THE SIZE OF THE OBJECT, EXAMPLE LIST, DICT
ETC. IMMUATABLE ARE USED WHEN YOU NEED TO ENSURE
THAT EH OBJECT YOU MADE WILL STAY THE SAME.
IMMUTABLE OBJECTS ARE FUNDAMENTALLY EXPENSIVE TO
CHANGE , BECAUSE DOING SO INVOLVES CREATING A
COPY. CHANGING MUTABLES OBJECTS IS CHEAP.
25. A LIST CAN BE PASSED AS ARGUMENTS TO A FUNCTION SIMILAR TO
PASSING NORMAL VARIABLE AS ARGUMENTS TO A FUNCTION:
Method #1 : Using tuple()
This task can be performed using the tuple(). In this, we convert
the pair list to tuple and by this way we separate individual
elements as variables, ready to be sent to function.
PASSING ARRAYS/LISTS TO FUNCTION
26. Method #2 : Using * operator
Using * operator is the most recommended method to
perform this task. The * operator unpacks the dual list
into args and hence solving our problem.
28. The tuple() function is a built-in function in Python that can be used to
create a tuple. A tuple is an immutable sequence type.
PASSING TUPLES TO FUNCTION
29. Unpack elements in dictionary to function arguments using **
Python provides an another symbol ** . On prefixing it with a dictionary,
all the key value pairs in dictionary will be unpacked to function
arguments. Lets understand this by an example,
.
PASSING DICTIONERIS TO FUNCTION