SlideShare a Scribd company logo
1 of 26
CHAPTER-FOUR
Modules
&
Functions
By: Mikiale T.
1
MODULES
 As you gain experience writing code, you will eventually
work on projects that are so large that keeping all of the
code in a single file becomes cumbersome.
 Instead of writing a single file, you can put related code into
separate files called modules.
 Individual modules can be put together like building blocks
to create a larger application.
 There are four main advantages to breaking a program into
modules:
1) Simplicity: Modules are focused on a single problem.
2) Maintainability: Small files are better than large files.
3) Reusability: Modules reduce duplicate code.
4) Scoping: Modules have their own namespaces.
2
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.
 A module is a file containing Python code that can be re-used in other Python code files.
 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
3
MODULES---
 A namespace is a collection of names, such as variable
names, function names, and class names.
 Every Python module has its own namespace. Variables,
functions, and classes in a module can be accessed from
within the same module by just typing their name.
 To access a name in an imported module from the calling
module, type the imported module’s name followed by a
dot (.) and the name you want to use:
Note: The name used to import a module is the same as the
module’s
file name.
4
Working With Packages
 Modules allow you to divide a program in to individual files
that can be reused as needed.
 Related code can be organized into a single module and
kept separate from other code.
 Package take this organizational structure one step further
by allowing you to group related modules under a single
namespace.
 A package is a folder that contains one or more Python
5
Module for Mathematical Functions
 By importing math module
6
Module for Mathematical Functions ---
 By importing math module
7
Module for Mathematical Functions---
 By importing math module
8
Module for Mathematical Functions ---
 By importing math module
9
Module for Mathematical Functions ---
 By importing math module
10
Module for Mathematical Functions ---
 By importing math module
11
module Date Time
 A date in Python is not a data type of its own, but we can import a module named
datetime to work with dates as date objects.
 When we execute the code from the example above the result will be: 2022-06-27
09:40:16.122293
 The date contains year, month, day, hour, minute, second, and microsecond.
 The datetime module has many methods to return information about the date object.
12
module Date Time ---
 The datetime() class also takes parameters for time and timezone
(hour, minute, second, microsecond, tzone), but they are optional, and
has a default value of 0, (None for timezone).
13
Examples
14
Examples ---
 A program to calculate area and circumstance of a circle
15
Functions In Python
 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 aFunction
 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.
16
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.
17
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.
18
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.
19
Functions---
 Example: Define a function which can generate and print a tuple where
the value are square of numbers between 1 and 20.
20
Functions---
 Example: Factorial of a number
21
Built-in functions
Hdhh
22
Exercises
1) Write a function in python that counts number of vowels in given
string
2) Write a function in python that counts number of even and odd in a given
list.
3) Write a function in python that computes the factorial of a given number.
4) Write a Python function to sum all the numbers in a list.
Sample List : [8, 2, 3, 0, 7]
Expected Output : 20
5) Write a function in python that converts Decimal number to Binary
numbers?
6) Write a function in python that converts Binary number to Decimal
numbers?
23
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, they are not equivalent to
inline statements in C or C++, whose purpose is to stack allocation by passing function, during
invocation for performance reasons.
 The syntax of lambda functions contains only a single statement, which is as follows
lambda [arg1 [,arg2,.....argn]]:expression 24
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
25
26

More Related Content

Similar to Chapter - 4.pptx

Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptxvishnupriyapm4
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptxkrushnaraj1
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptxvishnupriyapm4
 
FUNCTIONINPYTHON.pptx
FUNCTIONINPYTHON.pptxFUNCTIONINPYTHON.pptx
FUNCTIONINPYTHON.pptxSheetalMavi2
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
Functions part1
Functions part1Functions part1
Functions part1yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 
Lecture11 abap on line
Lecture11 abap on lineLecture11 abap on line
Lecture11 abap on lineMilind Patil
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)Jay Patel
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and commentMalligaarjunanN
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .tarunsharmaug23
 

Similar to Chapter - 4.pptx (20)

Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
Python training
Python trainingPython training
Python training
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
FUNCTIONINPYTHON.pptx
FUNCTIONINPYTHON.pptxFUNCTIONINPYTHON.pptx
FUNCTIONINPYTHON.pptx
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
Functions part1
Functions part1Functions part1
Functions part1
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Lecture11 abap on line
Lecture11 abap on lineLecture11 abap on line
Lecture11 abap on line
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
function.pptx
function.pptxfunction.pptx
function.pptx
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .
 

More from MikialeTesfamariam (11)

traditional cliphers 7-11-12.ppt
traditional cliphers 7-11-12.ppttraditional cliphers 7-11-12.ppt
traditional cliphers 7-11-12.ppt
 
6 KBS_ES.ppt
6 KBS_ES.ppt6 KBS_ES.ppt
6 KBS_ES.ppt
 
Chapter - 6.pptx
Chapter - 6.pptxChapter - 6.pptx
Chapter - 6.pptx
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
 
Chapter - 3.pptx
Chapter - 3.pptxChapter - 3.pptx
Chapter - 3.pptx
 
Chapter - 1.pptx
Chapter - 1.pptxChapter - 1.pptx
Chapter - 1.pptx
 
Chapter -7.pptx
Chapter -7.pptxChapter -7.pptx
Chapter -7.pptx
 
Python_Functions.pdf
Python_Functions.pdfPython_Functions.pdf
Python_Functions.pdf
 
functions-.pdf
functions-.pdffunctions-.pdf
functions-.pdf
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

Chapter - 4.pptx

  • 2. MODULES  As you gain experience writing code, you will eventually work on projects that are so large that keeping all of the code in a single file becomes cumbersome.  Instead of writing a single file, you can put related code into separate files called modules.  Individual modules can be put together like building blocks to create a larger application.  There are four main advantages to breaking a program into modules: 1) Simplicity: Modules are focused on a single problem. 2) Maintainability: Small files are better than large files. 3) Reusability: Modules reduce duplicate code. 4) Scoping: Modules have their own namespaces. 2
  • 3. 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.  A module is a file containing Python code that can be re-used in other Python code files.  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 3
  • 4. MODULES---  A namespace is a collection of names, such as variable names, function names, and class names.  Every Python module has its own namespace. Variables, functions, and classes in a module can be accessed from within the same module by just typing their name.  To access a name in an imported module from the calling module, type the imported module’s name followed by a dot (.) and the name you want to use: Note: The name used to import a module is the same as the module’s file name. 4
  • 5. Working With Packages  Modules allow you to divide a program in to individual files that can be reused as needed.  Related code can be organized into a single module and kept separate from other code.  Package take this organizational structure one step further by allowing you to group related modules under a single namespace.  A package is a folder that contains one or more Python 5
  • 6. Module for Mathematical Functions  By importing math module 6
  • 7. Module for Mathematical Functions ---  By importing math module 7
  • 8. Module for Mathematical Functions---  By importing math module 8
  • 9. Module for Mathematical Functions ---  By importing math module 9
  • 10. Module for Mathematical Functions ---  By importing math module 10
  • 11. Module for Mathematical Functions ---  By importing math module 11
  • 12. module Date Time  A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.  When we execute the code from the example above the result will be: 2022-06-27 09:40:16.122293  The date contains year, month, day, hour, minute, second, and microsecond.  The datetime module has many methods to return information about the date object. 12
  • 13. module Date Time ---  The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone). 13
  • 15. Examples ---  A program to calculate area and circumstance of a circle 15
  • 16. Functions In Python  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 aFunction  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. 16
  • 17. 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. 17
  • 18. 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. 18
  • 19. 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. 19
  • 20. Functions---  Example: Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20. 20
  • 23. Exercises 1) Write a function in python that counts number of vowels in given string 2) Write a function in python that counts number of even and odd in a given list. 3) Write a function in python that computes the factorial of a given number. 4) Write a Python function to sum all the numbers in a list. Sample List : [8, 2, 3, 0, 7] Expected Output : 20 5) Write a function in python that converts Decimal number to Binary numbers? 6) Write a function in python that converts Binary number to Decimal numbers? 23
  • 24. 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, they are not equivalent to inline statements in C or C++, whose purpose is to stack allocation by passing function, during invocation for performance reasons.  The syntax of lambda functions contains only a single statement, which is as follows lambda [arg1 [,arg2,.....argn]]:expression 24
  • 25. 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 25
  • 26. 26