Topic 1: Python Essentials

Introduction to Business Analytics — HKUST

Prof. Xuhu Wan

Why Python for Business?

The language of data

“Coding is not just for tech people — it is for anyone who wants to run a competitive company in the 21st century.”Mary Callahan Erdoes, JPMorgan

  • #1 language in data science and finance
  • Powers every major AI framework (TensorFlow, PyTorch, LangChain)
  • Required at Goldman Sachs, JPMorgan, McKinsey
  • Most taught language in top business schools

Lists — Your First Container

A list stores an ordered collection of items.

My portfolio has 4 stocks: ['AAPL', 'GOOG', 'TSLA', 'NVDA']

Why it matters

In business, lists represent portfolios, customer IDs, survey responses, product catalogs.

Indexing and Slicing

Python counts from 0. Negative indices count from the end.

First : AAPL
Last  : NVDA
Middle: ['GOOG', 'TSLA']

List Comprehension

Business problem: Apply a 10% price increase to all products.

[110.00000000000001, 275.0, 88.0, 352.0, 192.50000000000003]

Filter premium products (price > 150):

[250, 320, 175]

Conditional: Generate Buy/Sell signals:

['BUY', 'SELL', 'BUY', 'SELL', 'SELL', 'BUY', 'SELL']

From List to pandas Series

0    100
1    102
2     98
3    105
4    101
Name: AAPL, dtype: int64

Mean : 101.20
Std  : 2.59
Max  : 105

NumPy — Element-wise Math

Array * 2: [200 204 196 210 202]
Array + 10: [110 112 108 115 111]

Important

A plain Python list multiplied by 2 repeats the list. A NumPy array multiplied by 2 doubles each element.

Simulating Returns

Mean : 0.0549
Std  : 0.1849
Min  : -0.2919
Max  : 0.4705

Plotting Returns

SciPy — Distributions

P(X < 10) = 0.8413
5th percentile: x = 4.7103

Value at Risk Example

Wealth \(X \sim \mathcal{N}(\mu=\$1M, \sigma=\$500K)\). Find the 5% VaR.

95% VaR: $177,573
Loss if worst 5%: $822,427

Key Takeaways

Today you learned

  • Lists store ordered, mixed-type collections
  • List comprehensions replace 4-line loops with 1 line
  • pandas Series = list + built-in stats and plots
  • NumPy arrays = element-wise math and random simulation
  • SciPy = CDF/PPF for probability and risk calculations

Next time: DataFrames — the spreadsheet of Python. See Topic 2 →