Q1.
Write a Python function to check whether a number falls within a given
range
ANS.
def is_in_range(num, lower, upper):
if lower <= num <= upper:
return True
else:
return False
num = int(input("Enter the number to check: "))
lower = int(input("Enter the lower limit: "))
upper = int(input("Enter the upper limit: "))
if is_in_range(num, lower, upper):
print("The number lies within the given range.")
else:
print("The number does not lie within the given range.")
Q2. Write a Python program that accepts a hyphen-separated sequence of words
as input and prints the words in a hyphen-separated sequence after sorting them
alphabetically
ANS.
words = input("Enter a hyphen-separated sequence of words: ")
word_list = [Link]("-")
word_list.sort()
result = "-".join(word_list)
print(result)
Q3. Write a function called get_highest that takes 2 numbers as parameters and
returns the highest of the 2 numbers.
ANS.
def get_highest(a, b):
if a > b:
return a
else:
return b
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
highest = get_highest(num1, num2)
print(highest)
Q4. Write a Python function that accepts a string and counts the number of
upper and lower case letters.
ANS.
def count_case_letters(s):
upper = 0
lower = 0
for i in s:
if [Link]():
upper += 1
elif [Link]():
lower += 1
print("No. of Upper case characters :", upper)
print("No. of Lower case Characters :", lower)
text = input("Enter a string: ")
count_case_letters(text)
5. Write a Python function to check whether a number is "Perfect" or not.
ANS.
def is_perfect(n):
s=0
for i in range(1, n):
if n % i == 0:
s += i
if s == n:
print("Perfect Number")
else:
print("Not a Perfect Number")
num = int(input("Enter a number: "))
is_perfect(num)
6. Write a function to find all pythogorean triplets within a given range
ANS.
def pythagorean_triplets(n):
for a in range(1, n + 1):
for b in range(a, n + 1):
for c in range(b, n + 1):
if a*a + b*b == c*c:
print(a, b, c)
limit = int(input("Enter the range: "))
pythagorean_triplets(limit)
7. Write a Python function to count the number of occurrences of a specific
substring in a string
ANS.
def count_substring(s, sub):
count = 0
for i in range(len(s) - len(sub) + 1):
if s[i:i+len(sub)] == sub:
count += 1
print(count)
string = input("Enter the string: ")
substring = input("Enter the substring: ")
count_substring(string, substring)
8. Write a recursive function to print sum of squares till n.
ANS.
def sum_of_squares(n):
if n == 0:
return 0
return n*n + sum_of_squares(n-1)
num = int(input("Enter a number: "))
print(sum_of_squares(num))
9. Write a recursive function to find digit root of a [Link] digital root is the
single digit number obtained by repeatedly adding the digits of a number until a
one-digit number is reached. For example, the digital root of 12,345 is 6,
because 1 + 2 + 3 + 4 + 5 = 15, and then 1 + 5 = 6.
ANS.
def digital_root(n):
if n < 10:
return n
s=0
while n > 0:
s += n % 10
n //= 10
return digital_root(s)
num = int(input("Enter a number: "))
print(digital_root(num))
10. Write a recursive function to find the nth term of the AP
ANS.
def nth_ap(a, d, n):
if n == 1:
return a
return nth_ap(a, d, n-1) + d
a = int(input("Enter first term: "))
d = int(input("Enter common difference: "))
n = int(input("Enter term number: "))
print(nth_ap(a, d, n))