0% found this document useful (0 votes)
0 views13 pages

Unit 4 - Functions in Python

The document provides supplementary learning material for a Python for Data Science course, focusing on functions in Python. It covers topics such as predefined functions, user-defined functions, the syntax of functions, and lambda functions, explaining their usage and providing examples. The material emphasizes the importance of functions for code reusability and efficiency in programming.

Uploaded by

saad.sheriff20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views13 pages

Unit 4 - Functions in Python

The document provides supplementary learning material for a Python for Data Science course, focusing on functions in Python. It covers topics such as predefined functions, user-defined functions, the syntax of functions, and lambda functions, explaining their usage and providing examples. The material emphasizes the importance of functions for code reusability and efficiency in programming.

Uploaded by

saad.sheriff20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Supplementary Learning Material

saadsheriff1895@gmail.com
J732KOTCSB

Program: MCA
Specialization: Data Science
Semester: 2
Course Name: Python for Data Science
Course Code: 21VMT5S204
Unit Name: Functions in Python

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
Unit 4
Functions in Python
Overview:
Let’s say you want to invoke/call a piece of code multiple times in your
program. When it is to be called consequently, we use loops. But what about
instances when you have to invoke a set of codes in the beginning of the
program, and then at the end. It becomes tedious to keep copying the same
code again and again. It also makes the program slow. To solve issues like
these, functions are used. To reuse the same piece of codes in different parts
of the program.
Unit Outcomes:
1. Functions:
- What are functions?
- What are pre-defined functions in Python?
2. User-defined functions:
- What are user defined functions?
saadsheriff1895@gmail.com
J732KOTCSB - Syntax of a user defined function.
- Parts of a function.
- return keyword
-
3. Lambda functions:
- What are lambda functions?
- Why are lambda functions used?
- Syntax of lambda functions.
- Examples.

Functions:
Functions are used when we want to call a particular sets of codes in different
parts of a program. For instance, when we want to check if the addition of two
numbers more than once in a program. It is easy after function declaration,
which is done once, and then the function is simply called wherever it is
Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
needed. Functions help make the code reusable. Functions are also called
methods in a lot of OOP languages. Functions need to be properly indented for
the proper flow of statements.

Pre-defined/ Built-in functions in Python:


For ease of use, python has some built-in functions, which means, there are
functions in the python interpreter that are already present. These functions
are available to be used readily. Even print() is a function. In Python 3 there are
some 68 predefined functions.
Some of those functions are abs() which gives the absolute solution of tha
value entered within the parenthesis. range() is also a function which returns
the range of sequential integers from a beginning point to an end. str()
converts the object to a string/character datatype. These are functions we
have already used, there are so many more functions that are pre-defined and
haven’t been used in the course but help make the code concise and direct.

saadsheriff1895@gmail.com
J732KOTCSBUser-defined functions:
While there are a lot of pre-defined functions, there can be a lot of other sets
of code that a user might want to repeat in a program, that are not pre-
defined. For purposes like these, user-defined functions come in handy.
Through a user-defined function, a programmer can write his set of code in the
block function and call it everywhere in a program, whenever it has to be
reused.

Syntax of a function:

def functionName(argument1, argument2, …):


set of statemens
return valueToBeReturned

functionName()

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
Explanation of the syntax/Parts of a function:

- This block is called the function definition. It is the part where you
write all the features and codes of the function.
- ‘def’ is a keyword in the python. It is short for define/definition.
- It is followed by functionName which is the name of the function that
is going to be defined by the user.
- functionName is followed by a parenthesis which contains
arguments. Arguments are values of variables that are used within
the function.

These arguments are also called local variable since they can only be
used within the block of codes of the function. They go unrecognised
outside the function. However, one must note, it is not compulsory to
write arguments in a python function.
- The arguments or the parenthesis followed by a colon. Inside the
block enter all the conditions/statements for the case that the
function was created.
saadsheriff1895@gmail.com
J732KOTCSB

- return is a keyword in python. It is used to return a variable value


once the function code is executed.
- Once you are done writing the code, you must call the function.
Come outside the indented block. To execute the function, you must
call it. To call a function, just enter functionName(). If you mention
arguments in the function definition, then the called function must
also contain values for those arguments. When the function is called,
it is referenced back to the block of codes inside the same
functionName.

- One must note that the number of arguments in the function


definition must match the number of variable values mentioned
when the function is called. If there are no arguments, then there will
be no values inside the called function parenthesis.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
Example (without arguments, without return type):

Example (without arguments, with return type):

saadsheriff1895@gmail.com
J732KOTCSB

Example (with arguments, with return type):

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
return keyword:
return is used inside a function. It is assumed that once return is written, the block ends. A
function can have multiple return statements, however, if any one of them is executed, the
function is terminated. It cannot be written outside the function. The expression contained
in a return statement is evaluated, executed and then returned. When a return statement
has no value ‘None’ is returned.

lambda function:
lambda function is a function written in a single line. it does not have any
name, and can take any number of arguments but it can only have one
expression in it. It is called Anonymous function because it is nameless.
One must note that the output for a one line expression given by both, the
lambda function and a normal def function are the same. def is usually used
when the function has multiple lines of code.

Why use lambda functions?


saadsheriff1895@gmail.com
J732KOTCSB 1. lambda reduces the length of the code when compared to a normal
function that is written using the def.
2. It can be written and called immediately. Unlike def functions, they
don’t have to be called separately.
3. It is used when a function is temporarily required or for a short time.
Defining Lambda functions:
Instead of using def, here only the keyword lambda is used.

lambda arguments : expression

A lambda function does not have a return statement. Since it is a single line
function, when executed, the value of the expression is displayed in the
output.

Example:

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
In the above example, variable x stores the lambda function which is defined
by variables a, b, c, and d. it is followed by a semicolon and the expression that
is to be evaluated.
In order to display the output, we write print(x(10,12,14,16)) for example 1,
which means that we specify the variable to be printed along with the
temporary values of a,b,c,d.
The example can be written through a normal function.

saadsheriff1895@gmail.com
J732KOTCSB

In the above example, the lambda function is written as a def function. The
output is the same, however, the number of lines are more when written
inside the def function.
Example: With arguments, without return
1. Calculator, factorial of a number, and reverse of a number
To make a basic calculator using functions.:

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
To make calculator, we can use four functions or simply one function. One
function can contain all the arithmetic operations, or multiple functions can
contain different arithmetic operations and then can be executed separately.

saadsheriff1895@gmail.com
J732KOTCSBIn the above example, the function calculator contains numerous arithmetic
operations. Then the function is called, with arguments as a=20 and b=5. When
executed, it gives the output for each operation. A lambda function is stored in
variable x. since only one expression can be evaluated in the lambda function,
an amalgamation of operations is used with local variables a and b. Evaluation
of arithmetic operations happens in a left to right order. The solution is then
printed.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
Another form of writing the same program using functions is given above.
Instead of using one function, numerous small functions have been used
where each function performs an arithmetic operation. Either way the output
is the same.
2. Factorial of a number.
Factorial of a number n means multiplying all n numbers in decrementing
order. For instance, for number=6, you multiply 6*5*4*3*2*1.
Therefore, for any number, factorial= num*(num-1)*…*3*2*1

saadsheriff1895@gmail.com
J732KOTCSBIn the above example, when function factorial is executed with 8 as argument
value, the interpreter goes inside the function defined factorial, with local
variable a. Thus, a=8. Another local variable is assigned s=1 inside factorial. The
loop is then executed, since the condition is fulfilled, i.e. 8 !=0, the loop is
entered. s is reassigned as s*a, and then a is decremented. The loop keeps
repeating until the value of a is 0, when that condition is not fulfilled, the loop
will be terminated and then the factorial stored in s is displayed.
Examples: Without arguments, with return
1. Finding the sum of all multiple of 5 between 50-200:

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
In the above example, a function summation is defined containing the suum of
all multiples of 5. For the same, a local variable sum is initialised as 0. A for
loop is run between the numbers 50 and 200. Whenever the iterative variable i
is completely divisible by 5, i is added to the previous value of sum otherwise
the loop continues without any changes. Once the loop is terminated, the
function returns the sum.

2. Factorial (without arguments, with return)

saadsheriff1895@gmail.com
J732KOTCSB
The same factorial problem is executed without arguments and with return.
num is used as the local variable instead of entering arguments.

Examples: Without arguments, without return


1. Finding if a number is palindrome or not.

A number is said to be palindrome if the reverse of the number is the


same as the original number.
For instance, 35653 is a palindrome number since the reverse of the
number is 35653, which is the original number.
292 is another example of a palindrome.

To find palindrome we use a function and follow a logical order:


i. Define the function
ii. Initialise all the local/global variables that we might use.
iii. Write a loop to reverse a number:

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
iv. End the loop, compare the reversed number to the original
number.
v. If the reverse = original number, then we call it a palindrome
number.
vi. Call the function.

Number 1: 12321
saadsheriff1895@gmail.com
J732KOTCSB

Number 2: 12345
Example 2:
Armstrong number (without arguments, without return)
When the sum of the cube of every digit of a number is equal to the original
number, it is called an Armstrong number.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
For instance, 153 = 13 + 53 + 33 = 1 + 125 + 9= 153.
Similarly, 1634, 9474, 371, are few other examples of Armstrong numbers.
Just like finding the palindrome of a number, to find if a number is Armstrong,
we follow a logical order and use functions here to find it:
i. Initialise global variables.
ii. Define a function (here, without arguments, without return).
iii. Initialise local variables.
iv. Run a loop to find the cube of every digit.
v. Run a loop to find the sum of the cubes.
vi. Check if the sum is equal to the original number.
vii. If the sum and the original number are equal, then it is an Armstrong
number.

Output:

saadsheriff1895@gmail.com
J732KOTCSB

Number= 153
= > 13 + 53 + 33
= > 153. Thus, the number is an Armstrong number.

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.
Number= 1256
= > 13 + 23 + 53 + 63
= 350 ≠ 1256. Therefore, the number is not an Armstrong number.

saadsheriff1895@gmail.com
J732KOTCSB

Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.

This file is meant for personal use by saadsheriff1895@gmail.com only.


Sharing or publishing the contents in part or full is liable for legal action.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy