DAY 7
- Python
Intern
Functions in Python:
A function is named sequence of statement(s) that
performs a computation. It contains line of code(s)
that are executed sequentially from top to bottom by
Python interpreter. They are the most important
building block for any software in Python. For working
in script mode, we need to write the Python code in
functions and save it in the file having .py extension.
Types of function
There are two types of function in Python
programming:
Standard library functions - These are built-in
functions in Python that are available to use.
User-defined functions - We can create our own
functions based on our requirements.
Python Function Declaration
def function_name():
# function body
function_name()
def - keyword used to declare a function
function_name - any name given to the
function
Python Function Declaration
def function_name(arguments):
# function body
return
def - keyword used to declare a function
function_name - any name given to the
function
arguments - any value passed to function
return (optional) - returns value from a
function
Example - Python Function
def greet():
print('Hello World!')
# call the function
greet()
print('Outside function')
Function call
def greet():
def greet():
print('Hello World!')
print('Hello World!')
def add(): def add():
c=10+20 c=10+20
print(c) print(c)
# call the function
# call the function add()
greet() greet()
add() ##output
##output 30
Hello World! Hello World!
30
Function call within other func
def greet():
def greet():
print('Hello World!')
print('Hello World!')
def add():
def add():
#inside other function
greet() c=10+20
c=10+20 print(c)
print(c) # call the function
Comment-won’t run
add() #add()
##output greet()
Hello World! ##output
30 Hello World!
Local and Global Variable
Local Variable Global Variable
def add(): b=10Global variable
a=10 def add():
b=20Local variable a=20Local variable
print(a+b) print(a+b)
add() add()
##output Print(b)
30 ##output
30
10
Example - Python Function Arguments
,
# function with two arguments
def add_numbers(num1, num2):
sum = num1 + num2
print("Sum: ",sum)
# function call with two values
add_numbers(5, 4)
# Output: Sum: 9
Example - Python Function Arguments
,
# function with two arguments
def add_numbers(num1, num2,):
sum = num1 + num2
print("Sum: ",sum)
# function call with two values
a=5
b=4
add_numbers(a, b)
# Output: Sum: 9
Python Function Arguments
an argument is a value that is accepted by a
function.
In Python, functions can accept arguments in
various ways. There are mainly four types of
function arguments
1. Positional Arguments
2. Keyword Arguments
3. Default Arguments
4. Arbitrary Arguments
1. Positional Arguments
These are the most common types of
arguments passed to functions and are
matched based on their position in the
function's signature.
Example:
def greet(name, message):
print(f"Hello, {name}! {message}")
greet("Alice", "How are you?")
# "Alice" corresponds to name, "How are
you?" corresponds to message
2. Keyword Arguments
In this type, arguments are identified by
their parameter names, allowing for a more
explicit function call.
Example:
def display_info(first_name, last_name):
print('First Name:', first_name)
print('Last Name:', last_name)
display_info(last_name = 'Cartman',
first_name = 'Eric')
3. Function Argument with
Default Values
In Python, we can provide default values to function
arguments.
We use the = operator to provide default values. For
example,
def add_numbers( a = 7, b = 8):
sum = a + b
print('Sum:', sum)
# function call with two arguments
add_numbers(2, 3)
# function call with one argument
add_numbers(a = 2)
# function call with no arguments
add_numbers()
4. Arbitrary Arguments
Arbitrary arguments allow us to pass a varying
number of values during a function call.
We use an asterisk (*) before the parameter
name to denote this kind of argument. For
example,
# program to find sum of multiple numbers
def my_function(*kids):
print("The youngest child is " + kids[2])
# function call with 3 arguments
my_function("Emil", "Tobias", "Linus")
#output
The youngest child is Linus
Example - return Statement in Python
A Python function may or may not return a value.
If we want our function to return some value to a
function call, we use the return statement.
# function definition
def find_square(num):
result = num * num
return result
# function call
square = find_square(3)
print('Square:',square)
# Output: Square: 9
Use of pass Statement inside Function or Class
We can do the same thing in an empty
function or class as well.
For example,
def function(args):
Pass
class Example:
pass
Python lambda Function
Declaration
We use the lambda keyword instead
of def to create a lambda function. Here's
the syntax to declare the lambda function
lambda argument(s) : expression
argument(s) - any value passed to the
lambda function
expression - expression is executed and
returned
Example
# declare a lambda function
greet = lambda : print('Hello World')
# call lambda function
greet()
# Output: Hello World
# Define lambda function squares a number
square = lambda x: x ** 2
# Using the lambda function
result = square(5)
print(result) # Output: 25