Ashish Python
Ashish Python
Page 1
CONTENT
BTECH CSE 3RD SEM – PRACTICAL ASSIGNMENT
Page 3
Program no. 1
def count_vowels_and_consonants(s)
vowels = 'aeiouAEIOU'
vowel_count = 0
consonant_count = 0
for char in s:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
return vowel_count, consonant_count
input_string = input("Enter a string: ")
vowels, consonants = count_vowels_and_consonants(input_string)
print(f"Total number of vowels: {vowels}")
print(f"Total number of consonants: {consonants}")
Output-
Page 4
Program no. 2
def count_words(s)
words = s.split()
return len(words)
input_string = input("Enter a string: ")
word_count = count_words(input_string)
print(f"Total number of words: {word_count}")
Output-
Page 5
Program no. 3
WAP in Python to generate all possible substrings of a input strings.
def generate_substrings(s)
substrings = []
length = len(s)
for start in range(length):
for end in range(start + 1, length + 1):
substrings.append(s[start:end])
return substrings
input_string = input("Enter a string: ")
substrings = generate_substrings(input_string)
print("All possible substrings:")
for substring in substrings:
print(substring)
Output-
Page 6
Program no. 4
Output-
Page 7
Program no. 5
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def display_details(self):
print(f"Car Make: {self.make}")
print(f"Car Model: {self.model}")
print(f"Car Year: {self.year}")
car1 = Car("Ford", "Model T", 1927)
car2 = Car("Chevrolet", "Bel Air", 1957)
car3 = Car("Volkswagen", "Beetle", 1965)
car4 = Car("Porsche", "356", 1955)
car1.display_details()
print()
car2.display_details()
print()
car3.display_details()
print()
car4.display_details
Page 8
Output-
Page 9
Program no. 6
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def display_details(self):
print(f"Car Make: {self.make}")
print(f"Car Model: {self.model}")
print(f"Car Year: {self.year}")
class ElectricCar(Car):
def __init__(self, make, model, year, battery_capacity):
super().__init__(make, model, year)
self.battery_capacity = battery_capacity
def describe_battery(self):
print(f"This electric car has a battery capacity of {self.battery_capacity} kWh.")
car1 = Car("Ford", "Mustang", 1967)
electric_car1 = ElectricCar("Nissan", "Leaf", 2022, 40)
car1.display_details()
print()
electric_car1.display_details()
electric_car1.describe_battery()
Page 10
Output-
Page 11
Program no. 7
class BankAccount:
def __init__(self, account_number, initial_balance=0):
self.account_number = account_number
self.balance = initial_balance
def deposit(self, amount):
if amount > 0:
self.balance += amount
print(f"Deposited: ${amount:.2f}. New Balance: ${self.balance:.2f}")
else:
print("Deposit amount must be positive.")
def withdrawal(self, amount):
if amount > 0:
if amount <= self.balance:
self.balance -= amount
print(f"Withdrew: ${amount:.2f}. New Balance: ${self.balance:.2f}") else:
print("Insufficient funds for withdrawal.")
else:
print("Withdrawal amount must be positive.")
def balance_inquiry(self):
print(f"Account Number: {self.account_number}, Balance: ${self.balance:.2f}") account =
BankAccount("98765432", 5000)
account.balance_inquiry()
account.deposit(1200)
account.withdrawal(800)
account.withdrawal(6000)
account.balance_inquiry()
Page 12
Output-
Page 13
Program no. 8
class BankAccount:
def __init__(self, account_number, initial_balance=0):
self.account_number = account_number
self.__balance = initial_balance
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"Deposited: ${amount:.2f}. New Balance: ${self.__balance:.2f}") else:
print("Deposit amount must be positive.")
def withdrawal(self, amount):
if amount > 0:
if amount <= self.__balance:
self.__balance -= amount
print(f"Withdrew: ${amount:.2f}. New Balance: ${self.__balance:.2f}") else:
print("Insufficient funds for withdrawal.")
else:
print("Withdrawal amount must be positive.")
def balance_inquiry(self):
print(f"Account Number: {self.account_number}, Balance: ${self.__balance:.2f}")
def get_balance(self):
return self.__balance
def set_balance(self, amount):
if amount >= 0:
self.__balance = amount
print(f"Balance set to: ${self.__balance:.2f}")
else:
print("Balance cannot be negative.")
account = BankAccount("87654321", 5000)
account.balance_inquiry()
account.deposit(1500)
account.withdrawal(1000)
Page 14
account.withdrawal(7000)
account.balance_inquiry()
print(f"Current Balance (via getter): ${account.get_balance():.2f}")
account.set_balance(4000)
account.balance_inquiry()
Output-
Page 15
Program no. 9
import math
class Shape:
def area(self):
raise NotImplementedError("Subclasses must implement this method.")
def perimeter(self):
raise NotImplementedError("Subclasses must implement this method.")
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * (self.radius ** 2)
def perimeter(self):
return 2 * math.pi * self.radius
class Triangle(Shape):
def __init__(self, base, height, side1, side2, side3): self.base = base
self.height = height
self.side1 = side1
self.side2 = side2
self.side3 = side3
def area(self):
return 0.5 * self.base * self.height
Page 16
def perimeter(self):
return self.side1 + self.side2 + self.side3
rectangle = Rectangle(8, 12)
circle = Circle(5)
triangle = Triangle(10, 5, 7, 8, 9)
print(f"Rectangle - Area: {rectangle.area()}, Perimeter: {rectangle.perimeter()}") print(f"Circle - Area:
{circle.area():.2f}, Perimeter: {circle.perimeter():.2f}")
print(f"Triangle - Area: {triangle.area()}, Perimeter: {triangle.perimeter()}")
Output-
Page 17
Program no. 10
def sum_odd_even(numbers):
sum_odd = 0
sum_even = 0
for num in numbers:
if num % 2 == 0:
sum_even += num
else:
sum_odd += num
return sum_odd, sum_even
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
odd_sum, even_sum = sum_odd_even(numbers)
print(f"Sum of odd numbers: {odd_sum}")
print(f"Sum of even numbers: {even_sum}")
Output-
Page 18