0% found this document useful (0 votes)
3 views18 pages

Ashish Python

DONE

Uploaded by

Apne Dipu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views18 pages

Ashish Python

DONE

Uploaded by

Apne Dipu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Bahra University

Academic Year 2024


Programming in
python ( CSG307)

Submitted To: Submitted by:


Saurav sir Ashish Chawla
BU2023UGCS014

Page 1
CONTENT
BTECH CSE 3RD SEM – PRACTICAL ASSIGNMENT

Sr Description of Assignment Page Remarks


No. no.
1 WAP in Python to calculate 4
total number of consonants
and vowels in an input string.
2 WAP in Python to count the
number of words present in
5
an input string.
3 WAP in Python to generate all
possible substrings of a input
6
strings.

4 Write a recursive function 7


tower_of_hanoi(n, source,
auxiliary, target) that prints
the steps to solve the Tower
of Hanoi problem for n disks.
The function should move all
disks from the source tower to
the target tower using the
auxiliary tower.
5 Define a class representing a
car with attributes like make,
8-9
model, and year. Write a
program
to create instances of the
class and print their details.
6 Implement inheritance by 10-11
creating a subclass of the car
class called ElectricCar. Add
an
attribute for battery capacity
and a method to describe
battery information.
7 Create a class representing a
bank account with attributes
12-13
like account number and
balance.
Page 2
Implement methods for
deposit, withdrawal, and
balance inquiry.
8 Implement encapsulation by
making the balance attribute
14-15
of the bank account class
private.
Add methods to get and set
the balance.
9 Create a class representing a
geometric shape with
16-17
methods to calculate area and
perimeter.
Write subclasses for specific
shapes like rectangle, circle,
and triangle.
WAP in python to calculate 18
10 the sum of odd numbers and
even numbers separately in a
List.

Page 3
Program no. 1

WAP in Python to calculate total number of consonants and vowels


in an input string.

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

WAP in Python to count the number of words present in an input


string.

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

Write a recursive function tower_of_hanoi(n, source, auxiliary,


target) that prints the
steps to solve the Tower of Hanoi problem for n disks. The function
should move all disks
from the source tower to the target tower using the auxiliary
tower.

def tower_of_hanoi(n, source, auxiliary, target):


if n == 1:
print(f"Move disk 1 from {source} to {target}")
return
tower_of_hanoi(n - 1, source, target, auxiliary)
print(f"Move disk {n} from {source} to {target}")
tower_of_hanoi(n - 1, auxiliary, source, target)

tower_of_hanoi(2, 'A', 'B', 'C')

Output-

Page 7
Program no. 5

Define a class representing a car with attributes like make, model,


and year. Write a program
to create instances of the class and print their details.

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

Implement inheritance by creating a subclass of the car class called


ElectricCar. Add an
attribute for battery capacity and a method to describe battery
information.

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

Create a class representing a bank account with attributes like


account number and balance.
Implement methods for deposit, withdrawal, and balance inquiry.

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

Implement encapsulation by making the balance attribute of the


bank account class private.
Add methods to get and set the balance.

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

Create a class representing a geometric shape with methods to


calculate area and perimeter.
Write subclasses for specific shapes like rectangle, circle, and
triangle.

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

WAP in python to calculate the sum of odd numbers and even


numbers separately in a List.

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

You might also like