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

Functions - Execution Programs

The document contains a series of Python function examples demonstrating various programming concepts such as variable scope, global variables, and function arguments. Each example includes code snippets along with their outputs, showcasing how functions manipulate data and return results. The document serves as a practical guide for understanding function execution in Python.

Uploaded by

abhinavkhare285
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Functions - Execution Programs

The document contains a series of Python function examples demonstrating various programming concepts such as variable scope, global variables, and function arguments. Each example includes code snippets along with their outputs, showcasing how functions manipulate data and return results. The document serves as a practical guide for understanding function execution in Python.

Uploaded by

abhinavkhare285
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11/8/23, 6:41 AM Functions - Execution programs

In [1]: def ChangeVal(M,N):


for i in range(N):
if M[i]%5==0:
M[i]//=5
if M[i]%3==0:
M[i]//=3
L=[25,8,75,12]
ChangeVal(L,4)
for i in L:
print(i,end="#")

5#8#5#4#

In [2]: def Update(X=10):


X+=15
print("Value of X :", X)
X=20
Update()
print("Value of X :", X)

Value of X : 25
Value of X : 20

In [3]: S="WELCOME"
def Change(T):
T="HELLO"
print(T,end="@")
Change(S)
print(S)

HELLO@WELCOME

In [5]: def FunStr(S):


T=""
for i in S:
if i.isdigit():
T=T+i
return T
X="PYTHON 3.9"
Y=FunStr(X)
print(X,Y,sep="*")

PYTHON 3.9*39

In [7]: V=50
def Change(N):
global V
V,N = N,V
print(V,N,sep="#",end="@")
Change(20)
print(V)

20#50@20

localhost:8888/nbconvert/html/Functions - Execution programs.ipynb?download=false 1/6


11/8/23, 6:41 AM Functions - Execution programs

In [8]: def ListChange():


for i in range(len(L)):
if L[i]%2==0:
L[i]=L[i]*2
if L[i]%3==0:
L[i]=L[i]*3
else:
L[i]=L[i]*5
L=[2,6,9,10]
ListChange()
for i in L:
print(i,end="#")

20#36#27#100#

In [9]: p=5
def sum(q,r=2):
global p
p=r+q**2
print(p,end="#")
a=10
b=5
sum(a,b)
sum(r=5,q=1)

105#6#

In [10]: def func(message,num=1):


print(message*num)
func("Python")
func("Easy",3)

Python
EasyEasyEasy

In [11]: def fun(s):


k=len(s)
m=""
for i in range(0,k):
if(s[i].isupper()):
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
else:
m=m+"bb"
print(m)
fun("school2@com")

SCHOOLbbbbCOM

localhost:8888/nbconvert/html/Functions - Execution programs.ipynb?download=false 2/6


11/8/23, 6:41 AM Functions - Execution programs

In [12]: def check(n1=1,n2=2):


n1=n1+n2
n2+=1
print(n1,n2)
check()
check(2,1)
check(3)

3 3
3 2
5 3

In [13]: def Change(P,Q=30):


P=P+Q
Q=P-Q
print(P,"#",Q)
return(P)
R=150
S=100
R=Change(R,S)
print(R,"#",S)
S=Change(S)

250 # 150
250 # 100
130 # 100

In [15]: def Diff(N1,N2):


if N1>N2:
return N1 - N2
else:
return N2 - N1
NUM=[10,23,14,54,32]
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),"#",end=" ")

22 # 40 # 9 # 13 #

In [1]: def addEm(x,y,z):


print(x+y+z)

def prod(x,y,z):
return x*y*z

a=addEm(6,16,26)
b=prod(2,3,6)
print(a,b)

48
None 36

localhost:8888/nbconvert/html/Functions - Execution programs.ipynb?download=false 3/6


11/8/23, 6:41 AM Functions - Execution programs

In [2]: def Call(P=40,Q=20):


P=P+Q
Q=P-Q
print(P,"@",Q)
return P
R=200
S=100
R=Call(R,S)
print(R,"@",S)
S=Call(S)
print(R,"@",S)

300 @ 200
300 @ 100
120 @ 100
300 @ 120

In [3]: num = 1
def myfunc():
return num
print(num)
print(myfunc())
print(num)

1
1
1

In [4]: num = 1
def myfunc():
num = 10
return num
print(num)
print(myfunc())
print(num)

1
10
1

In [5]: num = 1
def myfunc():
global num
num = 10
return num
print(num)
print(myfunc())
print(num)

1
10
10

localhost:8888/nbconvert/html/Functions - Execution programs.ipynb?download=false 4/6


11/8/23, 6:41 AM Functions - Execution programs

In [6]: a=20
y=5
def myfunc():
y=a
a=2
print("y=",y,"a=",a)
print("a+y=", a+y)
return a+y
print("y=",y,"a=",a)
print(myfunc())
print("y=",y,"a=",a)

y= 5 a= 20

---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-6-108564c7289c> in <module>
8 return a+y
9 print("y=",y,"a=",a)
---> 10 print(myfunc())
11 print("y=",y,"a=",a)

<ipython-input-6-108564c7289c> in myfunc()
2 y=5
3 def myfunc():
----> 4 y=a
5 a=2
6 print("y=",y,"a=",a)

UnboundLocalError: local variable 'a' referenced before assignment

In [7]: def display():


print("Hello",end =" ")
display()
print("there!")

Hello there!

In [9]: def multiply(number1,number2):


answer=number1*number2
print(number1,"times",number2,"=", answer)
return (answer)
output=multiply(5,5)

5 times 5 = 25

In [11]: a=10
def call():
global a
a=15
b=20
print(a)
call()

15

localhost:8888/nbconvert/html/Functions - Execution programs.ipynb?download=false 5/6


11/8/23, 6:41 AM Functions - Execution programs

In [12]: def increment(n):


n.append([4])
return n
L=[1,2,3,4]
M=increment(L)
print(L,M)

[1, 2, 3, 4, [4]] [1, 2, 3, 4, [4]]

In [13]: def increment(n):


n.append([49])
return n[0] , n[1] , n[2] , n[3]
L=[23,35,47]
m1,m2,m3,m4=increment(L)
print(L)
print(m1,m2,m3,m4)
print(L[3]==m4)

[23, 35, 47, [49]]


23 35 47 [49]
True

In [14]: V=25
def Fun(Ch):
V=50
print(V,end=Ch)
V*=2
print(V,end=Ch)
print(V,end="*")
Fun("!")
print(V)

25*50!100!25

In [ ]:

localhost:8888/nbconvert/html/Functions - Execution programs.ipynb?download=false 6/6

You might also like