Here Are
Here Are
python
Copy
Download
print(f"{num} is {result}")
Chapter 2: Strings
python
Copy
Download
import re
python
Copy
Download
if i % 3 != 0:
Chapter 4: Functions
python
Copy
Download
cache = {0: 0, 1: 1}
def fib(n):
if n not in cache:
return cache[n]
print(fib(10)) # Output: 55
python
Copy
Download
python
Copy
Download
count1 = defaultdict(int)
for x in list1:
count1[x] += 1
result = []
for x in list2:
if count1[x] > 0:
result.append(x)
count1[x] -= 1
return result
python
Copy
Download
import csv
total = 0
with open("data.csv") as f:
reader = csv.reader(f)
total += float(row[1])
print("Sum:", total)
Competition-Style Challenges
python
Copy
Download
if nums[mid] == target:
return mid
right = mid - 1
else:
left = mid + 1
else:
left = mid + 1
else:
right = mid - 1
return -1
Error Handling
python
Copy
Download
try:
return a / b
except Exception as e:
f.write(f"Error: {e}\n")
return None
OOP
Copy
Download
class BankAccount:
def __init__(self):
self.balance = 0
self.transactions = []
self.balance += amount
self.transactions.append(f"Deposit: +{amount}")
self.balance -= amount
self.transactions.append(f"Withdraw: -{amount}")
else:
print("Insufficient funds")
def history(self):
return self.transactions
account = BankAccount()
account.deposit(100)
account.withdraw(30)