0% found this document useful (0 votes)
4 views3 pages

Python Class and Function Examples

Uploaded by

zalanop7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Class and Function Examples

Uploaded by

zalanop7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

BankAccount Class

Code:
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())

Output:
Transaction History: ['Deposit-1: 11000']
Insufficient balance.
Account Balance: 11000
Transaction History: ['Deposit-1: 11000', 'Withdraw-2: 9000', 'Deposit-3: 30000',
'Withdraw-4: 15000']
Account Balance: 17000

Explanation:
Line-by-line Explanation:
- Line 1-3: Define a class BankAccount with initializer storing name, zero balance, and
empty transactions list.
- Line 5-7: deposit method increases balance and logs the deposit in transactions.
- Line 9-14: withdraw method checks if amount is more than balance, prints error if so;
else deducts and logs it.
- Line 16-17: show_balance returns the current balance.
- Line 20: Creates account for Alice.
- Line 21: Deposits 11000.
- Line 22: Prints transaction history after deposit.
- Line 24: Tries to withdraw 25000, which fails.
- Line 25: Prints balance (still 11000).
- Line 27-29: More transactions; withdraw 9000, deposit 30000, withdraw 15000.
- Line 31-32: Final transaction history and balance are printed.

2. Top Students and Status

Code:
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}")

Output:
Top Students: ['Alice', 'Charlie']
Student: Alice, Score: 75, Status: P
Student: Bob, Score: 45, Status: P
Student: Charlie, Score: 80, Status: P
Student: David, Score: 40, Status: P

Explanation:
Line-by-line Explanation:
- Line 1-3: Define function to calculate average and return names scoring above it.
- Line 5: Dictionary of students with names and scores.
- Line 6: Calls top_students and prints students scoring above average (avg = 60).
- Line 8-10: For each student, checks if score >= 30, assigns "P" (pass) or "F" (fail),
and prints the result.

3. Custom Module Example

Code:
# File: [Link]
def greet(name):
return f"Hello, {name}!"

# Main file
import mymodule
print([Link]("Ali"))

Output:
Hello, Ali!

Explanation:
Line-by-line Explanation:
- Line 2: Defines function greet that returns "Hello, name!".
- Line 5: Imports the custom module named mymodule.
- Line 6: Calls greet function with "Ali", prints the result.

You might also like