際際滷

際際滷Share a Scribd company logo
Userdefined functions brief explaination.pdf
Unit I: Computational Thinking and Programming 
Revision of Python topics covered in Class XI.
Functions: types of function (built-in functions, functions defined in module, user defined functions),
creating user defined function, arguments and parameters, default parameters, positional parameters,
function returning value(s), flow of execution, scope of a variable (global scope, local scope)
Introduction to files, types of files (Text file, Binary file, CSV file), relative and absolute paths
Text file: opening a text file, text file open modes (r, r+, w, w+, a, a+), closing a text file, opening a file using
with clause, writing/appending data to a text file using write() and writelines(), reading from a text file using
read(), readline() and readlines(), seek and tell methods, manipulation of data in a text file
Binary file: basic operations on a binary file: open using file open modes (rb, rb+, wb, wb+, ab, ab+), close a
binary file, import pickle module, dump() and load() method, read, write/create, search, append and update
operations in a binary file CSV file: import csv module, open / close csv file, write into a csv file using
csv.writer() and read from a csv file using csv.reader( )
Data Structure: Stack, operations on stack (push & pop), implementation of stack using list.
Revision of Python topics covered in Class XI.
 Datatypes
 Operators
 Errors
 Flow of Control : if else construct
 Loops : For and While loop
 Strings
 List
 Tuples
 Dictionary
 Python modules : math , random, statistics
Functions: types of function (built-in functions, functions defined in module, user
defined functions), creating user defined function, arguments and parameters, default
parameters, positional parameters, function returning value(s), flow of execution,
scope of a variable (global scope, local scope)
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.
In Python a function is defined using the def keyword.
Syntax :
def my_function():
print("Hello from a function")
my_function()
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses.
You can add as many arguments as you want, just separate them with a comma.
Parameters or Arguments?
The terms parameter and argument can be used for the same thing: information that are
passed into a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the function definition.
An argument is the value that is sent to the function when it is called.
Parameters or Arguments?
def sum(x,y):
sum=x+y
print("sum=",sum) a
sum("5","7")
def sum(x,y):
sum=x+y
print("sum=",sum)
sum(5,7)
Output : sum= 57
Output : sum= 12
A parameter is a variable in a method definition. When a method is called, the
arguments are the data you pass into the method's parameters. Parameter is va
riable in the declaration of function. Argument is the actual value of this va
riable that gets passed to function.
When a method is called, the arguments are the data you pass into the method's
parameters. Parameter is variable in the declaration of function. Argument is
the actual value of this variable that gets passed to function.
Examples for Parameters and arguments
5, 7 are arguments
x,y are parameters
TYPES OF ARGUMENTS :
Positional Arguments , Default Arguments
positional arguments are those arguments which are passed to a
function in correct positional order
One parameter mentioned
Passing two arguments
Error generated
DEFAULT ARGUMENTS :Sometimes we may want to use parameters in a function that takes
default values in case the user doesnt want to provide a value for them.
For this, we can use default arguments which assumes a default value if a value is not supplied as
an argument while calling the function.
In parameters list, we can give default values to one or more parameters.
As its positional passing first argument
goes to first parameter which is a. It
wont give error as b already has a
default value
Using a non-default argument after default arguments raise a SyntaxError.
In Python functions, a default argument can only be followed by a
default argument.
Important : Non-default arguments must be placed before default arguments.
REASON : In function, the values are assigned to parameters in order, by their position. The value of
default argument may be optional as there is the default value if not provided, but the value for a non-
default argument is mandatory as there exists no default value for this CodeText
As its positional passing first argument goes to
first parameter which is a. As there is no
second argument passed and b does
not have a default value it will give an error
Returning values from a function
Return statement is used to return a value from a function
A return statement is used to end the execution of the function call and returns
the result
(value of the expression following the return keyword) to the caller. The statements
after the return statements are not executed. If the return statement is without any
expression, then the special value None is returned.
A return statement is overall used to invoke a function so that the passed
statements can be executed.
def fun():
statements
. .
return [expression]
fun()
Function definition
Value r being sent back at
place of calling in where it
is stored in variable ans
Function call
Control passing in Functions
Example to show that in python we can return one or more
values using return statement
TYPES OF PARAMETER PASSING
PASS BY VALUE METHOD
In the above code the datatype of argument passed was a list which is mutable . Thus even when
the changes are made in function increment() in another identifier name l1 then also the
changes are reflected back in original identifier l2 . This type of parameter passing is
called PASS BY REFERENCE
PASS BY REFERENCE
Mutable datatypes as arguments in functions
Scope of variables in functions :
Scope of a variable is the area in which it is defined and it can be used.
Local scope : A vaiable is defined and used inside a function.It can be used only in that
function. If you try to use it outside that function error comes .
Global scope: When a variable is defined outside, before all the functions that variable
can be used everywhere i.e. inside all the functions defined. it will not give an error.
Such variables are called global .
SCOPES OF VARIABLES
EXAMPLE OF SCOPES OF VARIABLES
GLOBAL a and LOCAL a are different variables.
Local variable a is active in function first()
As soon as control comes out of function first
global variable a becomes active
Global keyword is used when we want to read or write any global variable value inside the function.
The global keyword used for a variable declared outside the function does not have any effect on it.
In the same line, a variable cannot be declared global and assigned a value. E.g. global x = 5 is not
allowed.
Global keyword
Changes done inside
function in global
variable a are reflected
In global variable a

More Related Content

Similar to Userdefined functions brief explaination.pdf (20)

04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
Functions
FunctionsFunctions
Functions
Septi Ratnasari
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjw
MayankSinghRawat6
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
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.pptPy-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
Powerpoint presentation for Python Functions
Powerpoint presentation for Python FunctionsPowerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
functions in python By Eng. Osama Ghandour 悋惆悋  悋惡悋惓 惺 惆愕 悋愕悋 ...functions in python By Eng. Osama Ghandour 悋惆悋  悋惡悋惓 惺 惆愕 悋愕悋 ...
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
Osama Ghandour Geris
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
Hattori Sidek
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
Krushal Kakadia
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
RAJ KUMAR
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
PYTHON PPT.pptx python is very useful for day to day life
PYTHON PPT.pptx python is very useful for day to day lifePYTHON PPT.pptx python is very useful for day to day life
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
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
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
functions notes.pdf python functions and opp
functions notes.pdf python functions and oppfunctions notes.pdf python functions and opp
functions notes.pdf python functions and opp
KirtiGarg71
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjwFunctions in Python.pdfnsjiwshkwijjahuwjwjw
Functions in Python.pdfnsjiwshkwijjahuwjwjw
MayankSinghRawat6
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
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.pptPy-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
Powerpoint presentation for Python Functions
Powerpoint presentation for Python FunctionsPowerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
functions in python By Eng. Osama Ghandour 悋惆悋  悋惡悋惓 惺 惆愕 悋愕悋 ...functions in python By Eng. Osama Ghandour 悋惆悋  悋惡悋惓 惺 惆愕 悋愕悋 ...
functions in python By Eng. Osama Ghandour 悋惆悋 悋惡悋惓 惺 惆愕 悋愕悋 ...
Osama Ghandour Geris
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
RAJ KUMAR
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
PYTHON PPT.pptx python is very useful for day to day life
PYTHON PPT.pptx python is very useful for day to day lifePYTHON PPT.pptx python is very useful for day to day life
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
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

More from DeeptiMalhotra19 (10)

Modern Public School Newsletter for 2025
Modern Public School  Newsletter for 2025Modern Public School  Newsletter for 2025
Modern Public School Newsletter for 2025
DeeptiMalhotra19
csv and test files comparison of the files
csv and test files comparison of the filescsv and test files comparison of the files
csv and test files comparison of the files
DeeptiMalhotra19
Stacks in Python will be made using lists.pdf
Stacks in Python will be made using lists.pdfStacks in Python will be made using lists.pdf
Stacks in Python will be made using lists.pdf
DeeptiMalhotra19
Newsletter june 2022.pdf
Newsletter june 2022.pdfNewsletter june 2022.pdf
Newsletter june 2022.pdf
DeeptiMalhotra19
Newsletter of May -2022
Newsletter of May  -2022Newsletter of May  -2022
Newsletter of May -2022
DeeptiMalhotra19
Newsletter- Hindi Subject
 Newsletter- Hindi Subject Newsletter- Hindi Subject
Newsletter- Hindi Subject
DeeptiMalhotra19
KINDERGARTEN NEWSLETTER 2022
KINDERGARTEN NEWSLETTER 2022    KINDERGARTEN NEWSLETTER 2022
KINDERGARTEN NEWSLETTER 2022
DeeptiMalhotra19
Science Newsletter
Science NewsletterScience Newsletter
Science Newsletter
DeeptiMalhotra19
Maths Newsletter
Maths NewsletterMaths Newsletter
Maths Newsletter
DeeptiMalhotra19
Newsletter - English
Newsletter - English Newsletter - English
Newsletter - English
DeeptiMalhotra19
Modern Public School Newsletter for 2025
Modern Public School  Newsletter for 2025Modern Public School  Newsletter for 2025
Modern Public School Newsletter for 2025
DeeptiMalhotra19
csv and test files comparison of the files
csv and test files comparison of the filescsv and test files comparison of the files
csv and test files comparison of the files
DeeptiMalhotra19
Stacks in Python will be made using lists.pdf
Stacks in Python will be made using lists.pdfStacks in Python will be made using lists.pdf
Stacks in Python will be made using lists.pdf
DeeptiMalhotra19
Newsletter june 2022.pdf
Newsletter june 2022.pdfNewsletter june 2022.pdf
Newsletter june 2022.pdf
DeeptiMalhotra19
Newsletter of May -2022
Newsletter of May  -2022Newsletter of May  -2022
Newsletter of May -2022
DeeptiMalhotra19
Newsletter- Hindi Subject
 Newsletter- Hindi Subject Newsletter- Hindi Subject
Newsletter- Hindi Subject
DeeptiMalhotra19
KINDERGARTEN NEWSLETTER 2022
KINDERGARTEN NEWSLETTER 2022    KINDERGARTEN NEWSLETTER 2022
KINDERGARTEN NEWSLETTER 2022
DeeptiMalhotra19

Recently uploaded (20)

The Effectiveness of Manual Therapies on the Thoracic.pptx
The Effectiveness of Manual Therapies on the Thoracic.pptxThe Effectiveness of Manual Therapies on the Thoracic.pptx
The Effectiveness of Manual Therapies on the Thoracic.pptx
SamarHosni3
Pushyabhuti Dynesty Vardhan Vamsha Early Rulers.pptx
Pushyabhuti Dynesty Vardhan Vamsha Early Rulers.pptxPushyabhuti Dynesty Vardhan Vamsha Early Rulers.pptx
Pushyabhuti Dynesty Vardhan Vamsha Early Rulers.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. DabhadeAnti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean ExpressionsIntroduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
GS Virdi
How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17
Celine George
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdfIB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
Dr. Mahtab Alam
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
Anthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Anthelmintic Agent.pptx by Mrs. Manjushri P. DabhadeAnthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Anthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptxO SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
Literature Hero
Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...
keshanf79
Using GenAI for Universal Design for Learning
Using GenAI for Universal Design for LearningUsing GenAI for Universal Design for Learning
Using GenAI for Universal Design for Learning
Damian T. Gordon
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. DabhadeCombinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
How to manage Customer Tips with Odoo 17 Point Of Sale
How to manage Customer Tips with Odoo 17 Point Of SaleHow to manage Customer Tips with Odoo 17 Point Of Sale
How to manage Customer Tips with Odoo 17 Point Of Sale
Celine George
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. VirdiDigital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
GS Virdi
LITERATURE QUIZ | THE QUIZ CLUB OF PSGCAS | 11 MARCH 2025 .pdf
LITERATURE QUIZ | THE QUIZ CLUB OF PSGCAS | 11 MARCH 2025 .pdfLITERATURE QUIZ | THE QUIZ CLUB OF PSGCAS | 11 MARCH 2025 .pdf
LITERATURE QUIZ | THE QUIZ CLUB OF PSGCAS | 11 MARCH 2025 .pdf
Quiz Club of PSG College of Arts & Science
Using social media to learn from conferences
Using social media to learn from conferencesUsing social media to learn from conferences
Using social media to learn from conferences
Sue Beckingham
Test Bank Pharmacology 3rd Edition Brenner Stevens
Test Bank Pharmacology 3rd Edition Brenner  StevensTest Bank Pharmacology 3rd Edition Brenner  Stevens
Test Bank Pharmacology 3rd Edition Brenner Stevens
evakimworwa38
nature and importance of Indian Knowledge System
nature and importance of Indian Knowledge Systemnature and importance of Indian Knowledge System
nature and importance of Indian Knowledge System
hanishabatra0
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptxA-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
A-Z GENERAL QUIZ | THE QUIZ CLUB OF PSGCAS | 14TH MARCH 2025.pptx
Quiz Club of PSG College of Arts & Science
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
Prabhakar Singh Patel
The Effectiveness of Manual Therapies on the Thoracic.pptx
The Effectiveness of Manual Therapies on the Thoracic.pptxThe Effectiveness of Manual Therapies on the Thoracic.pptx
The Effectiveness of Manual Therapies on the Thoracic.pptx
SamarHosni3
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. DabhadeAnti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Anti-Protozoal Agents.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean ExpressionsIntroduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
GS Virdi
How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17
Celine George
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdfIB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
Dr. Mahtab Alam
Anthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Anthelmintic Agent.pptx by Mrs. Manjushri P. DabhadeAnthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Anthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptxO SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
Literature Hero
Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...Mixed_Sinhala_Dual_Male_Names (1).pdf...
Mixed_Sinhala_Dual_Male_Names (1).pdf...
keshanf79
Using GenAI for Universal Design for Learning
Using GenAI for Universal Design for LearningUsing GenAI for Universal Design for Learning
Using GenAI for Universal Design for Learning
Damian T. Gordon
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. DabhadeCombinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
How to manage Customer Tips with Odoo 17 Point Of Sale
How to manage Customer Tips with Odoo 17 Point Of SaleHow to manage Customer Tips with Odoo 17 Point Of Sale
How to manage Customer Tips with Odoo 17 Point Of Sale
Celine George
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. VirdiDigital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
Digital Electronics - Boolean Algebra (Module 2) - Dr. G.S. Virdi
GS Virdi
Using social media to learn from conferences
Using social media to learn from conferencesUsing social media to learn from conferences
Using social media to learn from conferences
Sue Beckingham
Test Bank Pharmacology 3rd Edition Brenner Stevens
Test Bank Pharmacology 3rd Edition Brenner  StevensTest Bank Pharmacology 3rd Edition Brenner  Stevens
Test Bank Pharmacology 3rd Edition Brenner Stevens
evakimworwa38
nature and importance of Indian Knowledge System
nature and importance of Indian Knowledge Systemnature and importance of Indian Knowledge System
nature and importance of Indian Knowledge System
hanishabatra0
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
Prabhakar Singh Patel

Userdefined functions brief explaination.pdf

  • 2. Unit I: Computational Thinking and Programming Revision of Python topics covered in Class XI. Functions: types of function (built-in functions, functions defined in module, user defined functions), creating user defined function, arguments and parameters, default parameters, positional parameters, function returning value(s), flow of execution, scope of a variable (global scope, local scope) Introduction to files, types of files (Text file, Binary file, CSV file), relative and absolute paths Text file: opening a text file, text file open modes (r, r+, w, w+, a, a+), closing a text file, opening a file using with clause, writing/appending data to a text file using write() and writelines(), reading from a text file using read(), readline() and readlines(), seek and tell methods, manipulation of data in a text file Binary file: basic operations on a binary file: open using file open modes (rb, rb+, wb, wb+, ab, ab+), close a binary file, import pickle module, dump() and load() method, read, write/create, search, append and update operations in a binary file CSV file: import csv module, open / close csv file, write into a csv file using csv.writer() and read from a csv file using csv.reader( ) Data Structure: Stack, operations on stack (push & pop), implementation of stack using list.
  • 3. Revision of Python topics covered in Class XI. Datatypes Operators Errors Flow of Control : if else construct Loops : For and While loop Strings List Tuples Dictionary Python modules : math , random, statistics
  • 4. Functions: types of function (built-in functions, functions defined in module, user defined functions), creating user defined function, arguments and parameters, default parameters, positional parameters, function returning value(s), flow of execution, scope of a variable (global scope, local scope) 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. In Python a function is defined using the def keyword. Syntax : def my_function(): print("Hello from a function") my_function()
  • 5. Arguments Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. Parameters or Arguments? The terms parameter and argument can be used for the same thing: information that are passed into a function. From a function's perspective: A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called. Parameters or Arguments?
  • 6. def sum(x,y): sum=x+y print("sum=",sum) a sum("5","7") def sum(x,y): sum=x+y print("sum=",sum) sum(5,7) Output : sum= 57 Output : sum= 12 A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters. Parameter is va riable in the declaration of function. Argument is the actual value of this va riable that gets passed to function. When a method is called, the arguments are the data you pass into the method's parameters. Parameter is variable in the declaration of function. Argument is the actual value of this variable that gets passed to function. Examples for Parameters and arguments 5, 7 are arguments x,y are parameters
  • 7. TYPES OF ARGUMENTS : Positional Arguments , Default Arguments positional arguments are those arguments which are passed to a function in correct positional order One parameter mentioned Passing two arguments Error generated
  • 8. DEFAULT ARGUMENTS :Sometimes we may want to use parameters in a function that takes default values in case the user doesnt want to provide a value for them. For this, we can use default arguments which assumes a default value if a value is not supplied as an argument while calling the function. In parameters list, we can give default values to one or more parameters.
  • 9. As its positional passing first argument goes to first parameter which is a. It wont give error as b already has a default value
  • 10. Using a non-default argument after default arguments raise a SyntaxError. In Python functions, a default argument can only be followed by a default argument. Important : Non-default arguments must be placed before default arguments. REASON : In function, the values are assigned to parameters in order, by their position. The value of default argument may be optional as there is the default value if not provided, but the value for a non- default argument is mandatory as there exists no default value for this CodeText As its positional passing first argument goes to first parameter which is a. As there is no second argument passed and b does not have a default value it will give an error
  • 11. Returning values from a function Return statement is used to return a value from a function A return statement is used to end the execution of the function call and returns the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall used to invoke a function so that the passed statements can be executed. def fun(): statements . . return [expression] fun()
  • 12. Function definition Value r being sent back at place of calling in where it is stored in variable ans Function call Control passing in Functions
  • 13. Example to show that in python we can return one or more values using return statement
  • 14. TYPES OF PARAMETER PASSING PASS BY VALUE METHOD
  • 15. In the above code the datatype of argument passed was a list which is mutable . Thus even when the changes are made in function increment() in another identifier name l1 then also the changes are reflected back in original identifier l2 . This type of parameter passing is called PASS BY REFERENCE PASS BY REFERENCE Mutable datatypes as arguments in functions
  • 16. Scope of variables in functions : Scope of a variable is the area in which it is defined and it can be used. Local scope : A vaiable is defined and used inside a function.It can be used only in that function. If you try to use it outside that function error comes . Global scope: When a variable is defined outside, before all the functions that variable can be used everywhere i.e. inside all the functions defined. it will not give an error. Such variables are called global . SCOPES OF VARIABLES
  • 17. EXAMPLE OF SCOPES OF VARIABLES GLOBAL a and LOCAL a are different variables. Local variable a is active in function first() As soon as control comes out of function first global variable a becomes active
  • 18. Global keyword is used when we want to read or write any global variable value inside the function. The global keyword used for a variable declared outside the function does not have any effect on it. In the same line, a variable cannot be declared global and assigned a value. E.g. global x = 5 is not allowed. Global keyword Changes done inside function in global variable a are reflected In global variable a