Functions and Module
Functions and Module
Kawaljit Kaur
Function
myFunction(lname=“Kumar",fname="Deepak")
OUTPUT:
My Name is Deepak
My Name is Rahul
Call by Value
OUTPUT:
Inside Function: Hello India
Outside Function: Deepak Kumar
Call by Reference
OUTPUT:
Inside Function myList is: [1001, 1002, 1003, 1004]
Outside Function myList is: [1001, 1002, 1003, 1004]
Module in Python
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)