0% found this document useful (0 votes)
6 views

Functions and Module

Uploaded by

chiku.myspot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Functions and Module

Uploaded by

chiku.myspot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Python Programming

Kawaljit Kaur
Function

• 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.


Creating and Calling a Function
In Python a function is defined using def numberReverse(n):
the def keyword:
rev = 0
def myFunction(): while n != 0:
r = n % 10
print("This is my first function")
rev = rev * 10 + r
n = n//10
myFunction()# FUNCTION CALL
return rev
rev = numberReverse(12345) # FUNCTION CALL
print("Reverse is:",rev)
Function Arguments
• Information can be passed into
functions as arguments. def myFunction(name):
• Arguments are specified after the
print("My Name is:", name)
function name, inside the
parentheses. You can add as many myFunction("Deepak ")# Function Call
arguments as you want, just separate myFunction("Rahul Kumar")# Function Call
them with a comma.
• By default, a function must be called Here Argument is name.
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, *args
• If you do not know how many def myFunction(*name):
arguments that will be passed l = len(name)
into your function, add for i in range(l):
a * before the parameter name print(name[i])
in the function definition.
• This way the function will myFunction("Ram","Sham")
receive a tuple of arguments, myFunction("Ram","Sham","Mohan")
and can access the items
accordingly. myFunction("Ram","Sham","Mohan","Manoj")

Note: Here myFunction() is called with arbitrary


number of arguments.
Keyword Arguments
• You can also send arguments def myFunction(fname,lname):
with the key = value syntax. print("Fname:",fname)
• This way the order of the print("Lname:",lname)
arguments does not matter.

myFunction(lname=“Kumar",fname="Deepak")

Note: Here myFunction() is called by passing


lname argument at first place even through it is the
second argument in Function definition.
Arbitrary Keyword Arguments, **kwargs

• If you do not know how many def myFunction(**name):


keyword arguments that will for i in name:
be passed into your function, print(name[i])
add two asterisk: ** before the
parameter name in the function
definition.
myFunction(lname="Vats",fname="Deepak")
• This way the function will
receive a dictionary of myFunction(lname="Vats",fname="Deepak",UID = "191001")
arguments, and can access the
items accordingly. Note: Here myFunction() is called by passing
arbitrary number of keyword arguments.
Default Parameter

• The following example shows def myFunction(name = "Deepak"):


how to use a default parameter print(“My Name is “ + name)
value.
• If we call the function without
argument, it uses the default myFunction()
value myFunction(“Rahul")

OUTPUT:
My Name is Deepak
My Name is Rahul
Call by Value

• When the object you are def myFunction(name1):


passing to the function is name1 = "Hello India"
immutable than it means we print("Inside Function:",name1)
are passing arguments using
Call by value.
name = "Deepak Kumar"
myFunction(name)
print("Outside Function:",name)

OUTPUT:
Inside Function: Hello India
Outside Function: Deepak Kumar
Call by Reference

• When the object you are def myFunction(myList1):


passing to the function is myList1.append(1004)
mutable than it means we are print("Inside Function myList is:",myList1)
passing arguments using Call
by reference.
myList = [1001, 1002, 1003]
myFunction(myList)
print("Outside Function myList is:",myList)

OUTPUT:
Inside Function myList is: [1001, 1002, 1003, 1004]
Outside Function myList is: [1001, 1002, 1003, 1004]
Module in Python

• Consider a module to be the same as a code library.

• A file containing a set of functions you want to include in your application.


How to create module?

mymodule.py
• To create a module just
def myFunction(name):
save the code you want in
print("Hello, " + name)
a file with the file
extension .py module_demo.py
import mymodule
mymodule.myFunction("Deepak")
How to add variables in module?
mymodule.py
def myFunction(name):
• Here Student dictionary print("Hello, " + name)
student = {
is added to the existing
"name": "RAM",
module and then one "UID": 1001,
element of the dictionary "country": "INDIA"
}
is accessed.
module_demo.py
import mymodule
mymodule.myFunction("Deepak")
a = mymodule.student["country"]
print("country:",a)

You might also like