Functions:
A group of statements into a single logical unit is called as
function.
Functions are used to perform the task.
Function is not called automatically.
Function body is executed whenever we call the function
Function can be called any no. of times.
Syntax:
def function_name():
=============
=============
Advantages of Functions:
1) Modularity
2) Reusability
Example:
def display():
print("Welcome")
display()
display()
display()
Functions are divided into 4 categories:
1) Functions with arguments with return value.
2) Functions with arguments and without return value.
3) Functions without arguments and with return value.
4) Functions without arguments and without return value.
Examples:
def add(a, b):
return a+b
def sub(a, b):
print(a-b)
def mul():
a=5
b=10
return a*b
def div():
a=5
b=10
print(a//b)
Parameters:
The variables that are declared in a function declaration is
called parameters.
Parameters of a function can be accessed with in the same
function only.
Python supports two types of parameters:
1) Non default parameters
2) Default parameters
Non default parameters:
The parameters that are declared without assigning values are
called non default parameters.
At the time of calling a function we should pass values of the
non default parameters of the function.
Example:
def add(a, b):
c=a+b
print(c)
add(10, 20)
add(30, 83)
Default Parameters:
The parameters that are declared by assigning values in a
function declaration are called default parameters.
At the time of calling a function, we need not to pass the values.
Example:
def add(a=10, b=20):
c=a+b
print(c)
add()
add(30)
add(40, 50)
Arguments:
The values that are passed to a function at the time of calling
a function are called arguments.
Types of arguments:
1) Non keyword arguments
2) Keyword arguments
3) Arbitrary arguments
Non keyword Arguments:
The arguments that are passed without assigning to variables
are called non keyword arguments.
Example:
def sub(a, b):
print(a-b)
sub(10, 2)
sub(2, 10)
Keyword arguments:
The arguments that are passed with assigning to variables
are called non keyword arguments.
Example:
def sub(a, b):
print(a-b)
sub(a=10, b=2)
sub(b=2, a=10)
Arbitrary arguments:
The arguments that are prefixed with * are called arbitrary
arguments.
Arbitrary arguments are by default tuple type.
It allows to pass 0 to any number of arguments to a function.
Example:
def add(*a):
for i in a:
print(i)
add()
add(10)
add(33, 45)
add(31, 13, 54)
Lambda Function (or) Anonymous Function:
A function that has no name is known as lambda function or
anonymous function.
Syntax: variable=lambda arguments : expression
Examples:
1) fun=lambda a : a*a*a
b=fun(10)
print(b)
2) add=lambda a, b : print(a+b)
add(10, 20)
Modules:
In python, every file treated as a module.
A module can contains variables, functions, classes,.. etc.,
Python supports two types of import statements to access
modules data.
1) Normal import
2) From import
1) Normal import:
In normal import we can access variables and functions by
using module name
Example:
test.py
a=10
def add(a, b):
print(a+b)
demo.py
import test
print(test.a)
test.add(10, 20)
In normal import, instead of module name we can use any
alias name also
Example:
test.py
a=10
def add(a, b):
print(a+b)
demo.py
import test as t
print(t.a)
t.add(10, 20)
2) From import:
In from import we can access variables and functions directly
Example:
test.py
a=10
def add(a, b):
print(a+b)
demo.py
from test import a, add
print(a)
add(10, 20)
In from import, we can import all variable and functions by
using * symbol
Example:
test.py
a=10
def add(a, b):
print(a+b)
demo.py
from test import *
print(a)
add(10, 20)
Module search path:
By default python interpreter will search in the following
locations:
1) Current directory
2) sys.path
3) pythonpath variable in Environment variables.
Packages:
A package is a folder or directory, which contains modules.
A package can also contain sub packages.
To access modules of package, we use packagename.modulename
A group of statements into a single logical unit is called as
function.
Functions are used to perform the task.
Function is not called automatically.
Function body is executed whenever we call the function
Function can be called any no. of times.
Syntax:
def function_name():
=============
=============
Advantages of Functions:
1) Modularity
2) Reusability
Example:
def display():
print("Welcome")
display()
display()
display()
Functions are divided into 4 categories:
1) Functions with arguments with return value.
2) Functions with arguments and without return value.
3) Functions without arguments and with return value.
4) Functions without arguments and without return value.
Examples:
def add(a, b):
return a+b
def sub(a, b):
print(a-b)
def mul():
a=5
b=10
return a*b
def div():
a=5
b=10
print(a//b)
Parameters:
The variables that are declared in a function declaration is
called parameters.
Parameters of a function can be accessed with in the same
function only.
Python supports two types of parameters:
1) Non default parameters
2) Default parameters
Non default parameters:
The parameters that are declared without assigning values are
called non default parameters.
At the time of calling a function we should pass values of the
non default parameters of the function.
Example:
def add(a, b):
c=a+b
print(c)
add(10, 20)
add(30, 83)
Default Parameters:
The parameters that are declared by assigning values in a
function declaration are called default parameters.
At the time of calling a function, we need not to pass the values.
Example:
def add(a=10, b=20):
c=a+b
print(c)
add()
add(30)
add(40, 50)
Arguments:
The values that are passed to a function at the time of calling
a function are called arguments.
Types of arguments:
1) Non keyword arguments
2) Keyword arguments
3) Arbitrary arguments
Non keyword Arguments:
The arguments that are passed without assigning to variables
are called non keyword arguments.
Example:
def sub(a, b):
print(a-b)
sub(10, 2)
sub(2, 10)
Keyword arguments:
The arguments that are passed with assigning to variables
are called non keyword arguments.
Example:
def sub(a, b):
print(a-b)
sub(a=10, b=2)
sub(b=2, a=10)
def sub(a, b):
print(a-b)
sub(a=10, b=2)
sub(b=2, a=10)
Arbitrary arguments:
The arguments that are prefixed with * are called arbitrary
arguments.
Arbitrary arguments are by default tuple type.
It allows to pass 0 to any number of arguments to a function.
Example:
def add(*a):
for i in a:
print(i)
add()
add(10)
add(33, 45)
add(31, 13, 54)
Lambda Function (or) Anonymous Function:
A function that has no name is known as lambda function or
anonymous function.
Syntax: variable=lambda arguments : expression
Examples:
1) fun=lambda a : a*a*a
b=fun(10)
print(b)
2) add=lambda a, b : print(a+b)
add(10, 20)
Modules:
In python, every file treated as a module.
A module can contains variables, functions, classes,.. etc.,
Python supports two types of import statements to access
modules data.
1) Normal import
2) From import
1) Normal import:
In normal import we can access variables and functions by
using module name
Example:
test.py
a=10
def add(a, b):
print(a+b)
Lambda Function (or) Anonymous Function:
A function that has no name is known as lambda function or
anonymous function.
Syntax: variable=lambda arguments : expression
Examples:
1) fun=lambda a : a*a*a
b=fun(10)
print(b)
2) add=lambda a, b : print(a+b)
add(10, 20)
Modules:
In python, every file treated as a module.
A module can contains variables, functions, classes,.. etc.,
Python supports two types of import statements to access
modules data.
1) Normal import
2) From import
1) Normal import:
In normal import we can access variables and functions by
using module name
Example:
test.py
a=10
def add(a, b):
print(a+b)
demo.py
import test
print(test.a)
test.add(10, 20)
In normal import, instead of module name we can use any
alias name also
import test
print(test.a)
test.add(10, 20)
In normal import, instead of module name we can use any
alias name also
Example:
test.py
a=10
def add(a, b):
print(a+b)
test.py
a=10
def add(a, b):
print(a+b)
demo.py
import test as t
print(t.a)
t.add(10, 20)
import test as t
print(t.a)
t.add(10, 20)
2) From import:
In from import we can access variables and functions directly
Example:
test.py
a=10
def add(a, b):
print(a+b)
In from import we can access variables and functions directly
Example:
test.py
a=10
def add(a, b):
print(a+b)
demo.py
from test import a, add
print(a)
add(10, 20)
In from import, we can import all variable and functions by
using * symbol
from test import a, add
print(a)
add(10, 20)
In from import, we can import all variable and functions by
using * symbol
Example:
test.py
a=10
def add(a, b):
print(a+b)
test.py
a=10
def add(a, b):
print(a+b)
demo.py
from test import *
print(a)
add(10, 20)
Module search path:
By default python interpreter will search in the following
locations:
1) Current directory
2) sys.path
3) pythonpath variable in Environment variables.
Packages:
A package is a folder or directory, which contains modules.
A package can also contain sub packages.
To access modules of package, we use packagename.modulename
from test import *
print(a)
add(10, 20)
Module search path:
By default python interpreter will search in the following
locations:
1) Current directory
2) sys.path
3) pythonpath variable in Environment variables.
Packages:
A package is a folder or directory, which contains modules.
A package can also contain sub packages.
To access modules of package, we use packagename.modulename