1.
BankAccount Class
This code defines a simple BankAccount class to manage deposits, withdrawals, and
balance tracking.
- __init__: Initializes account with name, zero balance, and empty transaction list.
- deposit: Adds amount to balance and records the transaction.
- withdraw: Deducts amount if balance is sufficient, otherwise prints an error.
- show_balance: Returns current balance.
The test code creates an account, performs transactions, and displays transaction
history and balance.
class BankAccount:
def __init__(self, name):
[Link] = name
[Link] = 0
[Link] = []
def deposit(self, amount):
[Link] += amount
[Link](f"Deposit-{len([Link])+1}: {amount}")
def withdraw(self, amount):
if amount > [Link]:
print("Insufficient balance.")
else:
[Link] -= amount
[Link](f"Withdraw-{len([Link])+1}: {amount}")
def show_balance(self):
return [Link]
# Test
account = BankAccount("Alice")
[Link](11000)
print("Transaction History:", [Link])
[Link](25000)
print("Account Balance:", account.show_balance())
[Link](9000)
[Link](30000)
[Link](15000)
print("Transaction History:", [Link])
print("Account Balance:", account.show_balance())
2. Top Students Function and Status Check
This script defines a function to find students who scored above average and checks
pass/fail status.
- top_students: Calculates average and returns names of students who scored above it.
- Loop: Iterates through each student and prints 'P' for pass (>= 30), 'F' for fail.
All student scores and their pass/fail status are printed.
def top_students(students):
avg = sum([Link]()) / len(students)
return [name for name, score in [Link]() if score > avg]
students = {"Alice": 75, "Bob": 45, "Charlie": 80, "David": 40}
print("Top Students:", top_students(students))
for name, score in [Link]():
status = "P" if score >= 30 else "F"
print(f"Student: {name}, Score: {score}, Status: {status}")
3. Custom Module Example ([Link])
This shows how to create and use your own Python module.
- [Link]: Defines a simple greet function.
- [Link]: Imports the module and calls greet with a name.
This helps you reuse code by organizing it into separate files.
# File: [Link]
def greet(name):
return f"Hello, {name}!"
# Main file ([Link])
import mymodule
print([Link]("Ali")) # Output: Hello, Ali!