SlideShare a Scribd company logo
PYTHON-FUNCTIONS
Functions
Python Functions is a block of statements that return the Specific Task
Why is function needed?
The idea is to put some commonly or repeatedly done tasks together
and make a function so that instead of writing the same code again and
again for different inputs, we can do the function calls to reuse code
contained in it over and over again.
Function Syntax
Creating Python Function
We can create a Python function using the def keyword.
# A simple Python function
def fun():
print("Welcome to Bharat")
Calling a Python Function
After creating a function, we can call it by using the name of the
function followed by parenthesis containing parameters of that
particular function.
Program:
Python Function with Parameters
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 Function 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.
Types of Arguments
Python supports various types of arguments that can be passed at the
time of the function call.
1. Default Arguments
2. Keyword Arguements
Number of Arguments
By default, a function must be called with the correct number of
arguments. Meaning that if your function expects 2 arguments, you
have to call the function with 2 arguments, not more, and not less.
Arbitrary Arguments
If you do not know how many arguments that will be passed into your
function, add a * before the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access
the items accordingly:
Keyword Arguments
You can also send arguments with the key = value syntax.
This way the order of the arguments does not matter.
Arbitrary Keyword Arguments
If you do not know how many keyword arguments that will be passed
into your function, add two asterisk: ** before the parameter name in
the function definition.
This way the function will receive a dictionary of arguments, and can
access the items accordingly:
Default Parameter Value
The following example shows how to use a default parameter value.
If we call the function without argument, it uses the default value:
Passing List as an Argument
You can send any data types of argument to a function (string, number,
list, dictionary etc.), and it will be treated as the same data type inside
the function.
Return Values
To let a function return a value, use the return statement
Pass Statement
function definitions cannot be empty, but if you for some reason have a
function definition with no content, put in the pass statement to avoid
getting an error.
Exercise in Python Functions
1. Find the Given Number is Even or Odd
Pass By Value and Pass By Reference
Call By Value -> A copy of the variable is passed to the function.
Call By Reference -> An address of the variable is passed to the
function.
Call by Value
It is a way of passing arguments to a function in which the arguments
get copied to the formal parameters of a function and are stored in
different memory locations. In short, Any changes made within the
function are not reflected in the actual parameters of the function
when called.
Call By Reference
It is a way of passing arguments to a function call in which both the
actual argument and formal parameters refer to the same memory
locations and any changes made within the function are reflected in
the actual parameters of the function when called.
If we consider call by value and call by reference in Python, then we
must keep in mind that,
Python variables are not storage containers rather Python’s variables
are like memory references. They refer to the memory address where
value is stored.
Mutable Objects
An object whose internal state can be changed is called a mutable
object. Examples of Mutable objects are Lists, Sets, Dictionaries, byte
and array.
User- defined classes can be mutable or immutable, depending on
whether their internal state can be changed or not.
Immutable Objects
An object whose internal state cannot be changed is called an
immutable object. Examples of immutable objects are Numbers(int,
float, bool , etc), Strings, Tuples, Frozen sets(mutable version of sets are
termed as Frozen sets)
Note : All operations using immutable objects make copies.
Call By value in Python
When Immutable objects such as whole numbers, strings, etc are
passed as arguments to the function call, it can be considered as Call by
Value.
This is because when the values are modified within the function, then
the changes do not get reflected outside the function.
Call by Reference
What is the Output of this Program?
What is the Output of this Program?
Guess the Output of the Code?
Mutable vs Immutable
Call By value vs Call By Reference
Python Exercise
1. Write a Python Function to print a Fibonacci Number
2. Write a Python Function to Print a Lucas Number
3. Write a Python Function to convert Decimal into Binary
4. Write a Python Function to Convert Binary into Decimal
Python Recursion
Recursion is a computational problem-solving technique used in
computer science where the solution is dependent on solutions to
smaller instances of the same problem. It uses functions that call
themselves from within their code to solve such recursive problems.
The strategy is adaptable to a wide range of problems.
What is Recursion?
A process in which a function calls itself is called recursion. This process
helps ease the method of solving problems by replacing iterative code
with recursive statements.
Recursion in python is used when problems can be broken into simpler
parts for easier computation and more readable code.
Although recursion is found to give results faster in some cases when
properly optimized, it can also add to memory usage.Thus, recursion
should be used only when needed.
Recursive Python Function
Base Case: This helps us to terminate the recursive function. It is a
simple case that can be answered directly and doesn't use recursion. If
satisfied, it returns the final computable answer. If this is omitted, the
function will run till infinity.
Python interpreter limits the number of recursive calls for a function to
1000 by giving a recursion error.
General (Recursive) Case - This case uses recursion and is called unless
the base condition is satisfied.
Example Recursive Function
Types of Recursion in Python
1. Direct Recursion - In this type of recursion, the function calls itself.
1. Tail Recursion
2. Head Recursion
2. Indirect Recursion
Tail Recursive Function
Output : 5 4 3 2 1
Not a Tail Recursive Function
Head Recursion
If in a recursive function, the last statement is not a recursive call, i.e.,
during the unwinding phase, there are still some steps to occur, and
then it is called head recursion.
Output:
1 2 3 4 5
Indirect Recursion
In this type of recursion, the function calls another function which calls
the original function. Here, when function A() is called, it first executes
the print statement and then calls function B() with an incremented
value of n. Its print statement is executed within function B, and then
the function A() with a value of n reduced by five is called. The process
continues as long as the terminating condition is not satisfied.
Output: 20 21 16 17 12 13 8 9 4 5
Factorial of a Number
Output : 120
Ascending or Not
Output:
False
True
To Find the Nth number in Fibonacci Series
Output : 21
Integer Reversal
Output : 425
To check the Palindrome
Output:
True
False
Advantages of Recursion in Python
Recursion in Python often reduces the length of the code.
It helps increase the elegance and conciseness of the code.
It helps in breaking a complex problem into simpler ones.
We can do sequence generation and tree traversal better and easily
with recursion.
Disadvantages of Recursion in Python
It is difficult to frame and understand the logic behind recursive
functions. Thus, making them hard to debug.
Recursion in Python uses more memory as values need to be added
to the call stack with each recursive call.
Recursion in Python can be slow if not implemented correctly.
Python Recursive calls can be inefficient as, in some cases, they take
up a lot of memory and time.
Exercise 3:
1. Write a Program to Reverse Integer
2. Finding the Vowels
Ad

More Related Content

What's hot (20)

Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
Akhil Kaushik
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
sambitmandal
 
Identifiers
Identifiers Identifiers
Identifiers
Then Murugeshwari
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Python List.ppt
Python List.pptPython List.ppt
Python List.ppt
T PRIYA
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
Sujith Kumar
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Python programming
Python  programmingPython  programming
Python programming
Ashwin Kumar Ramasamy
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
nitamhaske
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
PriyankaC44
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
Arzath Areeff
 
Python Lambda Function
Python Lambda FunctionPython Lambda Function
Python Lambda Function
Md Soyaib
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
sambitmandal
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Python List.ppt
Python List.pptPython List.ppt
Python List.ppt
T PRIYA
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
Sujith Kumar
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
nitamhaske
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
PriyankaC44
 
Python Lambda Function
Python Lambda FunctionPython Lambda Function
Python Lambda Function
Md Soyaib
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
Sujith Kumar
 

Similar to Python-Functions.pptx (20)

Learn more about the concepts Functions of Python
Learn more about the concepts Functions of PythonLearn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONSINTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
alaparthi
 
Python functions
Python   functionsPython   functions
Python functions
Learnbay Datascience
 
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
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre1
 
_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
MikialeTesfamariam
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
bhuvanalakshmik2
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
Hattori Sidek
 
PP unit 2.pptx............................
PP unit 2.pptx............................PP unit 2.pptx............................
PP unit 2.pptx............................
MathanrajS6
 
PP unit 2.pptx.................................
PP unit 2.pptx.................................PP unit 2.pptx.................................
PP unit 2.pptx.................................
MathanrajS6
 
Decided to go to the 65 and the value of the number
Decided to go to the 65 and the value of the numberDecided to go to the 65 and the value of the number
Decided to go to the 65 and the value of the number
harshoberoi2050
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
arvdexamsection
 
use of Functions to write python program.pptx
use of Functions to write python program.pptxuse of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
 
Functions Programming in Python Language
Functions Programming in Python LanguageFunctions Programming in Python Language
Functions Programming in Python Language
BalaSubramanian376976
 
Learn more about the concepts Functions of Python
Learn more about the concepts Functions of PythonLearn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONSINTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
alaparthi
 
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
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre1
 
_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
PP unit 2.pptx............................
PP unit 2.pptx............................PP unit 2.pptx............................
PP unit 2.pptx............................
MathanrajS6
 
PP unit 2.pptx.................................
PP unit 2.pptx.................................PP unit 2.pptx.................................
PP unit 2.pptx.................................
MathanrajS6
 
Decided to go to the 65 and the value of the number
Decided to go to the 65 and the value of the numberDecided to go to the 65 and the value of the number
Decided to go to the 65 and the value of the number
harshoberoi2050
 
use of Functions to write python program.pptx
use of Functions to write python program.pptxuse of Functions to write python program.pptx
use of Functions to write python program.pptx
rahulsinghsikarwar2
 
Functions Programming in Python Language
Functions Programming in Python LanguageFunctions Programming in Python Language
Functions Programming in Python Language
BalaSubramanian376976
 
Ad

More from Karudaiyar Ganapathy (8)

Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
Karudaiyar Ganapathy
 
Python-exceptionHandling.pptx
Python-exceptionHandling.pptxPython-exceptionHandling.pptx
Python-exceptionHandling.pptx
Karudaiyar Ganapathy
 
Python-Encapsulation.pptx
Python-Encapsulation.pptxPython-Encapsulation.pptx
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
Python-DataAbstarction.pptx
Python-DataAbstarction.pptxPython-DataAbstarction.pptx
Python-DataAbstarction.pptx
Karudaiyar Ganapathy
 
Overloading vs Overriding.pptx
Overloading vs Overriding.pptxOverloading vs Overriding.pptx
Overloading vs Overriding.pptx
Karudaiyar Ganapathy
 
Python-Polymorphism.pptx
Python-Polymorphism.pptxPython-Polymorphism.pptx
Python-Polymorphism.pptx
Karudaiyar Ganapathy
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
Karudaiyar Ganapathy
 
Ad

Recently uploaded (20)

DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 

Python-Functions.pptx

  • 2. Functions Python Functions is a block of statements that return the Specific Task Why is function needed? The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again.
  • 4. Creating Python Function We can create a Python function using the def keyword. # A simple Python function def fun(): print("Welcome to Bharat")
  • 5. Calling a Python Function After creating a function, we can call it by using the name of the function followed by parenthesis containing parameters of that particular function. Program:
  • 6. Python Function with Parameters 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.
  • 7. Parameters or Arguments The terms parameter and argument can be used for the same thing: information that are passed into a function. From Function 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.
  • 8. Types of Arguments Python supports various types of arguments that can be passed at the time of the function call. 1. Default Arguments 2. Keyword Arguements
  • 9. Number of Arguments By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.
  • 10. Arbitrary Arguments If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition. This way the function will receive a tuple of arguments, and can access the items accordingly:
  • 11. Keyword Arguments You can also send arguments with the key = value syntax. This way the order of the arguments does not matter.
  • 12. Arbitrary Keyword Arguments If you do not know how many keyword arguments that will be passed into your function, add two asterisk: ** before the parameter name in the function definition. This way the function will receive a dictionary of arguments, and can access the items accordingly:
  • 13. Default Parameter Value The following example shows how to use a default parameter value. If we call the function without argument, it uses the default value:
  • 14. Passing List as an Argument You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
  • 15. Return Values To let a function return a value, use the return statement
  • 16. Pass Statement function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error.
  • 17. Exercise in Python Functions 1. Find the Given Number is Even or Odd
  • 18. Pass By Value and Pass By Reference Call By Value -> A copy of the variable is passed to the function. Call By Reference -> An address of the variable is passed to the function.
  • 19. Call by Value It is a way of passing arguments to a function in which the arguments get copied to the formal parameters of a function and are stored in different memory locations. In short, Any changes made within the function are not reflected in the actual parameters of the function when called.
  • 20. Call By Reference It is a way of passing arguments to a function call in which both the actual argument and formal parameters refer to the same memory locations and any changes made within the function are reflected in the actual parameters of the function when called. If we consider call by value and call by reference in Python, then we must keep in mind that, Python variables are not storage containers rather Python’s variables are like memory references. They refer to the memory address where value is stored.
  • 21. Mutable Objects An object whose internal state can be changed is called a mutable object. Examples of Mutable objects are Lists, Sets, Dictionaries, byte and array. User- defined classes can be mutable or immutable, depending on whether their internal state can be changed or not.
  • 22. Immutable Objects An object whose internal state cannot be changed is called an immutable object. Examples of immutable objects are Numbers(int, float, bool , etc), Strings, Tuples, Frozen sets(mutable version of sets are termed as Frozen sets) Note : All operations using immutable objects make copies.
  • 23. Call By value in Python When Immutable objects such as whole numbers, strings, etc are passed as arguments to the function call, it can be considered as Call by Value. This is because when the values are modified within the function, then the changes do not get reflected outside the function.
  • 25. What is the Output of this Program?
  • 26. What is the Output of this Program?
  • 27. Guess the Output of the Code?
  • 29. Call By value vs Call By Reference
  • 30. Python Exercise 1. Write a Python Function to print a Fibonacci Number 2. Write a Python Function to Print a Lucas Number 3. Write a Python Function to convert Decimal into Binary 4. Write a Python Function to Convert Binary into Decimal
  • 31. Python Recursion Recursion is a computational problem-solving technique used in computer science where the solution is dependent on solutions to smaller instances of the same problem. It uses functions that call themselves from within their code to solve such recursive problems. The strategy is adaptable to a wide range of problems.
  • 32. What is Recursion? A process in which a function calls itself is called recursion. This process helps ease the method of solving problems by replacing iterative code with recursive statements. Recursion in python is used when problems can be broken into simpler parts for easier computation and more readable code. Although recursion is found to give results faster in some cases when properly optimized, it can also add to memory usage.Thus, recursion should be used only when needed.
  • 33. Recursive Python Function Base Case: This helps us to terminate the recursive function. It is a simple case that can be answered directly and doesn't use recursion. If satisfied, it returns the final computable answer. If this is omitted, the function will run till infinity. Python interpreter limits the number of recursive calls for a function to 1000 by giving a recursion error. General (Recursive) Case - This case uses recursion and is called unless the base condition is satisfied.
  • 35. Types of Recursion in Python 1. Direct Recursion - In this type of recursion, the function calls itself. 1. Tail Recursion 2. Head Recursion 2. Indirect Recursion
  • 37. Not a Tail Recursive Function
  • 38. Head Recursion If in a recursive function, the last statement is not a recursive call, i.e., during the unwinding phase, there are still some steps to occur, and then it is called head recursion. Output: 1 2 3 4 5
  • 39. Indirect Recursion In this type of recursion, the function calls another function which calls the original function. Here, when function A() is called, it first executes the print statement and then calls function B() with an incremented value of n. Its print statement is executed within function B, and then the function A() with a value of n reduced by five is called. The process continues as long as the terminating condition is not satisfied. Output: 20 21 16 17 12 13 8 9 4 5
  • 40. Factorial of a Number Output : 120
  • 42. To Find the Nth number in Fibonacci Series Output : 21
  • 44. To check the Palindrome Output: True False
  • 45. Advantages of Recursion in Python Recursion in Python often reduces the length of the code. It helps increase the elegance and conciseness of the code. It helps in breaking a complex problem into simpler ones. We can do sequence generation and tree traversal better and easily with recursion.
  • 46. Disadvantages of Recursion in Python It is difficult to frame and understand the logic behind recursive functions. Thus, making them hard to debug. Recursion in Python uses more memory as values need to be added to the call stack with each recursive call. Recursion in Python can be slow if not implemented correctly. Python Recursive calls can be inefficient as, in some cases, they take up a lot of memory and time.
  • 47. Exercise 3: 1. Write a Program to Reverse Integer 2. Finding the Vowels