Key Takeaways — Chapter 3: Python Basics
Variables
- A variable is a named container:
revenue = 45000 - Names: lowercase with underscores (
monthly_revenue), descriptive, no keywords - Reassignment replaces the old value:
balance += 500(shorthand forbalance = balance + 500)
The Five Core Types
| Type | Example | Business Use |
|---|---|---|
int |
200 |
Counts, quantities |
float |
29.99 |
Prices, rates, percentages |
str |
"Acme Corp" |
Names, codes, descriptions |
bool |
True, False |
Flags, conditions |
None |
None |
Missing/unknown values |
Type Conversion Quick Reference
int("45000") # → 45000 (string to int)
float("29.99") # → 29.99 (string to float)
str(200) # → "200" (number to string)
int(29.99) # → 29 (truncates, doesn't round!)
Arithmetic Operators
| Operator | Use | Example |
|---|---|---|
+, -, *, / |
Basic math | revenue - cost |
// |
Floor division | 100 // 12 → 8 |
% |
Remainder | 100 % 12 → 4 |
** |
Power | (1+rate) ** years |
Compound growth formula: initial * (1 + rate) ** years
F-String Format Codes
f"{revenue:,}" # 1,450,000
f"{revenue:,.2f}" # 1,450,000.00
f"{margin:.1%}" # 34.2%
f"{value:>10}" # Right-aligned in 10 chars
Comparison and Logical Operators
# Comparison → returns bool
revenue > target # Greater than?
revenue >= target # Greater than or equal to?
status == "Active" # Equal to?
# Logical → combines bools
is_vip and is_paid # Both must be True
over_budget or at_risk # At least one True
not is_active # Invert the bool
Common Pitfalls
- String numbers:
"200" + "200"="200200"(not 400!) — convert first:int("200") + int("200")=400 - Float imprecision:
0.1 + 0.2=0.30000000000000004— useDecimalfor financial calculations - Case sensitivity:
"Midwest" == "midwest"isFalse— normalize with.lower()or.upper() - None is not zero:
None == 0isFalse
Next: Chapter 4 — Control Flow: How to make your programs make decisions.