python recursion practice class 12

 python recursion notes class 12

python recursion - python tutorial 2020

Recursion chapter Revision Notes

Recursion in Python complete Chapter

Recursion:-

IT is a method function calling where a function can be called ( INVOKE) where a function can be called within its definition.

Termination condition:

A recursive function has to fulfil an important condition to be used in a program: it has to terminate. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can end up in an infinite loop, if the base case is not met in the calls.

Example:

4! = 4 * 3!
3! = 3 * 2!
Replacing the calculated values gives us the following expression
4! = 4 * 3 * 2 * 1
In other words, recursion in computer science is a method where the solution to a problem is based on solving smaller instances of the same problem.

important question:

Q1. Write a programme using Recursion to calculate factorial of a number

Ans.   def fact(n):
                  if n<2:
                      return1
                  else:
                      return(n*fact(n-1))
          print(fact(5))
          output
          120

Q2.  using recursion to calculate sum of n numbers.

ans.   def   sum(n):
                    if n<1:
                           return 0
          else:
               return(n + sum(n-1))
           print(sum(5))

Q3. write a programme using recursion to calculate and display Fibonacci series.

ans.  def fibonacci(x):
                 if x==o:
                    return 0
                  elif x==1:
                          return   1
                  else:
                      fibonacci(x-1)  +   fibonacci(x-2):
          print(fibonacci(10))

Q4. recursive function to print string in reverse order.

ans.  def  reverse(s):
                 if len(s)==1:
                      return s[0]
                 else:
                     return(s[-1] + reverse(s[0:len(s)-1))
        print(reverse(“abc”))
       output
       cba

Q5. recursive programme to find power of one number  raised to another.

ans.  def   power(x,y):
                  f=1
                  for x in range (1,Y+1):
                          f=f*x
                   return f
          a=5
          b=3
          c=power(a,b)
          print(“power of “,a,”raised to “,b,”=”,c)

Q6. programme to count no. of T in file story.txt .

ans.  f=open(“story.txt”,”r”)
          count=0
          for line in f:
                   l=len(line)
                   i=0
                 while(i<1):
                           if line[i]==”T”:
                            count+=1
                 i+=1
         print(” No. of T present=”,count)

Q7. to find no. of lines starting with T in story.txt file.

ans.   f=open(“story.txt”,”r”)
          count=0
          for line in f:
                   if line[0]==”T”:
                         count+=1
                         print(line)
          print(” no. of line starting with T=”,count)

Q8. write recursive function to print table of 5.

ans.  def   printcount(s,n):
                  if s==0:
                        return 10*n
                 else:
                       print(s*n)
                       return(printcount(s+1,n))
        n=5
        print(printcount(1,n))

python recursion important questions

You may also like...

1 Response

  1. November 9, 2020

    […] of using python libraries       important question of using python libraries (4)recursion     notes of recursion    important question of recursion (5)idea of algorithmic efficiency      notes of […]

Leave a Reply

Your email address will not be published. Required fields are marked *