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 for balance = 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 // 128
% Remainder 100 % 124
** 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

  1. String numbers: "200" + "200" = "200200" (not 400!) — convert first: int("200") + int("200") = 400
  2. Float imprecision: 0.1 + 0.2 = 0.30000000000000004 — use Decimal for financial calculations
  3. Case sensitivity: "Midwest" == "midwest" is False — normalize with .lower() or .upper()
  4. None is not zero: None == 0 is False

Next: Chapter 4 — Control Flow: How to make your programs make decisions.