Lecture 02 — Python: The Language of AI
~2–3 hours (core recap lecture)
🐍 Why Python?
Python is the language of thinking for AI and algorithms.
It is used in:
- 🤖 Artificial Intelligence
- 📊 Data Science
- 🧠 Research & prototyping
- 🧩 Coding interviews (LeetCode, Google, Meta)
If you know Python well, you can think clearly.
🧠 Mental Model (IMPORTANT)
Before syntax, remember this:
Python executes line by line, top to bottom.
Variables point to objects.
Everything is an object.
This mental model helps you avoid 80% of bugs.
1️⃣ Variables & Data Types
📦 Variables
x = 10
name = "AI"
pi = 3.14
is_active = True
Python is dynamically typed:
x = 10
x = "now a string"
🔢 Core Data Types
| Type | Example |
|---|---|
| int | 10 |
| float | 3.14 |
| str | "hello" |
| bool | True, False |
| None | None |
❓ Quiz
Is Python statically typed?
No. Python is dynamically typed.
2️⃣ Control Flow (if / else)
x = 5
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
🧠 Interview Tip
Indentation is logic in Python.
❓ Quiz
What happens if indentation is wrong?
The program raises an IndentationError or behaves incorrectly.
3️⃣ Loops (for / while)
🔁 for-loop (most common)
for i in range(5):
print(i)
🔄 while-loop
count = 0
while count < 3:
print(count)
count += 1
🧠 Interview Tip
Use for unless you must use while.
❓ Quiz
When is while-loop dangerous?
When the stopping condition is incorrect → infinite loop.
4️⃣ Lists (Most Important for LeetCode)
nums = [1, 2, 3, 4]
Common operations
nums.append(5)
nums.pop()
nums[0]
nums[-1]
len(nums)
Looping over list
for n in nums:
print(n)
With index
for i, n in enumerate(nums):
print(i, n)
❓ Quiz
What is the time complexity of list append?
Amortized O(1)
5️⃣ Strings (Text = Sequence)
s = "machine"
Strings behave like lists:
s[0] # 'm'
s[-1] # 'e'
s[1:4] # 'ach'
Useful methods
s.lower()
s.upper()
s.split()
❓ Quiz
Are strings mutable?
No. Strings are immutable.
6️⃣ Dictionaries (Key–Value Thinking)
student = {
"name": "Kao",
"age": 25,
"major": "AI"
}
Access
student["name"]
student.get("age")
Loop
for key, value in student.items():
print(key, value)
🧠 Interview Tip
Dictionaries give O(1) average lookup.
❓ Quiz
Why are dictionaries powerful?
Fast lookup using hashing.
7️⃣ Sets (Uniqueness)
nums = [1, 2, 2, 3]
unique = set(nums)
Use cases
- Remove duplicates
- Fast membership check
if 3 in unique:
print("Found")
❓ Quiz
Does a set preserve order?
No.
8️⃣ Functions (Reusable Thinking)
def add(a, b):
return a + b
Default arguments
def greet(name="AI"):
print("Hello", name)
❓ Quiz
Why use functions?
Abstraction, reuse, clarity.
9️⃣ Common Built-ins (LeetCode Gold)
len()
sum()
min()
max()
sorted()
range()
Example:
nums = [3, 1, 2]
sorted(nums)
🔟 Time Complexity Awareness (VERY IMPORTANT)
| Operation | Typical Cost |
|---|---|
| List access | O(1) |
| Dict lookup | O(1) |
| List search | O(n) |
| Sorting | O(n log n) |
Python coding interviews are algorithm interviews, not syntax tests.
🧠 Python for AI Mindset
Python is:
- 🧩 How we express logic
- 🧠 How we describe algorithms
- 🤖 How we talk to machines
Clean Python = Clear Thinking