0% found this document useful (0 votes)
2 views3 pages

Programs To Execute in Lab 2

The document outlines a series of programming tasks to be executed from November 11 to November 16, 2024, including recursive functions for converting decimal to binary, calculating the sum of natural numbers, and generating Fibonacci sequences. It also includes programs for number guessing, random choice and shuffle demonstrations, and various exception handling techniques. Additionally, there are examples of assertions in Python and placeholder entries 'aa' and 'zz'.

Uploaded by

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

Programs To Execute in Lab 2

The document outlines a series of programming tasks to be executed from November 11 to November 16, 2024, including recursive functions for converting decimal to binary, calculating the sum of natural numbers, and generating Fibonacci sequences. It also includes programs for number guessing, random choice and shuffle demonstrations, and various exception handling techniques. Additionally, there are examples of assertions in Python and placeholder entries 'aa' and 'zz'.

Uploaded by

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

Programs to execute in Lab 11/11/24 to 16/11/24

1. Write a Program to convert decimal to binary using recursion


def binary(n):
if n>=1:
binary(n//2)
print(n%2, end='')
dec = int(input("Enter an Integer:"))
binary(dec)

2. Write a python program to find sum of n natural numbers using recursion


def recursive_sum(n):
if n<=1:
return n
else:
return n * recursive_sum(n-1)

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


if num < 0:
print("Enter a positive number:")
else:
print("The sum of first", num, "natural numbers is",recursive_sum(num))

3. Write a Python Program to Display Fibonacci Sequence Using Recursion.


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

nterms = int(input("Enter the number of terms (greater than 0): "))


# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))

4. Write a program to guess a number and display a suitable message


import random

secretNumber = random.randint(1, 20)


print('I am thinking of a number between 1 and 20.')
# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):
print('Take a guess.')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low.')
elif guess > secretNumber:
print('Your guess is too high.')
else:
break #This condition is the correct guess!
if guess == secretNumber:
print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))

5. Demonstration of random choice and random shuffle on Lists


import random
pets = ['Dog', 'Cat', 'Moose']
print("Calling random first time")
print(random.choice(pets))
print("Calling random second time")
print(random.choice(pets))
print("Calling random third time")
print(random.choice(pets))
print("shuffling pets")
random.shuffle(pets)
print(pets)

6. Program to demonstrate exception handling


try:
x=6/0
print(str(x))
except ZeroDivisionError:
print("ZeroDivisorError is caught")
# Below exception will not be caught
except FileNotFoundError:
print("FileNotFoundError is caught")
finally:
print("Finally block is always executed")

7. Program to demonstrate exception handling


try:
a=int(input("First Number:"))
b=int(input("Second Number:"))
result=a/b
print("Result=",result)
except ZeroDivisionError:
print("Division by zero")
else:
print("Successful Division")

8. Program to demonstrate except clause with no exception


#program to demonstrate no exception
try:
a=int(input("First Number:"))
b=int(input("Second Number:"))
result=a/b
print("Result=",result)
except:
print("Error Occured")
else:
print("Successful Division")

9. Program to demonstrate finally block in exception


try:
a=int(input("First Number:"))
b=int(input("Second Number:"))
result=a/b
print("Result=",result)
finally:
print("Executed Always, Successful Division")

10. Program to demonstrate assertion in python


def sum(a,b):
sum=a+b
assert(sum>0), "Two low value"
return(sum)

a=int(input("First Number:"))
b=int(input("Second Number:"))
print(sum(a,b))

11. aa
12. zz

You might also like