RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Python for AI — Zero to Useful
  6. /Ch. 3
Python for AI — Zero to Useful

03. Variables and Types

Chapter 3 of 36 · 20 min
KEY INSIGHT

Python types exist but are inferred, not declared. F-strings are your primary tool for building strings with embedded values. Type errors happen when you mix types—handle conversions explicitly.

Python Is Dynamically Typed

Python variables have types, but you do not declare them. The interpreter figures it out at runtime:

x = 42          # int
y = 3.14        # float
name = "GPT-4"  # str
active = True   # bool

The Basic Types

Type Example Mutable
int 42 No
float 3.14 No
str "hello" No
bool True No
list [1, 2, 3] Yes
dict {"key": "value"} Yes
None None No

Strings Are Objects

Strings have methods. This is normal Python:

model_name = "gpt-4"
print(model_name.upper())           # GPT-4
print(model_name.replace("-", ""))  # gpt4
print(model_name.startswith("gpt")) # True

Type Conversion

Python does not auto-convert between types:

# This fails:
result = "Total: " + 42  # TypeError

# Fix it:
result = "Total: " + str(42)  # "Total: 42"

# Number conversion:
int("42")    # 42
float("3.14") # 3.14

F-Strings for Interpolation

F-strings (formatted string literals) are the modern way to embed variables:

model = "gpt-4"
tokens = 1000
cost_per_1k = 0.03

message = f"{model} costs ${cost_per_1k:.2f} per 1K tokens for {tokens} tokens"
print(message)
# gpt-4 costs $0.03 per 1K tokens for 1000 tokens

The :2f formats the float to 2 decimal places.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Write a script that:

  1. Creates variables for an AI model's name, context window size, and cost per token
  2. Prints a formatted description using an f-string
  3. Converts the cost per token from a string to a float and performs a calculation
← Chapter 2
Your Environment
Chapter 4 →
Control Flow