0% found this document useful (0 votes)
11 views6 pages

Basic Programs on Functions

Uploaded by

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

Basic Programs on Functions

Uploaded by

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

12/10/23, 10:23 PM Basic Programs on Functions - Jupyter Notebook

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


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

5#8#5#4#

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


2 X+=15
3 print("Value of X :", X)
4 X=20
5 Update()
6 print("Value of X :", X)

Value of X : 25
Value of X : 20

In [3]: 1 S="WELCOME"
2 def Change(T):
3 T="HELLO"
4 print(T,end="@")
5 Change(S)
6 print(S)

HELLO@WELCOME

In [5]: 1 def FunStr(S):


2 T=""
3 for i in S:
4 if i.isdigit():
5 T=T+i
6 return T
7 X="PYTHON 3.9"
8 Y=FunStr(X)
9 print(X,Y,sep="*")

PYTHON 3.9*39

In [7]: 1 V=50
2 def Change(N):
3 global V
4 V,N = N,V
5 print(V,N,sep="#",end="@")
6 Change(20)
7 print(V)

20#50@20

localhost:8888/notebooks/Basic Programs on Functions.ipynb 1/6


12/10/23, 10:23 PM Basic Programs on Functions - Jupyter Notebook

In [8]: 1 def ListChange():


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

20#36#27#100#

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

105#6#

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


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

Python
EasyEasyEasy

In [11]: 1 def fun(s):


2 k=len(s)
3 m=""
4 for i in range(0,k):
5 if(s[i].isupper()):
6 m=m+s[i].lower()
7 elif s[i].isalpha():
8 m=m+s[i].upper()
9 else:
10 m=m+"bb"
11 print(m)
12 fun("school2@com")

SCHOOLbbbbCOM

localhost:8888/notebooks/Basic Programs on Functions.ipynb 2/6


12/10/23, 10:23 PM Basic Programs on Functions - Jupyter Notebook

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


2 n1=n1+n2
3 n2+=1
4 print(n1,n2)
5 check()
6 check(2,1)
7 check(3)

3 3
3 2
5 3

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


2 P=P+Q
3 Q=P-Q
4 print(P,"#",Q)
5 return(P)
6 R=150
7 S=100
8 R=Change(R,S)
9 print(R,"#",S)
10 S=Change(S)

250 # 150
250 # 100
130 # 100

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


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

22 # 40 # 9 # 13 #

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


2 print(x+y+z)
3
4 def prod(x,y,z):
5 return x*y*z
6 ​
7 a=addEm(6,16,26)
8 b=prod(2,3,6)
9 print(a,b)

48
None 36

localhost:8888/notebooks/Basic Programs on Functions.ipynb 3/6


12/10/23, 10:23 PM Basic Programs on Functions - Jupyter Notebook

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


2 P=P+Q
3 Q=P-Q
4 print(P,"@",Q)
5 return P
6 R=200
7 S=100
8 R=Call(R,S)
9 print(R,"@",S)
10 S=Call(S)
11 print(R,"@",S)

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

In [3]: 1 num = 1
2 def myfunc():
3 return num
4 print(num)
5 print(myfunc())
6 print(num)

1
1
1

In [4]: 1 num = 1
2 def myfunc():
3 num = 10
4 return num
5 print(num)
6 print(myfunc())
7 print(num)

1
10
1

In [5]: 1 num = 1
2 def myfunc():
3 global num
4 num = 10
5 return num
6 print(num)
7 print(myfunc())
8 print(num)

1
10
10

localhost:8888/notebooks/Basic Programs on Functions.ipynb 4/6


12/10/23, 10:23 PM Basic Programs on Functions - Jupyter Notebook

In [1]: 1 a=20
2 y=5
3 def myfunc():
4 global a
5 y=a
6 a=2
7 print("y=",y,"a=",a)
8 print("a+y=", a+y)
9 return a+y
10 print("y=",y,"a=",a)
11 print(myfunc())
12 print("y=",y,"a=",a)

y= 5 a= 20
y= 20 a= 2
a+y= 22
22
y= 5 a= 2

In [7]: 1 def display():


2 print("Hello",end =" ")
3 display()
4 print("there!")

Hello there!

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


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

5 times 5 = 25

In [11]: 1 a=10
2 def call():
3 global a
4 a=15
5 b=20
6 print(a)
7 call()

15

In [12]: 1 def increment(n):


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

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

localhost:8888/notebooks/Basic Programs on Functions.ipynb 5/6


12/10/23, 10:23 PM Basic Programs on Functions - Jupyter Notebook

In [13]: 1 def increment(n):


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

[23, 35, 47, [49]]


23 35 47 [49]
True

In [14]: 1 V=25
2 def Fun(Ch):
3 V=50
4 print(V,end=Ch)
5 V*=2
6 print(V,end=Ch)
7 print(V,end="*")
8 Fun("!")
9 print(V)

25*50!100!25

In [ ]: 1 ​

localhost:8888/notebooks/Basic Programs on Functions.ipynb 6/6

You might also like