Anwer for Assignment 1.
Answer for Question 1
def sum_of_numbers(numbers):
# Example:
numbers_list = [1, 2, 3, 4.5, 5] # Sample list of numbers
result = sum_of_numbers(numbers_list)
print(f"The sum of the numbers is: {result}")
Answer for Question 2.
def is_in_range(number, lower_bound, upper_bound)
# Example:
num = 5
lower = 1
upper = 10
if is_in_range(num, lower, upper):
print(f"{num} is within the range of {lower} to {upper}.")
else:
print(f"{num} is outside the range of {lower} to {upper}.")
Answer for Question 3:
def count_case_letters(input_string)
upper_count = 0
lower_count = 0
for char in input_string:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
return upper_count, lower_count
# Example:
input_str = "Hello World!" uppercase, lowercase = count_case_letters(input_str)
print(f"Uppercase letters: {uppercase}, Lowercase letters: {lowercase}")
Answer for Question 4:
# Sample list of tuples
people = [("Alice", 30), ("Bob", 25), ("Charlie", 35), ("David", 25)]
# Sort by age (second element of the tuple)
sorted_by_age = sorted(people, key=lambda person: person[1])
print("Sorted by age:")
for person in sorted_by_age:
print(person)
# Sort by name (first element of the tuple)
sorted_by_name = sorted(people, key=lambda person: person[0])
print("\nSorted by name:")
for person in sorted_by_name:
print(person)
# Sort by age, then by name
sorted_by_age_then_name = sorted(people, key=lambda person: (person[1], person[0]))
print("\nSorted by age, then by name:")
for person in sorted_by_age_then_name:
print(person)
Answer for Question 5:
class TicketBookingSystem:
def __init__(self):
self.available_tickets = 100 # Total available tickets
self.booked_tickets = 0 # Total booked tickets
def display_available_tickets(self):
print(f"Available tickets: {self.available_tickets}")
print(f"Booked tickets: {self.booked_tickets}")
def book_ticket(self, num_tickets):
if num_tickets <= 0:
print("Please enter a valid number of tickets to book.")
return
if num_tickets > self.available_tickets:
print("Not enough available tickets.")
return
self.available_tickets -= num_tickets
self.booked_tickets += num_tickets
print(f"{num_tickets} ticket(s) booked successfully!")
def cancel_ticket(self, num_tickets):
if num_tickets <= 0:
print("Please enter a valid number of tickets to cancel.")
return
if num_tickets > self.booked_tickets:
print("You cannot cancel more tickets than you have booked.")
return
self.available_tickets += num_tickets
self.booked_tickets -= num_tickets
print(f"{num_tickets} ticket(s) canceled successfully!")
def run(self):
while True:
print("\n--- Ticket Booking System ---")
self.display_available_tickets()
print("1. Book Tickets")
print("2. Cancel Tickets")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == '1':
num = int(input("Enter number of tickets to book: "))
self.book_ticket(num)
elif choice == '2':
num = int(input("Enter number of tickets to cancel: "))
self.cancel_ticket(num)
elif choice == '3':
print("Exiting the system. Thank you!")
break
else:
print("Invalid choice. Please try again.")
# To run the ticket booking system
if __name__ == "__main__":
booking_system = TicketBookingSystem()
booking_system.run()