Question 1 (A)==========================================
total=0
for num in range(22,46,2):
total=total+num
print(total)
Question 1(B)=========================================
n = input("Enter a number: ")
even_count = 0
odd_count = 0
zero_count = 0
for digit in n:
if digit == '0':
zero_count += 1
elif int(digit) % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even digits:", even_count)
print("Odd digits:", odd_count)
print("Zero digits:", zero_count)
Question 1(C)=======================================
n=int(input("Enter number:"))
first_digit = int(n[0])
last_digit=int(n[-1])
sum_digits = first_digit+last_digit
print(sum_digits)
Question 2(A)=========================================
num=int(input("Enter the number:"))
s=0
for i in range(1,num+1):
s=s+i
print(s)
Question 2(b)=======================================
#factorial
num=int(input("Enterthenumber:"))
fact=1
i=1
if num==1:
print("factorial of 1 is 1")
else:
while(i<=num):
fact=fact*i
i=i+1
print("Factorial:",fact)
Question 2(C)=======================================
#print sum of digits of a number
num = int(input("Enter a number: "))
s = 0
while num > 0:
digit = num % 10 # Get the last digit
s = s + digit # Add the digit to the sum
num = num // 10 # Remove the last digit
print("Sum of digits:", s)
Question 3(A)=======================================
#reverse
num = int(input("Enter a number: "))
rev = 0
while num > 0:
digit = num % 10 # Get the last digit
rev = rev * 10 + digit # Append the digit to the reversed number
num = num // 10 # Remove the last digit
print("Reversed number:", rev)
Question 3(b)===========================================
#find sum of digits of odd places
num = int(input("Enter a number: "))
s = 0
pos = 1 # Position counter
while num > 0:
digit = num % 10 # Get the last digit
if pos % 2 != 0: # Check if the position is odd
s = s + digit # Add the digit to the sum if it's in an odd place
num = num // 10 # Remove the last digit
pos = pos + 1 # Move to the next position
print("Sum of digits at odd places:", s)
Question 4(A)==============================================
n = 3 # Number of rows
start = 5 # Start from 5
for i in range(1, n+1):
for j in range(i):
print(start, end=" ")
start += 5
print()
Question 4(b)============================================
Question 4(c)===========================================
n = 4
for i in range(n, 0, -1):
for j in range(i):
print(i, end=" ")
print()
Question 5(A)=========================================
string = input("Enter a string: ")
count = 0
for char in string:
count += 1
print("Length of the string:", count)
Question 5(b)=========================================
string1 = input("Enter the original string: ")
string2 = ""
for char in string1:
string2 += char
print("Original string:", string1)
print("Copied string:", string2)
Question 5(c)========================================
string = input("Enter the string: ")
char = input("Enter the character to count: ")
count = 0
for c in string:
if c == char:
count += 1
print(f"The character '{char}' occurs {count} times in the string.")
Question 6==========================================
s = input("Enter a string: ")
if len(s) >= 3:
if s.endswith("ing"):
s += "ly"
else:
s += "ing"
print("Modified string:", s)
Question 7(A)=====================================
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
result = ""
for c in s:
if c not in vowels:
result += c
print("String after removing vowels:", result)
Question 7(B)====================================
s = input("Enter the main string: ")
sub = input("Enter the substring to count: ")
count = s.count(sub)
print("Occurrences of the substring:", count)
Question 8======================================
s = input("Enter a string: ")
first_char = s[0]
s_modified = first_char + s[1:].replace(first_char, '$')
print("Modified string:", s_modified)
Question 9(A)======================================
s1 = input("Enter the first string: ")
s2 = input("Enter the second string: ")
s3 = "".join([s1, s2])
print("Concatenated string:", s3)
Question 9(B)======================================
s = input("Enter a string: ")
n = int(input("Enter the number of characters to lowercase: "))
s_modified = s[:n].lower() + s[n:]
print("Modified string:", s_modified)
Question 9(c)=======================================
Question 9(d)=======================================
Question 9(e)=======================================
s1 = input("Enter the first string: ")
s2 = input("Enter the second string: ")
# Exchange the first two characters of each string
new_s1 = s2[:2] + s1[2:]
new_s2 = s1[:2] + s2[2:]
# Combine the new strings with a space
result = new_s1 + " " + new_s2
print("Resulting string:", result)
Question 10(A)=====================================
name=input("Enter you full name:").split()
abr=''
for n in name:
abr=abr+n[0].upper()
print("Abbreviated form:",abr)
Question 10(B)==================================
s = input("Enter a string:")
mirror=s[::-1]
print("Mirror of the string is:",mirror)
Question 10(c)==================================
Question 11(A)================================
num=input("Enter the number separated by spaces:").split()
total=0
for n in num:
total=total+int(n)
print("Sum :",total)
Question 11(B)==================================
num = list(map(int, input("Enter the numbers separated by spaces: ").split()))
largest = num[0]
smallest = num[0]
for n in num: # Changed nums to num
if n > largest:
largest = n
if n < smallest:
smallest = n
print("Largest number:", largest)
print("Smallest number:", smallest)
Question 11(c)=================================
nums=list(map(int,input("Enter number separated by spaces").split()))
product=1
for num in nums:
product=product*num
print("Product of number",product)
Question 11(D)================================
nums=list(map(int,input("Enter number separated by spaces:").split()))
all_even=True
for num in nums:
if num%2!=0:
all_even=False
break
if all_even:
print("All number are even")
else:
print("Not all numbers are even")
Question 11(E)===============================
lst = list(map(int,input("Enter the list elements separated by spaces: ").split()))
if lst == sorted(lst):
print("The list is in ascending order.")
else:
print("The list is not in ascending order.")
Question 13(c)================================
num=set(map(int,input("Enter number separated by spaces:").split()))
max_value=max(num)
min_value=min(num)
print("Maximum value:",max_value)
print("Minimum value:",min_value)
Question 14(A)=================================
num=tuple(map(int,input("Enter number separated by spaces:").split()))
print("The tuple is:",num)
Question 14(B)=================================
tup=tuple(map(int,input("Enter the number:").split()))
ele=int(input("Enter number you want to add:"))
new_tup=tup+(ele,)
print("New list after adding elememt",new_tup)
Question 14(c)================================
tup=tuple(map(int,input("Enter element separated by spaces:").split()))
print("4th element from the start:",tup[3])
print("4th element from the end:",tup[-4])
Question 14(d)=================================
tup=tuple(map(int,input("Enter number sep by spaces:").split()))
item=int(input("Enter the item to find its index:"))
if item in tup:
print(tup.index(item))
else:
print("item not found")
Question 14(e)=================================
tup=tuple(map(int,input("Enter number sep by spaces:").split()))
ele=input("Enter the element to check:")
if(ele in tup):
print("Element found in tuple")
else:
print("Element not found in tuple")
Question 15(b)=================================
def sum_list(numbers):
return sum(numbers)
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
print("Sum of numbers:", sum_list(nums))
Question 15(c)=================================
Question 16(a)================================
def apply_function(func, x):
return func(x)
cube_lambda = lambda x: x ** 3
number = int(input("Enter a number: "))
result = apply_function(cube_lambda, number)
print(f"The cube of {number} is {result}")
Question 16(b)=================================
def is_perfect(n):
if n < 1:
return 0
sums = 0
for i in range(1, n):
if n % i == 0:
sums =sums + i
if sums == n:
return 1
else:
return 0
number = int(input("Enter a number: "))
result = is_perfect(number)
if result:
print(f"{number} is a perfect number.")
else:
print(f"{number} is not a perfect number.")
Question 21====================================
class BankAccount:
def __init__(self, name, balance):
self.name = name
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"Deposited {amount}. Balance is now {self.balance}.")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"Withdrew {amount}. Balance is now {self.balance}.")
else:
print("Not enough balance.")
name = input("Enter account holder name: ")
balance = float(input("Enter initial balance: "))
account = BankAccount(name, balance)
deposit_amount = float(input("Enter deposit amount: "))
account.deposit(deposit_amount)
withdraw_amount = float(input("Enter withdrawal amount: "))
account.withdraw(withdraw_amount)
print(f"Final balance: {account.balance}")
Question 24====================================
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def display(self):
print(f"Student Name: {self.name}, Age: {self.age}, ID: {self.student_id}")
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age)
self.subject = subject
def display(self):
print(f"Teacher Name: {self.name}, Age: {self.age}, Subject:
{self.subject}")
name = input("Enter student name: ")
age = int(input("Enter student age: "))
student_id = input("Enter student ID: ")
student = Student(name, age, student_id)
student.display()
name = input("\nEnter teacher name: ")
age = int(input("Enter teacher age: "))
subject = input("Enter subject: ")
teacher = Teacher(name, age, subject)
teacher.display()
Question 25====================================
class Person:
def __init__(self, name):
self.name = name
class Employee(Person):
def __init__(self, name, emp_id):
super().__init__(name)
self.emp_id = emp_id
class Manager(Employee):
def __init__(self, name, emp_id, dept):
super().__init__(name, emp_id)
self.dept = dept
def display(self):
print(f"Name: {self.name}, ID: {self.emp_id}, Dept: {self.dept}")
name = input("Enter name: ")
emp_id = input("Enter employee ID: ")
dept = input("Enter department: ")
manager = Manager(name, emp_id, dept)
manager.display()
Question 30(A)==================================
class NegativeNumber(Exception):
pass
num=int(input("Enter the number:"))
if(num<0):
raise NegativeNumber(f"{num} is less than zero.")
else:
print(f"{num} is valid.")
Question 30(b)=================================
class NameNotProperException(Exception):
pass
name=input("Enter name:")
for char in name:
if (char >= '0' and char <='9'):
raise NameNotProperException("The name contains numbers, which is not
allowed.")
print(f"{name} is valid")
Question 31=====================================
num=int(input("Enter number:"))
temp=num
reverse=0
while num>0:
digit=num%10
reverse=reverse*10+digit
num=num//10
if temp==reverse:
print(f"{temp} is palindrome")
else:
print(f"{temp} is not palindrome")
Question 32=====================================
class Account:
def __init__(self, accno, name, bal):
self.accno=accno
self.name=name
self.bal=bal
if bal>=1000 and bal<=5000:
raise BalanceWithinRange("Balance within range.")
else:
print(f"{bal} not within range")
try:
accno = int(input("Enter account number: "))
name = input("Enter account holder's name: ")
bal = float(input("Enter balance: "))
# Create an account object
account = Account(accno, name, bal)
print(f"Account created successfully for {account.name} with balance
{account.bal}.")
except BalanceWithinRange as e:
print(e)