Python Module 3
Python Module 3
Question-1
Assist Mrs. Johnson in simplifying her task by creating a function to
compute the class's average score. Mrs. Johnson meticulously assessed each
student's test and compiled their individual scores. As a conscientious
teacher, she intends to determine the class's average score to evaluate
their collective performance.
class_scores = [85, 92, 78, 69, 73, 88, 94, 80, 87, 90]
total_score = sum(class_scores)
total_students = len(class_scores)
average_score_class = total_score/total_students
Question-2
During an exciting school sports event, students demonstrated their
impressive speed and agility on the track. Write a concise Python code
using the built-in function min() to promptly identify the athlete who
achieved the shortest race time.
import operator
if not race_times:
else:
Question-3
Picture yourself as an English teacher engaging in an enjoyable language
exercise with your students. Now, let's craft a program that demonstrates
how to utilize Python's count() function to determine the quantity of
vowels within a given word.
vowel_count = 0
word_lower = word.lower()
vowel_count = word_lower.count('a') + word_lower.count('e') + word_lower.count('i') +
word_lower.count('o') + word_lower.count('u')
User-Defined Functions
Question-1
Assume that you’re working at a school and the school has a vision of
building a rectangular playground with a specific width and height. They
needed a quick and efficient way to calculate the playground's area.
Question-2
One sunny morning, the school's principal, Mrs. Anderson, gathered all the
students in the assembly hall to announce an exciting event: the first-
ever "Healthy Heights Health Fair. In this endeavor, they have decided to
launch a health awareness campaign to educate students about the
importance of maintaining a healthy lifestyle, including monitoring their
Body Mass Index (BMI).
def calculated_BMI(weight,height):
BMI=(weight/(height**2))*100
print(BMI)
return "Underweight"
return "Overweight"
else :
return "Obese"
BMI = calculated_BMI(weight,height)
student_data = [ ("Alice", 85), ("Bob", 70), ("Charlie", 95), ("David", 60), ("Eve", 78) ]
print(f"{student[0]} - {student[1]}")
Question-2
You have two employee salaries, EmployeeX and EmployeeY, along with a
specific operation.
Write a Python code snippet that takes the values of EmployeeX, EmployeeY,
and operation, and then performs the specified operation on the salaries
using lambda functions. Write a Python code snippet that takes EmployeeX,
EmployeeY, and operation as input and performs the specified arithmetic
operation on the salaries using lambda functions. Print the result of the
operation.
EmployeeX = 150000
EmployeeY = 125000
if operation == "+":
result = add(EmployeeX,EmployeeY)
result = multiplication(EmployeeX,EmployeeY)
result = division(EmployeeX,EmployeeY)
else:
print("Result:", result)
Recursive Functions
Question-1
You are given a sorted list of integers as follows:
sorted_list = [3, 8, 12, 15, 21, 27, 36, 42, 53, 60]
Write a Python function called binary_search_index that takes in three
parameters: the sorted list, the target integer to search for, and the
indices left and right representing the search range.
The function should return the index of the target integer in the list if
it is present, otherwise, it should return -1.
Implement the binary_search_index function using the binary search
algorithm and test it with the provided sorted_list and target integers:
21, 8, and 37.
return -1
if array[mid] == target:
return mid
else:
else:
Question-2
Your task is to write a Python program that calculates the result of
raising a given base number to a specified exponent using recursion.
def power(base,exponent):
if exponent == 0:
return 1
Question-3
Palindrome detection is a common problem in computer science, involving
identifying strings that read the same forwards and backward. To tackle
this, recursion offers an elegant solution.
Your task is to write a Python program that determines whether a given
string is a palindrome or not. A palindrome is a string that remains the
same when its characters are reversed.
def is_palindrome(s):
if len(s) <= 1:
return True
return False
else:
return is_palindrome(s[1:-1])
if is_palindrome(string):
print(f"'{string}' is a palindrome.")
else:
Capstone
Question-1
Create a simple banking interface that welcomes the account owner "Alice",
provides her with the option to inquire about her account balance, and
then displays her current balance of $3000.
account_owner = "Alice"
current_balance = 3000
def welcome(username):
print("Welcome,", username)
def balance_inquiry(account_balance):
print("Current balance:",account_balance)
Question-2
You are tasked with creating a simple ATM application interface that
allows an account owner to perform deposit and withdrawal operations on
their account balance. You need to implement the code following the
provided instructions and demonstrate its functionality with examples of
both deposit and withdrawal operations. Instructions:
Handle User Choice: Take input from the user for their choice of operation
(1 for deposit, 2 for withdrawal). Store this choice in a variable named
choice. (Do not use the input function, directly save the value in the
variable eg. choice = 1) Withdrawal Operation:
1. If the user's choice is 2, calculate the predefined withdrawal amount
as 500.
2. Call a function named withdrawal with the following parameters:
account_owner, withdrawal_amount, and current_balance.
3. Inside the withdrawal function, check if the withdrawal_amount is
greater than the current_balance. If it is, display an "Insufficient
Balance" message. Otherwise, subtract the withdrawal_amount from the
current_balance and display a success message with the updated balance.
account_owner = "Eve"
account_balance = 1800
def welcome(username):
account_balance += amount
return account_balance
account_balance -= amount
return account_balance
return None
welcome(account_owner)
print("1. Deposit")
print("2. Withdrawal")
if choice == 1:
deposit_amount = 1000
withdrawal_amount = 500
if updated_balance:
else:
else:
print("Invalid choice.")
Question-3
You are tasked with extending the functionality of the existing ATM
application interface to include two new features: viewing a mini
statement and changing the PIN. You need to implement the code according
to the provided instructions and demonstrate the functionality of both
mini statement viewing and the change PIN operation.
account_owner_pin = "Grace"
current_pin = "4321"
account_owner_mini = "David"
new_pin ="5678"
def welcome(username):
def mini_statement(transactions):
return transactions[-5:]
if len(new_pin) == 4:
if choice == 1:
welcome(account_owner_mini)
recent_transactions = mini_statement(transaction_history)
elif choice == 2:
welcome(account_owner_pin)
print(result)
else:
print("Invalid choice.")
Question-4
Create a function named "main_menu" with the account number as a parameter
(2346289024444). Display a welcome message, including the user's account
number. Display the following options to the user:
NOTE: USER INPUT is not supported in this code box. So, You can hardcode
the input in the 'choice' variable. While submitting the code. Remove the
choice variable.
def main_menu(account_number):
while True:
if choice == 1:
print("You have checked the account balance, Balance enquiry function called")
break
elif choice == 2:
break
elif choice == 3:
break
elif choice == 4:
break
elif choice == 5:
break
else:
break
Question-5
The variables given below are already defined: account_owner = "Alice"
current_balance = 3000 choice = 4 transaction_history = ["Deposit +$100",
"Withdrawal -$50", "Deposit +$200", "Transfer -$30", "Withdrawal -$20",
"Transfer +$50"] withdrawal_amount = 1000 deposit_amount = 2000
account_number = "2346289024444" current_pin = 3456 new_pin = 1899
current_balance = 3000
withdrawal_amount = 1000
deposit_amount = 2000
account_number = "2346289024444"
current_pin = 3456
new_pin = 1899
def welcome(username):
def balance_inquiry(account_balance):
return account_balance
account_balance += amount
return account_balance
account_balance -= amount
return account_balance
def mini_statement(transactions):
return transactions[:5]
else:
def main_menu(account_number):
global transaction_history
global current_balance
while True:
if choice == 1:
break
elif choice == 2:
if current_balance:
recent_transactions = mini_statement(transaction_history)
break
else:
elif choice == 3:
recent_transactions = mini_statement(transaction_history)
break
elif choice == 4:
result=change_pin(current_pin, new_pin)
print(result)
break
elif choice == 5:
break
else:
break
welcome(account_owner)