User defined
Functions
def fun( ):
print(“Hello Aditi”)
fun( ) Output
Hello
Aditi
function
Argument
def fun( ): function
definition
print(“Hello Aditi”)
fun( ) function Calling
Order of execution
2 def fun( ):
3 print(“Hello Aditi”)
1 fun( )
def fun( ):
print(“Hello Aditi”)
fun( )
fun( ) Output
Hello Aditi
Hello Aditi
Order of execution
2 5 def fun( ):
3 6 print(“Hello Aditi”)
1 fun( )
4 fun( ) Output
Hello Aditi
Hello Aditi
Order of Execution
2. def fun1( ):
3. print("Hello ")
5. def fun2( ):
6. print("Aditi ")
1. fun1( ) Output
4. fun2( ) Hello
Aditi
Order of Execution
5. def fun1( ):
6. print("Hello ")
2. def fun2( ):
3. print("Aditi ")
1. fun2( ) Output
4. fun1( ) Aditi
Hello
Order of Execution
5. def fun2( ):
6. print("Hello ")
2. def fun1( ):
3. print("Aditi ")
1. fun1( ) Output
4. fun2( ) Aditi
Hello
def fun2( ):
print(“Bhalu ")
print("Ramu ")
def fun1( ):
print("Aditi ")
fun2( )
print("Ritu ")
fun1( )
print("Raju ")
fun1()
print("Kalu ")
Order of Execution
5 14 def fun2( ):
6 15 print(“Bhalu ")
7 16 print("Ramu ")
2 11 def fun1( ):
3 12 print("Aditi ")
4 13 fun2( )
8 17 print("Ritu ")
1 fun1( )
9 print("Raju ")
10 fun1()
18 print("Kalu ")
def fun():
print("Shambhavi")
def fun1():
print("Jha")
OUTPUT
Shambhavi
fun()
def fun():
print("Shambhavi")
def fun1():
print("Jha")
OUTPUT
Shambhavi
Error
fun()
fun1()
def fun():
print("Shambhavi")
fun1()
def fun1():
print("Jha")
OUTPUT
Shambhavi
Error
fun()
def fun():
print("Shambhavi")
def fun1():
print("Jha")
fun1() OUTPUT
Shambhavi
Jha
fun()
Passing Parameters to function
def fun1(a):
print(a)
fun1(10) Output
10
Passing Parameters to function
def fun1(a):
print(a)
b=10
fun1(b) Output
10
Passing Parameters to function
def fun1(a):
print(a)
b=int(input(“Enter a number”))
fun1(b) Output
depends on input
Passing Parameters to function
def fun1(a):
print(a)
b=input(“Enter name”)
fun1(b) Output
depends on input
Passing Parameters to function
def fun1(a):
print(a)
b=input(“Enter name”)
fun1(b) Output
depends on input
Passing Parameters to function
def fun1(a):
print(“ Hello ”, a)
b=input(“Enter name”)
fun1(b) Output
depends on input
Passing Parameters to function
def fun1(d,e):
print(d+e)
b=int(input("Enter a number "))
c=int(input("Enter another number
"))
fun1(b,c)
Passing Parameters to function
def fun2(g):
print(g)
def fun1(d,e):
f=d+e
fun2(f)
b=int(input("Enter a number "))
c=int(input("Enter another number
"))
fun1(b,c)
def b(j):
print("sum is ",j)
def a(f,g):
h=f+g
b(h)
def k():
c=int(input("enter number "))
d=int(input("enter number "))
a(c,d)
k()
Returning values by the function
def fun2(g):
h=g*g
return h
b=int(input("Enter a number "))
c=fun2(b)
print(c)
Returning values by the function
def fun2():
b=int(input("Enter a number "))
h=b*b
return h
c=fun2()
print(c)
Returning values by the function
def fun2():
b=int(input("Enter a number "))
return b*b
c=fun2()
print(c)
Returning Integer
def b(c,d):
if c>d:
return c
else:
return d
a=b(10,20)
print(a)
Returning String
def b(c,d):
if c>d:
return "debanshi"
else:
return 'sampurna'
a=b(10,20)
print(a)
def y(z):
print("smahi")
for x in range(1,5):
y(x)
def y(a,b):
print(a,b,end=' ')
z=input("your name please ")
for x in range(1,5):
y(x,z)
def y(a):
for b in range(1,11):
print(a,'x',b,'=',a*b)
z=int(input("enter number "))
y(z)
def y(m):
return m*m
x=int(input("enter number "))
print("square of ",x,"is",y(x))
def y(m):
return m*m
def z(f):
return f*f*f
x=int(input("enter number "))
print("square is",y(x),"cube
is",z(x))
def z(a,b):
c=a+b-b*b+14-a-29
return c
x=10
y=20
if z(x,y)>12:
print ("RITU")
else:
print("ADITI")
def z(a,b):
e=a+b-24
f=24+a-b
return e,f
x=10
y=20
c,d=z(x,y)
print(c,d)
def z(a,b):
e=a+b-24
f=24+a-b
return e,f
x=10
y=20
c=z(x,y)
print(c)
def z(a,b):
e=a+b-24
f=24+a-b
return e,f
x=10
y=20
c=z(x,y)
print(c[0]+c[1])
def z(a,b):
e=a+b
f=a-b
g=a*b
h=a/b
i=a//b
j=a%b
return e,f,g,h,i,j
x=10
y=20
c=z(x,y)
for k in c:
print (k)
def z(a+3,b):
print (a*23+b-
32/a+b)
x=20
y=10
z(x,y)
def z(a,'b'):
print ("hello",b,a)
x=20
y='aditi'
z(x,y)
def x(y,z):
print(y+z)
x(10,20,30)
def x(y,z,a,b):
print(y+z+a*b)
x(10,20,30)
def x(y,z,a,b):
a=4
b=3
print(y+z+a*b)
x(10,20,30)
def x(y="RITU"):
print("hello ",y)
z="ADITI"
x()
x(z)
def x(y="RITU"):
print("hello "+y)
z="ADITI"
x()
x(z)
def x(y=100):
print("hello "+y)
z=50
x()
x(z)
def x(y=100):
print("hello ",y)
z=50
x()
x(z)
def x(y="RITU"):
print(y*5)
z="ADITI"
x()
x(z)
def x(a,y="RITU "):
print(y*a)
z="ADITI "
x(4)
x(3,z)
def x(a,r):
print("hello
",a,"and",r)
a="ritu"
r="aditi"
x(a,r)
def x(a,r):
print("hello
",a,"and",r)
a="ritu"
r="aditi"
x(r,a)
def x(a,r):
print("hello
",a,"and",r)
x(r="Aditi",a="ritu")
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
s="sampurna"
x(s)
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
s="sampurna"
x(s,a="akanksha")
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
s="sampurna"
x(s,a="akanksha",r="tanvi")
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
s="sampurna"
x(s,r="akanksha",a="tanvi")
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
s="sampurna"
x(r="akanksha",s,a="tanvi")
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
s="sampurna"
x(r="akanksha",a="tanvi",s)
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
s="sampurna"
x(s,r="akanksha")
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
t="sampurna"
x(t,r="akanksha")
def
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)
x(r="akanksha",s="sampurna
")
def x(*s):
print (s)
x(5)
def x(*s):
print (s)
x(5,6)
def x(*s):
print (s)
x()
def x(*s):
for y in s:
print (y)
x(5)
def x(*s):
for y in s:
print (y)
x(5,8)
def x(*s):
for y in s:
print (y)
x(5,10,15)
def x(*s):
for y in s:
print (y)
x(‘amit’, ‘sumit’)
def x(*s):
for y in s:
print (y)
x(‘amit’, 45)
def x(*s):
for y in s:
print (y)
x([‘amit’, 45],[56,
‘sumit’])
def x(*s):
for y in s:
print (y)
x((‘amit’, 45),(56,
‘sumit’))
def x(*s):
for y in s:
print (y)
x((‘amit’, 45),[56,
‘sumit’])
def x(*s):
for y in s:
print (y)
x((‘amit’, 45),56, ‘sumit’)
def x(*s):
print (s)
x((‘amit’, 45),56, ‘sumit’)
def x(*s):
print (s[0])
x((‘amit’, 45),56, ‘sumit’)
def x(*s):
print (s[0],s[1])
x((‘amit’, 45),56, ‘sumit’)
def x(*s):
How to print amit
x((‘amit’, 45),56, ‘sumit’)
def x(*s):
print(s[0][0])
x((‘amit’, 45),56, ‘sumit’)
GLOBAL
Vs
LOCAL
x=1
def y():
print(x)
x=1
def y():
print(x)
y()
x=1
def y():
print(x)
y()
print(x)
def y():
x=1
print(x)
y()
print(x)
def y():
x=1
print(x)
y()
print(x) ERROR
def f1():
x=15
print(x) 15
x=12
f1()
def f1():
x=100
print(x) 100
x=+1
f1()
def san(x):
print(x+1) 13
x=-2
x=4
san(12)
x=1 GLOBAL
def y():
print(x)
z=2 LOCAL
y()
print(z) ERROR
x=1
def y():
z=1
print(x) OKAY
print(z) OKAY
y()
print(x) OKAY
print(z) ERROR
x=1 GLOBAL
def y():
x=2 LOCAL
print(x)
y() OUTPUT
print(x) 2
x=3 1
y() 2
print(x) 3
x=1 GLOBAL
def y():
x=2 LOCAL
print(x)
x=4 LOCAL
y() OUTPUT
print(x) 2
x=3 1
y() 2
print(x) 3
x=1
def y():
print(x)
x=4 Error trying to
change global x
y()
print(x)
x=3
y()
print(x)
x=1
def y():
x=4 OK creating local
x
print(x)
y()
print(x)
x=3
y()
print(x)
x=1
def y():
x=x+1 ERROR due to x
after =
print(x)
y()
print(x)
x=3
y()
print(x)
x=12
def f1(a,b=x):
print(a,b) 4
12
x=15
f1(4)
def f1(a,b=x):
print(a,b)
x=15
f1(4) Error
def f1():
global x
x+=1
print(x)
x=12
print(“x”)
def f1():
global x
x+=1
print(x)
x=12
f1()
print(“x”)
def f1():
global x
x+=1
print(x)
x=12
f1()
print(x)
def f1(x):
global x ERROR
x+=1
print(x)
f1(15)
print("hello")
def f1(x):
ERROR
global x
x+=1
print(x)
x=10
f1(15)
print("hello")
def f1(z):
OKAY
global x
x+=1
print(x) 11
x=10 hello
f1(15)
print("hello")
def f1(z):
global x
x+=1+z
print(x) 26
x=10 hello
f1(15)
print("hello")
def f():
global a
print(a) world
a = "hello"
print(a) hello
a = "world"
f()
print(a) hello
def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)
def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)
print(p,q,r,s)
Passing
LIST/TUPLE
def x(a):
print(a)
b=[10,20,30,40]
x(b)
def x(a):
print(a)
b=(10,20,30,40)
x(b)
def x(a):
for c in a:
print(c)
b=[10,20,30,40]
x(b)
def x(a):
d=0
for c in a:
d=d+c
print(c)
b=[10,20,30,40]
x(b)
def x(a):
d=0
for c in a:
d=d+c
print(d)
b=[10,20,30,40]
x(b)
def x(a):
d=0
for c in a:
d=d+c
print(d+c)
b=[10,20,30,40]
x(b)
Pass
By
VALUE
def x(y):
print(y)
y=30
print(y)
y=20
x(y)
print(y)
Pass By
REFERENCE
def x(a):
print(a)
a[0]=a[1]+a[2]
a[1]=a[0]+a[2]
a[2]=a[0]+a[1]
print(a)
PASS BY
REFERENCE
b=[10,20,30]
print(b)
x(b)
print (b)
def x(a):
print(a)
a[0]=a[1]+a[2] ERROR
a[1]=a[0]+a[2]
a[2]=a[0]+a[1]
print(a)
b=(10,20,30)
print(b)
x(b)
print (b)
x="Ritu and Aditi is the best
friend"
y=x.split()
print (y)
x="Ritu and Aditi is the best
friend"
y=x.split(" ")
print (y)
x="Ritu,and,Aditi,is,the,best,friend"
y=x.split(",")
print (y)
x="Ritu@and@Aditi@is@the@best@frie
nd"
y=x.split("@")
print (y)
x="Ritu@and@Aditi@is@the@best@frie
nd"
y=x.split("#")
print (y)
x="Ritu and aditi is the best friend"
y=x.split("a")
print (y)
def x(y):
z.append(45)
print(z)
z=input("enter comma separated numbers ")
print(z)
z=z.split(",")
print(z)
for t in range(len(z)):
z[t]=int(z[t])
print(z)
x(z)
print (z)
def x(y):
for z in range(len(y)):
if z%2==0:
y[z]+=2
else:
y[z]-=1
print (y)
a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2==0:
y[z]+=2
else:
y[z]-=1
print (y)
a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y[z]=y[z+1]
else:
y[z]=y[z]+1
print (y)
a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y.append(23)
else:
y.clear()
print (y)
a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y.append(22)
else:
y.clear()
print (y)
a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y.append(22)
print (y, len(a))
a=[9,8,3,5,3,7,2,5,5,6]
print(a,len(a))
x(a)
print(a,len(a))
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y.append(23)
print (y)
a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
x=3
for y in range(x):
print(y)
x=x+1
x=3
for y in range(x):
print(y)
x=x+1
print(x)
x=3
for y in range(x):
print(y)
x=x+1
print(x)
print(x)
x=3
for y in range(x):
print(y)
x=x+1
print(x)
print(x)
print(y)
x=3
for y in range(x):
print(y)
x=x+1
print(x)
y=100
print(x)
print(y)
x=3
for y in range(x):
print(y)
x=x+1
print(x)
y=y+1
print(x)
print(y)
def x():
print("Debanshi ")
x()
x()
def x():
print("Debanshi ")
x()
print("SAMPURNA ")
x()
def x():
print("Debanshi ")
y()
def y():
print("Smahi ")
x()
print("SAMPURNA ")
x()
def x(z):
if z==10:
print("Ritu ")
else:
x(z-1)
y=10
x(y)
def x(z):
if z==1:
return z
else:
x(z-1)
return z
y=4
print(x(y))
def x(z):
if z==1:
return z
else:
x(z-1)
return z
y=4
x(y)
def x(z):
if z==1:
return z
else:
print(x(z-1))
return z
y=4
print(x(y))

7 Python udf.pptx

Editor's Notes

  • #40 It is a two star 🌟🌟