Python Labbook
Python Labbook
total=0
for num in range(22,46,2):
total=total+num
print(total)
Question 1(B)=========================================
for digit in n:
if digit == '0':
zero_count += 1
elif int(digit) % 2 == 0:
even_count += 1
else:
odd_count += 1
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)=========================================
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:", s)
Question 3(A)=======================================
#reverse
num = int(input("Enter a number: "))
rev = 0
Question 3(b)===========================================
Question 4(A)==============================================
n = 3 # Number of rows
start = 5 # Start from 5
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)=========================================
Question 5(b)=========================================
Question 5(c)========================================
for c in string:
if c == char:
count += 1
Question 6==========================================
if len(s) >= 3:
if s.endswith("ing"):
s += "ly"
else:
s += "ing"
print("Modified string:", s)
Question 7(A)=====================================
vowels = "aeiouAEIOU"
result = ""
for c in s:
if c not in vowels:
result += c
Question 7(B)====================================
count = s.count(sub)
Question 8======================================
first_char = s[0]
s_modified = first_char + s[1:].replace(first_char, '$')
Question 9(A)======================================
s3 = "".join([s1, s2])
Question 9(B)======================================
Question 9(c)=======================================
Question 9(d)=======================================
Question 9(e)=======================================
Question 10(A)=====================================
Question 10(B)==================================
s = input("Enter a string:")
mirror=s[::-1]
print("Mirror of the string is:",mirror)
Question 10(c)==================================
Question 11(A)================================
Question 11(B)==================================
Question 11(c)=================================
Question 11(D)================================
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)================================
print("Maximum value:",max_value)
print("Minimum value:",min_value)
Question 14(A)=================================
Question 14(B)=================================
new_tup=tup+(ele,)
print("New list after adding elememt",new_tup)
Question 14(c)================================
Question 14(d)=================================
Question 14(e)=================================
Question 15(b)=================================
def sum_list(numbers):
return sum(numbers)
Question 15(c)=================================
Question 16(a)================================
cube_lambda = lambda x: x ** 3
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
Question 21====================================
class BankAccount:
def __init__(self, name, balance):
self.name = name
self.balance = 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}")
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}")
Question 30(A)==================================
class NegativeNumber(Exception):
pass
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:")
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
try:
accno = int(input("Enter account number: "))
name = input("Enter account holder's name: ")
bal = float(input("Enter balance: "))
except BalanceWithinRange as e:
print(e)