1/49
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
revenue = 15000
cogs = 40000
operating_expense = 10000
calculate gross profit, net income, and gross margin and print them
gross_profit = revenue - cogs
net_income = gross_profit - operating_expense
gross_margin = gross_profit / revenue
print(“Gross Profit:”, gross_profit)
print(”Net Income:”, net_income)
print(“Gross Margin:”, gross_margin)
account_name = “Accounts Receivable”
account_number = 1200
balance = 45230.75
is_active = True
check the type of each
print(type(account_name))
print(type(account_number))
print(type(balance))
print(type(is_active))
messy_name = “ accounts RECEIVABLE “
clean the name
cleaned_name = messy_name.strip().title()
print(cleaned_name)
messy_accounts = [
“ CASH “
“accounts payable”
“ INVENTORY “
“ Notes Receivable “
“prepaid EXPENSES”
clean just the first item and print it
first_account = messy_accounts[0]
cleaned_first = first_account.strip().title()
print(cleaned_first)
balances = [15000, 8200, 42750, 3100, 67000, 920]
find the total, max, min, and count and print it
1st way:
total_balances = aum(balances)
print(total_balances)
max_balances = max(balances)
print(max_balances)
min_balances = min(balances)
print(min_balances)
count_balances = len(balances)
print(count_balances)
2nd way:
print(“Total Balances:”, sum(balances))
print(“Max Balances:”, max(balances))
print(“Min Balances:”, min(balances))
print(“Count Balances:”, len(balances))
balances = [15000, 8200, 42750, 3100, 67000, 920]
define a variable equal to the total of balances and print it
total_balance = sum(balances)
print(“Total of Balance:”, total_balance)
balances = [15000, 8200, 42750, 3100, 67000, 920]
print the thirs and last balance without using a loop
print(“Third Balance:”, balances[2])
print(“Last Balance:”, balances[-1])
balances = [15000, 8200, 42750, 3100, 67000, 920]
without using a loop, write one line of code that prints:
Total of all balances: 136970, average balance: 22828.333333333333332
print(f”Total of all balances: {sum(balances)}, average balance: {sum(balances) / len(balances)}”)
balances = [15000, 8200, 42750, 3100, 67000, 920]
a new transaction comes in for $5,400. Add it to balances, and then recompute the total and the average
balances.append(5400) #this is the function to add
print(f “Total of all balances: {sum(balances)}, average balance: {sum(balances) / len(balances)}”)
balances = [15000, 8200, 42750, 3100, 67000, 920]
print the middle four values
middle_four = balances[1:5]
print(middle_four)
balances = [15000, 8200, 42750, 3100, 67000, 920]
add 5400 to the balances list and reprint the list
balances.append(5400)
print(balances)
approved_vendors = [ “Acme Corp”, “Beta LLC”, “Gamma Supplies”, “Delta Industries”]
check whether Acme Corp is in approved_vendors, and separately whether Zeta Partners is as well. print both results
print(“Acme Corp” in approved_vendors)
print(“Zeta Partners” in approved_vendors)
#it will respond with either true or false based on the “in” function
transaction_amount = 31000
materiality_threshold = 25000
write an if/else that prints “Above materiality threshold” or “Below materiality threshold”
if transaction_amount > materiality_threshold:
print(“Above materiality threshold”)
else:
print(“Below materiality threshold”)
#the bullet points indicates only one tab over
transaction_amount = 60000
ammount > 50000 → “High risk - requires partner review”
amount > 25000 → “Medium risk - requires manager review”
otherwise → “Low risk - standard review”
use an if/else/elif to perform the categorization
if transaction_amount > 50000:
print(“High risk - requires partner review”)
elif transaction_amount > 25000:
print(“Medium risk - requires manager review”)
else:
print(“Low risk - stamdard review”)
#use elif if there is another level needed to be added
#else is used when nothing else fits the description then you do this…
transactions = [12000, 45000, 8500, 63000, 22000, 51000, 9800]
loop through the list and print each trasnaction
for amount in transactions:
print(amount)
#”amount” here can be any variable
transactions = [12000, 45000, 8500, 63000, 22000, 51000, 9800]
materiality_threshold = 25000
print a flag message only for transactions exceeding the threshold
for amount in transactions:
if amount > materiality_threshold:
print(f “FLAGGED: ${amount} exceeds materiality threshold”)
write a function that returns a list of amounts exceeding the threshold ( do not print inside the function — return the values)
def flag_large_transactions(amounts, threshold):
flagged = []
for amount in amounts:
if amount > threshold:
flagged.append(amount)
return flagged
#the first line is the function header
second line is an empty list made to collect the amounts and check which is greater than the threshold
third line is starting the loop
fourth line is the if function
fifth line is using append rather than print it into flagged which collects it into a list rather than displaying it
sixth line just says return the completed list back out of the function so whoever is next to use the function can do so
call the flag_large_transactions function with a threshold of 25000 and 50000 and store it in result_xK and print it
then compare the two lists to see if the second list is shorter
added task: print a sentence that reports how many transactions were flagged for 25000. Ex. 3 transactions were flagged
result_25k = flag_large_transactions(transactions, 25000)
result_50k = flag_large_transactions(transactions, 50000)
print(“Flagged at $25,000 threshold:”, result_25k)
print(“Flagged at $50,000 threshold: result_50k)
print(“Second list is shorter:”, len(result_50k) > len(result_25k))
print(f “{len(result_25k)} transactions were flagged.”)
amounts = [12000, 58000, 31000, 9500, 47000]
print a message for any transaction that exceeds $25,000 formatted as FLAGGED: $
for amount in amounts:
if amounts > 25000:
print(f “FLAGGED: ${amount}”)
transaction = {“date”: “2026-01-15”, “vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”}
print the vendor, then the amount by assessing the, through their keys
print(transaction [“vendor’])
print(transaction [“amount”])
transaction = {“date”: “2026-01-15”, “vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”}
add a new key “invoice number” with value “INV-2031”, then update “amount” to 4750. Print the full dictionary
transaction[“invoice number”] = “INV-2031”
transaction[“amount”] = 4750
print(transaction)
transaction = {“date”: “2026-01-15”, “vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”, “invoice number”: “INV-2031”}
loop over keys and values by printing every key and value, one pair per line
for key, value in transaction.items():
print(f”{key}: {value}”)
#.items() gives you pairs, so the loop needs two variable names
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
print each transaction’s vendor and amount
for t in transactions:
print(f “{t[“vendor”]}: ${t[“amount”]}”)
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
sum the total and print the total across all transactions
total = 0
for t in transactions:
total +- t[“amount”]
print(“Total:”, total)
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
for any transaction where amount > 10000, print a flagged message
for t in transactions:
if t[“amount”] > 10000:
print(f “FLAGGED: {t[“vendor’]} - ${t[“amount”]} - {t[“account”]}”)
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
build a dictionary “totals_by_vendor” where each key is a vendor and each value is their total spend
totals_by_vendor = {}
for t in transactions:
vendor = t[“vendor”]
amount = t[“amount”]
if vendor in totals_by_vendor:
totals_by_vendor[vendor] +- amount
else:
totals_by_vendor[vendor] = amount
print(totals_by_vendor)
Using totals by vendor, find and print which vendor has the highest total spend
top_vendor = None
top_amount = 0
for vendor, amount in totals_by_vendor.items()
if amount > top_amount:
top_amount = amount
top_vendor = vendor
print(f “Top Vendor: {top_vendor} ${top_amount}”)
how to build a dataframe
import padas as pd
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
build a dataframe
import padas as pd
df = pd.DataFrame(transactions)
df
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
look at the dataframe 3 ways
print(df.head())
print(df.info())
print(df.describe())
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
select one column, in this case “amount” and then print its type
print(df[“amount”])
print(type(df[“amount”]))
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
on the amount column, print the sum, mean, and max
print(“Sum:”, df[“amount'“].sum())
print(“Mean:'“, df[“amount].mean())
print(“Max:”, df[“amount”].max())
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
print a boolean mask with amount > 10000
print(df[“amount”] > 10000)
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
create a variable flagged containing only rows where amount exceeds 10000
flagged = df[df[“amount”] > 10000]
flagged
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
Create a variable “travel” containing only the rows where acount equals travel
travel = df[df[“account”] == “Travel”]
travel
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
Create a variable “equipment_flagged” containing only the rows where amount is over 10000 and account equals equipment
equipment_flagged = df[(df[“amount”] > 10000) & (df[“account”] == “Equipment”)]
equipment_flagged
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
Group df by vendor and sum the amount column. Store the result in totals_by_vendor and print it
totals_by_vendor = df.groupby(“vendor”)[“amount”].sum()
print(totals_by_vendor)
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
print totals_by_vendor[“Acme Corp”], then print the type
print(totals_by_vendor[“Acme Corp”])
print(type(totals_by_vendor))
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
use .value_counts on the vendor column to count how many transactions each vendor has. Store it in counts_by_vendor and print it
counts_by_vendor = df[“vendor”].value_counts()
print(counts_by_vendor)
transactions = [
{“vendor”: “Acme Corp”, “amount”: 4500, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: 800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: 15500, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
find the top vendor with the highest total and tha total’s value
top_vendor = totals_by_vendor.idmax()
top_amount = totals_by_vendor.max()
print(f “Top vendor: {top_vendor} (${top_amount})”)
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
import, create dataframe, then print info
import pandas as pd
df = pd.DataFrame(raw_transactions)
df.info()
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
clean the amount column and print it as a list
cleaned = df[“amount”].str.replace(“$”, ““, regex=False).str.replace(“,”, ““, regex=False)
print(cleaned.tolist())
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
convert the real numbers and put the column back
df[“amount”] = pd.to_numeric(cleaned, errors = “coerce”)
df.info()
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
Handle the missing value
print(“Missing amounts:”, df[“amount”].isna().sum()) #this gives you a sentence
df[df[“amount”].isna()] #this gives you the chart
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
drop the rows with no amount
df = df.dropna(subset = [“amount”])
print(df)
print(“Rows now:”, len(df))
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
print the vendors value counts. and then clean the vendor
print(df[“vendor”].value_counts()
df[“vendor”] = df[“vendor”].str.strip().str.title()
print(df[“vendor”].value_counts()
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
remove duplicate rows
print(“Duplicate rows:”, df.duplicated().sum()
df[df.duplicated(keep = False)]
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
drop duplicate rows
df = df.drop_duplicates()
print(“Rows now:”, len(df)
raw_transactions = [
{“vendor”: “Acme Corp”, “amount”: $4,500.00, “account”: “Office Supplies”},
{“vendor”: “Acme Corp”, “amount”: 12000, “account”: “Office Supplies”},
{“vendor”: “Beta LLC”, “amount”: $800, “account”: “Travel”},
{“vendor”: “Gamma Supplies”, “amount”: None, “account”: “Equipment”},
{“vendor”: Beta LLC”, “amount”: 2200, “account”: “Travel”}
]
convert the dates then group by the vendor and sum the amount on the cleaned data
df[“date”] = pd.to_datetime(df]”date”])
df.info()
print(“latest transactions:”, df[“date”].max())
totals_by_vendor = df.groupby(“vendor”)[“amount”].sum()
print(totals_by_vendor)