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

Chapter 4

This document discusses Python modules and functions, explaining that a module is a file containing Python code for organizing related functions, classes, and variables. It describes how to define functions, including their syntax, types of arguments, and the distinction between built-in, user-defined, and anonymous functions. Additionally, it provides examples of function definitions and the use of lambda functions for creating anonymous functions.

Uploaded by

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

Chapter 4

This document discusses Python modules and functions, explaining that a module is a file containing Python code for organizing related functions, classes, and variables. It describes how to define functions, including their syntax, types of arguments, and the distinction between built-in, user-defined, and anonymous functions. Additionally, it provides examples of function definitions and the use of lambda functions for creating anonymous functions.

Uploaded by

mejidkingo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

CHAPTER – FOUR

Python Modules
&
Functions

1
Modules

 A module is a file consisting of Python code that can define functions, classes and variables.
 A module allows you to organize your code by grouping related code which makes the code
easier to understand and use.
 You can use any Python source file as a module by executing an import statement

 Python's from statement lets you import specific attributes from a module into the
current namespace.

 import * statement can be used to import all names from a module into the current
namespace

2
Examples

3
Examples

4
Functions
 A 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.
Defining a Function
 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. 8/22/201
 The code block within every function starts with a colon
7
(:)
and is indented. 5
Functions
 Function Syntax

 Function Arguments
You can call a function by using any of the following types of arguments:
• Required arguments: the arguments passed to the function in correct
positional order.
• Keyword arguments: the function call identifies the arguments by the
parameter names.
• Default arguments: the argument has a default value in the function
declaration used when the value is not provided in the function call.

6
Functions

 Variable-length arguments: This used when you need to process unspecified additional
arguments.
 An asterisk (*) is placed before the variable name in the function declaration.

7
Functions

Example: Define a function which can generate and print a tuple where the value are square of
numbers between 1 and 20.

8
Functions

9
Types of Functions
 Types of functions in Python:
1. Built-in functions : The Python interpreter has a number of
functions built into it that are always available. They are listed here in
alphabetical order.
2. User-Defined Functions (UDFs): The Functions defined by User is
known as User Defined Functions. These are defined with the keyword
def.
3. Anonymous functions, which are also called lambda functions
because they are not declared with the standard def keyword.

10
Built-in functions

Hdhh

11
The Anonymous Functions
 These functions are called anonymous because they are not declared in the
standard manner by using the def keyword. You can use the lambda keyword
to create small anonymous functions.
 Lambda forms can take any number of arguments but return just one
value in the form of an expression. They cannot contain commands or
multiple expressions.
 An anonymous function cannot be a direct call to print because lambda
requires an expression.
 Lambda functions have their own local namespace and cannot access
variables other than those in their parameter list and those in the global
namespace.
 Although it appears that lambdas are a one-line version of a function, they12
The Anonymous Functions ---

Example:
# Function definition is here
sum = lambda arg1, arg2: arg1 + arg2
# Now you can call sum as a function
print ("Value of total : ", sum( 10, 20 ))
print ("Value of total : ", sum( 20, 20 ))
 When the above code is executed, it produces the following
result −
Value of total : 30
Value of total : 40 13

You might also like