0% found this document useful (0 votes)
21 views21 pages

Unit - 4 - III Cs - Python

Uploaded by

jagethiramesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
21 views21 pages

Unit - 4 - III Cs - Python

Uploaded by

jagethiramesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

Sri Ganesh College of Arts & Science – Salem- 14.

Department of Computer Science & Applications


Study Material – (2023 – 24 Even Semester)
Sub: Programming in Python Paper Code: 21UCS10 Class: III B.Sc CS
Sub I/c: Date: HOD:

UNIT –IV
Functions and Functional Programming – Functions – calling functions – creating functions – passing
functions – Built-in Functions: apply( ), filter( ), map( ) and reduce( ) – Modules – Modules and Files –
Modules built-in functions - classes – class – attributes – Instances.

FUNCTIONS AND FUNCTIONAL PROGRAMMING


 Functions and functional programming are fundamental concepts in computer science and software
development. Let's explore these concepts
FUNCTIONS
1. Definition:
 A function is a self-contained block of code that performs a specific task.
 Functions take input, process it, and produce output.
2. Characteristics:
Modularity: Functions promote code modularity by breaking down a program into smaller,
manageable parts.
Reusability: Functions can be reused in different parts of a program or even in different programs.
Abstraction: Functions hide the internal details of their implementation, providing a high-level
interface.
3. Syntax (in a programming language like Python):
def function_name(parameters):
# function body return result
4. Types of Functions:
Built-in Functions: Provided by the programming language.
User-defined Functions: Created by the programmer.

1
1. Definition:
 Functional programming is a programming paradigm that treats computation as the evaluation of
mathematical functions and avoids changing state and mutable data.
2. Core Concepts:
First-Class Functions:
 Functions are treated as first-class citizens, meaning they can be passed as arguments, returned from
other functions, and assigned to variables.
Pure Functions:
 Functions that, given the same input, always produce the same output and have no side effects.
Immutability:
 Data, once created, cannot be changed. Instead of modifying existing data, new data is created.
3. Principles:
 Referential Transparency: An expression can be replaced with its value without changing the
program's behavior.
 Avoiding Side Effects: Functions should not modify external state; their only purpose is to produce
output.
4. Examples of Functional Programming Languages:
 Haskell: A purely functional programming language.
 Clojure, Lisp, and Scheme: Functional programming languages that run on the Lisp platform.
 Functional Features in Python, JavaScript, and Scala: These languages support functional
programming concepts.
5. Benefits:
 Conciseness: Code tends to be shorter and more expressive.
 Parallelism: Functional programs can be easier to parallelize, as functions with no side effects can
be executed concurrently.
6. Higher-Order Functions:
 Functions that take other functions as arguments or return functions.
7. Examples of Higher-Order Functions:
 map: Applies a function to each element in a list.
 filter: Selects elements from a list based on a given condition.
 reduce: Combines elements of a list into a single value.
 In Python, functions are a fundamental building block of the language. Here are the key aspects of
working with functions in Python:
2
Defining a Function:
 In Python, you define a function using the def keyword, followed by the function name, parameters
in parentheses, and a colon. The function body is indented.
Syntax:
def my_function(parameter1, parameter2):
# function body
result = parameter1 + parameter2
return result
CALLING FUNCTIONS
 Once a function is defined, you can call it by using the function name followed by parentheses,
passing any required arguments.
result = my_function(3, 4)
print(result) # Output: 7
Function Parameters:
Python supports various types of function parameters:
 Positional Parameters: Parameters passed by position.
 Keyword Parameters: Parameters passed by name.
 Default Parameters: Parameters with default values.
 Variable-Length Parameters: Allowing a variable number of arguments.
def example_function(a, b, c=0, *args, **kwargs):
# function body
pass
example_function(1, 2, 3, 4, 5, keyword1="value1", keyword2="value2")
Return Statement:
 A function can return a value using the return statement. If no return statement is present, the
function returns None by default.
def add(a, b):
return a + b
result = add(2, 3)
print(result) # Output: 5
Docstrings:
 It is good practice to include a docstring (documentation string) to describe the purpose and usage
of the function.

3
def greet(name):
"""
This function greets the person passed in as a parameter.
"""
return f"Hello, {name}!"
Lambda Functions:
 Lambda functions, or anonymous functions, can be created using the lambda keyword. They are
useful for short, one-line operations.
multiply = lambda x, y: x * y
result = multiply(3, 4)
print(result) # Output: 12

Scope of Variables:
 Variables defined inside a function have local scope, while variables defined outside functions have
global scope. Use the global keyword to modify a global variable within a function.
Examples:
global_var = 10
def my_function(x):
local_var = 5
return x + local_var + global_var
result = my_function(3)
print(result) # Output: 18
 These are the basics of working with functions in Python. Understanding function parameters,
return values, and scoping is crucial for effective Python programming.
Python Functions
 A Python function is a block of organized, reusable code that is used to perform a single, related
action.
 Functions provide better modularity for your application and a high degree of code reusing.
 A top-to-down approach towards building the processing logic involves defining blocks of
independent reusable functions.
 A Python function may be invoked from any other function by passing required data
(called parameters or arguments). The called function returns its result back to the calling
environment.

4
Types of Python Functions
Python provides the following types of functions −
 Built-in functions
 Functions defined in built-in modules
 User-defined functions
 Python's standard library includes number of built-in functions.
 Some of Python's built-in functions are print(), int(), len(), sum(), etc.
 These functions are always available, as they are loaded into computer's memory as soon as you start
Python interpreter.
 The standard library also bundles a number of modules. Each module defines a group of functions.
These functions are not readily available.
 You need to import them into the memory from their respective modules.
 In addition to the built-in functions and functions in the built-in modules, you can also create your own
functions.
 These functions are called user-defined functions.
Defining a Function in Python
 You can define custom functions to provide the required functionality. Here are simple rules to
define a function in Python.
 Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
 Any input parameters or arguments should be placed within these parentheses. You can also define
parameters inside these parentheses.
 The first statement of a function can be an optional statement; the documentation string of the
function or docstring.
 The code block within every function starts with a colon (:) and is indented.
 The statement return [expression] exits a function, optionally passing back an expression to the
caller. A return statement with no arguments is the same as return None.

5
Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
 By default, parameters have a positional behavior and you need to inform them in the same order
that they were defined.
 Once the function is defined, you can execute it by calling it from another function or directly from
the Python prompt.
Example
def greetings():
"This is docstring of greetings function"
print ("Hello World")
return
greetings()
 The following example shows how to define a function greetings(). The bracket is empty so there
aren't any parameters.
 The first line is the docstring. Function block ends with return statement. when this function is
called, Hello world message will be printed.

PASSING FUNCTIONS
Defining a function only gives it a name, specifies the parameters that are to be included in the function
and structures the blocks of code.
 Once the basic structure of a function is finalized, you can execute it by calling it from another
function or directly from the Python prompt.
 Following is the example to call printme() function −
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print (str)
return;
# Now you can call printme function
printme("I'm first call to user defined function!")
6
printme("Again second call to the same function")
When the above code is executed, it produces the following output −
I'm first call to user defined function!
Again second call to the same function
Pass by Reference vs Value
 The function calling mechanism of Python differs from that of C and C++. There are two main
function calling mechanisms:
o Call by Value
o Call by Reference.
 When a variable is passed to a function, what does the function do to it? If any changes to its
variable does not get reflected in the actual argument, then it uses call by value mechanism.
 On the other hand, if the change is reflected, then it becomes call by reference mechanism.

Difference between call by value and call by reference in Python


Call by value Call by reference
Call by value:-The parameter values are copied to Call by reference:-
function parameter and then two types of The actual, as well as the formal parameters refer to
parameters are stored in the memory locations. the same location and if changes are made inside the
When changes are made the functions are not functions the functions are reflected in the actual
reflected in the parameter of the caller. parameter of the caller.
While calling the function we pass values and At the time of calling instead of passing the values
called as call by values. we pass the address of the variables that means the
location of variables so called call by reference.
The value of each variable is calling the function The address of variable in function while calling is
and copy variable into the variables of the copied into dummy variables of a function.
function.

7
The changes are made to dummy variables in the By using the address we have the access to the actual
called function that have no effect on the actual variable.
variable of the function.
We cannot change the values of the actual variable We can change the values of the actual variable
through a function call. through a function call.
The values are passed by the simple techniques The pointers are necessary to define and store the
address of variables.
Call by value Example:- Call by reference:-
def printme (str): def changeme(mylist):
print str mylist.append(1, 2, 3, 4]);
str print”values inside the function:” mylist
return; return
printme(“I’m first call to user defined mylist= [10, 20, 30];
function!”) changeme (mylist);
printme(“Again second call to same print”values outside the function:” mylist
function”) Output:-
Output:- values inside the function: [10, 20, 30, [1, 2, 3, 4]]
I’m first call to user defined function! values outside the function: [10, 20, 30, [1, 2, 3, 4]]
Again second call to same function

call by value call by reference


C/C++ functions are said to be called by value. Python uses pass by reference mechanism
When a function in C/C++ is called, the value of As variable in Python is a label or reference to the
actual arguments is copied to the variables . object in the memory, the both the variables used as
representing the formal arguments. actual argument as well as formal arguments really
refer to the same object in the memory.

In the Call by Value method, there is no modification In the Call by Reference method, there is a
in the original value. modification in the original value.
If the function modifies the value of formal We can verify this fact by checking the id() of the
argument, it doesn't reflect the variable that was passed variable before and after passing.
passed to it

8
Function Arguments
The process of a function often depends on certain data provided to it while calling it.
 While defining a function, you must give a list of variables in which the data passed to it is
collected.
 The variables in the parentheses are called formal arguments.
 When the function is called, value to each of the formal arguments must be provided.
 Those are called actual arguments.

Example
Let's modify greetings function and have name an argument. A string passed to the function as actual
argument becomes name variable inside the function.
def greetings(name):
"This is docstring of greetings function"
print ("Hello {}".format(name))
return
greetings("Samay")
greetings("Pratima")
greetings("Steven")
It will produce the following output −
Hello Samay
Hello Pratima
Hello Steven

Function with Return Value


 The return keyword as the last statement in function definition indicates end of function block, and
the program flow goes back to the calling function.

9
 Although reduced indent after the last statement in the block also implies return but using explicit
return is a good practice.
 Along with the flow control, the function can also return value of an expression to the calling
function.
 The value of returned expression can be stored in a variable for further processing.
Example
 Let us define the add() function. It adds the two values passed to it and returns the addition. The
returned value is stored in a variable called result.
def add(x,y):
z=x+y
return z
a=10
b=20
result = add(a,b)
print ("a = {} b = {} a+b = {}".format(a, b, result))
It will produce the following output −
a = 10 b = 20 a+b = 30
Types of Function Arguments
Based on how the arguments are declared while defining a Python function, they are classified into the
following categories −
 Positional or required arguments
 Keyword arguments
 Default arguments
 Positional-only arguments
 Keyword-only arguments
 Arbitrary or variable-length arguments
In the next few chapters, we will discuss these function arguments at length.
Order of Arguments
 A function can have arguments of any of the types defined above. However, the arguments must be
declared in the following order −
 The argument list begins with the positional-only args, followed by the slash (/) symbol.
 It is followed by regular positional args that may or may not be called as keyword arguments.
 Then there may be one or more args with default values.

10
 Next, arbitrary positional arguments represented by a variable prefixed with single asterisk, that is
treated as tuple. It is the next.
 If the function has any keyword-only arguments, put an asterisk before their names start. Some of
the keyword-only arguments may have a default value.
 Last in the bracket is argument with two asterisks ** to accept arbitrary number of keyword
arguments.
The following diagram shows the order of formal arguments –

How To Create A Functions In Python


 A function is a block of code which only runs when it is called.
 You can pass data, known as parameters, into a function.
 A function can return data as a result.
Creating a function
In Python a function is defined using the def keyword:
Example
def my_function():
print("Hello from a function")
Creating a function in Python involves using the def keyword, specifying the function name, parameters (if
any), and the function body.
Here's the basic syntax:
def function_name(parameter1, parameter2, ...):
# function body
# perform operations
return result # optional, used to return a value
# Example function without parameters
def greet():
print("Hello, welcome!")
# Example function with parameters and return statement
def add_numbers(a, b):
result = a + b
return result
11
# Example function with default parameter
def greet_person(name, greeting="Hello"):
print(f"{greeting}, {name}!")
# Example function with variable-length arguments
def sum_all(*args):
result = sum(args)
return result
# Example function with keyword arguments
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
# Example function with a combination of parameter types
def complex_function(a, b, *args, c=0, **kwargs):
result = a + b + c + sum(args) + sum(kwargs.values())
return result
# Calling the functions
greet()
sum_result = add_numbers(3, 4)
print(sum_result)
greet_person("Alice")
greet_person("Bob", greeting="Hi")
total = sum_all(1, 2, 3, 4, 5)
print(total)
print_info(name="John", age=25, city="New York")
result = complex_function(1, 2, 3, 4, c=5, x=6, y=7)
print(result)

In this example:
greet() is a simple function without parameters.
add_numbers(a, b) takes two parameters and returns their sum.
greet_person(name, greeting="Hello") has a default value for the greeting parameter.
sum_all(*args) uses variable-length arguments to accept any number of positional arguments.

12
print_info(**kwargs) uses variable-length keyword arguments to accept any number of keyword
arguments.
complex_function(a, b, *args, c=0, **kwargs) showcases a function with a mix of parameter types.

Passing a function as an argument


 We have a list of strings represent fruit names:
>>> fruits = ['kumquat', 'cherimoya', 'Loquat', 'longan', 'jujube']
 If we wanted to alphabetize this list we could pass it to the built-in sorted function:
>>> sorted(fruits)
['Loquat', 'cherimoya', 'jujube', 'kumquat', 'longan']
 Except that sorted doesn't quite alphabetize this list because when Python sorts strings it puts all the
uppercase letters before all the lowercase letters (due to their ASCII/unicode ordering).
We can fix this, by supplying a key argument to the built-in sorted function.
>>> help(sorted)
sorted(iterable, /, *, key=None, reverse=False)
This key argument should be a function that will be called with each of the individual items in our fruits
iterable and it should return the thing to sort by.
In our case, we could make a function that lowercases any string given to it in order to sort by the
lowercase versions of each string:
>>> def lowercase(string):
... return string.lower()
...
To sort by lowercased strings we'll call sorted with a key argument that points to this lowercase function:
>>> sorted(fruits, key=lowercase)
['cherimoya', 'jujube', 'kumquat', 'longan', 'Loquat']
That properly alphabetized our list of strings (while maintaining their original capitalization in the resulting
list).
Notice that when we called sorted, we didn't put parentheses (()) after lowercase to call it, like this:
>>> lowercase('X')
'x'
We didn't call lowercase at all. Instead we just referred to lowercase:
>>> lowercase
<function lowercase at 0x7fc8a3fc09d0>
13
We passed the lowercase function object to the sorted function (via its key argument):
>>> sorted(fruits, key=lowercase)
So that sorted could call lowercase itself repeatedly on each of the individual items of fruits list (using the
return value it got as a sort of comparison key to sort by).
So in Python, there are functions that accept other functions as arguments.

BUILT-IN FUNCTIONS
 Python's, map(), filter(), and reduce() functions add a touch of functional programming to the
language.
 All of these are convenience functions that can be replaced with List Comprehensions or loops but
offer a more elegant and concise solution to some problems.
 map(), filter(), and reduce() all work in the same way. These functions accept a function and a
sequence of elements and return the result of applying the received function to each element in the
sequence.
apply() method:
Definition and Usage
 The apply() method allows you to apply a function along one of the axis of the DataFrame, default 0,
which is the index (row) axis.
Syntax
dataframe.apply(func, axis, raw, result_type, args, kwds)

Example :
import pandas as pd
def calc_sum(x):
return x.sum()
data = {
"x": [50, 40, 30],
"y": [300, 1112, 42]
}
df = pd.DataFrame(data)
x = df.apply(calc_sum)
print(x)

14
Parameters

Parameter Value Description

func Required. A function to apply to the DataFrame.

axis 0 Optional, Which axis to apply the function to. default 0.


1
'index'
'columns'

raw True Optional, default False. Set to true if the row/column should be
False
passed as an ndarray object

result_type 'expand' Optional, default None. Specifies how the result will be
'reduce' returned
'broadcast'
None

args a tuple Optional, arguments to send into the function

kwds keyword Optional, keyword arguments to send into the function


arguments

The axis, raw, result_type, and args parameters are keyword arguments.
map() function
 Like reduce(), the map() function allows you to iterate over each item in an iterable. Map(), on the
other hand, operates independently on each item rather than producing a single result.
 Finally, the map() function can be used to perform mathematical operations on two or more lists. It
can even be used to manipulate any type of array.
The map() function’s time complexity= O (n)
Syntax
map(function, iterable)

15
Parameters
 function − The function to be used in the code.
 iterable − This is the value that is iterated in the code.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –
 Create a function with the name multiplyNumbers that returns the multiplication result of the
number passed to it.
 Return the given number multiplied by 3, inside the function.
 Use the map() function for applying the multiplyNumbers() function for each element of the list by
passing the function name, and list as arguments to it.
 Print the resultant list items after multiplying them with 3.
Filter() function
The filter() function creates a new iterator that filters elements from a previously created one (like a list,
tuple, or dictionary).
The filter() function checks whether or not the given condition is present in the sequence and then prints
the result.
The filter() function’s time complexity= O (n)
Syntax
 filter(function, iterable)
Parameters
 function − The function to be used in the code.
 iterable − This is the value that is iterated in the code.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –
 Create a function with the name votingAge that returns the eligibility ages for voting from the list.
 Use the if conditional statement to check whether the number passed to the function is greater than
or equal to 18.
 If the above statement is true Return the number.
 Create a variable to store the input list.
 Use the filter() function by passing the function name, and input list as arguments to it to filter the
ages greater than or equal to 18 from the list. Here it applies the votingAge() function to every
element of the list and the result stores only the values of the list that are returned by the
votingAge() function(Here votingAge() function returns the number if it is greater than 18).

16
 Print the filter object
 Use the list() function(returns a list of an iteratable), to convert the above filter object into a list and
print it.
reduce()
 In Python, the reduce() function iterates through each item in a list or other iterable data type,
returning a single value. It's in the functools library. This is more efficient than looping.
Syntax
reduce(function, iterable)
Parameters
 function − The function to be used in the code.
 iterable − This is the value that is iterated in the code.
Algorithm (Steps)
 Following are the Algorithm/steps to be followed to perform the desired task –
 Use the import keyword to import the reduce() function from functools module
 Create a function with the name addNumbers() that returns the sum of all list items.
 Create a variable to store the input list
 Use the reduce() function, bypassing the addNumbers() function, and input list as arguments to
get the sum of all list items.

MODULES AND FILES


 In Python, modules and files are essential concepts for organizing and structuring code.
Modules:
 A module in Python is a file containing Python definitions and statements. The file name is the
module name with the suffix .py.
Creating a Module:
 Create a Python file (e.g., my_module.py).
 Write functions, classes, or variables in the file.
Example :
# my_module.py
def greet(name):
print(f"Hello, {name}!")
def square(x):
return x ** 2

17
Using a Module:
1.Import the module using the import keyword.
2.Access functions, classes, or variables using dot notation.
# main.py
import my_module
my_module.greet("Alice")
result = my_module.square(5)
print(result)
Files in Python:
Reading from a File:
 Use the open() function to open a file.
 Read its contents using methods like read(), readline(), or readlines().
# Reading from a file
with open("example.txt", "r") as file:
content = file.read()
print(content)
Writing to a File:
 Use the open() function with the mode "w" to open a file for writing.
 Write to the file using the write() method.
# Writing to a file
Example :
with open("output.txt", "w") as file:
file.write("Hello, this is a sample text.")
 Appending to a File:
Use the open() function with the mode "a" to open a file for appending.
# Appending to a file
with open("output.txt", "a") as file:
file.write("\nThis text is appended.")
Working with CSV Files (using csv module):
Use the csv module to read and write CSV files.
import csv
# Reading from a CSV file
with open("data.csv", "r") as csv_file:

18
csv_reader = csv.reader(csv_file)
for row in csv_reader:
print(row)
# Writing to a CSV file
with open("output.csv", "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(["Name", "Age"])
csv_writer.writerow(["Alice", 25])
csv_writer.writerow(["Bob", 30])

 These examples provide a basic overview of modules and file handling in Python. Modules help
organize code, and file operations allow you to read from and write to files, making data storage
and retrieval possible in your programs.

MODULES-BUILT IN FUNCTIONS
 Python has a rich set of built-in modules and functions that provide various functionalities. Here are
some of the commonly used built-in modules and functions:
Built-in Functions:
print():
 Used to print the specified values.
print("Hello, World!")
len():
 Returns the length (the number of items) of an object.
length = len([1, 2, 3, 4, 5])
type():
 Returns the type of an object.
data_type = type(10)
range():
 Generates a sequence of numbers within a specified range.
numbers = list(range(1, 6))
input():
 Reads a line from the user's input.
name = input("Enter your name: ")

19
int(), float(), str(): Convert a value to an integer, float, or string, respectively.
num_str = "42" num_int = int(num_str)
sum():
 Returns the sum of a sequence.
total = sum([1, 2, 3, 4, 5])
Built-in Modules:
math:
 Provides mathematical functions.
import math result = math.sqrt(25)
random:
 Generates random numbers and provides related functions.
import random random_number = random.randint(1, 10)
datetime:
 Manipulates dates and times.
from datetime import datetime current_time = datetime.now()
os:
 Interacts with the operating system.
import os current_directory = os.getcwd()
 These are just a few examples, and Python has many more built-in modules and functions that cater to a
wide range of needs. Always refer to the official Python documentation for the most accurate and
comprehensive information: Python Standard Library.

CLASSES, ATTRIBUTES, AND INSTANCES


 In Python, a class is a code template for creating objects. Objects have member variables and have
behavior associated with them. Here's a breakdown of key concepts related to classes:
1. Class Definition:
class MyClass:
# Class attributes (shared by all instances)
class_attribute = "I am a class attribute" def __init__(self, attribute1, attribute2):
# Instance attributes (specific to each instance)
self.attribute1 = attribute1 self.attribute2 = attribute2 def instance_method(self):
print(f"Instance method called for {self.attribute1}")
class MyClass:: Defines a class named MyClass.

20
Class Attributes:
 Attributes defined at the class level are shared by all instances of the class. In the example,
class_attribute is a class attribute.
 __init__ Method: The constructor method initializes instance attributes. It is called when an object
is created. self refers to the instance being created.
 Instance Attributes: Attributes defined inside the __init__ method are specific to each instance. In
the example, attribute1 and attribute2 are instance attributes.
 Instance Method: A method defined within the class that operates on instance attributes. The
instance_method in this example is an instance method.
2. Creating Instances:
 # Creating instances of the class instance1 = MyClass(attribute1="value1", attribute2="value2")
 instance2 = MyClass(attribute1="value3", attribute2="value4")
 instance1 and instance2: Objects (instances) of the MyClass class.
3. Accessing Attributes and Calling Methods:
 # Accessing class attribute print(MyClass.class_attribute)
 # Accessing instance attributes print(instance1.attribute1) print(instance2.attribute2)
 # Calling instance method instance1.instance_method()
Accessing Attributes:
 MyClass.class_attribute: Accesses the class attribute.
 instance1.attribute1 and instance2.attribute2: Accesses instance attributes.
Calling Methods:
 instance1.instance_method(): Calls the instance method for instance1.
Class: Blueprint for creating objects.
Attributes: Variables that store data. Class attributes are shared; instance attributes are specific to each
instance.
Instances: Objects created from a class.
Methods: Functions defined within a class that operate on instance attributes.
This is a basic example, and classes in Python can have more complex structures, including inheritance,
encapsulation, and polymorphism.

UNIT – IV COMPLETED

Signature of the staff with date


21

You might also like