SlideShare a Scribd company logo
LECTURE – V
BY: ABHISHEK BHARDWAJ
PGT- COMPUTER SCIENCE
Functions
 Functions are sub – programs (part of the program), which
carries out some well defined tasks.
 Functions are categories into two parts:-
(i) Built - in Functions / Library Functions
(ii) User Defined Functions
 Built – in Functions are already defined functions and we just
need to use that.
Examples :- print(), input() etc.
 User defined functions are defined by the user as per
requirements.
User Defined Functions
 Any function definition is not executed until it is called.
 In Python function definition starts with “def” keyword.
Syntax :-
def function_name():
Statement…
Statement…
funtion_name()
Keyword
Function Call
Function Definition
“Program to Add Two Numbers” by Function
Step : 1
Step : 2
Step : 3
“Program to Check Even/ Odd Number” by Function
Step : 1
Step : 2
Step : 3
Argument/ Return from Function
 Arguments are those values which are passed to the
function.
 Arguments are provided during the function call.
 Arguments are the values (Inputs) on which action is
to be performed. (Don’t write this point in exam, just to
understand)
 Return is usually a value which a function returns to its
calling place.
 Return is final calculated value. (Don’t write this point
in exam, just to understand)
“Program to Add Two Numbers” by Argument
Step : 1
Step : 2
Step : 3
Step : 1
Step : 2
Step : 3
“Program to Add Two Numbers” by Argument
“Add Two Numbers” by Argument & Return
In place of a,b as
parameters you may
take x, y also.
Then c = x + y.
z = add (a, b) will
remain same as a, b
are arguments.
1
2
3
4
Types of Function Arguments
 Positional Argument/ Required Argument
Default Argument
Keyword Argument/ Named Argument
Positional Argument/ Required Argument:- If a function has
arguments then we need to pass the values to the function.
These values are passed in a sequence and this sequence need
to be followed strictly. Sequence means while
passing arguments
remember argument 4 will
be passed to parameter ‘a’
and argument 5 will be
passed to parameter ‘b’.
That’s why add (4, 5) will
be termed as positional
argument.
Positional Argument
Types of Function Arguments
Default Argument:- When we want to provide a default value to
the argument. In case no values are passed during the function
call, it will be initialized by the default value otherwise with the
past value. This concept is called default argument.
 Rule:- Once you assign an argument as default then rest of
the arguments after this must also be default arguments.
Example:- (a, b=5, c=10) Correct
(a, b=5, c) Incorrect
Default Argument
Output
Types of Function Arguments
Keyword Argument/ Named Argument:- Sequence of the
arguments may be changed using the concept of Keyword/
Named Argument.
Keyword Argument
Output
Scope of Variable (Local & Global Variables)
Global Variable:- A global variable is one which is not the part of
any function. It can be used in any function being a global
variable.
Local Variable:- A local variable is one which is the part of a
function and that’s why it can be used only within that function.
NOTE:
 Program Execution starts from main function.
_ _main_ _ (Understood in Python don’t need to write)
 Don’t need to write main function, it is understood.
Scope of Variable (Local & Global Variables)
 We may define global variable at two places.
(i) In the starting of the Program where variable is not
a part of any function.
(ii) Inside main function.
At add () call, value of m & n will be passed to a and b.
At sub () call, value of m & n will be passed to x and y.
Scope of Variable (Local & Global Variables)
What does happen if, a variable is available as “Local” as well as
“Global”?
 If same name variable has been declared globally as well as
locally, then first of all program control will be at main function
i.e at global variable. In this case value of global variable will be
printed. When control will be at local section then local value of
variable will be printed.
Output
1
2
3
1
2
3
Scope of Variable (Local & Global Variables)
If, we want a global variable need to be used in local section of a function!
 From below Example it is clear that first of all the program execution will
start from main and the value of global variable is printed as a = 10 then
demo() will be called and variable a will be set as global which means no
new variable will be created, so existing global variable will be used and its
value will be changed to 5 from 10. This value will not change during whole
program execution now. That’s why at step 2 and 3 the print function
produce value of a = 5.
Output
1
2
3
1
2
3
a = 10 5 5
At Step 1 Variable a is set to 10
At Step 2 Variable a is set to 5 (Declared Global)
Mutability & Immutability of Function Parameter
All the data types in Python have been divided into two types:-
Immutable Objects:- These are of in-built types like int, float,
bool, string, unicode, tuple. In simple words, an immutable
object can’t be changed after it is created.
Mutable Objects:- These are of type list, dictionary, set. Custom
classes are generally mutable. If the value of an object can
change, the object is called mutable.
NOTE:
When we pass arguments in a function, we actually pass any data
type (eg. int, float, list, bool, string etc). If we pass any mutable
object as an argument in function, we may change its value but if we
pass immutable object as an argument in function we may not
change its value. Anyhow, if we are able to change immutable
objects, we cannot replicate that change in main().
Mutability & Immutability of Function Parameter
1
2
3
Global ‘a’
Local ‘a’
Global ‘a’
1
2
3
Global ‘a’
Local ‘a’
Global ‘a’
Mutability & Immutability of Function Parameter
1
2
3
20 20 30 40
mylist
list1
10 20 30 40
list1
1
2
3
List is Mutable Data Type in Python
0 1 2 3 0 1 2 3
Program to Check a Number is Prime or Not using
Function?
Function
Definition
_ _main_ _
Program to Check a Number is Prime or Not using
Function & Return?
Function
Definition
_ _main_ _
Program to Check a Number is Palindrome or Not
using Function?
Program to Check a Number is Palindrome or Not
using Function and Return?
Program to Count Total Odd/ Even in a List Using
Function?
Program to Count Total Odd/ Even in a List Using
Function and Return?
Program to Search a Number in the List Using
Function?
OUTPUT
If break statement is not used
in this program then position in
output section will be shown as
“Num Found at 2 Position”
because 1 Position will be
overwritten by 2 Position. In
Searching List the Loop must
terminate as soon as the
element is found. So, less
search time. Best Run Time.
Program to Search a Number in the List Using
Function and Return? (Linear/ Sequential Search)
If Value is not found, for this case
position has been initialized as -1.
As we know indexing in list starts
from 0.
A linear search or sequential
search is a method for finding an
element within a list.

More Related Content

Similar to Functions in Python.pdfnsjiwshkwijjahuwjwjw

functions-.pdf
functions-.pdffunctions-.pdf
functions-.pdf
MikialeTesfamariam
 
Function
FunctionFunction
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
Functions
FunctionsFunctions
Functions
Septi Ratnasari
 
C functions list
C functions listC functions list
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
SulekhJangra
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
RAJ KUMAR
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Rhishav Poudyal
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
arvdexamsection
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
Ahmad Kamal
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
15 functions
15 functions15 functions
15 functions
fyjordan9
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
Daddy84
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
alaparthi
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
KalashJain27
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 

Similar to Functions in Python.pdfnsjiwshkwijjahuwjwjw (20)

functions-.pdf
functions-.pdffunctions-.pdf
functions-.pdf
 
Function
FunctionFunction
Function
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Functions
FunctionsFunctions
Functions
 
C functions list
C functions listC functions list
C functions list
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
 
15 functions
15 functions15 functions
15 functions
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 

Recently uploaded

MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 

Recently uploaded (20)

MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 

Functions in Python.pdfnsjiwshkwijjahuwjwjw

  • 1. LECTURE – V BY: ABHISHEK BHARDWAJ PGT- COMPUTER SCIENCE
  • 2. Functions  Functions are sub – programs (part of the program), which carries out some well defined tasks.  Functions are categories into two parts:- (i) Built - in Functions / Library Functions (ii) User Defined Functions  Built – in Functions are already defined functions and we just need to use that. Examples :- print(), input() etc.  User defined functions are defined by the user as per requirements.
  • 3. User Defined Functions  Any function definition is not executed until it is called.  In Python function definition starts with “def” keyword. Syntax :- def function_name(): Statement… Statement… funtion_name() Keyword Function Call Function Definition
  • 4. “Program to Add Two Numbers” by Function Step : 1 Step : 2 Step : 3
  • 5. “Program to Check Even/ Odd Number” by Function Step : 1 Step : 2 Step : 3
  • 6. Argument/ Return from Function  Arguments are those values which are passed to the function.  Arguments are provided during the function call.  Arguments are the values (Inputs) on which action is to be performed. (Don’t write this point in exam, just to understand)  Return is usually a value which a function returns to its calling place.  Return is final calculated value. (Don’t write this point in exam, just to understand)
  • 7. “Program to Add Two Numbers” by Argument Step : 1 Step : 2 Step : 3 Step : 1 Step : 2 Step : 3
  • 8. “Program to Add Two Numbers” by Argument
  • 9. “Add Two Numbers” by Argument & Return In place of a,b as parameters you may take x, y also. Then c = x + y. z = add (a, b) will remain same as a, b are arguments. 1 2 3 4
  • 10. Types of Function Arguments  Positional Argument/ Required Argument Default Argument Keyword Argument/ Named Argument Positional Argument/ Required Argument:- If a function has arguments then we need to pass the values to the function. These values are passed in a sequence and this sequence need to be followed strictly. Sequence means while passing arguments remember argument 4 will be passed to parameter ‘a’ and argument 5 will be passed to parameter ‘b’. That’s why add (4, 5) will be termed as positional argument. Positional Argument
  • 11. Types of Function Arguments Default Argument:- When we want to provide a default value to the argument. In case no values are passed during the function call, it will be initialized by the default value otherwise with the past value. This concept is called default argument.  Rule:- Once you assign an argument as default then rest of the arguments after this must also be default arguments. Example:- (a, b=5, c=10) Correct (a, b=5, c) Incorrect Default Argument Output
  • 12. Types of Function Arguments Keyword Argument/ Named Argument:- Sequence of the arguments may be changed using the concept of Keyword/ Named Argument. Keyword Argument Output
  • 13. Scope of Variable (Local & Global Variables) Global Variable:- A global variable is one which is not the part of any function. It can be used in any function being a global variable. Local Variable:- A local variable is one which is the part of a function and that’s why it can be used only within that function. NOTE:  Program Execution starts from main function. _ _main_ _ (Understood in Python don’t need to write)  Don’t need to write main function, it is understood.
  • 14. Scope of Variable (Local & Global Variables)  We may define global variable at two places. (i) In the starting of the Program where variable is not a part of any function. (ii) Inside main function. At add () call, value of m & n will be passed to a and b. At sub () call, value of m & n will be passed to x and y.
  • 15. Scope of Variable (Local & Global Variables) What does happen if, a variable is available as “Local” as well as “Global”?  If same name variable has been declared globally as well as locally, then first of all program control will be at main function i.e at global variable. In this case value of global variable will be printed. When control will be at local section then local value of variable will be printed. Output 1 2 3 1 2 3
  • 16. Scope of Variable (Local & Global Variables) If, we want a global variable need to be used in local section of a function!  From below Example it is clear that first of all the program execution will start from main and the value of global variable is printed as a = 10 then demo() will be called and variable a will be set as global which means no new variable will be created, so existing global variable will be used and its value will be changed to 5 from 10. This value will not change during whole program execution now. That’s why at step 2 and 3 the print function produce value of a = 5. Output 1 2 3 1 2 3 a = 10 5 5 At Step 1 Variable a is set to 10 At Step 2 Variable a is set to 5 (Declared Global)
  • 17. Mutability & Immutability of Function Parameter All the data types in Python have been divided into two types:- Immutable Objects:- These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can’t be changed after it is created. Mutable Objects:- These are of type list, dictionary, set. Custom classes are generally mutable. If the value of an object can change, the object is called mutable. NOTE: When we pass arguments in a function, we actually pass any data type (eg. int, float, list, bool, string etc). If we pass any mutable object as an argument in function, we may change its value but if we pass immutable object as an argument in function we may not change its value. Anyhow, if we are able to change immutable objects, we cannot replicate that change in main().
  • 18. Mutability & Immutability of Function Parameter 1 2 3 Global ‘a’ Local ‘a’ Global ‘a’ 1 2 3 Global ‘a’ Local ‘a’ Global ‘a’
  • 19. Mutability & Immutability of Function Parameter 1 2 3 20 20 30 40 mylist list1 10 20 30 40 list1 1 2 3 List is Mutable Data Type in Python 0 1 2 3 0 1 2 3
  • 20. Program to Check a Number is Prime or Not using Function? Function Definition _ _main_ _
  • 21. Program to Check a Number is Prime or Not using Function & Return? Function Definition _ _main_ _
  • 22. Program to Check a Number is Palindrome or Not using Function?
  • 23. Program to Check a Number is Palindrome or Not using Function and Return?
  • 24. Program to Count Total Odd/ Even in a List Using Function?
  • 25. Program to Count Total Odd/ Even in a List Using Function and Return?
  • 26. Program to Search a Number in the List Using Function? OUTPUT If break statement is not used in this program then position in output section will be shown as “Num Found at 2 Position” because 1 Position will be overwritten by 2 Position. In Searching List the Loop must terminate as soon as the element is found. So, less search time. Best Run Time.
  • 27. Program to Search a Number in the List Using Function and Return? (Linear/ Sequential Search) If Value is not found, for this case position has been initialized as -1. As we know indexing in list starts from 0. A linear search or sequential search is a method for finding an element within a list.