You are on page 1of 55

Program :1 Program :2

Write python code to obtain a Write python code to obtain


number as an argument in a three angles of the triangle in
function and display its a function and return YES if
factors. they form a triangle
otherwise return NO.
def factors(x):
def triangle(a,b,c):
print("The factors of",x,"are:")
if a+b+c==180:
for i in range(1, x + 1):
return "yes"
if x % i == 0:
print(i) else:

num = int(input("enter a number ")) return "no"


factors(num) x = int(input("enter a angle 1 "))
y = int(input("enter a angle 1 "))
z = int(input("enter a angle 1 "))
result=triangle(x,y,z)
print(result)
Program :3 Program : 4
Write python code to obtain a Write python code to obtain
string argument in a function to obtain a number in a
and return a reversed string. function and return YES if it
is PERFECT NUMBER
otherwise return NO
def funstr(s):
def check(num):
rs =""
r=0
l=len(s) s=0
while l>=0: m=num
while num!=0:
rs=rs+s[l-1]
r=num%10
l=l-1 s=s+r
return rs num=num//10
if m%s==0:
str = input("enter a strin ")
return "yes"
rev = funstr(str) else:
print("reverse string ",rev) return "no"
n=int(input("enter a number "))
ans = check(n)
print(ans)
Concept of local variables and global variables
Local variable Global variable
It is a variable which It is variable which is
is declared with in a declared outside all
function or with in a the functions.
block. It is accessible
It is accessible only throughout the
within the program in which it
function/block in is declared
which it is declared
def cube(n):
cn=n*n*n*n
return cn In this example :
x=10 X,res – global var.
res = cube(x) Cn , n- local variables.
print(res)
If your function has a local variable with same name
as global variable and you want to modify the global
variable inside function then use 'global' keyword before
the variable name at start of function.
Concept of actual and formal parameters:
Actual parameters Formal parameters
They are the They are the
parameters which are parameters which is
used in function call to used in function
send the values from header of the called
calling function to the function to receive the
called functions. value from the actual
parameters.
def fun1(x,y):
z=x+y
Here
print(z) X,y- formal parameters
a=int(input(“enter num 1”)) A,b – actual parameters
b=int(input(“enter num “))
c=fun1(a,b)
print(c)
Concept of default arguments
A default argument is an argument that assumes a
default value if a value Is not provided in the function
call for that argument.
You can send any data types of argument to a
function (string, number, list, dictionary etc.), and it
will be treated as the same data type inside the
function.
Passing a list to a function as an argument :
Python user define function program for practice

Write a python program to have function which obtain three


integers as an arguments and Return the maximum integer.
def maximum(a, b, c):
if (a >= b) and (a >= c):
largest = a

elif (b >= a) and (b >= c):


largest = b
else:
largest = c

return largest
a = int(input("enter number 1 "))
b = int(input("enter number 2 "))
c = int(input("enter number 3 "))
res = maximum(a,b,c)
print("maximum ",res)
Python user define function program for practice

Write a python program to have function which obtain two


integer arguments and check if first argument is a factorial of
second argument or not.
def fun1(a,b):
f=1
for i in range(1,b+1):
f=f*i
if f==a:
print("yes it is a factorial")
else:
print("no it is not a factorial")

x = int(input("enter number 1 "))


y = int(input("enter number 2 "))
fun1(x,y)
Python user define function program for practice

Write a program to find greater no in two


given number’s using function.
def check(a,b):
if a>b:
return a
else:
return b
a=int(input("Enter Value of a: "))
b=int(input("Enter Value of b: "))
G=check(a,b) Enter Value of a: 12
print(G," is greater “)) Enter Value of b: 21
21 is greater
Python user define function program for practice

Write a program to HAVE A FUNCTION WHICH


OBTAIN A STRING AS AN ARGUMENT AND RETURN
THE TOTAL NUMBER OF CAPITAL VOWELS IN IT.
def fun1(str):
l=len(str)
f=0
for i in range(l):
if str[i]=='A' or str[i]=='E' or str[i]=='I'or str[i]=='O' or str[i]=='U':
f=f+1
return f
s = input("enter a string ")
nv=fun1(s)
print("number of vowels ",nv)
Python user define function program for practice

WRITE A PROGRAM TO HAVE A FUNCTION WHICH OBTAIN A


NUMBER AS A ARGUMENT AND RETURN “YES” IF IT IS A
PALINDROME NUMBER OTHERWISE RETURN “NO”.
def fun1(num):
r=0
s=0
m=num
while num!=0:
r=num%10
s=s*10+r
num//=10
if s==m:
return "yes"
else:
return "no"
n = int(input("enter a number "))
result=fun1(n)
print(result)
FUNCTION OUTPUT BASED QUESTIONS
def fun(a,b=10): #definartion
print(a,",",b)

a,b=4,13
print(a,",",b)
fun(a,b) #calling
fun(a) #calling
fun(b) #calling
fun(b,a)

def FUN(a): #definartion


return a*a,a**3
r1,r2=FUN(5)
print(r1,", ",r2)
FUNCTION OUTPUT BASED QUESTIONS
def switchover(A,N, split):
K=0
while K<N:
if K<split :
A[K]+= K
else:
A[K]*= K
K+=1
def display(A,N):
K=0
while K<N:
if K%2== 0:
print(A[K],end="%")
else:
print(A[K],"\n")
K+=1
H= [30,40,50,20,10,5]
print(H)
switchover(H,6,3)
display(H,6)
print(H)
Python user define function program for practice

WRITE A PROGRAM TO HAVE A FUNCTION WHICH OBTAIN A


NUMBER AS A ARGUMENT AND RETURN 1 IF IT IS PRIME
NUMBER OTHERWISE RETURN -1

def chkprm(n):
c=0
for i in range(1,n+1):
if n%i==0:
c=c+1
if c==2:
return 1
else:
return -1
num = int(input("enter a number "))
res = chkprm(num)
print(res)
FUNCTION OUTPUT BASED QUESTIONS
def s(a, b):
a+=2
b*=5
print(a,":",b,"\n")
a,b=4,8
print(a,":",b,"\n")
s(a,b)
print(a,":",b,"\n")

def s(a,b):
a+=2
b[0]=b[0]+5
print(a,":",b,"\n")

a,b=4,[8]
print(a,":",b,"\n")
s(a,b)
FUNCTION OUTPUT BASED QUESTIONS
def s(a, b=7):
a+=2
b*=5
print(a,":",b,"\n")
a,b=4,8
print(a,":",b,"\n")
s(a,b)
print(a,":",b,"\n")
s(a)
print(a,":",b,"\n")

def s(a=6, b=7):


a+=2
b*=5
print(a,":",b)
a,b=4,8
print(a,":",b)
s(a,b)
print(a,":",b)
s(a)
print(a,":",b)
s()
print(a,":",b)
FUNCTION OUTPUT BASED QUESTIONS
def func(x,y):
if x[0]%y[0]==0 :
return x[0]+2
else:
return y[0]-1
p,q=[20],[23];
print(p,",",q)
q=func(p,q)
print(p,",",q)

def execute(x,y=15):
temp=x+y
x+=temp
if(y<=20):
print(temp,",",x,",",y)
a,b =50,20
execute(b)
print(a,",",b)
execute(b)
print(a,",",b)
FUNCTION OUTPUT BASED QUESTIONS
def func(x,y=10):
if x%y==0 :
return x+2
else:
return y-1
p,q=20,23;
print(p,",",q)
q=func(p,q)
print(p,",",q)

def func(x,y=10):
if x%y==0 :
return x+2
else:
return y-1
p,q=20,23;
print(p,",",q)
q=func(p,q)
print(p,",",q)
p=func(q)
print(p,",",q))
PREDICT THE FLOW OF CONTROL/EXECUTION AND ALSO FIND THE OUTPUT

Flow of execution
1-5-9-10-5-6-1-2-3-6-7-10-1-2-3-10-11
Output:
52

Flow of execution
1-5-6-7-1-2-8
Output:
3
4
Python
EasyEasyEasy

SCHOOLbbbbCOM
FUNCTION OUTPUT BASED QUESTIONS
a=10
y=5
def myfun(a):
y=a
a=2
print(y,"@",a)
print(a+y)
return a+y
print(y,"@",a)
print(myfun(a))
print(y,"@",a)

a=10
y=5
def myfun(a):
global y
y=a
a=2
print(y,"@",a)
print(a+y)
return a+y
print(y,"@",a)
print(myfun(a))
print(y,"@",a)
What is Recursion?
The process in which a function calls itself directly or indirectly is
called recursion and the corresponding function is called as
recursive function. Using recursive algorithm, certain problems can
be solved quite easily.

Ques- Function to find out the factorial of a number using recursion


def recur_factorial(n):
if n == 0:
return 1

return n*recur_factorial(n-1)

num = int(input("Enter a number: "))


print("The factorial
of",num,"is",recur_factorial(num))
Ques- Function to find out the sum of natural numbers upto a certain
Number using recursion
def funsum(n):
if n <=1:
return n

return n+funsum(n-1)

num = int(input("Enter a number: "))


print("The sum is ",funsum(num))

Find the output


def func1(a,b):
if a>1:
if a%b==0:
print(b,end=" ")
func1(int(a/b),b) Also find the output if a=84 , b=2
else: Then
func1(a,b+1) Output is
a=24 2237
b=2
func1(a,b)
Ques- Function to find out the sum of natural numbers upto a certain
Number using recursion

def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))

num = int(input("netr number of terms "))


if num<= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(num):
print(recur_fibo(i))
Python user define function program for practice
WRITE A PROGRAM TO HAVE A FUNCTION HEIGHT() WHICH
OBTAIN HEIGHT OF TWO STUDENTS IN FEET AND INCHES
RESPECTIVELY AND THEN RETURN THEIR TOTAL HEIGHT ALSO
IN FEET AND INCHES.
def height(f1,i1,f2,i2):
f3=(f1+f2)+(i1+i2)//12
i3=(i1+i2)%12
return f3,i3
ft1=int(input("enter height of student 1 in feet "))
ic1=int(input("enter height of student 1 in inches "))
ft2=int(input("enter height of student 2 in feet "))
ic2=int(input("enter height of student 2 in inches "))
ft3,ic3=height(ft1,ic1,ft2,ic2)
print("total feets ",ft3)
print("total inches ",ic3)
FUNCTION OUTPUT BASED QUESTIONS
def f1(a,b):
a+=b
b*=a
print(a,b)
x,b=5,10
f1(x,b)
print(x,b)

def f5(s):
s1=''
for i in range(len(s)):
if s[i]==s[-1-i]:
s1+=s[i]
else:
s1=s[i]+s1
print(s1)
f5('abcba')
f5('abcaba')
def f4(s): FUNCTION OUTPUT BASED QUESTIONS
for ch in s:
if ch in 'aeiou':
print('*',end='')
elif ch in ['AEIOU']:
print('#',end='')
elif ch.isdigit():
print(s[0],end='')
else:
print(s[-1],end='')
f4('India91')
print()
f4('KUwait965')

def f2(x,y):
global b
x+=b
b*=y
y+=x+y
print(x,y,b)
x,b=5,10
f2(x,b)
print(x,b)

You might also like