commit d7a3dff32e00f707de501a48c2e007728977d89c Author: Aman Nalakath Date: Sun Mar 29 13:34:42 2026 +0200 init commit; source - Intermediate Python - recorded for Frontend Masters; Created by Nina Zakharenko; https://www.learnpython.dev diff --git a/bankclass.py b/bankclass.py new file mode 100644 index 0000000..89fb400 --- /dev/null +++ b/bankclass.py @@ -0,0 +1,257 @@ +# Create a BankAccount class +# An __init__ that takes owner (string) and optional balance (defaults to 0) +# A deposit method that adds money and returns the new balance +# A withdraw method that subtracts money — but should prevent overdraft (no negative balance) +# A __repr__ that shows something like BankAccount('Alice', balance=500) + +from typing import Optional # trying it out with optional +from datetime import datetime +# Add @property to your BankAccount class to protect the balance: + +# Make balance a private attribute (_balance) +# Create a @property so you can still read it with acc.balance +# Create a @balance.setter that prevents setting a negative value +# Add a @property for owner that's read-only (no setter — nobody should rename the account owner) + +class BankAccount: + + @property + def balance(self): + return self._balance + + ## now setter ### should i now comment out these in the init dunder? + @balance.setter + def balance(self, value): + if value < 0: + raise ValueError("Balance cannot be negative") + else: + self._balance = value + @property + def owner(self): + return self._owner + + + def __init__(self, owner: str, balance: Optional[float] = None) -> None: # how do i provide a type hint here? is there a better way to mark something as optional?? how do i specify type here? + self._owner = owner + self.balance = balance if balance is not None else 0 + self._history = [] + + def __repr__(self): # how would i improve this with best practices?? + # return f"Account: {self._owner}, balance: {self.balance}" # a much better way would be - return(f"BankAccount({self._owner, self.balance})") + return f"BankAccount('{self.owner}', balance = {self.balance})" + + def __str__(self): + return f"yes, {self.owner} has an account of good standing with {self.balance} balance in the bank. " + + def deposit(self, money): + self.balance += money + self._history.append(Transaction("deposit", money)) + return self.balance + + def withdraw(self, money): + if (self.balance - money) >= 0: + self.balance -= money + self._history.append(Transaction("withdraw", money)) + else: + # print("Overdraft not possible") + raise InsufficientFundsError(self.balance, money) + return self.balance + + def get_history(self): + return self._history + + +## this is the testing framework - + +# acc = BankAccount("Alice", 100) +# print(acc) # BankAccount('Alice', balance=100) +# acc.deposit(50) # returns 150 +# print(acc) +# acc.withdraw(30) # returns 120 +# print(acc) +# acc.withdraw(200) # should print a warning and not withdraw +# print(repr(acc)) +### + +# Inheritance — the core of OOP (SavingsAccount extends BankAccount) +# Exceptions — custom error classes are themselves a great inheritance exercise +# @property — encapsulation, controlling access to data +# Track transaction history — composition (objects containing objects/lists) +# Dunder methods (__add__, __len__, __eq__) — operator overloading +# Transfer method — objects interacting with each other + +# --- + +# Create a SavingsAccount class that inherits from BankAccount: + +# It takes owner, optional balance, and an interest_rate (like 0.05 for 5%) +# It should use super() to reuse the parent's __init__ +# Add an apply_interest() method that increases the balance by the interest rate +# Override withdraw so it blocks withdrawals above 1000 (savings protection), but otherwise works normally +# Update __repr__ to also show the interest rate + +class SavingsAccount(BankAccount): + def __init__(self, owner, balance = None, interest_rate: float = 0.05): # should balance be just None or should i do the same optional thing as I did for BankAccount class? + super().__init__(owner, balance) + self.interest_rate = interest_rate + + def __repr__(self): + return f"SavingsAccount('{self.owner}', balance = {self.balance}, interest rate = {self.interest_rate})" + + def apply_interest(self): + self.balance += self.balance * self.interest_rate + return self.balance# would this work? + + def withdraw(self, money): # how to properly do the override? is this the right way? + if money >= 1000: + print("Transactions above 1000 are not allowed") + else: + return super().withdraw(money) + +# --- + +# savings = SavingsAccount("Alice", 1000, 0.05) +# print(repr(savings)) # shows owner, balance, and interest rate +# savings.apply_interest() +# print(savings) # balance becomes 1050.0 +# savings.withdraw(500) # works fine +# print(savings) +# savings.withdraw(1500) # blocked — too large for savings +# print(savings) +# savings.deposit(200) # should still work (inherited) +# print(savings) + + + +# Create a Classroom class that: + +# __init__ takes a teacher (string) and starts with an empty list of students +# An add_student method that takes a name and a grade (store them as a dict like {"name": "Alice", "grade": 85}) +# __getitem__ — lets you do classroom[0] to get a student +# __len__ — returns number of students +# __eq__ — two classrooms are equal if they have the same number of students +# __lt__ — for comparing/sorting classrooms by number of students +# __repr__ + +class Classroom: + def __init__(self, teacher: str) -> None: + self.teacher = teacher + self.students = [] + + def add_student(self, name: str, grade: float): + self.students.append({"name":name, "grade":grade}) + + def __str__(self): + return f"The teacher is {self.teacher} and the students are {self.students}" + + def __repr__(self): + return f"Classroom({self.teacher})" + + def __getitem__(self, key): + return self.students[key] + + def __len__(self): + return len(self.students) + + def __eq__(self, value): + return len(self.students) == len(value.students) + + def __lt__(self, other): + return len(self.students) < len(other.students) + +# c1 = Classroom("Mr. Smith") +# c1.add_student("Alice", 85) +# c1.add_student("Bob", 92) +# c1.add_student("Charlie", 78) + +# print(c1[0]) # {'name': 'Alice', 'grade': 85} +# print(c1[-1]) # {'name': 'Charlie', 'grade': 78} + +# c2 = Classroom("Ms. Jones") +# c2.add_student("Diana", 90) + +# print(c1 == c2) # False +# print(c1 > c2) # True +# print(sorted([c1, c2])) # c2 first (fewer students) + +# Create a custom InsufficientFundsError and integrate it into your BankAccount class: + +# Create an InsufficientFundsError class that inherits from Exception +# Its __init__ should take balance and amount (the attempted withdrawal) +# It should have a meaningful error message like "Cannot withdraw 500. Balance is only 200." +# Update BankAccount.withdraw to raise this error instead of printing +# Use try/except when calling withdraw to handle it gracefully + +class InsufficientFundsError(Exception): + def __init__(self, balance, amount): # explain to me what this is - *args + self.balance = balance + self.amount = amount + super().__init__(f"Cannot withdraw {self.amount}. Balance is only {self.balance}") + +# acc = BankAccount("Alice", 200) + +# try: +# acc.withdraw(500) +# except InsufficientFundsError as e: +# print(e) # Cannot withdraw 500. Balance is only 200. +# print(e.balance) # 200 +# print(e.amount) # 500 + +# acc.withdraw(50) # works fine, no error + +# acc = BankAccount("Alice", 100) + +# print(acc.balance) # 100 — reading works +# acc.balance = 500 # setting works +# print(acc.balance) # 500 + +# #acc.balance = -50 # should raise ValueError + +# print(acc.owner) # Alice — reading works +# acc.owner = "Bob" # should raise AttributeError (read-only) + +# --- + +# Write tests for your SavingsAccount class: + +# Test that apply_interest calculates correctly +# Test that withdrawals over 1000 are blocked +# Test that deposit still works (inherited) +# Test that the interest rate is stored correctly + +## done, check the tests_bankclass.py + +# --- + +# Add transaction history to your BankAccount: + +# Create a Transaction class with type, amount, and timestamp +# Add _history = [] in BankAccount.__init__ +# In deposit, create a Transaction("deposit", money) and append it to _history +# Do the same in withdraw with "withdrawal" +# Add get_history() that returns the list +# Add __repr__ to Transaction + +class Transaction: + + def __init__(self, type: str, amount: float) -> None: + self.type = type + self.amount = amount + self.timestamp = datetime.now + + def __repr__(self): + return f"Transaction(type='{self.type}', amount={self.amount}, time={self.timestamp})" + +# test the above +acc = BankAccount("Alice", 100) +acc.deposit(50) +acc.deposit(200) +acc.withdraw(30) + +for t in acc.get_history(): + print(t) +# Transaction(type='deposit', amount=50, time=...) +# Transaction(type='deposit', amount=200, time=...) +# Transaction(type='withdrawal', amount=30, time=...) + +print(len(acc.get_history())) # 3 \ No newline at end of file diff --git a/bankclass_prettified.py b/bankclass_prettified.py new file mode 100644 index 0000000..8112016 --- /dev/null +++ b/bankclass_prettified.py @@ -0,0 +1,228 @@ +""" +Python OOP Practice +=================== +Concepts covered: +1. Classes & __init__ +2. @property & encapsulation +3. Custom exceptions (inheritance from Exception) +4. Composition (Transaction history) +5. Inheritance (SavingsAccount) +6. Dunder methods (__repr__, __str__, __getitem__, __len__, __eq__, __lt__) +""" + +from typing import Optional +from datetime import datetime + + +# ============================================================================= +# 1. CUSTOM EXCEPTIONS +# ============================================================================= + +class InsufficientFundsError(Exception): + """Raised when a withdrawal exceeds the available balance.""" + + def __init__(self, balance, amount): + self.balance = balance + self.amount = amount + super().__init__(f"Cannot withdraw {self.amount}. Balance is only {self.balance}") + + +# ============================================================================= +# 2. COMPOSITION — Transaction class (used by BankAccount) +# ============================================================================= + +class Transaction: + """Represents a single bank transaction.""" + + def __init__(self, type: str, amount: float) -> None: + self.type = type + self.amount = amount + self.timestamp = datetime.now() + + def __repr__(self): + return f"Transaction(type='{self.type}', amount={self.amount}, time={self.timestamp})" + + +# ============================================================================= +# 3. BASE CLASS — BankAccount +# Demonstrates: @property, encapsulation, composition, custom exceptions +# ============================================================================= + +class BankAccount: + """A bank account with balance protection and transaction history.""" + + def __init__(self, owner: str, balance: Optional[float] = None) -> None: + self._owner = owner + self.balance = balance if balance is not None else 0 # triggers setter + self._history = [] + + # --- Properties --- + + @property + def balance(self): + return self._balance + + @balance.setter + def balance(self, value): + if value < 0: + raise ValueError("Balance cannot be negative") + self._balance = value + + @property + def owner(self): + """Read-only property — owner cannot be changed.""" + return self._owner + + # --- Core methods --- + + def deposit(self, money): + self.balance += money + self._history.append(Transaction("deposit", money)) + return self.balance + + def withdraw(self, money): + if (self.balance - money) >= 0: + self.balance -= money + self._history.append(Transaction("withdrawal", money)) + else: + raise InsufficientFundsError(self.balance, money) + return self.balance + + def get_history(self): + return self._history + + # --- Dunder methods --- + + def __repr__(self): + return f"BankAccount('{self.owner}', balance={self.balance})" + + def __str__(self): + return f"{self.owner}'s account — balance: {self.balance}" + + +# ============================================================================= +# 4. INHERITANCE — SavingsAccount extends BankAccount +# ============================================================================= + +class SavingsAccount(BankAccount): + """A savings account with interest and withdrawal limits.""" + + def __init__(self, owner, balance=None, interest_rate: float = 0.05): + super().__init__(owner, balance) + self.interest_rate = interest_rate + + def apply_interest(self): + self.balance += self.balance * self.interest_rate + return self.balance + + def withdraw(self, money): + if money >= 1000: + print("Transactions above 1000 are not allowed") + return self.balance + return super().withdraw(money) + + def __repr__(self): + return f"SavingsAccount('{self.owner}', balance={self.balance}, rate={self.interest_rate})" + + +# ============================================================================= +# 5. DUNDER METHODS — Classroom (container behavior + comparison) +# ============================================================================= + +class Classroom: + """Demonstrates __getitem__, __len__, __eq__, __lt__ for container behavior.""" + + def __init__(self, teacher: str) -> None: + self.teacher = teacher + self.students = [] + + def add_student(self, name: str, grade: float): + self.students.append({"name": name, "grade": grade}) + + def __getitem__(self, key): + return self.students[key] + + def __len__(self): + return len(self.students) + + def __eq__(self, other): + return len(self.students) == len(other.students) + + def __lt__(self, other): + return len(self.students) < len(other.students) + + def __repr__(self): + return f"Classroom('{self.teacher}', students={len(self.students)})" + + def __str__(self): + return f"Teacher: {self.teacher}, Students: {self.students}" + + +# ============================================================================= +# 6. TESTS — Uncomment sections to test each feature +# ============================================================================= + +if __name__ == "__main__": + + # --- BankAccount basics --- + print("=== BankAccount ===") + acc = BankAccount("Alice", 100) + print(acc) + acc.deposit(50) + print(f"After deposit: {acc.balance}") + acc.withdraw(30) + print(f"After withdraw: {acc.balance}") + + # --- Custom exception --- + print("\n=== InsufficientFundsError ===") + try: + acc.withdraw(500) + except InsufficientFundsError as e: + print(e) + print(f" Balance: {e.balance}, Attempted: {e.amount}") + + # --- Property protection --- + print("\n=== Property Protection ===") + print(f"Owner: {acc.owner}") + try: + acc.owner = "Bob" + except AttributeError: + print("Cannot change owner — read-only") + try: + acc.balance = -50 + except ValueError as e: + print(f"Cannot set negative balance: {e}") + + # --- Transaction history --- + print("\n=== Transaction History ===") + for t in acc.get_history(): + print(f" {t}") + print(f"Total transactions: {len(acc.get_history())}") + + # --- SavingsAccount --- + print("\n=== SavingsAccount ===") + savings = SavingsAccount("Alice", 1000, 0.05) + print(repr(savings)) + savings.apply_interest() + print(f"After interest: {savings.balance}") + savings.withdraw(500) + print(f"After withdraw: {savings.balance}") + savings.withdraw(1500) + savings.deposit(200) + print(f"After deposit: {savings.balance}") + + # --- Classroom --- + print("\n=== Classroom ===") + c1 = Classroom("Mr. Smith") + c1.add_student("Alice", 85) + c1.add_student("Bob", 92) + c1.add_student("Charlie", 78) + + c2 = Classroom("Ms. Jones") + c2.add_student("Diana", 90) + + print(f"c1[0]: {c1[0]}") + print(f"len(c1): {len(c1)}") + print(f"c1 == c2: {c1 == c2}") + print(f"c1 > c2: {c1 > c2}") + print(f"Sorted: {sorted([c1, c2])}") \ No newline at end of file diff --git a/intermediate_course_repl_playaround.ipynb b/intermediate_course_repl_playaround.ipynb new file mode 100644 index 0000000..0ba3119 --- /dev/null +++ b/intermediate_course_repl_playaround.ipynb @@ -0,0 +1,1981 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "c1591e66", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1\n", + "SyntaxError: not a chance\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from __future__ import braces" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9a379dd3", + "metadata": {}, + "outputs": [], + "source": [ + "import antigravity" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b78f69e1", + "metadata": {}, + "outputs": [], + "source": [ + "a = \"Aman, Nalakath\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eb764ea9", + "metadata": {}, + "outputs": [], + "source": [ + "first_name, last_name = a.split(\",\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "52be2c69", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + " Nalakath\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "last_name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2db83cae", + "metadata": {}, + "outputs": [], + "source": [ + "name_raw = a.split(\",\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c8193d3", + "metadata": {}, + "outputs": [], + "source": [ + "full_name = (\",\").join(name_raw)\n", + "full_name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55ba7954", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Aman, Nalakath\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "full_name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d28a3343", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Aman', ' Nalakath']\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "name_raw" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c2710bd", + "metadata": {}, + "outputs": [], + "source": [ + "full_name_with_an_x = (\"_x \").join(name_raw)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a4b897ea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Aman_x Nalakath\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "full_name_with_an_x" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c8854767", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "123\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "int(\"123\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "636dbf8d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'm', 'a', 'n']\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "list(\"aman\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4fbeafe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "aman\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "\"\".join(list(\"aman\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f33ca728", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "type(\"\".join(list(\"aman\")))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f225cf85", + "metadata": {}, + "outputs": [], + "source": [ + "names = \"aman, shamil, aman, nalakath\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a52dc0ea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'i', ' ', 'a', 'k', 'm', 'n', 's', 'h', 'l', 't', ','}\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "set(names)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "981e5cc7", + "metadata": {}, + "outputs": [], + "source": [ + "names = [\"aman\", \"shamil\", \"aman\", \"nalakath\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "99b6b65d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'aman', 'nalakath', 'shamil'}\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "set(names)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d82c84c5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['aman', 'nalakath', 'shamil']\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "list(set(names))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "90a0e3d0", + "metadata": {}, + "outputs": [], + "source": [ + "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b04aa3f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4\n", + "16\n", + "36\n", + "64\n", + "100\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "for num in numbers:\n", + " if num % 2 == 0:\n", + " print(num ** 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8bc8c2bf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4\n", + "16\n", + "36\n", + "64\n", + "100\n", + "[None, None, None, None, None]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "[print(num ** 2) for num in numbers if num % 2 == 0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "42865d44", + "metadata": {}, + "outputs": [], + "source": [ + "n_a = [(num ** 2) for num in numbers if num % 2 == 0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3934de5c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 16, 36, 64, 100]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "print(n_a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "652aa601", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "range(0, 10, 2)\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "range(0, 10, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d10ebe62", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "list(range(0, 10, 2))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d59f0b1e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['length: len(i)', 'length: len(i)', 'length: len(i)']\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "[\"length: len(i)\" for i in list(set(names))]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b9b3a3f7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['length: len(i)', 'length: len(i)', 'length: len(i)']\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "[f\"length: len(i)\" for i in list(set(names))]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4be7347", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['aman', 'nalakath', 'shamil']\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "list(set(names))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7df92f72", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "type(list(set(names)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5b2afa7b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1\n", + " [(\"length\": len(name)) for name in list(set(names))]\n", + " ^\n", + "SyntaxError: invalid syntax\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "[(\"length\": len(name)) for name in list(set(names))]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fd4ce891", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1\n", + " [(\"length:\" len(name)) for name in list(set(names))]\n", + " ^^^^^^^^^^^^^^^^^^^\n", + "SyntaxError: invalid syntax. Perhaps you forgot a comma?\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "[(\"length:\" len(name)) for name in list(set(names))]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "47710a40", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('length:', 4), ('length:', 8), ('length:', 6)]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "[(\"length:\",len(name)) for name in list(set(names))]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fefaed11", + "metadata": {}, + "outputs": [], + "source": [ + "n_a = [(num ** 2) for num in numbers if num % 2 == 0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74b072fa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 16, 36, 64, 100]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "n_a = [(num ** 2) for num in numbers if num % 2 == 0]\n", + "print(n_a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31dc6e51", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 9, 25, 49, 81]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "n_b = [(num ** 2) for num in numbers if bool(num % 2)]\n", + "print(n_b)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa7ee3a2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[100, 64, 36, 16, 4]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "n_c = sorted([(num ** 2) for num in numbers if num % 2 == 0], reverse=True)\n", + "print(n_c)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28892a59", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2304, 2116, 1936, 1764, 1600, 1444, 1296, 1156, 1024, 900, 784, 676, 576, 484, 400, 324, 256, 196, 144, 100, 64, 36, 16, 4, 0]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "n_c = sorted([(num ** 2) for num in range(0, 50) if num % 2 == 0], reverse=True)\n", + "print(n_c)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "224a8fa2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1\n", + " type{1}\n", + " ^\n", + "SyntaxError: invalid syntax\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "type{1}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0434d00d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "type({1})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "644014bb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "type({1:1})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73f976c5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "numbers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea116a64", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "numbers[::-1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d4b44df", + "metadata": {}, + "outputs": [], + "source": [ + "names = [\"Bob\", \"Alice\", \"Eve\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9ec22549", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Bob': 10, 'Alice': 9, 'Eve': 8}\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "score_dict = dict(zip(names, numbers[::-1]))\n", + "print(score_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "113172b4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1, in \n", + "TypeError: type 'range' is not subscriptable\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "(num for num in range[0:100])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7cdee127", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + " at 0x100e96e90>\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "(num*2 for num in range(0, 100))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad62a1a1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "next((num*2 for num in range(0, 100)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10151a13", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "next((num*2 for num in range(0, 100)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "de5fe793", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "next((num*2 for num in range(0, 100)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b75ca4a5", + "metadata": {}, + "outputs": [], + "source": [ + "next((num*2 for num in range(0, 100)))\n", + "next((num*2 for num in range(0, 100)))\n", + "next((num*2 for num in range(0, 100)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "de2cf784", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "print(next((num*2 for num in range(0, 100))))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d9447d91", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0\n", + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "print(next((num*2 for num in range(0, 100))))\n", + "print(next((num*2 for num in range(0, 100))))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8fb0b22d", + "metadata": {}, + "outputs": [], + "source": [ + "times_two = (num*2 for num in range(0, 100))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "34caa341", + "metadata": {}, + "outputs": [], + "source": [ + "next(times_two)\n", + "next(times_two)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6e06e428", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "next(times_two)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c8fa9c5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "next(times_two)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57db5d9b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "next(times_two)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "09ad8281", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "next(times_two, \"stopped\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57de4dd4", + "metadata": {}, + "outputs": [], + "source": [ + "names = [\"Alice\", \"Bob\", \"Charlie\", \"Diana\"]\n", + "ages = [28, 34, 22, 41]\n", + "cities = [\"Berlin\", \"Paris\", \"Tokyo\", \"London\"]\n", + "scores = [88, 95, 72, 90]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d88aa70", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1\n", + " name, age, city, score = [for name, age, city, score in zip(names, ages, cities, scores)]\n", + " ^^^\n", + "SyntaxError: invalid syntax\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "name, age, city, score = [for name, age, city, score in zip(names, ages, cities, scores)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a881d34", + "metadata": {}, + "outputs": [], + "source": [ + "name, age, city, score = [f\"{name}, age {age}, from {city}, scored {score}\" for name, age, city, score in zip(names, ages, cities, scores)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf77aa88", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Alice, age 28, from Berlin, scored 88 Bob, age 34, from Paris, scored 95 Charlie, age 22, from Tokyo, scored 72 Diana, age 41, from London, scored 90\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "print(name, age, city, score)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57126452", + "metadata": {}, + "outputs": [], + "source": [ + "name, age, city, score = [f\"{name}, age {age}, from {city}, scored {score}\\n\" for name, age, city, score in zip(names, ages, cities, scores)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "86cddf75", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Alice, age 28, from Berlin, scored 88\n", + " Bob, age 34, from Paris, scored 95\n", + " Charlie, age 22, from Tokyo, scored 72\n", + " Diana, age 41, from London, scored 90\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "print(name, age, city, score)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4cd285b", + "metadata": {}, + "outputs": [], + "source": [ + "name, age, city, score = [f\"{name} age {age} from {city} scored {score}\\n\" for name, age, city, score in zip(names, ages, cities, scores)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88365f45", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Alice age 28 from Berlin scored 88\n", + " Bob age 34 from Paris scored 95\n", + " Charlie age 22 from Tokyo scored 72\n", + " Diana age 41 from London scored 90\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "print(name, age, city, score)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "38f6d821", + "metadata": {}, + "outputs": [], + "source": [ + "new_list = range(20)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5630f790", + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d58cfde2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Alice age 28 from Berlin scored 88\\n': 23}\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "{name: random.randint(20, 40)}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa064f63", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Bob age 34 from Paris scored 95\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "age" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a71e036", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Charlie age 22 from Tokyo scored 72\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "city" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "56680f75", + "metadata": {}, + "outputs": [], + "source": [ + "names = [\"Alice\", \"Bob\", \"Charlie\", \"Diana\"]\n", + "ages = [28, 34, 22, 41]\n", + "cities = [\"Berlin\", \"Paris\", \"Tokyo\", \"London\"]\n", + "scores = [88, 95, 72, 90]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "142e2410", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Alice age 28 from Berlin scored 88\\n': 32}\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "{name: random.randint(20, 40)}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c767862c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1, in \n", + "TypeError: unhashable type: 'list'\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "{names: random.randint(20, 40)}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "762c54a7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Alice': 37, 'Bob': 24, 'Charlie': 27, 'Diana': 31}\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "{name: random.randint(20, 40) for name in names}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "78b56fc4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "True + True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3689b8c4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0, 1, 4}\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "{1, 4, True, True, 0, False}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "222d2997", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1, in \n", + "TypeError: Exception() takes no keyword arguments\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "raise Exception(message = \"trying this out, don't mind me\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0d7851f5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 1, in \n", + "Exception: trying this out, don't mind me\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "raise Exception(\"trying this out, don't mind me\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6be71a65", + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " int(\"this is not a number\")\n", + "\n", + "except ValueError as e:\n", + " f\"Damn you got a {e}, be more careful, it is not a number. check again\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c240fde", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Damn you got a invalid literal for int() with base 10: 'this is not a number', be more careful, it is not a number. check again\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "try:\n", + " int(\"this is not a number\")\n", + "\n", + "except ValueError as e:\n", + " print(f\"Damn you got a {e}, be more careful, it is not a number. check again\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e40dfe1c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "list(range(10,-1,-1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d88fdd5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n", + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import time\n", + "for item in list(range(10,-1,-1)):\n", + " time.sleep(1)\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bab5f481", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 2, in \n", + "TypeError: 'range' object is not an iterator\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import time\n", + "for item in next(range(10,-1,-1)):\n", + " time.sleep(1)\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e16aecc0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Traceback (most recent call last):\n", + " File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n", + " retval = callable_(user_input, user_globals)\n", + " File \"\", line 2, in \n", + "TypeError: 'list' object is not an iterator\n", + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import time\n", + "for item in next(list(range(10,-1,-1))):\n", + " time.sleep(1)\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "318288c3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n", + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import time\n", + "for item in range(10,-1,-1):\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8705c132", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n", + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import time\n", + "for item in range(10,-1,-1):\n", + " print(item)\n", + " time.sleep(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e9884254", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n", + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import time\n", + "for item in range(10, -1, -1):\n", + " print(item)\n", + " if item > 0:\n", + " time.sleep(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "423b786a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n", + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import time\n", + "for item in range(10,-1,-1):\n", + " print(item, flush=True)\n", + " time.sleep(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac8150b7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n", + "0\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import time\n", + "for item in range(10, -1, -1):\n", + " print(item, flush=True)\n", + " time.sleep(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b3440af8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n", + "-- \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import os\n", + "with os.scandir(os.getcwd()) as folder:\n", + " for entry in folder:\n", + " print(f\"-- {entry}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c281b76", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-- home\n", + "-- usr\n", + "-- .resolve\n", + "-- bin\n", + "-- sbin\n", + "-- .file\n", + "-- etc\n", + "-- var\n", + "-- Library\n", + "-- System\n", + "-- .VolumeIcon.icns\n", + "-- private\n", + "-- .vol\n", + "-- Users\n", + "-- Applications\n", + "-- opt\n", + "-- dev\n", + "-- Volumes\n", + "-- .nofollow\n", + "-- tmp\n", + "-- cores\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import os\n", + "with os.scandir(os.getcwd()) as folder:\n", + " for entry in folder:\n", + " print(f\"-- {entry.name}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c263640", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "sys.platform" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b30a6c8d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "darwin\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import sys\n", + "print(sys.platform)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dd3c4f77", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "working with these arguments ['/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py']\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "print(f\"working with these arguments {sys.argv}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "68090701", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "type(sys.argv)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b2e39db1", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tests_bankclass.py b/tests_bankclass.py new file mode 100644 index 0000000..9a030be --- /dev/null +++ b/tests_bankclass.py @@ -0,0 +1,20 @@ +from bankclass import SavingsAccount +import unittest + +class TestBankClass(unittest.TestCase): + + def setUp(self): + self.acc = SavingsAccount("Alice", 1000, 0.05) + + def test_apply_interest(self): + self.assertEqual(self.acc.apply_interest(), 1050) + + def test_savings_withdraw(self): + self.acc.withdraw(400) + self.assertEqual(self.acc.balance, 600) + +if __name__ == "__main__": + unittest.main() + +# --- +# now do with higher level pytests instead \ No newline at end of file